﻿$(document).ready(
	function() {
		$(".enter_as_click").keydown(EnterAsClick);

		$("a#theTeam").click(
			function() {
				this.blur();
				TrackEvent('The Team', 'View');
				CreateBodyOverlay(hidePopUp);
				$("#popup_overlay").css('opacity', '0.6');
				GetContentCallback('team', '');
				return false;
			}
		);

		$("a#virtual-tour").click(
			function() {
				this.blur();
				TrackEvent('Virtual Tour', 'View');
				CreateBodyOverlay(hidePopUp);
				$("#popup_overlay").css('opacity', '0.6');
				GetContentCallback('tour', '');
				return false;
			}
		);

		$("a#gallery").click(
			function() {
				this.blur();
				TrackEvent('Gallery', 'View');
				CreateBodyOverlay(hidePopUp);
				$("#popup_overlay").css('opacity', '0.6');
				GetContentCallback('gallery', '');
				return false;
			}
		);

		$("a.news").click(
			function() {
				this.blur();
				TrackEvent('News', 'View');
				CreateBodyOverlay(hidePopUp);
				$("#popup_overlay").css('opacity', '0.6');
				GetContentCallback('news', '');
				return false;
			}
		);

		$("a#googleEarth").click(
			function() {
				this.blur();
				TrackEvent('Google Earth', 'View');
				CreateBodyOverlay(hidePopUp);
				$("#popup_overlay").css('opacity', '0.6');
				GetContentCallback('google', '');
				return false;
			}
		);

		$("a#contact").click(
			function() {
				this.blur();
				TrackEvent('Contact Us', 'View');
				CreateBodyOverlay(hidePopUp);
				$("#popup_overlay").css('opacity', '0.6');
				GetContentCallback('contact', '');
				return false;
			}
		);

		$("a#tips").click(
			function() {
				this.blur();
				TrackEvent('Tips', 'View');
				window.open("http://dx-code.org/", "_blank");
				return false;
			}
		);

		$("a#donate").click(
			function() {
				this.blur();
				TrackEvent('Donate', 'View');
				CreateBodyOverlay(hidePopUp);
				$("#popup_overlay").css('opacity', '0.6');
				GetContentCallback('donate', '');
				return false;
			}
		);

		$("a#history").click(
			function() {
				this.blur();
				TrackEvent('History', 'View');
				CreateBodyOverlay(hidePopUp);
				$("#popup_overlay").css('opacity', '0.6');
				GetContentCallback('history', '');
				return false;
			}
		);

		$("a#rtlog").click(
			function() {
				this.blur();
				TrackEvent('Real Time Log', 'View');
				CreateBodyOverlay(hidePopUp);
				$("#popup_overlay").css('opacity', '0.6');
				GetContentCallback('rtlog', '');
				return false;
			}
		);

		$("a#sponsors").click(
			function() {
				this.blur();
				TrackEvent('Sponsors', 'View');
				CreateBodyOverlay(hidePopUp);
				$("#popup_overlay").css('opacity', '0.6');
				GetContentCallback('sponsors', '');
				return false;
			}
		);

		$("a#logsearch").click(
			function() {
				this.blur();
				TrackEvent('Log Search', 'View');
				CreateBodyOverlay(hidePopUp);
				$("#popup_overlay").css('opacity', '0.6');
				GetContentCallback('loadLogSearch');
				return false;
			}
		);

		$("input#ctl00_ContentPlaceHolder_btnSubmit").click(
			function() {
				this.blur();
				CreateBodyOverlay();
				$('#popup_overlay').css('opacity', '0.6');
				ShowUploadSpinner();
			}
		);

		var hidePopUp = function() {
			$("#popup_overlay").remove();
		};
	}
);

/** HELPER FUNCTIONS **/
function CreateBodyOverlay(onClickFunction, tagName) {
	tagName = tagName || 'popup_overlay';
	$("body").append('<div id="' + tagName + '"/>');
	$("#" + tagName).click(onClickFunction);
	if (window.innerHeight && window.scrollMaxY) {
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight) { // all but Explorer Mac
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		yScroll = document.body.offsetHeight;
	}
	$("#" + tagName).css("height", yScroll + "px");
}

function CloseAddToCartConfirmation() {
	if ($("#popup_overlay").css("display") == "block") {
		$("#popup_overlay").remove();
	}
	$("#addtocart_confirmation").hide();
	return false;
}

function ParseBool(toTest) {
	var result = false;
	// initialization of Boolean with a string "false" results in true.  So intercept it here.
	if ((toTest != undefined) && (toTest.toLowerCase() == 'false')) {
		toTest = false;
	}
	if (new Boolean(toTest) == true) {
		result = true;
	}
	return result;
}

function ReceiveContentCallback(args) {
	CreateBodyOverlay();
	$("body").append('<div id="content_hopup"/>');
	// $("#content_hopup").css('display', 'block');
	$("#content_hopup").html(args);
	$("#content_hopup").fadeIn('500');
	var layerWidth = $("#content_hopup").width();
	var layerHeight = $("#content_hopup").height();
	var windowDimensions = GetWindowDimensions();
	var windowOffests = GetWindowOffsets();
	var positionLeft = windowOffests[0] + Math.round(((windowDimensions[0] - layerWidth) / 2));
	var positionTop = windowOffests[1] + Math.round(((windowDimensions[1] - layerHeight) / 2));
	$("#popup_overlay").css('opacity', '0.6');
	DragLayer.init(document.getElementById("hopup_title_bar"), positionLeft, positionTop, document.getElementById("content_hopup"));
	$("#hopup_title_bar img.close_hopup").click(
	function() {
		CloseHopup();
		return false;
	});

	$("a#donationTrigger").click(
		function() {
			ProcessDonationClick();
		}
	);

	$("a#searchTrigger").click(
		function() {
			ProcessSearchClick();
		}
	);
	$(".enter_as_click").keydown(EnterAsClick);
	SetRtLogHoverCss();
}

function ShowUploadSpinner() {
	$("body").append('<div id="content_upload_spinner"/>');
	$("#content_upload_spinner").html($("#upload_spinner").html());
	$("#content_upload_spinner").fadeIn('500');
	var layerWidth = $("#content_upload_spinner").width();
	var layerHeight = $("#content_upload_spinner").height();
	var windowDimensions = GetWindowDimensions();
	var windowOffests = GetWindowOffsets();
	var positionLeft = windowOffests[0] + Math.round(((windowDimensions[0] - layerWidth) / 2));
	var positionTop = windowOffests[1] + Math.round(((windowDimensions[1] - layerHeight) / 2));
	DragLayer.init(document.getElementById("upload_qrx"), positionLeft, positionTop, document.getElementById("content_upload_spinner"));
}
	
var liveLogTimerEvent = undefined;
var liveLogTimerRunning = false;
function UpdateLiveLog() {
	if (liveLogTimerEvent == undefined) {
		liveLogTimerEvent = setTimeout("UpdateLiveLog()", 10000);
	} else {
		GetLiveLogCallback('rtLogUpdate', '');
		liveLogTimerEvent = setTimeout("UpdateLiveLog()", 10000);
	}
	if (!liveLogTimerRunning) {
		liveLogTimerRunning = true;
	}
}

function ReceiveLiveLogCallback(args) {
	$("#rtLogContacts").html(args);
	SetRtLogHoverCss();
}

function ReceiveSearchLogCallback(args) {
	$('#log_search_content').html(args);
	// $("input#search").css({ border: '1px solid #ff0000', background: '#ffc' });
	var searchInput = $('#search_table').find('#search');
	$("#spinnerAction").html('');
	ResetSearchBox();
}

function SetRtLogHoverCss() {
	$(".rtlog_line_wrap_bg1").css({ background: '#E6E6E6' }).hover(
		function() {
			$(this).css({ background: '#B8E1FE' });
		},
		function() {
			$(this).css({ background: '#E6E6E6' });
		}
	);
	$(".rtlog_line_wrap_bg2").css({ background: '#F8F8F8' }).hover(
		function() {
			$(this).css({ background: '#B8E1FE' });
		},
		function() {
			$(this).css({ background: '#F8F8F8' });
		}
	);
}

function CloseHopup() {
	if (liveLogTimerRunning == true) {
		ClearLiveLogTimer();
	}
	$("#content_hopup").fadeOut('fast', function() {
		RemoveHopup()
	});
	$("#popup_overlay").fadeOut('slow', function() {
		RemoveOverlay()
	});
}

function ClearLiveLogTimer() {
	clearTimeout(liveLogTimerEvent);
	liveLogTimerEvent = undefined;
	liveLogTimerRunning = false;
}

function RemoveHopup() {
	$("#content_hopup").remove();
}

function RemoveOverlay() {
	$("#popup_overlay").remove();
}

function ProcessDonationClick() {
	var donationFrom = $('input#donationFrom').val();
	if (IsFilled(donationFrom)) {
		$("#donateContent").html($("#spinner").html());
		var url = 'https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=NCZHVGK27NPLG&item_name=Bonaire%202010%20DXpedition%20Donation%20from%20' + UrlEncode(Trim(donationFrom));
		window.location = url;
	} else {
		HighlightDonationFromBox();	
	}
	return false;
}

function HighlightDonationFromBox() {
	$("input#donationFrom").css({ border: '1px solid #ff0000', background: '#ffc' });
	var fromInput = $('#donation_table').find('#donationFrom');
	FocusInputControl(fromInput);
}

function ProcessSearchClick() {
	var callToSearch = $('input#search').val();
	if (IsFilled(callToSearch)) {
		$("#spinnerAction").html($("#spinner").html());
		// ClearPreviousQsosGrid();
		GetSearchLogCallback('doLogSearch:' + Trim(callToSearch));
	} else {
		HighlightSearchBox();
	}
	return false;
}

function HighlightSearchBox() {
	$("input#search").css({ border: '1px solid #ff0000', background: '#ffc' });
	var searchInput = $('#search_table').find('#search');
	FocusInputControl(searchInput);
}

function ResetSearchBox() {
	$("input#search").css({ border: '1px solid #052D4E', background: '#fff' });
	var searchInput = $('#search_table').find('#search');
	ClearInputControl(searchInput);
	FocusInputControl(searchInput);
}

function FocusInputControl(controlToFocus) {
	if (controlToFocus != undefined && controlToFocus != null) {
		controlToFocus.focus();
	}
}

function ClearInputControl(controlToClear) {
	if (controlToClear != undefined && controlToClear != null) {
		controlToClear.val('');
	}
}

function GetWindowDimensions() {
	var theWidth = 0;
	var theHeight = 0;
	if (typeof (window.innerWidth) == 'number') {
		//Non-IE
		theWidth = window.innerWidth;
		theHeight = window.innerHeight;
	} else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
		//IE 6+ in 'standards compliant mode'
		theWidth = document.documentElement.clientWidth;
		theHeight = document.documentElement.clientHeight;
	} else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
		//IE 4 compatible
		theWidth = document.body.clientWidth;
		theHeight = document.body.clientHeight;
	}
	return [theWidth, theHeight];
}

function GetWindowOffsets() {
	var xScroll = 0;
	var yScroll = 0;
	if (typeof (window.pageYOffset) == 'number') {
		//Netscape compliant
		yScroll = window.pageYOffset;
		xScroll = window.pageXOffset;
	} else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
		//DOM compliant
		yScroll = document.body.scrollTop;
		yScroll = document.body.scrollLeft;
	} else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
		//IE6 standards compliant mode
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	}
	return [xScroll, yScroll];
}

function Trim(toTrim) {
	return toTrim.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}

function IsFilled(toValidate) {
	var result = false;
	if (toValidate != undefined && toValidate != null && Trim(toValidate) != '') {
		result = true;
	}
	return result;
}

function JavascriptEscape(toEscape) {
	return toEscape.replace("\r", " ").replace("\n", " ").replace("'", "\\'").replace("\"", "\\\"");
}

function UrlEncode(toEncode) {
	return escape(toEncode).replace(/\+/g, '%2B').replace(/%20/g, '+').replace(/\*/g, '%2A').replace(/\//g, '%2F').replace(/@/g, '%40');
}

function TrackEvent(category, action, label, value) {
	_gaq.push(['_trackEvent', category, action, label, value]);
}

function EnterAsClick (e) {
	var btn = $("input.default_button");
	var lnk = $("a.default_button");
	if (e.keyCode == 13) {
		if (btn.html() != null) {
  	 		e.preventDefault();
  	 		btn.get(0).click();
  	 	} else if (lnk.html() != null) {
  	 		e.preventDefault();
  	 		lnk.click();
		}
	}
}


