var activePopup = false;
var activeFeedback = false;
var activeUpload = false;
var activePrivacy = false;

var activeMySearches = false;
var activeMyShortlist = false;

var warning = false;

$(document).ready(function() {

	toplevellink();
	onePopupToRuleThemAll();
	feedback();
	
	$("div.privacy_policy p.please_read").hide();
	$("div.privacy_policy").append('<div id="privacy_policy"><iframe src="http://www.gatenbysanderson.com/privacy_policy/privacy_policy_job_microsite/" frameborder="0" scrolling="no"></iframe></div>');
	
	function onePopupToRuleThemAll()  {
		
		$("#acc_search").click(function (e) {
			if(!activeMySearches) {
				if(activePopup) removePopup();
				var left = $(this)[0].offsetLeft - 310;
				var thelink = this.getElementsByTagName("A")[0];
				$(this).append('<div id="popup" class="my_account"><iframe style="left:'+ left + 'px;top:-10px;" src="' + thelink + '" frameborder="0" scrolling="no"></iframe></div>');	
				$(this).addClass('popped_out')
				activePopup = true;
				activeMySearches = true;
			}
			return false;
		});
		
		$("#acc_shortlist").click(function (e) {
			if(!activeMyShortlist) {
				if(activePopup) removePopup();
				var left = $(this)[0].offsetLeft - 310;
				var thelink = this.getElementsByTagName("A")[0];
				$(this).append('<div id="popup" class="my_account"><iframe style="left:'+ left + 'px;top:-10px;" src="' + thelink + '" frameborder="0" scrolling="no"></iframe></div>');	
				$(this).addClass('popped_out')
				activePopup = true;
				activeMyShortlist = true;
			}
			return false;
		});
		
		$(".email_to_friend").click(function(e) {
			if(activePopup) removePopup();
			var left = e.pageX - 325;
			var top = e.pageY - 260;
			$("BODY").append('<div id="popup" class="email_to_friend"><div class="pop_holder" style="left:'+ left + 'px;top:' + top + 'px;"><span class="top">&nbsp;<\/span><span class="right">&nbsp;<\/span><span class="bottom">&nbsp;<\/span><span class="left">&nbsp;<\/span><iframe  src="' + this.href + '" frameborder="0" scrolling="no"><\/iframe><\/div><\/div>');
			activePopup = true;
			return false;
		});
		
		
		//$("#confirm_privacy").hide();
		$("a.privacy_policy").click(function(e) {
			if(!activePrivacy) {
				removePrivacy();
				$("div.privacy_policy").append('<div id="privacy_policy"><iframe src="' + this.href + '" frameborder="0" scrolling="no"></iframe></div>');
				//$("#confirm_privacy").show();
				activePrivacy = true;
			} else {
				removePrivacy();
				//$("#confirm_privacy").hide();
			}
			return false;
		});
		
		$(".add").click(function(e) {
			if(activePopup) removePopup();
			var left = e.pageX + 4;
			var top = e.pageY + 4;
			$("BODY").append('<div id="popup" class="add_doc"><iframe style="left:'+ left + 'px;top:' + top + 'px;" src="' + this.href + '" frameborder="0" scrolling="no"></iframe></div>');
			//activePopup = true;
			activeUpload = true;
			return false;
		});				 
		
		$("BODY").click(function() {
			if(activePopup == true) {
				removePopup();
			}
			if(activeUpload == true) {
				window.location.reload();
			}
			
			$('.popped_out').removeClass();
		});
	}
	
	function feedback() {
		$(".feedback").click(function() {
			if(!activeFeedback) {			
			var feedbackP = $(this).parent();
			var feedbackDiv = $(this).parent().parent();
				feedbackDiv.append('<div id="feedback" class="feeback_holder"><iframe  src="' + this.href + '" frameborder="0"></iframe></div>');
				activeFeedback = true;
				return false;
			} else {
				$('#feedback').remove();	
				activeFeedback = false;
			}
			return false;
		});
	}
	
	function removePopup() {
		$("#popup").remove();
		$('.flag').remove();
		activePopup = false;
		activeMySearches = false;
		activeMyShortlist = false;
		$('.popped_out').removeClass();
	}
	function removePrivacy() {
		$('.flag').remove();
		$("#privacy_policy").remove();
		activePrivacy = false;
	}
	
	function toplevellink() {
		$(".target_parent").click(function() {
			window.parent.location = this;
		});

	}
	
});

function closePopup() {
	var popup = document.getElementById('popup');
	popup.style.display = "none";
	$('.popped_out').removeClass();
}
function closeFeedback() {
	var feedback = document.getElementById('feedback');
	feedback.style.display = "none";
	activeFeedback = false;
}
function closePrivacy() {
	var privacy = document.getElementById('privacy_policy');
	privacy.style.display = "none";
	activePrivacy = false;
}
function closeUpload() {
	window.location.reload();
} 

function add_tandc_feedback()
{
	$("DIV.confirmation").prepend('<div id="confirmation_feedback" class="warning"><p class="warning">You must read the privacy policy to continue.<\/p><\/div>');
}

// removed additional questions error that's displayed when all boxes not ticked.
function clear_tandc_feedback() 
{
		$('#confirmation_feedback').remove();
}

$('form#continue_form')
    	// add a submit handler to the form for dealing with additional questions...
	    .submit(function(){
			// remove existing warning if trying again...
			clear_tandc_feedback();
			
			// check each additional question tickbox is ticked.
			if(!  $('#confirm_privacy_checkbox').is(':checked') )
			{
				add_tandc_feedback();
				return false;
			}
			return true;
		}
	);
