
jQuery(function( $ ){
	
$(".iframe").colorbox({width:"800", height:"500", scrollbars:false, iframe:true, opacity:.5});
$(".rulesframe").colorbox({width:"600", height:"500", scrollbars:true, iframe:true, opacity:.5});
$(".prizeframe").colorbox({width:"515", height:"446", scrollbars:false, iframe:true, opacity:.5});
$(".single").colorbox();


$('#votes').html(votes[picid]+' vote'+(votes[picid]>1?'s':''));
$('#caption').html('"'+captions[picid]+'"');

changeLayout = function() {
	$('#picunderlay-h').hide();
	$('#picunderlay-v').hide();
	$('#picoverlay').hide();
	$('#picoverlay-v').hide();
	
	if(piclayouts[picid]=='vertical') {
		$("#pics").addClass("pics-v");
		$("#picoverlay-v").show();
		$("#picunderlay-v").show();
	} else {
		$("#pics").removeClass("pics-v");
		$("#picoverlay").show();
		$("#picunderlay-h").show();
	} 
}
changeLayout();

nextPic = function() {
	lastpic = picid;
	picid++;
	if(picid>countpics) picid = 1;
	$('.pic'+lastpic).css('display','none');
	$('.pic'+picid).css('display','block');
	$('#votes').html(votes[picid]+' vote'+(votes[picid]>1?'s':''));
	$('#caption').html('"'+captions[picid]+'"');	
	changeLayout();
}

lastPic = function() {
	lastpic = picid;
	picid--;
	if(picid<1) picid = countpics;
	$('.pic'+lastpic).css('display','none');
	$('.pic'+picid).css('display','block');
	$('#votes').html(votes[picid]+' vote'+(votes[picid]>1?'s':''));
	$('#caption').html('"'+captions[picid]+'"');	
	changeLayout();
}

openVote = function() {
	$("#votepane").fadeIn(500);
}

openVoteLoad = function() {
	$("#voteloadpane").fadeIn(500);
}

openVoted = function() {
	$("#votedpane").fadeIn(500);
}

openVotedAlready = function() {
	$("#votedalreadypane").fadeIn(500);
}

openTooManyVotes = function() {
	$("#toomanyvotespane").fadeIn(500);
}

openCannotConnectIP = function() {
	$("#cannotconnectippane").fadeIn(500);
}

openInvalidIP = function() {
	$("#invalidippane").fadeIn(500);
}

closeVote = function() {
	$("#votepane").fadeOut(500);
}

closeVoteLoad = function() {
	$("#voteloadpane").fadeOut(500);
}

closeVoteCaptcha = function() {
	$("#votepanecaptcha").fadeOut(500);
}

closeVoteConfirm = function() {
	$("#vote_confirmed").fadeOut(500);
}

closeVoted = function() {
	$("#votedpane").fadeOut(500);
}

closeVotedAlready = function() {
	$("#votedalreadypane").fadeOut(500);
}

closeTooManyVotes = function() {
	$("#toomanyvotespane").fadeOut(500);
}

closeCannotConnectIP = function() {
	$("#cannotconnectippane").fadeOut(500);
}

closeInvalidIP = function() {
	$("#invalidippane").fadeOut(500);
}

sendVote = function() {
  var vt = $(".votetext").val();
  var vc = $(".votecheck:checked").val();
  var vcv = 1;
  if(vc==undefined) vcv = 0;
  closeVote();
  openVoteLoad();
  $.ajax({
   type: "POST",
   url: "validate_email2.php",
   data: "email="+vt,
   success: function(msg){
     if(msg=='fail') {
		alert('please enter a valid email address');
		closeVoteLoad();
		openVote();
		return;
	  } else if(msg=='invalid') {
		alert("Your email address is not properly formatted.\n\nPlease try again.");
		closeVoteLoad();
		openVote();
		return;
	  } else {
		   $.ajax({
		   type: "POST",
		   url: "addvote.php",
		   data: "email="+vt+"&optin="+vcv+"&picid="+picids[picid],
		   success: function(msg){
			 if(msg=='success') {
				 closeVoteLoad();
				 openVoted();
			 } 
			 if(msg=='votedalready') {
				 closeVoteLoad();
				 openVotedAlready();
			 } 
			 if(msg=='toomanyvotes') {
				 closeVoteLoad();
				 openTooManyVotes();
			 } 
			 if(msg=='invalidip') {
				 closeVoteLoad();
				 openInvalidIP();
			 } 
			 if(msg=='cannotconnectip') {
				 closeVoteLoad();
				 openCannotConnectIP();
			 } 
		   }
		 });
	  }
   }
 });
  
}

sendVoteCaptcha = function() {
  var vt = $(".votetext").val();
  var ct = $(".captchatext").val();
  var vc = $(".votecheck:checked").val();
  var vcv = 1;
  var err = false;
  if(vc==undefined) vcv = 0;
  $.ajax({
	 type: "POST",
	 url: "checkcaptcha.php",
	 data: "captcha="+ct,
	 success: function(msg){
		 alert(msg);
	   if(msg=='Invalid') {
		   alert("Captcha phrase did not match image.\n\nPlease try again.");
		   err = true;
		   return;
	   } else {
			checkEmail(vt,vc,vcv);   
	   }
	 }
   });  
}

checkEmail = function(vt,vc,vcv) {
	$.ajax({
	   type: "POST",
	   url: "validate_email.php",
	   data: "email="+vt,
	   success: function(msg){
		 if(msg=='fail') {
			alert('Please enter a valid email address');
			return;
		  } else if(msg=='invalid') {
			alert("Your email address is not properly formatted.\n\nPlease try again.");
			return;
		  } else {
			   $.ajax({
			   type: "POST",
			   url: "addvote.php",
			   data: "email="+vt+"&optin="+vcv+"&picid="+picids[picid],
			   success: function(msg){
				 if(msg=='success') {
					 closeVote();
					 openVoted();
				 } 
				 if(msg=='votedalready') {
					 closeVote();
					 openVotedAlready();
				 } 
				 if(msg=='toomanyvotes') {
					 closeVote();
					 openTooManyVotes();
				 } 
			   }
			 });
		  }
	   }
	 });
	
}

/*
@author: Remy Sharp / http://remysharp.com
@date: 2007-04-21
@usage:
$('img').zoom('200%', '200%');
$('img').zoom('300px', '200px');
$('img').zoom(300);
*/
jQuery.fn.zoom = function(height, width) {
  if (!width && height) {
    width = height;
  } else if (!width && !height) {
    width = height = '100%';
  }
    
  function toem(px) {
    px = parseFloat(px);
    return (px * 0.0626).toString() + 'em';
  }
  
  function getLen(elm, side, target) {
    var l = 1;
    if (target.indexOf('%') === -1) {
      l = toem(target);
    } else {
      l = toem(elm[side] * (parseFloat(target)/100));
    }
    
    return l;
  }
  
  return this.each(function() {
    var hem = getLen(this, 'height', height);
    var wem = getLen(this, 'width', width);
    
    jQuery(this).css({ height: hem, width: wem });
  });
};

//$('#pics div img').zoom('300px','464px');

});