// Delay Plugin for jQuery
// - http://www.evanbot.com
// - © 2008 Evan Byrne

jQuery.fn.delay = function(time,func){
	this.each(function(){
		setTimeout(func,time);
	});
	
	return this;
};	


// 	jquery function calls

$(document).ready(function() {
	$('#email_signUp').validate();
	$('#frmHouseParty').validate();
	$('#frmVolunteer').validate();
	
	
	$('#emailSignUp').validate({
	  rules: {
		email: {
		  required: true,
		  email: true
		}
	  }
	});
	$('#unified').validate({
	  rules: {
		email: {
		  required: true,
		  email: true
		}
	  }
	});
	

/*	$('.ddfm').eq(0).validate({
	  rules: {
		email: {
		  required: true,
		  email: true
		}
	  }
	});
	$('.ddfm').eq(1).validate({
	  rules: {
		email: {
		  required: true,
		  email: true
		}
	  }
	});
	$('.ddfm').eq(2).validate({
	  rules: {
		email: {
		  required: true,
		  email: true
		}
	  }
	});	*/

// Workaround for Firefox losing checkbox value when unchecked and then rechecked.

	$('.ddfm').eq(0).submit(function(){
//	$('p.submit input').click(function(){
		if ($('#endorse').is(':checked')) {
			$('#endorse').attr('value', 'I endorse Rex for Metro President.');
		}
		if ($('#endorse').is(':checked') == false) {
			$('#why').attr('value', '');
		}
		if ($('#volunteer').is(':checked')) {
			$('#volunteer').attr('value', 'I would like to volunteer for the campaign.');
		}
		if ($('#host').is(':checked')) {
			$('#host').attr('value', 'I would like to host a house party for Rex.');
		}
	});

// slide down why I endorse textarea on get_involved form
	
	if ($('.static #endorse').is(':checked')) {
		$('p.why').show();
	}

	$('.static #endorse').click(function(){
		$('p.why').slideToggle();
 	});

	
// Drop menu function

	$("ul.dropdown li").hover(function(){
	
	$(this).addClass("hover");
	$('ul:first',this).css('visibility', 'visible');
    
    }, function(){
    
	$(this).removeClass("hover");
	$('ul:first',this).css('visibility', 'hidden');
    
    });
    
//    $("ul.dropdown li ul li:has(ul)").find("a:first").append(" &raquo; ");

// Preload background CSS images	
	
	$.preloadCssImages();
//	$('.issuesWrap a.moreArw').hide();
	$('.issuesWrap[rel="1"]').show();
	
	$('#frmHouseParty').submit(function() {
		$(this).validate();
		});


	$().delay(1250,function() {
		issuesCycle(1);
	});
	
//	$().delay(1500,function()	{
//		$(".issuesWrap p.whiteScreen, .issuesWrap a.moreArw").fadeIn(2000);
//	});

	var issueTimer = {}

	var cycleFlag = 0;
	var cycleNum = 0;
//	var issueNum = 1;
	
	function issuesCycle(num) {
	 	if((cycleFlag == 1) || (cycleNum >=11)) { return false };
	 	cycleNum += 1;
		$('.issuesWrap').hide();
		$('.issuesWrap[rel='+ num +']').show();
	//	$(".issuesWrap p.whiteScreen").hide();
	//	$(".issuesWrap a.moreArw").hide();
		$('.issues_icons li a').removeClass('onIcon');
		$('.issues_icons li a[rel='+ num +']').addClass('onIcon');
	//	$('.issuesWrap a.moreArw').fadeIn(6500);
		if(num == 1) {
			$().delay(2500, function() {
				$('.issuesWrap[rel='+ num +'] p.whiteScreen').fadeIn(0, function() {
					$().delay(4500, function() {issuesCycle(2)});
				});
			});
		} else if(num == 2) {
			$().delay(2500, function() {
				$('.issuesWrap[rel='+ num +'] p.whiteScreen').fadeIn(0, function() {
					$().delay(4500, function() {issuesCycle(3)});
				});
			});
		} else if(num == 3) {
			$().delay(2500, function() {
				$('.issuesWrap[rel='+ num +'] p.whiteScreen').fadeIn(0, function() {
					$().delay(4500, function() {issuesCycle(4)});
				});
			});
		} else if(num == 4) {
			$().delay(2500, function() {
				$('.issuesWrap[rel='+ num +'] p.whiteScreen').fadeIn(0, function() {
					$().delay(4500, function() {issuesCycle(5)});
				});
			});
		} else if(num == 5) {
			$().delay(2500, function() {
				$('.issuesWrap[rel='+ num +'] p.whiteScreen').fadeIn(0, function() {
					$().delay(4500, function() {issuesCycle(1)});
				});
			});
		}   
	}
	
	$('.issues_icons li a, .issuesWrap h4 a').click(function() {
		cycleFlag = 1;
		var $this = $(this);
		var num = $(this).attr('rel');
		var numCk = "[rel='" + num + "']";
		var target = "'.issuesWrap" + numCk + "'";
		$('.issues_icons li a').removeClass('onIcon');
		$('.issues_icons li a[rel='+ num +']').addClass('onIcon');
//		$this.addClass('onIcon');
		$('.issuesWrap').hide();
//		$(".issuesWrap p.whiteScreen").hide();
//		$(".issuesWrap a.moreArw").hide();
//		$(target).show().fadeIn(1000, function() {
			fadeup(num);
//		});
		
		return false;
	});

	function fadeup(num) {
		$('.issuesWrap').hide();
		$('.issuesWrap[rel='+ num +']').show();
//		$(".issuesWrap p.whiteScreen").hide();
//		$(".issuesWrap a.moreArw").hide();
//		$(".issuesWrap p.whiteScreen, .issuesWrap a.moreArw").fadeIn(3500);
	}
	
	$(".ddfm input, .signUpWrap input#email, .signUpWrap input#zip").focus(function() {
		$(this).val("");
	});

	$(".signUpWrap #mc_signup_form input#mc_mv_EMAIL").val("Email Address");
	$(".signUpWrap #mc_signup_form input#mc_mv_MMERGE6").val("11111");
//	$(".signUpWrap #mc_signup_form input.button").val("");

	$("#mc_signup_form input").focus(function() {
		if ($(this).attr("id") != "mc_mv_MMERGE6") {
			$(this).val("");
		}
	});

	$("#mc_mv_MMERGE29").val('Zip Code');
// 	$("#mc_mv_MMERGE29").attr('type', 'hidden');

	$("#mc_mv_EMAIL").attr('tabindex', '1');
	$("#mc_mv_MMERGE29").attr('tabindex', '2');
	$("#mc_mv_MMERGE6").attr('tabindex', '4');
	$("#mc_signup_submit").attr('tabindex', '3');
	
	$(".header #mc_signup_form .mc_custom_border p").html("News, ideas, and events, direct to you");
	$(".header #mc_signup_submit").attr("value", "");
		
	if ($(".header #mc_message .mc_success_msg").length != 0) {
		$(".header #mc_signup_submit").hide();
		$(".header #mc_signup_form .mc_custom_border h4").hide();
		$(".header #mc_signup_form .mc_custom_border p").hide();
		$(".header #mc_mv_EMAIL").hide();
		$(".header #mc_mv_MMERGE29").hide();
		$(".header #mc_mv_MMERGE6").hide();
	};


   /* Global augmentation of form submit field with 'ovalButton' anchors.
    * Input element is moved off screen and replaced with an ovalButton,
    * poviding better styling/hover effects but retaining enter key behavior.
    */
    
//    $(':submit').each(function() {
//    var $this = $(this);
//    var f = this.form;
//    var link = $('<a href="" class="gfxButton1">' + $this.val() + '</a>')
//                .bind('click', function() {
//                     $(f).trigger('submit');
//                     return false;
//                  });
//        $this.after(link).css({position:'absolute', top:'-4000px'});
//    });

});