var tabID ='';
var address="";
var emailValid = true;
var shareMessage = '';
var shareAuthor = '';
var shareTitle='';
var shareExcerpt = '';
var shareID = 0;
var tempAdd = '';

jQuery(".popularTab").click(function(){
	jQuery(".activeTab").removeClass("activeTab");
	jQuery(this).addClass("activeTab");
	
	tabID = jQuery(this).attr("id");
	
	
	if ( tabID == 'mostReadTab'){
		jQuery("#mostReadList").show();
		jQuery("#mostCommentedList").hide();
		jQuery("#mostSharedList").hide();
	}
	else if ( tabID == 'mostCommentedTab'){
		jQuery("#mostCommentedList").show();
		jQuery("#mostReadList").hide();
		jQuery("#mostSharedList").hide();
	}
	else {
		jQuery("#mostSharedList").show();
		jQuery("#mostReadList").hide();
		jQuery("#mostCommentedList").hide();
	}
	
});

jQuery(document).ready(function(){
	
	
	jQuery(".mc_signup_submit #mc_signup_submit").click(function(){
		try { var pageTracker2 = _gat._getTracker("UA-5586805-2"); } catch(err) {}
		pageTracker2._trackPageview("/emailSignUp");
	});
	
	function popitup(url) {
		newwindow=window.open(url,'name','height=600,width=1024,scrollbars=1,toolbar=1');
		if (window.focus) {newwindow.focus()}
		return false;
	}
	
	jQuery(".twitter-share-button").click(function(){
		tempAdd = $(this).attr("href");
		popitup(tempAdd);
		pageTracker._trackPageview("/shared_twitter");
		return false;
	});
	jQuery(".fb_share_button").click(function(){
		pageTracker._trackPageview("/shared_fb");
	});
	jQuery(".facebook_like").click(function(){
		pageTracker._trackPageview("/shared_fb_like");
	});
	
	jQuery(".high_holidays_header").click(
		function(){
			pageTracker._trackPageview("/high_holidays_banner");
		});
	
	jQuery(".high_holidays_home").click(
		function(){
			pageTracker._trackPageview("/high_holidays_banner");
		});
	
	
	jQuery("#emailThis").click(function(){
		jQuery("#emailThisBox").show();
	});
	
	jQuery("#emailThisInner .close").click(function(){
		jQuery("#emailThisBox").hide();
	});
	
	
	jQuery("#emailThisSend").click(function(){
		if ( validateEmail() ){
			sendEmail();
			pageTracker._trackPageview("/shared_email");
		}
	})
	
	function validateEmail(){
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		address = jQuery("#yourEmail").val();
		$(".emailError").hide();
		emailValid = true;
		
		if(reg.test(address)) {
	   	}
		else	{
			emailValid = false;
			$("#yourEmailError").show();
		}
		
		
		address = jQuery("#recipientEmail").val();
		if(reg.test(address) != false) {
		}
		else	{
			emailValid = false;
			$("#recipientEmailError").show();
		}
		
		if (emailValid)	{
			$(".emailThisForm").css("display","none");
			$("#emailThisSend").hide();
			$(".emailThisSent").css("display","block");
			return true;
		}
		else	{
			return false;
		}
	   
	}
	
	function sendEmail(){
		shareMessage = $("#shareMessage").val();
		shareAuthor = $(".txt_author").html();
		shareTitle = $(".entry-title h1").html();
		shareExcerpt = $(".entry-title h2").html();
		shareID = $(".post").attr("id");
		
		if ( shareTitle == 'Shanah Tovah! – Apples and Honey by the Macaroons'){
			shareTitle = 'Shanah Tovah!';
			shareExcerpt = 'Apples and Honey';
			shareAuthor = 'by The Macaroons';
		}
		
		$.ajax({
			type: "POST",
			url: "/share/share.php",
			data: {
				sender: jQuery("#yourEmail").val(),
				recip: jQuery("#recipientEmail").val(),
				message: shareMessage,
				title: shareTitle,
				author: shareAuthor,
				excerpt: shareExcerpt,
				articleURL: location.pathname,
				articleID: shareID
			},
			success: function(response) {
			},
			error: function(r, t, e) {
				alert(e);
			}
		});
	}
	
	jQuery("#sendAnother").click(function(){
		
			$(".emailThisForm").css("display","block");
			$("#emailThisSend").show();
			$(".emailThisSent").css("display","none");
			jQuery("#recipientEmail").val("");
	});
	
	jQuery(".newWindow a").not("#emailThis").not(".thumb").click(function(){
		var str = $(this).attr("href");
		if ( str.search("tabletmag") == -1 )	{
			var load = window.open($(this).attr("href"));
			return false;
		}
		else	{
			return true;
		}
	});
	
	jQuery("#quizSubmit").click(function(){
		$(this).hide();
		$("#finalCount").show().css("color","green");
		
		var totalCorrect = 0;
		
		if ( $("input[@name=q1]:checked").hasClass('correct') ){
			totalCorrect++;
		}
		if ( $("input[@name=q2]:checked").hasClass('correct') ){
			totalCorrect++;
		}
		if ( $("input[@name=q3]:checked").hasClass('correct') ){
			totalCorrect++;
		}
		if ( $("input[@name=q4]:checked").hasClass('correct') ){
			totalCorrect++;
		}
		if ( $("input[@name=q5]:checked").hasClass('correct') ){
			totalCorrect++;
		}
		if ( $("input[@name=q6]:checked").hasClass('correct') ){
			totalCorrect++;
		}
		if ( $("input[@name=q7]:checked").hasClass('correct') ){
			totalCorrect++;
		}
		if ( $("input[@name=q8]:checked").hasClass('correct') ){
			totalCorrect++;
		}
		if ( $("input[@name=q9]:checked").hasClass('correct') ){
			totalCorrect++;
		}
		if ( $("input[@name=q10]:checked").hasClass('correct') ){
			totalCorrect++;
		}
		
		$(".correctAnswer").css("color","green").css("font-weight","bold");
		$(".entry input:radio").attr('disabled',true);
	    $("#playAgain").show();
		$("#showAnswers").show();
		
		if ( totalCorrect != 0 )	{
			$("#countNum").html(totalCorrect);
		}
		
		//$('html, body').animate({ scrollTop: $("#finalCount").offset().top }, 500);
		return false;
	});
	
	$("#playAgain").click(function(){
		
			$(this).hide();
			$("#finalCount").hide().css("color","green");
			$(".correctAnswer").css("color","black").css("font-weight","normal");
			$(".entry input:radio").attr('disabled',false);
			$("#quizSubmit").show();
			$("#showAnswers").hide();
			$('html, body').animate({ scrollTop: $(".entry-title").offset().top }, 500);
				
	});
	
	$(".lianaSlide .hotSpot").click(function(){ $(".lianaSlide .textPiece").show(); });
	$(".lianaSlide .textPiece .lianaClose").click(function(){ $(".lianaSlide .textPiece").hide(); });
	
	$(".lianaSlide .hotSpotTwo").click(function(){ $(".lianaSlide .textPieceTwo").show(); });
	$(".lianaSlide .textPieceTwo .lianaClose").click(function(){ $(".lianaSlide .textPieceTwo").hide(); });

	$(".lianaSlide .hotSpotThree").click(function(){ $(".lianaSlide .textPieceThree").show(); });
	$(".lianaSlide .textPieceThree .lianaClose").click(function(){ $(".lianaSlide .textPieceThree").hide();	});

	$(".lianaSlide .hotSpotFour").click(function(){ $(".lianaSlide .textPieceFour").show(); });
	$(".lianaSlide .textPieceFour .lianaClose").click(function(){ $(".lianaSlide .textPieceFour").hide();	});
	
	$(".lianaSlide .hotSpotFive").click(function(){ $(".lianaSlide .textPieceFive").show(); });
	$(".lianaSlide .textPieceFive .lianaClose").click(function(){ $(".lianaSlide .textPieceFive").hide();	});
	
	$(".lianaSlide .hotSpotSix").click(function(){ $(".lianaSlide .textPieceSix").show(); });
	$(".lianaSlide .textPieceSix .lianaClose").click(function(){ $(".lianaSlide .textPieceSix").hide();	});
});

