﻿function showHide(show, hide){
	var showArr = new Array();
	var hideArr = new Array();
	var showArr = show.split(',');
	var hideArr = hide.split(',');
	for(var i=0; i<hideArr.length; i++) {
       	if(hideArr[i]) {document.getElementById(hideArr[i]).style.display = 'none';}
	}   
	for(var j=0; j<showArr.length; j++) {
		if(showArr[j]) {document.getElementById(showArr[j]).style.display = 'block';}    
	}
}

function popup(url,w,h,scroll){
	winpops=window.open(url,"","scrollbars="+scroll+",width="+w+",height="+h+",")
}

function getComments(id, type){
	$.post("http://www.FetishBuzz.com/logic/comments_ajax.php", {term: id},
		function(data){
			insertComments(data,id);
		}, "text");
}

function insertComments(data,id){
	$('#comment'+id).html(data).slideDown();
}

function hideComments(id){
	$('#comment'+id).slideUp();
}

function setPid(id){
	$('#pid').val(id);
}

function resizeWindow(height, width, path){
	width  = parseInt(width)+50;
	height = parseInt(height)+130;
	if(width != ''){
		window.resizeTo(width,height);
	}
	window.location = path;
}

function adClick(id){
	$.post("http://www.fetishbuzz.com/logic/adcount_ajax.php", {ad_id: id});
}

function resize(id){
	ifr = document.getElementById(id);
	var height = ifr.contentWindow.document.body.scrollHeight;
	
	if(height < 950) height = 950;
	ifr.height = height;
}

function logout(){
	$.post("http://www.fetishbuzz.com/logic/logout.php", {},
		function(data){
			$("#manageL").fadeOut();
			$("#logOut").fadeOut('', function () {
			$("#logOut").fadeIn('').html('Sign In').attr('href','http://www.fetishbuzz.com/admin/login.php').removeAttr('onclick');
      });

		}, "text");
	return false;
}

function swapImg(id, img){
	$('#'+id).attr('src',img);
}

function checkPoints(total, item){
	if(total > item){
		alert('Sorry, you have '+total+' redeemable points and this item is '+item+' points.');
		return false;
	}
	return true;
}

function updateTime(){
	timeCount++;
	var recur = setTimeout("updateTime()", 1000);
}

function setCookie(){
	var date = new Date();
	var expires = date.setTime(date.getTime()+(60*60*1000)); // 1 hour
	document.cookie = "timeCookie="+timeCount+";expires="+expires+";path=/;";
}

function ajaxFileUpload(num){
	if(num == 3) $('#fileLoading').show();
	
	if($('#file'+num).val() != ''){	
		var type = $('#class'+num).val();
		var note = $('#note'+num).val();

		$.ajaxFileUpload({
			url:'logic/fileupload_ajax.php?cid='+cid+'&t='+type+'&d='+note+'&id='+num,
			secureuri:false,
			fileElementId:'file'+num,
			dataType: 'text',
			success: function (data, status){
				if(typeof(data.error) != 'undefined'){
					if(data.error != ''){
						alert(data.error);
					}
				}
				if(num > 1){
					ajaxFileUpload(num-1);
				} else {
					$('#fileLoading').hide();
					tb_remove();
					window.location.reload();
				}
			},
			error: function (data, status, e){
				alert(e);
			}
		})
	} else {
		$('#fileLoading').hide();
		tb_remove();
		window.location.reload();
	}
	return false;
}

function contestMove(tack, num){	
	var d = new Date();
	//var year = d.getFullYear();
	var year = '2009';
	//var month = d.getMonth()+1;
	var month = '07';
	if(month.toString.length == 1) month = '0'+month;

	if((position+tack >= 0 && position+tack <= images.length-1) || num != 'false'){
		if(num == 'false'){
			position += tack;
		} else {
			position = num;
		}

		$('#contImg').fadeOut('fast', function(){
			$(this).attr('src', BASE_PATH+'/assets/contests/'+year+'-'+month+'/t_'+images[position][0]).unbind("click").click(function(){popup(BASE_PATH+'/assets/contests/'+year+'-'+month+'/'+images[position][0])}).fadeIn('fast');
		});
		$('#user').text(images[position][1]);
		$('#type').text(images[position][2]);
		$('#totalVotes').text(images[position][3]);
		$('#note').text(images[position][4]);
		$('#imgId').val(images[position][5]);
	}
}

function checkKey(e){
    switch (e.keyCode) {
        case 37:
            contestMove(-1, 'false');
            break;
        case 39:
            contestMove(1, 'false');
            break;
    }      
}

function sendVote(){
	var votes = $('#vote').val();
	var imgId = $('#imgId').val();
	$('#cast').attr('disabled', 'true');
	
	$.post(BASE_PATH+"/logic/contest_ajax.php", {votes: votes, imgId: imgId},
	function(data){
		voteConfirmation(votes, data);
	}, "text");	
}

function voteConfirmation(votesCast, votesRemaining){
	if(votesRemaining < 0){
		$('#confirm').html('<span>No votes remaining. Purchase more below.</span>').fadeIn('slow');
	} else {
		$('#confirm').html('<span>'+votesCast+' votes cast for this photo. '+votesRemaining+' votes remaining.</span>').fadeIn('slow');
		$('#cast').removeAttr('disabled');
	}
}

function getHashKey(){
	$('#sendCC').attr('disabled', 'true').attr('class', 'disabled');
	$('#loading').fadeIn();
	
	var amount = $('#votes').val();
	var cardnum = $('#ccnumber').val();
	var expir = $('#ccmonth').val()+"-"+$('#ccyear').val(); //MM-YY
	var cvv2 = $('#cvv2').val();
	
	$.post(BASE_PATH+"/logic/contest_ajax.php", {amount: amount, cardnum: cardnum, expir: expir, cvv2: cvv2},
	function(data){
		confirmPay(data);
	}, "json");
}

function confirmPay(data){
	$('#sendCC').removeAttr('disabled').attr('class', 'button');
	$('#loading').fadeOut();
	
	if(data.status == "success"){
		if($('#voteDrop: select').length == 0){ //if dropdown not on the page, then create it
			$('#voteDrop').html('');
			$('#voteDrop').append('<label for="vote">Cast: <select id="vote" name="vote"></select><input type="button" value="Submit" id="cast" onclick="sendVote();">');	
		}
		var totVotes = parseInt($('#voteDrop: select').find('option:last').val());
		for(var i = 1; i <= data.votes; i++){
			$('#voteDrop: select').append('<option value="'+(parseInt(i)+parseInt(totVotes))+'">'+(parseInt(i)+parseInt(totVotes))+' votes</option>');
		}
		$('#credit').fadeIn(function(){
			$(this).append('<p class="message">You have successfully bought '+data.votes+' votes.</p>');
		})
	} else if(data.status == "declined"){
		$('#credit').fadeIn(function(){
			$(this).append('<p class="error">Transaction DECLINED: '+data.reason+'</p>');
		})
	} else {
		$('#credit').fadeIn(function(){
			$(this).append('<p class="error">Error: '+data.reason+'</p>');
		})		
	}
}

function resetBuyVotes(){
	$('#selectPay').siblings().hide();
	$('#selectPay').show();
}