var aNominees = new Array();
var iNomineeNumber;

$(document).ready(function(){
	iNomineeNumber = $('.nominees .item').size();					   
	loginPanel();						   
	registerForm();	
	setupJudges();
	nomineeVotes();	
	showRegister();
	if(iNomineeNumber > 0){	setupNomineePopup(); }
	
});

function zFix(){
	var zIndexNumber = 1000;
	$('div').each(function() {
		$(this).css('zIndex', zIndexNumber);
		zIndexNumber -= 10;
	});
}

function showRegister(){
	$('.register').click(function(){
		$('.loginPanel').slideUp();
		$('.registerPanel').slideDown();
		
		return false;
	});
	$('.registerPanel .cancel').click(function(){
		$('.registerPanel').slideUp();
		$('.loginPanel').slideDown();
		return false;
	});
}

function registerForm(){

	$('.registerForm').validate();
	
	//username field
	$('.registerForm #username').focus(function(){
		$('.usernameError').remove();										
	});
	
	$('.registerForm #username').blur(function(){
		
		//get the current value of the username field
		var thisUsername = $(this).val();
		
		//make ajax post to see if username is already registered
		$.post("/ajax/request/users.php", { 
			   type: "unique_username",
			   username: thisUsername 
			   },
		//get response	   
		function(data){
				
				//if username registerd
				if (data == "true") {
					
					$('#username').removeClass('valid');

					$('#username').addClass('error');
					
					$('.registerForm #username').parent().append('<label class="error usernameError" for="username" generated="true">Username already registered.</label>');
					
				} else {

					$('#username').addClass('valid');

					$('#username').removeClass('error');
					
					$('.usernameError').remove();

				}
		});	
		
	});
	
}

function setupJudges() {
	
	$('.judge .showHide a').bind("click", function() {	
												   
		var bOn = $(this).hasClass("on");
		if(bOn) {
			$(this).removeClass("on");
			$(this).parent().parent().children('.copy').slideUp();
			$(this).text("More");
		} else {	
			$(this).addClass("on");
			$(this).parent().parent().children('.copy').slideDown();
			$(this).text("Hide");
		}	
		
	});	
}

function closeNomineePopup(){
	
	$('.nomineeDetail .controls .close').bind("click", function() {									   
		$('.nomineeDetail').fadeOut(function(){
			$('.nomineeDetail').html('');									 
		});	
	});						
}

function nextPrevPopup(sDirection, iIndex) {
	
	$('.nomineeDetail .controls .' + sDirection).bind("click", function() {
							
		var sNomineeId = aNominees[iIndex];		
		
		$('.nomineeDetail').fadeOut(function() {
				
			// Set position from thumb
			var pPosition = $('#' + sNomineeId).position();	
			$('.nomineeDetail').css({"left":pPosition.left,"top":pPosition.top});	
			
			$('.nomineeDetail').load("/ajax/request/nominee.php?id=" + sNomineeId, function(){
							
				closeNomineePopup();
				nextPrevPopup("next", iIndex + 1);
				nextPrevPopup("prev", iIndex - 1);					
				showHideNextPrevButtons(iIndex);
				
				$('.nomineeDetail').fadeIn();
				
			});	
		});		
	});	
}

function showHideNextPrevButtons(iIndex){
	
	if (iIndex < 1) {
		$('.nomineeDetail .controls .prev').hide();
	} else {
		$('.nomineeDetail .controls .prev').show();
	}
	
	if (iIndex > iNomineeNumber - 2) {
		$('.nomineeDetail .controls .next').hide();
	} else {
		$('.nomineeDetail .controls .next').show();
	}
	
}

function setupNomineePopup(){		

	$('.nominees .item').each(function(iIndex){
		
		var sNomineeId = $(this).attr('id');
		
		aNominees.push(new Array(sNomineeId));
				
		$(this).bind("click", function() {	
									   
			 // Set position from thumb
			var pPosition = $('#' + sNomineeId).position();					
			$('.nomineeDetail').css({"left":pPosition.left,"top":pPosition.top});
			
			var thisOrder = $(this).attr('title');
			
			$('.nomineeDetail').load("/ajax/request/nominee.php?id=" + sNomineeId + '&order=' + thisOrder, function(){
					
				closeNomineePopup();
				nextPrevPopup("next", iIndex + 1);
				nextPrevPopup("prev", iIndex - 1);						
				showHideNextPrevButtons(iIndex);	
				
				$('.nomineeDetail').fadeIn();	
				
				nomineeVotes();
				
			});	
									
	   	});	
		
	});	
}

function nomineeVotes(){
	
	
	$('.vote').click(function(){
		
		if ($(this).hasClass('loggedout')) {
		
			var thisDiv = $(this).parent();
			
			lightBox(thisDiv);
		
		} else {
		
		var nomineeId = $(this).children().attr('name');
		
		$(this).find('img').attr('src', '/images/structure/bu_small_vote_green.png');
		
		 $.post("/ajax/post/nominee-vote.php", { 
			 id: nomineeId 
		 });
		 
		}
		
	});
	
	$('.like').click(function(){
		
		if ($(this).hasClass('loggedout')) {
		
			var thisDiv = $(this).parent();
			
			lightBox(thisDiv);
		
		} else {
		
		var nomineeId = $(this).attr('name');
		
		$(this).find('img').attr('src', '/images/structure/bu_like_green.jpg');
		
		 $.post("/ajax/post/nominee-vote.php", { 
			 id: nomineeId 
		 });
		 
		}

	});			
	
	$('.liked').click(function(){

		var thisName = $(this).attr('title');
		
		alert('You have already voted for '+thisName);
	
	});
								  
}

function loginPanel(){
    
    $('.loginPanel #username').focus(function(){ $(this).val(''); });
    $('.loginPanel #password').focus(function(){ $(this).val(''); });
	
        
}

//function to scroll to a div in the document
function scrollWin(div){
	$('html, body').animate({
		scrollTop: $(div).offset().top
	}, 900);
}


function lightBox(div){
	
	
	$('#loginLightbox').remove();
	$('#shadow').remove();
	
	//grab panel which user originally clicked
	var detail = $('.nomineeDetail');
	
	//fix z-indexs for IE
	zFix();
	
	// append shadow div overlay
	$('body').append("<div id='shadow'></div>")
	
	//append modal over shadow
	$('#container').append("<div id='loginLightbox'></div>");
	
	//scroll to the top of the window incase user is scrolled down the page so they can see modal
	scrollWin("#container");

	//load login panel from php file
	$('#loginLightbox').load('/include/site/login-panel-25.php?ajaxloaded=true', function(){
																		  
	loginPanel();
	
	showRegister();
	
	FB.init("3528c0424fe489b393941458ab08eb5d");
		
		//append close button to login box
		$('#loginLightbox .loginPanel').prepend('<a class="closeShadow" style="float:right" href="javascript:void(0)">close</a>');
		
		//call function for when close button is clicked
		$('.closeShadow').click(function(){
			
			//hide shadow overlay
			$('#shadow').slideUp(function(){
				$('#shadow').remove();
			});
			
			//fadein panel that user orignaly clicked and was hidden
			$('.nomineeDetail').fadeIn();
			
			//fadeout login modal
			$('#loginLightbox').fadeOut(function(){
				$('#loginLightbox').remove();
			});
			
			//scroll back to original location
			//scrollWin(div);
		
		});
		
	});
	
	//get window height and width
	var windowWidth = $(window).width();
	var windowHeight = $(window).height();
	
	//get document height
	var documentHeight = $(document).height();
	
	//get height and width of login modal
	var frameHeight = $('#loginLightbox').height();
	var frameWidth = $('#loginLightbox').height();
	
	//do some math to position modal in the middle of the screen	
    var	newHeight = windowHeight /4;
    
	panelHeight = newHeight - frameHeight;
	panelWidth = (windowWidth - frameWidth) / 2.7;
	
	panelWidth = panelWidth - frameWidth;
	
	// set login modal css properties
	$('#loginLightbox').css({
		'position' : 'absolute',
		'top' : panelHeight,
		'left' : panelWidth,	
		'display' : 'none',
		'z-index' : 500000							
	});
							
	//set shadow css properties
	$('#shadow').css({
		'position' : 'absolute',
		'top' : '0',
		'left' : '0',
		'width' : windowWidth,
		'height' : documentHeight,
		'display' : 'none',
		'z-index' : 300000	
	})
	
	//set opacity of shadow overlay
	$('#shadow').fadeTo('fast',0.7);
	
	//slide in shadow
	$('#shadow').slideDown();
		
	if(detail){
		//fadeout panel while shadow and modal are displaying
		$(detail).fadeOut();
	}
	
	//fade in login modal
	$('#loginLightbox').fadeIn();
	
	//if user clicks shadow then run this function to close everything
	$('#shadow').click(function(){
		
		//slide up the shadow
		//$('#shadow').slideUp(function(){
			$('#shadow').remove();
		//});
		
		if(detail) {
			//fade in the panel the user orignally clicked
			$(detail).fadeIn();
		}
		
		//remove login modal
		$('#loginLightbox').fadeOut(function(){
			$('#loginLightbox').remove();
		});
		
		//scroll back to original positon
		//scrollWin(div);

	});
	
	return false;	
}


// embed flash
function embedFlash(swf, div, width, height) {
	
	var params     = {};
	var flashvars  = {};
	var attributes = {};
	
	params.menu = "false";
	params.quality = "high";
	params.allowFullScreen = "true";
	params.base	= "."; // using a slash '/' here breaks ie6
	params.salign = "tl";
	params.scale = "default";
	params.wMode = "transparent";
	
	attributes.allowFullScreen = "true";
	attributes.id = div;
	attributes.bgcolor = "#ffffff";
	attributes.wMode = "transparent";
	swfobject.embedSWF(swf, div, width, height, "8.0.0", '/swf/expressinstall.swf', flashvars, params, attributes);
}
