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="'+window.location.protocol+'//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;
		});

	}
	
	
	if($('#apply-login-form').attr('class')) {
	} else {
		$('#apply-login-form').hide();		
	}
	
	if($('#apply-registration-form').attr('class')) {
	} else {
		$('#apply-registration-form').hide();		
	}

	$('#apply a.apply-login-form').click(function() {
		if($('#apply-registration-form').attr('class')) {
			$('#apply-registration-form').removeClass('show').slideUp();
		}
		if($('#apply-login-form').attr('class')) {
			
		} else {
			$('#apply-login-form').addClass('show').slideDown();		
		}
		return false;
	});
	$('#apply a.apply-registration-form').click(function() {
		if($('#apply-login-form').attr('class')) {
			$('#apply-login-form').removeClass('show').slideUp();
		}
		if($('#apply-registration-form').attr('class')) {
			
		} else {
			$('#apply-registration-form').addClass('show').slideDown();		
		}
		return false;
	});
	
	
});

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();
} 
