//Setup
var oldReferTo;
var popUpHidden = true;

//ShowHide Function for Popup
function showHidePopUp() {
	if(popUpHidden == true) {
		$('#popUpLayer').show();
		$('#popUpSubLayer').show();
		popUpHidden = false;
	}else{
		$('#popUpLayer').html('');
		$('#popUpLayer').hide();
		$('#popUpSubLayer').hide();
		popUpHidden = true;
	}
}

//initialize hover effect for the side & referbar
function initHover() {
	//ReferTo Hover
	$('.referToIcon').hover(
		function(event) {
			oldReferTo = $(event.target).attr('src');
			newReferTo = oldReferTo.substring(0, oldReferTo.length - 7);
			$(event.target).attr('src', newReferTo + '.png');
		},
		function(event) {
			$(event.target).attr('src', oldReferTo);
		}
	);
}

//get scrollbar width, height
function scrollbarWidth() {
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
}

//Set PopUpSize
function setPopUpSize() {
	
	//calculate screen size
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	}
	else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}
	
	//Set width etc. accordingly
	var myElWidth = $('#popUpLayer').css('width');
	var myElHeight = $('#popUpLayer').css('height');
	myElWidth = parseFloat(myElWidth);
	myElHeight = parseFloat(myElHeight);
	var myElNewLeft = parseFloat((windowWidth / 2) - parseFloat(myElWidth / 2));
	var myElNewTop = parseFloat((windowHeight / 2) - parseFloat(myElHeight / 2));
	
	$('#popUpLayer').css({
		left: myElNewLeft,
		top: myElNewTop
	});
}

//document ready
$(document).ready(function() {
	
	$('#popUpSubLayer').click(function() {
		showHidePopUp();
	});
	
	//ReferTo Hover
	$('.referTo').hover(

		function(event) {
			oldReferTo = $(event.target).attr('src');
			newReferTo = oldReferTo.substring(0, oldReferTo.length - 7);
			$(event.target).attr('src', newReferTo + '.png');
		},
		function (event){
			$(event.target).attr('src', oldReferTo);
		}
	);
		
	//PopUp: referBar
	$('#referBar').click(function() {
		$('#popUpLayer').attr('class', 'popUpSmall');
		if(popUpHidden == true) { //Save bandwidth
			// ajax get template
		   	$.ajax({
				type: 'POST',
				data: 'action=referBar&title='+document.title+'&location='+document.location,
				url: '/recommendationAjax.php',
				success: function(result) {			
					if (result == false) {
						$('#popUpLayer').html('error...');
					}
					else
					{
						$('#popUpLayer').attr('class', 'popUpMedium');
						$('#popUpLayer').html(result);
						setPopUpSize();
						initHover();
					}
				}
			});
			//get Scale Info
			showHidePopUp();
			}
			else
			{
				showHidePopUp();
			}; //end popupHidden ifelse statements		
		});
	
	//PopUp: AGB
	$('#gtc_link').click(function() {
		$('#popUpLayer').attr('class', 'popUpLarge');
		if(popUpHidden == true) { //Save bandwidth
			// ajax get template
		   	$.ajax({
				type: 'POST',
				data: 'action=agb',
				url: '/recommendationAjax.php',
				success: function(result) {
					if (result == false) {
						$('#popUpLayer').html('error...');
					}
					else {
						$('#popUpLayer').html(result);
						setPopUpSize();
					}
				}
			});
			showHidePopUp();
		}
		else {
			showHidePopUp();
		}; //end popupHidden ifelse statements	
		return false;	
	});
	

	
	// PopUp: referEmail
	$('#referEmail').click(function() {
		if(popUpHidden == true) { //Save bandwidth
		// ajax get template
	   	$.ajax({
			type: 'POST',
			data: 'action=referEmail',
			url: '/recommendationAjax.php',
			success: function(result) {			
				if (result == false) {
					$('#popUpLayer').html('error...');
				}
				else {
					$('#popUpLayer').attr('class', 'popUpMedium');
					$('#popUpLayer').html(result);
					setPopUpSize();
				}
			}
		});
		showHidePopUp();
		}
		else
		{
			showHidePopUp();
		}; //end popupHidden ifelse statements		
	});
	
	
	//PopUpisVisible
	$('#popUpClose').livequery('click', function() {
		showHidePopUp();
	});
	
	$('#popUpBtn').livequery('click', function() {
		var name = $('#popUpName').val();
		var email = $('#popUpEmail').val();

		// ajax get template
		$.ajax({
			type: 'POST',
			url: '/recommendationAjax.php',
			data:
				'email=' + email + '&name=' + name + '&button_submit=1' + '&action=referEmail',
			success: function(result) {			
				if (result == false) {
					$('#popUpLayer').html('error...');
				}
				else {
					$('#popUpLayer').html(result);
				}
			}
		});
	});
});