﻿function goToUrl(url, event) //Used by the button at the end of the demo movie
{
	//we'll ignore where the movie wants to send the browser and explicitly send it to get started here.
	Analytics_TrackPage('/session-demo.html?event=' + event, '/session-demo.html?event=' + event);
	location.href = url;
}

Event.observe(window, 'load', function() {
	//The navigation uses the 'superfish' implementation.  Here's how we set it up.
	var $j = jQuery;
	$j('ul.sf-menu').superfish({
		pathClass: 'current',
		autoArrows: false,
		speed: 0,
		delay: 400
	});

	//attach behaviors for spanSubmit / spanSubmitWait (hiding submit buttons if they exist)
	var atags = $$('#spanSubmit a');
	if (atags.length > 0) {
		//handling LinkButtons which render as <a> tags
		atags.each(function(item) { item.observe('click', doPreSubmit) });

		//form submit
		var form = $$('form')[0];
		if (form) {
			form.observe('submit', doPreSubmit);
		}

		//observe enter key press for form submit
		document.observe('keydown', doKeyPress);
	}

	var currentFocus = null;
	$j('textarea').focus(function() {
		FOCUS_TEXTAREA = true;
	}).blur(function() {
		FOCUS_TEXTAREA = false;
	});
});

var JS_SEARCH = 'javascript:';
var FOCUS_TEXTAREA = false;

function doKeyPress(e) {
	if (FOCUS_TEXTAREA)
		return;

	if (window.event) // IE
		keynum = e.keyCode;
	else if (e.which) // Netscape/Firefox/Opera
		keynum = e.which;

	if (keynum == Event.KEY_RETURN) {
		if (doPreSubmit()) {
			//find the href and pretend we clicked it
			var atags = $$('#spanSubmit a');

			var ref = atags[0].href;

			if (ref.substring(0, JS_SEARCH.length) == JS_SEARCH) {
				ref = unescape(ref.replace(/javascript:/g, ''));
				eval(ref);
			}
			else {
				//rmg: this doesn't work on mac safari
				location = atags[0].href;
			}
		}
	}
}

function doPreSubmit() {
	//call client validate so we don't hide button unless we pass validation
	if (typeof Page_ClientValidate != 'undefined')
		Page_ClientValidate();

	//IMPORTANT: only hide the submit button if the page appears valid, otherwise the postback doesnt fire but the button goes away, which is, how you say, teh suck
	if (typeof Page_IsValid == 'undefined' || (typeof Page_IsValid != 'undefined' && Page_IsValid)) {
		var init = false;
		//init is used for "onpageshow" event with firefox 1.5
		if ($('spanSubmit') != null && $('spanSubmitWait') != null) {
			document.getElementById('spanSubmit').style.display = ((init) ? 'inline' : 'none');
			document.getElementById('spanSubmitWait').style.display = ((init) ? 'none' : 'inline');
		}

		//if we have spanSubmitTop and spanSubmitWaitTop, toggle those too
		if ($('spanSubmitTop') != null && $('spanSubmitWaitTop') != null) {
			document.getElementById('spanSubmitTop').style.display = ((init) ? 'inline' : 'none');
			document.getElementById('spanSubmitWaitTop').style.display = ((init) ? 'none' : 'inline');
		}

		return true;
	}

	return false;
}

function FlipNavState(event) {
	var nav_item = Event.element(event);
	if (event.type == "mouseover") {
		nav_item.src = nav_item.src.replace("dark", "light");
	}
	else {
		nav_item.src = nav_item.src.replace("light", "dark");
	}
}

function AjaxLogErrors() {
	alert("Oops! An error occured.  Our tech team is on it.  Please try again later.");
}

function doCounter() {
	var method = "GetTotalSessionsCount";

	//do we have a position we are remembering?
	var c = document.cookie;
	if (c.indexOf(method) != -1) {
		var start = c.indexOf(method) + method.length + 1;
		var end = c.indexOf(';', start);

		if (end == -1)
			end = c.length;

		var val = c.substring(start, end);

		setCounter(val);
	}			

	//call webservice to get count of current sessions
	var request = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("MSXML2.XMLHTTP.3.0");

	var soapDoc = '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">' +
						'<soap:Body>' +
							'<' + method + ' xmlns="http://www.tutor.com/" />' +
						'</soap:Body>' +
					  '</soap:Envelope>';

	request.open("POST", "/counter/counter.asmx", true);
	request.setRequestHeader("Content-Type", "text/xml");

	request.onreadystatechange = function() {
		if (request.readyState == 4) {
			var nextOne = Math.floor(Math.random() * 1000) + 5000;

			if (request.status == 200) {
				var num = getNumberFromResponse(request.responseText);

				//remember it
				document.cookie = method + '=' + num + '; path=/';

				setCounter(num);
			}

			//do it again in 5 secs
			setTimeout("doCounter()", nextOne);
		}
	}

	request.send(soapDoc);
}

function getNumberFromResponse(response) {
	//code for IE
	if (window.ActiveXObject) {
		var doc = new ActiveXObject("Microsoft.XMLDOM");
		doc.async = "false";
		doc.loadXML(response);
	}
	// code for Mozilla, Firefox, Opera, etc.
	else {
		var parser = new DOMParser();
		var doc = parser.parseFromString(response, "text/xml");
	}

	return doc.documentElement.childNodes[0].childNodes[0].childNodes[0].childNodes[0].nodeValue;
}

function setCounter(num) {
	document.getElementById('countertext').innerHTML = addCommas(num) + '<span style="font-weight:normal"> sessions and counting!</span>';
}

function addCommas(nStr) {
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}
