/*
FUN PLANNER main JS file : FunPlanner.js
FUN PLANNER support JS file : util.js
FUN PLANNER support JS file : motionHandler.js
FUN PLANNER CSS IE file : funplanner.css
FUN PLANNER CSS Moz file : funplanner_ff.css

Structure : 
			There are four cookies that are used for information storage and retrieval.
			These are mirrored by four arrays that maintain information while you are on the same page.
			Once you leave the page those arrays get repopulated from cookies and so the information is
			persistent until cookies are cleared off the client machine.  Also, cookies have expiration
			set for December 31, 2007, this can be changed by changing the value of the 'CookiesExpiration' variable.

			Cookie structure :	Every entry in the cookie is pipe '|' deliminated.
								Every value in the entry is comma ',' deliminated.

			Names :		Cookies :			Arrays :
						'funname' :			'ArrName' : 	Fun Planner title
						'funmusts' :		'ArrEvents' :	Fun Planner added events
						'funmember' :		'ArrPeople' :	Fun Planner added members
						'funfiles' :		'ArrInfo' :		Fun Planner information and files selection

			Array structure :	All arrays elements are strings.
								'funname' :		(name,0) default is 'Your Fun Plan,0', zero is useless (for consistency)
								'funmusts' :	(type,name,link) empty by default, pass in link unencoded
								'funmember' : 	(name,height) empty by default, height is total inches
								'funfiles' :	(name,value) populated by default with all values 'on',
												value is either 'on' or 'off'.

								* All variables: name, type, link, value have to be passed in as strings; height is
												 calculated from feet and inches and also is stored as a string.

Integration :
			The .js files have to be included.  The 'motionHandler.js' is used only for the motion of the Fun Planner.
			The .css files are included automatically (in util.js) depending on the client browser.
			The two DIVs ('pulldown_wrapper' and 'pd_transOverlay') have to be included in the main body of the document,
			either by pasting or by a server-side include.
			
			To add an event an onclick or an href have to be used with the function 'AddEvent(type, name, lnk)' where :
				type is a string of the event type i.e. 'ride'
				name is a string of the event name i.e. 'Kingda Ka'
				lnk is a string of the raw link (unescape) to the event page
				
			* When stored in the cookie all links, fun plane title and all names of added members are encoded (escape).
*/

//onload = pd_init;
window.onresize = resetOverlay;
var name_to_edit;
function pd_init(){
	if (document.getElementById('pulldown_wrapper')) {
		loadCookies();
	}
}
function moveslide(){
	window.scroll(0,0);
	var c = getElementsByClass("container")[0];
	if(getObj('pulldown_wrapper').style.marginTop == "" || getObj('pulldown_wrapper').style.marginTop == "0px"){
		c.style.overflow = "visible";
		document.getElementById('midNav-menu').style.display = 'none';
		var hideFlash = function() {
			if ((client.engine == 'webkit') && client.os == 'mac') {
				var allDivs = document.getElementsByTagName('div');
				for (var loop=0;loop<allDivs.length;loop++) {
					var thisDiv = allDivs[loop];
					if (thisDiv.getAttribute('domreplace') == 'swfMovie' || thisDiv.id == 'flash-home') { thisDiv.style.visibility = 'hidden'; }
				}
			}
		}
		motion.set(getObj('pulldown_wrapper'), 'marginTop', ['0px', '352px'], 0.5, hideFlash);
		getObj('cb').style.backgroundImage = 'url(\'/funplanner/img/pulldown_close.gif\')';
		Overlay();
		if(!ArrEvents.length) showHelp();
		else hideHelp();
	}
	else{
		c.style.overflow = "hidden";
		document.getElementById('midNav-menu').style.display = 'block';
		var showFlash = function() {
			if ((client.engine == 'webkit') && client.os == 'mac') {
				var allDivs = document.getElementsByTagName('div');
				for (var loop=0;loop<allDivs.length;loop++) {
					var thisDiv = allDivs[loop];
					if (thisDiv.getAttribute('domreplace') == 'swfMovie' || thisDiv.id == 'flash-home') { thisDiv.style.visibility = 'visible'; }
					// show global nav
				}
			}
		}
		showFlash();
		motion.set(getObj('pulldown_wrapper'), 'marginTop', ['352px', '0px'], 0.5);
		getObj('cb').style.backgroundImage = 'url(\'/funplanner/img/pulldown_open.gif\')';
		document.getElementById('pd_transOverlay').style.display = 'none';
		document.documentElement.style.overflowX = "auto";
	}
	FirstTime = 0;
}
function Overlay(){
	var ff = 0;
	if(BrowserDetect.browser != "Explorer") ff = 20;
	document.documentElement.style.overflowX = "hidden";
	getObj('pd_transOverlay').style.width = getTruePageWidth() + ff + 'px';
	getObj('pd_transOverlay').style.height = getTruePageHeight() + 'px';
	getObj('pd_transOverlay').style.left = (10 - (getTruePageWidth() - 945)/2) + 'px';
	getObj('pd_transOverlay').style.display = 'block';
}
function gotoPage(lnk){
	if(window.location.href.charAt(window.location.href.length-1) != lnk) window.location = lnk;
}
function useGreenButtons(arg){
	if(ArrEvents.length || ArrPeople.length)
	{
		if (arg == 'create')
			window.open("/global/asp/code/" + arg + "funplanner.aspx?parkname=" + parkname);
		if (arg == 'email')
			window.open("/global/asp/sendfriend/" + arg + "funplanner.aspx?parkname=" + parkname);
	}
	else alert("Your Fun Planner is Empty!");
}
function AddEvent(type, name, lnk, mode, e){
	if(typeof(mode) == 'undefined') { mode = 1; }
	if(!ArrEvents.length){
		DoAdd(type, name, lnk, mode);
		moveslide();
		window.scrollTo(0, 0);
	}
	else{
		for(var i=0, j=1; i<ArrEvents.length; i++)
			if(mode == 1)
				if(ArrEvents[i] == (type + "," + escape(name) + "," + escape(lnk))) { /* alert("This event has already been added to your Fun Planner.");*/ j = 0; }
		if(j) DoAdd(type, name, lnk, mode);
	}
	if(mode == 1) { bakeCookie("funmusts", ArrEvents, CookiesExpiration); }



	// create/get div
	if (!document.getElementById('uiMsg')) {
		var uiMsg     = document.createElement('div');
			uiMsg     = document.body.appendChild(uiMsg);
			uiMsg.id           = 'uiMsg'
			uiMsg.innerHTML    = 'This item has been added<br />to your Fun Planner.';
			uiMsg.style.zIndex = 75;
	} else {
		var uiMsg = document.getElementById('uiMsg');
	}


	if (mode == 1) {
		try {
			uiMsg.style.left =  window.mouseX + 'px';
			uiMsg.style.top  = (client.engine == 'msie') ? (parseInt(window.mouseY) + parseInt(document.documentElement.scrollTop)) + 'px' : window.mouseY + 'px'; // ie needs to have its hand held
		// set Fade for element
			var fadeOut = function() { 
				var doThis = function() {
					var hideIt = function() { uiMsg.style.display = 'none'; }
					motion.set(uiMsg, 'opacity', [0.99, 0.01], .5, hideIt);
				}
				setTimeout(doThis, 1000)
			}
			uiMsg.style.display = 'block';
			motion.set(uiMsg, 'opacity', [0.01, 0.99], .5, fadeOut);
		// do it
		} catch (e) { uiMsg.style.display = 'none'; }
	} else {
		uiMsg.style.display = 'none';
	}





}
function tempMessage(e) {
	// create/get div
	if (!document.getElementById('tempError')) {
		var uiMsg     = document.createElement('div');
			uiMsg     = document.body.appendChild(uiMsg);
			uiMsg.id           = 'tempError';
			uiMsg.innerHTML    = 'Our Fun Planner is almost ready. We\'ll save your plan so please come back next week when you can email and print it.';
			uiMsg.style.zIndex = 200;
			uiMsg.style.width  = '200px';
	} else {
		var uiMsg = document.getElementById('tempError');
	}
	if (e) {
		try {
			uiMsg.style.left =  window.mouseX + 'px';
			uiMsg.style.top  = (client.engine == 'msie') ? (parseInt(window.mouseY) + parseInt(document.documentElement.scrollTop)) + 'px' : window.mouseY + 'px'; // ie needs to have its hand held

			if ((parseInt(uiMsg.style.left) + parseInt(uiMsg.style.width)) >= (client.viewport()[0] - 50)) {
				uiMsg.style.marginLeft = (0 - ((parseInt(uiMsg.style.left) + parseInt(uiMsg.style.width)) - (client.viewport()[0] - 50))) + 'px'
			} else {
				uiMsg.style.marginLeft = '0px'
			}

		// set Fade for element
			var fadeOut = function() { 
				var doThis = function() {
					var hideIt = function() { uiMsg.style.display = 'none'; }
					motion.set(uiMsg, 'opacity', [0.99, 0.01], .5, hideIt);
				}
				setTimeout(doThis, 6000);
			}
			uiMsg.style.display = 'block';
			motion.set(uiMsg, 'opacity', [0.01, 0.99], .5, fadeOut);
		// do it
		} catch (e) { uiMsg.style.display = 'none'; }
	}
}
function DoAdd(type, name, lnk, mode){
	if(mode == 1) ArrEvents[ArrEvents.length] = type + "," + escape(name) + "," + escape(lnk);
	getObj('pd_preview_count').innerHTML = ArrEvents.length + " item";
	if(ArrEvents.length != 1) getObj('pd_preview_count').innerHTML += "s";
	var newE = document.createElement("DIV");
	getObj('events_container').appendChild(newE);
	newE.id = 'e_' + stripSpecial(name);
	newE.className = "pd_added_event";
	newE.innerHTML = "<div class=\"pd_added_event_r\" onclick=\"RemoveEvent(\'" + type + "\',\'" + escape(name) + "\',\'" + escape(lnk) + "\')\"><img style=\"vertical-align: top;\"src=\"/funplanner/img/event_remove_dot.gif\" width=\"9\" height=\"9\" title=\"Remove\" /></div>";
	newE.innerHTML += "<div style=\"width:388px; padding-bottom: 5px;\">" + name + "</div>";

	//if(newE.addEventListener) newE.addEventListener('click', function(event) { gotoPage(lnk) }, false);
	//else newE.attachEvent("onclick", function() { gotoPage(lnk) });
}
function RemoveEvent(type, name, lnk){
	name = unescape(name);
	lnk = unescape(lnk);
	getObj('events_container').removeChild(getObj('e_'+stripSpecial(name)));
	getObj('pd_preview_count').innerHTML = (ArrEvents.length-1) + " item";
	if((ArrEvents.length-1) != 1) getObj('pd_preview_count').innerHTML += "s";
	for(var i=0; i<ArrEvents.length; i++)
		if(ArrEvents[i] == type + "," + escape(name) + "," + escape(lnk)) ArrEvents.splice(i,1);
	bakeCookie("funmusts", ArrEvents, CookiesExpiration);
}
function EditName(){
	getObj('pd0_un_text').style.display = 'none';
	getObj('pd0_un_entry').style.display = 'block';
	getObj('pd0_un_entry_fld').focus();
}
function SaveName(){
	getObj('pd0_un_text').style.display = 'block';
	getObj('pd0_un_entry').style.display = 'none';
	if(getObj('pd0_un_entry_fld').value != ""){
		getObj('pd0_un_text').innerHTML = getObj('pd0_un_entry_fld').value;
		ArrName[0] = escape(getObj('pd0_un_entry_fld').value) + ",0";
		bakeCookie("funname", ArrName, CookiesExpiration);
	}
}	
function AddInfo(e){
	e = e.target || e.srcElement;
	for(var i=0; i<ArrInfo.length; i++)
		if(ArrInfo[i].substring(0,ArrInfo[i].indexOf(",")) == e.nextSibling.nodeValue)
			ArrInfo[i] = ArrInfo[i].replace((!e.checked)? ",on" : ",off", (e.checked)? ",on" : ",off");
	bakeCookie("funfiles", ArrInfo, CookiesExpiration);
}
function ShowMemberDiv(){
	o = getObj('add_member_div');
	getChildNodes(o)[1].value = "";
	getChildNodes(o)[4].value = "";
	getChildNodes(o)[6].value = "";
	o.style.display = 'block';
	getObj('people_container_fill').style.display = 'block';
	getChildNodes(o)[1].focus();
}
function HideMemberDiv(){
	getObj('add_member_div').style.display = 'none';
	getObj('people_container_fill').style.display = 'none';
}
function HideEdit(){
	getObj(name_to_edit).style.display = 'block';
	getObj('people_container').insertBefore(getObj("edit_member_div"), getObj('people_container').firstChild);
	getObj("edit_member_div").style.display = 'none';
}
function AddMember(){
	if(ValidateMe('add_member_div')){
		HideMemberDiv();
		var name = feet = inch = "non";
		oDiv = getObj('add_member_div');
		for(var i=0; i<oDiv.childNodes.length; i++)
			if(typeof(oDiv.childNodes[i].value) != "undefined")
				if(name == "non") name = oDiv.childNodes[i].value;
				else if(feet == "non") feet = parseInt(oDiv.childNodes[i].value);
				else if(inch == "non") inch = parseInt(oDiv.childNodes[i].value);
		var height = (feet * 12) + inch;
		ArrPeople[ArrPeople.length] = escape(name) + "," + height;
		var newE = document.createElement("DIV");
		getObj('people_container').appendChild(newE);
		var rand_ID = "p_";
		for(var i=0; i<10; i++) rand_ID += Math.floor(Math.random()*10);
		newE.id = rand_ID;
		newE.innerHTML = "<div id=" + rand_ID + "_n class=\"pd_added_person\" style=\"cursor:pointer;\" onclick=\"EditPerson(\'" + rand_ID + "\')\">" + name + "</div>" + "<div class=\"pd_added_person_r\" onclick=\"RemovePerson(\'" + rand_ID + "\')\"><img src=\"/funplanner/img/event_remove_dot.gif\" width=\"9\" height=\"9\" title=\"Remove\"></div><div id=\"" + rand_ID + "_h\" class=\"pd_added_person\" style=\"float:right;\" onclick=\"EditPerson(\'" + rand_ID + "\')\">" + height + "\"</div>";
		newE.className = "pd_added_person";
		bakeCookie("funmember", ArrPeople, CookiesExpiration);
	}
	else;
}
function RemovePerson(ID){
	for(var i=0; i<ArrPeople.length; i++)
		if(ArrPeople[i].substring(0, ArrPeople[i].lastIndexOf(",")) == getObj(ID + "_n").innerHTML) ArrPeople.splice(i,1);
	getObj('people_container').removeChild(getObj(ID));
	bakeCookie("funmember", ArrPeople, CookiesExpiration);
}
function EditPerson(ID){
	oDiv = getObj('edit_member_div');
	if(oDiv.style.display == 'block') getObj(name_to_edit).style.display = 'block';
	getObj(ID).style.display = 'none';
	getObj('people_container').insertBefore(oDiv, getObj(ID).nextSibling);
	getChildNodes(oDiv)[1].value = getObj(ID+"_n").innerHTML;
	getChildNodes(oDiv)[4].value = Math.floor(parseInt(getObj(ID + "_h").innerHTML) / 12);
	getChildNodes(oDiv)[6].value = parseInt(getObj(ID + "_h").innerHTML) % 12;
	name_to_edit = ID;
	oDiv.style.display = 'block';
	getChildNodes(oDiv)[1].focus();
}
function EditPersonSave(){
	if(ValidateMe('edit_member_div')){
		oT = getObj(name_to_edit); //Target
		oS = getObj('edit_member_div'); //Source
		var name = getChildNodes(oS)[1].value;
		var height = (parseInt(getChildNodes(oS)[4].value)) * 12 + (parseInt(getChildNodes(oS)[6].value));
		for(var i=0; i<ArrPeople.length; i++)
			if(ArrPeople[i].substring(0,ArrPeople[i].lastIndexOf(",")) == getObj(name_to_edit + "_n").innerHTML){
				ArrPeople[i] = name + "," + height;
			}
		getChildNodes(oT)[0].innerHTML = name;
		getChildNodes(oT)[2].innerHTML = height + "\"";
		oT.style.display = 'block';
		HideEdit();
		bakeCookie("funmember", ArrPeople, CookiesExpiration);
	}
	else;
}
function ValidateMe(arg){
	var objRegExp  =  /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/;
	var a = "";
	var o = getObj(arg);
	var retval = 1;
	for(var i=0; i<o.childNodes.length; i++)
		if(o.childNodes[i].nodeName == "INPUT"){
			if(o.childNodes[i].size == 23 && o.childNodes[i].value == ""){ a = "You must enter a name."; retval = 0; }
			if(o.childNodes[i].size == 23 && o.childNodes[i].value != "")
				for(var j=0; j<ArrPeople.length; j++)
					if(ArrPeople[j].substring(0, ArrPeople[j].lastIndexOf(",")) == o.childNodes[i].value) { a += "\nThat name has already been entered."; retval = 0; }
			if(o.childNodes[i].size == 2)
				if(!objRegExp.test(o.childNodes[i].value)){ a += "\n" + ((o.childNodes[i+2].innerHTML == "ft.")? "Feet" : "Inches") + " must be a number."; o.childNodes[i].value = ""; o.childNodes[i].focus(); retval = 0; }
		}
	if(!retval) alert(a);
	return retval;
}
function toggleHelp(){
	if(getObj('pd_instructions').style.display == 'block' || getObj('pd_instructions').style.display == '') hideHelp();
	else showHelp();
}
function showHelp(){
	if(BrowserDetect.OS == "Mac") getObj('events_container').style.overflow = 'hidden';
	getObj('pd0_top_help').style.backgroundImage = "url('/funplanner/img/pd0_top_help_hide.gif')";
	getObj('pd_instructions').style.display = 'block';
	getObj('pd_instructions_lower').style.display = 'block';
}
function hideHelp(){
	if(BrowserDetect.OS == "Mac") getObj('events_container').style.overflowY = 'auto';
	getObj('pd0_top_help').style.backgroundImage = "url('/funplanner/img/pd0_top_help_show.gif')";
	getObj('pd_instructions').style.display = 'none';
	getObj('pd_instructions_lower').style.display = 'none';
}
function showArr(){
	var temp = "";
	for(var i=0; i<ArrEvents.length; i++) temp += ArrEvents[i] + "\n";
	alert("Array:\n" + temp);
	return false;
}
function notReady(){
	alert("Oops.\nWe're still perfecting our Fun Planner technology, so we can't complete your Fun Plan just yet.\nPlease come back on March 15 when the new Fun Planner will be up and running.\nMeanwhile, we'll store the information you've entered in your Fun Plan until you return.");
}