$(document).ready(function(){
	
	/*	
	//detect empty tags
	$.getJSON("http://jsonip.appspot.com?callback=?",function(data){
	
		if(data.ip == '99.243.241.17'){
			
			$('p,em,b,strong,span,h1,h2,h3,h4,h5').each(function() {
				if (jQuery.trim ($(this).text()) == "") $(this).css({'border':'solid 1px red'});
			});
		
		}
	
	});
  	*/
  	
  	//click the whole campaign
  	$("#campaign").click(function(){
		location.href = $(this).find('a:first').attr('href');
    });
  	
  	//rollover highlights fix
  	$("#highlights ul li a").hover(
      function () {
        $(this).children('span').addClass('hover');
      }, 
      function () {
        $(this).children('span').removeClass('hover');
      }
    );

  	
  	//add span for sign Up button
  	$('a.signUp').each(function() {
        txt = $(this).text();
        newHtml = '<span>'+txt+'</span>';
        $(this).html(newHtml);
    });
    
    //fix ie7 and ie6 signup button
  	if(!jQuery.support.objectAll){
  		$('a.signUp').addClass('signUpFix');
  	}
    
	//same height related
	var maxHeight1 = 0; //first 4 items
	var maxHeight2 = 0; // the rest
	$('#related div.item').each(function(index) {
		if(index < 4 ){
			var currentH = $(this).height();
			if(maxHeight1 < currentH) maxHeight1 =  currentH;
		}else{
			var currentH = $(this).height();
			if(maxHeight2 < currentH) maxHeight2 =  currentH;
		}
	});
	
	$('#related div.item').each(function(index) {
		if(index < 4 ){
			$(this).height(maxHeight1);	
		}else{
			$(this).height(maxHeight2);
		}		
	});
	
	//remove end border
	$('.boxShortTxt:last, #highlights .item:last').css('border','0px');
	
	//set height for right side menu if smaller then the content or the other way around
	if($('#content').length > 0 && $('#main-right-wrapper').length > 0){
		var offset = 126; //offset for matching right colum height
		var CH = $('#content').height();
		var MRWH = $('#main-right-wrapper').height();
		
		if((CH + offset) > MRWH){
			$('#main-right-wrapper').height(CH + offset);
		}else{
			$('#content').height(MRWH - offset);
		}
	}
	
	if($('#contentWide').length > 0 && $('#main-right-wrapper').length > 0){
		var offset = 126; //offset for matching right colum height
		var CH = $('#contentWide').height();
		var MRWH = $('#main-right-wrapper').height();
		
		if((CH + offset) > MRWH){
			$('#main-right-wrapper').height(CH + offset);
		}else{
			$('#contentWide').height(MRWH - offset);
		}
	}
	
	//set height for left side menu if smaller then the content
	if($('#content').length > 0 && $('#leftMenu').length > 0){
		var CH = $('#content').height();
		var LMH = $('#leftMenu').height();
		
		if(CH > LMH){
			$('#leftMenu').height(CH);
		}
	}
	
	//set same hight for news items. Also add click event
	var maxHeight = 0;
	$("div.boxShortTxt").each(function (i) {
    	var currentH = $(this).height();
    	if(maxHeight < currentH) maxHeight =  currentH;
    	
    	$(this).click(function(){
    		location.href = $(this).children('p').children('a:first').attr('href');
    	})
    });
	$("div.boxShortTxt").height(maxHeight);
	
	//wrap highlight in valign table in order to aign it in the middle
	$hHtml = $('#campaign').html();
	$hH = $('#campaign').height();
	$('#campaign').empty();
	$('#campaign').html('<table><tr><td style="vertical-align:middle;height:'+ $hH +'px">' + $hHtml + '</td><tr></table>'); 
	
	//add hor line for the first h3 in the left menu
	$('#leftMenu h3:first').addClass('firstH3');
	

	//zebra for rates fees pages
	var classNames = {
		0: 'first',
		1: 'second'
	}
	
	$('.rates tbody tr').each(function(index) {
		$(this).addClass(classNames[index % 2])
	}) 
	
	$('.rates2 tbody tr').each(function(index) {
		$(this).addClass(classNames[index % 2])
	}) 
	
	$('.fees tbody tr').each(function(index) {
		$(this).addClass(classNames[index % 2])
	}) 
	$('.campaign5000 tbody tr').each(function(index) {
		$(this).addClass(classNames[index % 2])
	}) 
	
	$('.rates tbody tr td:odd').addClass("alignright");
	$('.fees tbody tr td:odd').addClass("alignright");
	$('.fees thead tr td:odd').addClass("alignright");
	
	
	
	$('#subscribeNewsletter span.submitNewsletter').click(function(){
         //alert("test");
         var email = $("input[name='email']").val();
		 $('.message1').empty();
         $(".message2").empty();
         $.ajax({
           type: "POST",
           url: "addcontact.php",
           data: "email=" + email,
           success: function(msg){
             if (msg == 'Invalid email') {
                 $(".message1").append('<span class="error">' + msg + '</span>');
             }else{
                 $(".message2").append(msg);
             }
           }
         });
    });

	$('#subscribeNewsletterHomePage span.submitNewsletter').click(function(){
         var email = $("input[name='email']").val();
         $(".message").empty();
         $.ajax({
           type: "POST",
           url: "addcontact.php",
           data: "email=" + email,
           success: function(msg){
             if (msg == 'Invalid email') {
                 $(".message").append('<span class="error">' + msg + '</span>');
             }else{
                 $(".message").append(msg);
             }
           }
         });
    });
    
    $('#unsubscribeNewsletter span.submitNewsletter').click(function(){
         var email = $("input[name='email']").val();
         $(".message1").empty();
         $(".message2").empty();
         $.ajax({
           type: "POST",
           url: "removecontact.php",
           data: "email=" + email,
           success: function(msg){
             if (msg == 'Invalid email') {
                 $(".message1").append('<span class="error">' + msg + '</span>');
             }else{
                 $(".message2").append(msg);
             }
           }
         });
    });
    
    //5000 reasons - add new background color and change the rounded corner image
    if($('.reasons5000').length > 0 ){
    	
    	$('body').css({'background':'#f58025'});
    	$('#second-wrapper').css({'background-image':'url(assets/comp/bg-right-round-5000.png)'})
    	$('#first-wrapper').css({'background-image':'none'});
    	
    }
    
    
    //wrap highlight in valign table in order to aign it in the middle
	$hHtml = $('#imgText5000').html();
	$hH = $('#imgText5000').height();
	$('#imgText5000').empty();
	$('#imgText5000').html('<table><tr><td style="vertical-align:middle;height:'+ $hH +'px">' + $hHtml + '</td><tr></table>'); 
    
    
    ///5000 reasons - hichjack the click event to detect the name and add name + 1 ballot to the entries table
   // $('#mc-embedded-subscribe-test').click(function(){
    	/*
    	if($("input[name='firstname']").val() && $("input[name='lastname']").val()){
    	
    	}else{
    	
    	}
    	*/
    	//var fn = input[name='firstname']").val();
    	//var ln = input[name='lastname']").val();
    	//var email = input[name='EMAIL']").val();
    	/*
    	$.post("add5000entry.php", $("#mc-embedded-subscribe-form").serialize(),
  			function(data){
    			alert(data);
  			}
  		);
  		*/
  		
  		//$('#mce-success-response').change();
  		/*
  		if( $('#mce-error-response').text() != "" ){
  			alert ("alo");
  		}else{
  			alert("ola");
  		}
  		*/
    //});
    
    //$('#mce-success-response').change();    
    
});


// functions to poll the result sent by chimp mail. if the email is not
// in the Db then we add the email address to the 5000 reason contest
// The email has to be double checked with the actual list since the email 
// is added to the db after the client gets an email from the system and clicks on a link
var c=0;
var t;
var timer_is_on=0;

//save form values so they can be submited after the form is cleared by mailchimp
var firstnameJ = "";
var lastnameJ = "";
var emailJ = "";

function timedCount(){
	
	if( $('#mce-success-response:visible').text() ){
		$.post("add5000entry.php", { firstname: firstnameJ, lastname: lastnameJ, email: emailJ } ,
			function(data){
	   			$('#mce-success-response').after('<div id="emailBallotMsg">'+data+'</div>');
	   			firstnameJ = "";
				lastnameJ = "";
				emailJ = "";
			}
  		);
  		firstnameJ = "";
		lastnameJ = "";
		emailJ = "";
  		stopCount();
	}
	
	
	if(timer_is_on){
		//c=c+1;
		t=setTimeout("timedCount()",1000);
	}
}

function pollSubscribeResponse(){
	if (!timer_is_on){
  		timer_is_on=1;
  		timedCount();
  	}
  	
  	firstnameJ = $("input[name='firstname']").val();
	lastnameJ = $("input[name='lastname']").val();
	emailJ = $("input[name='EMAIL']").val();
  	
  	if($("input[name='firstname']").val() && $("input[name='lastname']").val()){
  		$('.namesError').hide();	
  	}else{
  		$('.namesError').show();
  		$('#emailBallotMsg').remove();
  		return false;
  	}
  	$('#emailBallotMsg').remove();
  	
}

function stopCount(){
	clearTimeout(t);
	timer_is_on=0;
}


