var currentVideo = 0;
var n = '';

$(document).ready(function() {
	$("#submitButton").click(
		function(){
			postNewsletter();
		}
	);
	
	
	$("#dither").click(function(e) {
        $("#dither").css({ display: "none" });
        $("#popover").css({ display: "none" });
        $("#popover2").css({ display: "none" });
        $("#popover3").css({ display: "none" });
		e.stopPropagation();
	});
	
	$("#popover2 .innerContainer a").click(function(){
		commentPostNewsletter();
	});
	
	$(".close").click(function(e) {
        $("#dither").css({ display: "none" });
        $("#popover").css({ display: "none" });
        $("#popover2").css({ display: "none" });
        $("#popover3").css({ display: "none" });
		e.stopPropagation();
	});
	
	$('.popup450x500').click(function() {
		window.open(this.href, "privacyPolicy", "status=0, height=500, width=450, resizable=0, scrollbars=yes");
		Tracker.track('/popup/'+$(this).attr("id"));
		return false;	
	});
	
	$("#tixButton").click(function(){
		Tracker.track('/ticketsButton/');
	});
	
	$("#socialMedia img").click(function(){
		Tracker.track('/socialMedia/'+$(this).attr("id"));	
	});
	
	$("#commentSubmit").unbind("click").click(function() {
		var errors = [];
		$(this).attr("disabled","disabled");
		if(!$("#commentName").val().length) {
			errors.push("You must provide your first name.");
		}
		
		if(!(/^[\w\.+-]+@[\w\.+-]+\.[\w\.+-]*[a-z]{2}$/i.test($("#commentEmail").val()))) {
			errors.push("You must provide a valid email address.")
		}
		
		if(!Captcha.validate($("#captchaInput").val())) {
			errors.push("The captcha is incorrect.");
		}
		
		
		//uncomment on go live
		
		//if(errors.length) {
		//	alert("Please correct the following error" + (errors.length != 1 ? "s" : "") + ":\n\n" + errors.join("\n"));
		//} else {
			postComment();
			popover();
			popover3();
		//}
		
	});
});

function trackThis(path){
		Tracker.track('/flash/'+path);
}

function popover() {
	if ($("#commentInput").val()!=''){
	    if($("#popover").css("display") != "none") {
	        $("#dither").css({ display: "none" });
	        $("#popover").css({ display: "none" });
	    } else {
	        $("#dither").css({ display: "block", height: $(document).height() }).fadeTo(1, 0.7);
	        $("#popover").css({ display: "block" });
	    }
	}
	else{
		alert('Please enter a comment');
	}
}

function popover2() {
    if($("#popover2").css("display") != "none") {
        $("#dither").css({ display: "none" });
        $("#popover2").css({ display: "none" });
    } else {
        $("#dither").css({ display: "block", height: $(document).height() }).fadeTo(1, 0.7);
        $("#popover2").css({ display: "block" });
    }

}
function popover3() {
    if($("#popover3").css("display") != "none") {
        $("#dither").css({ display: "none" });
        $("#popover3").css({ display: "none" });
    } else {
        $("#dither").css({ display: "block", height: $(document).height() }).fadeTo(1, 0.7);
        $("#popover3").css({ display: "block" });
    }

}

function postNewsletter() {
	
	e	= $("#email").val();
	if(!e || !e.match(/^[\w\.+-]+@[\w\.+-]+\.[\w\.+-]+$/)) {
		alert("You must enter a valid email address.");
		return;
	}
	
	popover2();
	$("#commentEmail").val(e);
	$("#newsletter").fadeTo(300, 0.0, function() {
		$(this).html('<div id="newsletterText">Please wait...</div>');
		$(this).fadeTo(300, 1.0);
		
	});
	
	$.ajax({
		type: "POST",
		url: "/addNewsletter.php",
		data: {
			account_id: "312",
			email: e
		},
		success: function(response) {
			//if(/error/.test(response)) { alert(response); }
			$("#signup").fadeTo(300, 0.0, function() {
				$(this).html('<img src="/img/you_have_been_added.gif"/>');
				$(this).fadeTo(300, 1.0);
			});
		},
		error: function(r, t, e) {
			$("#newsletter").fadeTo(300, 0.0, function() {
				$(this).html('An error has occurred, please try again later.');
				$(this).fadeTo(300, 1.0);
			});
		}
	});
}

function postComment(){
	var comment = $("#commentInput").val();
	var nameIn = $("#commentName").val();
	var emailIn = $("#commentEmail").val();
	$.ajax({
		type: "POST",
		url: "/postComment.php",
		data: {
			text: comment,
			target_id: currentVideo,
			email: emailIn,
			name: nameIn
		},
		complete: function(response){
			$("#commentSubmit").removeAttr("disabled");
		}
	});
}

function setCurrentVideo(num){
	currentVideo = num;
}

function commentPostNewsletter() {
	
	e	= $("#commentEmail").val();
	n	= $("#commentName").val();
	$("#email").val(e);
	if(!is_valid_email(e)) {
		alert("You must enter a valid email address.");
		return;
	}
	$("#commentEmail").val(e);
	$("#newsletter").fadeTo(300, 0.0, function() {
		$(this).html('<div id="newsletterText">Please wait...</div>');
		$(this).fadeTo(300, 1.0);
	});
	
	$.ajax({
		type: "POST",
		url: "/addNewsletterName.php",
		data: {
			account_id: "312",
			email: e,
			name: n
		}
	});
}

function is_valid_email (email) {
   	return /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(email);
}

var Captcha = new function CaptchaObject() {
	this.stored_value;
	this.validate = function(foo) {
		if(foo.length) {
			return hex_sha1(foo) == Captcha.stored_value;
		}
	}
}