var $hijaxEnterForm = "",
	$downloadPage = "",
	$termsPage = "",
	boxheight = "",
	termsHeight = "";


$(document).ready(function(){
	// If on competition page hijax form
	if ($('#entryForm').length > 0) {
		$hijaxEnterForm = $('#entryForm');
		hijaxForm();
		setupTermsLink();
	}				   
						   
	$("#tweets").getTwitter({
			userName: "500Pink",
			numTweets: 1,
			loaderText: "Loading tweets...",
			slideIn: false,
			slideDuration: 250,
			showHeading: true,
			headingText: "Latest Tweets",
			showProfileLink: false,
			showTimestamp: true
		});	
	
	$('#enter').click(function (e) {
		e.preventDefault();

		if ($hijaxEnterForm.length > 0) {
			animateBox();
			hijaxForm();
		} else {		
			$.ajax({
				type: "GET",
				url: "/includes/form.php",
				success: function(html){
					$('#popupContainer').html(html);
					$hijaxEnterForm = $('#entryForm');
					
					$("#tracking_iframe").attr("src","/form.php");
					
					boxHeight = $hijaxEnterForm.height();
					animateBox();	
					hijaxForm();
					setupTermsLink();
				}
			});
		}
	});	
	
	$('#downloads-links').click(function (e) {
		e.preventDefault();

		if ($downloadPage.length > 0) {
			animateDownloads();
		} else {		
			$.ajax({
				type: "GET",
				url: "/includes/downloads.php",
				success: function(html){
					$('#downloadsContainer').html(html);
					$downloadPage = $('#downloads-pop');
					
					$("#tracking_iframe").attr("src","/downloads.php");
					
					//boxHeight = $downloadPage.height();
					animateDownloads();
				}
			});
		}
	});				
});

function setupTermsLink(){
	$('#callTerms').click(function (e) {
		e.preventDefault();

		if ($termsPage.length > 0) {
			animateTerms();
		} else {	
			$.ajax({
				type: "GET",
				url: "/includes/terms.php",
				success: function(html){

					$('#termsContainer').html(html);
					$termsPage = $('#terms-content');
					
					$("#tracking_iframe").attr("src","/terms.php");
					
					termsHeight = $termsPage.height();
					animateTerms();	
				}
			});
		}
	});
}

function animateTerms () {
	$('#terms-content').hide();
	$('#hideTop').hide();
	$('#hideTop').fadeIn(450);
	$('#terms-content').fadeIn(450, function () {
		$('#terms-close').fadeIn(100, function(){
			$('#terms-close').click(function(e){
				e.preventDefault();
				$('#terms-content').fadeOut(350);	
				$('#hideTop').fadeOut(450);
				$('#terms-close').hide();
			});
		});
	});
}

function animateBox () {
	$('#popup').hide();
	$('#popup-close').hide();
	$('#popup').fadeIn(450, function () {
		$('#popup-close').fadeIn(100, function(){
			$('#popup-close').click(function(e){
				e.preventDefault();
				$('#popup').fadeOut(350);	
				$('#popup-close').hide();
			});
		});
	});
}

function animateDownloads () {
	$('#d-popup').hide();
	$('#downloads-popup-close').hide();
	$('#d-popup').fadeIn(450, function () {
		$('#downloads-popup-close').fadeIn(100, function(){
			$('#downloads-popup-close').click(function(e){
				e.preventDefault();
				$('#d-popup').fadeOut(350);	
				$('#downloads-popup-close').hide();
			});
		});
	});
}

function hijaxForm(){
	$hijaxEnterForm.validate({
		rules: {
			"frm-terms_agree": "required",
			"frm-Mobile": "required",
			"frm-Email": {
				required: true,
				email: true
			},
			"frm-Surname": "required",
			"frm-Name": "required"
		},
		messages: {
			"frm-terms_agree" : "- You must agree to the terms and conditions",
			"frm-Mobile" : "",
			"frm-Email" : "",
			"frm-Surname" : "",
			"frm-Name" : ""
		},
		invalidHandler: function(form, validator) {
			var errors = validator.numberOfInvalids();
			//$stayInformedFrm.find("label.lbl-error").removeClass("lbl-error");
			$hijaxEnterForm.find(".errorMsg").remove();
			if (errors) {
				$("#submit").before('<p class="errorMsg">Please check all highlighted fields</p>');
			}
		},
		onfocusout: false,
		onkeyup: false,
		submitHandler: function() { 
			var url			= $hijaxEnterForm.attr("action"),
				data		= $hijaxEnterForm.serialize();			
			$hijaxEnterForm.find(".errorMsg").remove();
			
			$.ajax({  
				type: "POST",  
				url: url,  
				data: data,  
				success: function(html){
					$('#popupContainer').html(html);
					$("#tracking_iframe").attr("src","/thanks.php");
					$('#popup-close').fadeIn(100, function(){
						$('#popup-close').click(function(e){
							e.preventDefault();
							$('#popup').fadeOut(350);	
							$('#popup-close').hide();
						});
					});
				}  
			});
			return false;
		}
	});
}