
// Create New Element if Exists
function newElement(element) {
	return (document.getElementById(element) ? document.getElementById(element) : false) ;
}


// Preload Images
function imagePreload(input) { 
  document.imageArray = new Array(input.length);
  for (var i=0; i<input.length; i++) {
	 document.imageArray[i] = new Image;
	 document.imageArray[i].src = "/images/nav-" + input[i] + "-over.gif";
  }
}


// Swap Image States
function sugarImageSwap(element,names) {
	imagePreload(names);
	if (document.getElementById(element)) {
		var theMenu = document.getElementById(element);
		for (var i=0; i<theMenu.getElementsByTagName("A").length; i++) {
			var node = theMenu.getElementsByTagName("A")[i];
			node.onmouseover = function() {
				this.getElementsByTagName("IMG")[0].src=this.getElementsByTagName("IMG")[0].src.replace("up","over");
			}
			node.onfocus = function() {
				this.getElementsByTagName("IMG")[0].src=this.getElementsByTagName("IMG")[0].src.replace("up","over");
			}
			node.onmouseout = function() {
				this.getElementsByTagName("IMG")[0].src=this.getElementsByTagName("IMG")[0].src.replace("over","up");
			}
			node.onblur = function() {
				this.getElementsByTagName("IMG")[0].src=this.getElementsByTagName("IMG")[0].src.replace("over","up");
			}
			node.onclick = function() { window.location.href=this.href; }
		}
	}
}



// Create a New Object
function sugarPacket(element,tag,theClass,onClick) {
	this.element = element;
	this.tag = tag;
	this.theClass = theClass;
	this.onclick = onClick;
}



// Swap Element's Class
function swapClass() {
	with (this) {
		if (document.getElementById(element)) {
			element = document.getElementById(element);
			for (var i=0;i!=element.getElementsByTagName(tag).length;i++) {
				object = element.getElementsByTagName(tag)[i];
				if (object.className == 'skip') continue;
				object.onmouseover = function() { this.className += theClass; }
				object.onmouseout = function() { this.className = this.className.replace(theClass,''); }
				object.onclick = onclick;
			}
		}	
	}
}



sugarPacket.prototype.swapClass = swapClass;


function akst_share(id, url, title) {
	var form = $('akst_form');
	var post_id = $('akst_post_id');
	
	if (form.style.display == 'block' && post_id.value == id) {
		form.style.display = 'none';
		return;
	}
	
	var link = $('akst_link_' + id);
	var offset = Position.cumulativeOffset(link);

	$("akst_delicious").href = akst_share_url("http://del.icio.us/post?url={url}&title={title}", url, title);
	$("akst_digg").href = akst_share_url("http://digg.com/submit?phase=2&url={url}&title={title}", url, title);
	$("akst_furl").href = akst_share_url("http://furl.net/storeIt.jsp?u={url}&t={title}", url, title);
	$("akst_netscape").href = akst_share_url(" http://www.netscape.com/submit/?U={url}&T={title}", url, title);
	$("akst_yahoo_myweb").href = akst_share_url("http://myweb2.search.yahoo.com/myresults/bookmarklet?u={url}&t={title}", url, title);
	$("akst_stumbleupon").href = akst_share_url("http://www.stumbleupon.com/submit?url={url}&title={title}", url, title);
	$("akst_google_bmarks").href = akst_share_url("  http://www.google.com/bookmarks/mark?op=edit&bkmk={url}&title={title}", url, title);
	$("akst_technorati").href = akst_share_url("http://www.technorati.com/faves?add={url}", url, title);
	$("akst_blinklist").href = akst_share_url("http://blinklist.com/index.php?Action=Blink/addblink.php&Url={url}&Title={title}", url, title);
	$("akst_newsvine").href = akst_share_url("http://www.newsvine.com/_wine/save?u={url}&h={title}", url, title);
	$("akst_magnolia").href = akst_share_url("http://ma.gnolia.com/bookmarklet/add?url={url}&title={title}", url, title);
	$("akst_reddit").href = akst_share_url("http://reddit.com/submit?url={url}&title={title}", url, title);
	$("akst_windows_live").href = akst_share_url("https://favorites.live.com/quickadd.aspx?marklet=1&mkt=en-us&url={url}&title={title}&top=1", url, title);
	$("akst_tailrank").href = akst_share_url("http://tailrank.com/share/?link_href={url}&title={title}", url, title);
	post_id.value = id;
	form.style.left = offset[0] + 'px';
	form.style.top = (offset[1] + link.offsetHeight + 3) + 'px';
	form.style.display = 'block';
}

function akst_share_url(base, url, title) {
	base = base.replace('{url}', url);
	return base.replace('{title}', title);
}

function akst_share_tab(tab) {
	var tab1 = document.getElementById('akst_tab1');
	var tab2 = document.getElementById('akst_tab2');
	var body1 = document.getElementById('akst_social');
	var body2 = document.getElementById('akst_email');
	switch (tab) {
		case '1':
			tab2.className = '';
			tab1.className = 'selected';
			body2.style.display = 'none';
			body1.style.display = 'block';
			break;
		case '2':
			tab1.className = '';
			tab2.className = 'selected';
			body1.style.display = 'none';
			body2.style.display = 'block';
			break;
	}
}

function akst_xy(id) {
	var element = $(id);
	var x = 0;
	var y = 0;
}


window.onload = function() {
	if (!document.getElementById || !document.getElementsByTagName) return false;
	// Universal Form Validator
	var form_elements = new Array("reportSnapshot", "reportStory", "storySubmit","snapshotSubmit","storyComment","surveyAnswer");
	var email_field = "email";
	for (x=0;x<form_elements.length;x++) {
		var form_element = newElement(form_elements[x]);
		if (form_element) {
			form_element.onsubmit = function() {
				for (y=0;y<this.elements.length;y++) {
					valid = false;
					field = this.elements[y];
					field.className = field.className.replace("error","");
					// Find the minimum field length
					if (field.tagName=="TEXTAREA") min_length = 15;
					else if (field.getAttributeNode('minlength')) min_length = field.getAttributeNode('minlength').value;
					else min_length = (field.maxLength==1 || field.size==field.maxLength ? field.maxLength : 3);
					// Test value based on type or tag
					if (field.type=="radio" || field.type=="hidden") {
						valid = true;
					} else if (field.type=="checkbox") {
						if (field.checked) valid = true;
					} else if (field.tagName=="SELECT") {
						if (field.options[field.selectedIndex].value!="null") valid = true;
					} else if (field.name==email_field) {
						if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(field.value)) valid = true;
					} else if (field.value!="null" && field.value.length>=min_length) {
						valid = true;
					}
					// Make a decision
					if (valid) {
						if (y==this.elements.length-1) return true; 
					} else {
						if (field.type=="checkbox") alert("You must agree to the Terms of Use.");
						else if (field.tagName=="TEXTAREA") alert("Please enter at least " + min_length + " characters in the " + field.name + " box.");
						else {
							field.className += " error";
							field.focus();
						}
						return false;
					}
				}
				return false;
			}
		}
	}
	// Universal Form Validator


	// Main Menu
	var names = Array("home","hotels","promos","attractions");
	sugarImageSwap("siteNav",names);
	var buttons = Array("submit","cancel","map","viewMap");
	for (var i=0;i!=buttons.length;i++) {
		if (document.getElementById(buttons[i]+"Button")) {
			document.getElementById(buttons[i]+"Button").onmouseover = function() { this.src=this.src.replace("-up.","-over."); }
			document.getElementById(buttons[i]+"Button").onmouseout = function() { this.src=this.src.replace("-over.","-up."); }
		}
	}
		
	// Attraction Selecta
	var attract_select = newElement("attractSelecta");
	if (attract_select) {
		webroot = attract_select.form.webroot.value;
		attract_select.disabled = false;
		attract_select.onchange = function() {
			if (this.selectedIndex==0) return false;
			top.location.href = webroot + this.options[this.selectedIndex].value + "-attractions-lodging/";
			return true;
		}
	}

	// City Selecta
	var city_select = newElement("citySelecta");
	if (city_select) {
		city_select.disabled = false;
		city_select.onchange = function() {
			if (this.selectedIndex==0) return false;
			top.location.href = this.options[this.selectedIndex].value;
			return true;
		}
	}

	// State Selecta
	var state_select = newElement("stateSelecta");
	if (state_select) {
		state_select.disabled = false;
		state_select.onchange = function() {
			if (this.selectedIndex==0) return false;
			top.location.href = this.options[this.selectedIndex].value;
			return true;
		}
	}

	// City Weather
	var city_weather = newElement("cityWeather");
	if (city_weather) {
		city_weather.disabled = false;
		city_weather.onchange = function() {
			if (this.selectedIndex==0) return false;
			document.location.href = this.options[this.selectedIndex].value;
			return true;
		}
	}

	// Hotel Selecta
	var hotel_select = newElement("hotelSelecta");
	if (hotel_select) {
		hotel_select.disabled = false;
		hotel_select.onchange = function() {
			if (this.selectedIndex==0) return false;
			top.location.href = this.options[this.selectedIndex].value;
			return true;
		}
	}
	
	// Survey Selecta
	var survey_select = newElement("surveySelecta");
	if (survey_select) {
		survey_select.disabled = false;
		survey_select.onchange = function() {
			if (this.selectedIndex==0) return false;
			top.location.href = "/surveys/" + this.options[this.selectedIndex].value + "/";
			return true;
		}
	}
	delete survey_select;
	
	// Hotel Redirect Link
	var redirect_link = newElement("redirectLink");
	if (redirect_link) {
		redirect_link.onclick = function() { location.replace(this.href); return false; }
	}

	// Report Link
	var report_link = newElement("reportLink")
	if (report_link) {
		report_link.onclick = function() {
			display = document.getElementById("reportThis").style.display;
			document.getElementById("reportThis").style.display = (display!="block" ? "block" : "none");
			return false;
		}
	}
	delete report_link;

	// Rules Link
	var rules_link = newElement("rulesLink")
	if (rules_link) {
		rules_link.onclick = function() {
			theWidth = "640"; theHeight = "400"; theScroll = "yes";
			var rules_win = window.open(this.href,"textwin","top=" + (screen.availHeight-theHeight)/2 + ",left=" + (screen.availWidth-theWidth)/2 + ",width=" + theWidth + ",height=" + theHeight + ",scrollbars=" + theScroll);
			if (window.focus) rules_win.focus();
			return false;
		}
	}
	
	// Terms & Conditions Link
	var terms_link = newElement("termsLink")
	if (terms_link) {
		terms_link.onclick = function() {
			theWidth = "640"; theHeight = "400"; theScroll = "yes";
			var terms_win = window.open(this.href,"textwin","top=" + (screen.availHeight-theHeight)/2 + ",left=" + (screen.availWidth-theWidth)/2 + ",width=" + theWidth + ",height=" + theHeight + ",scrollbars=" + theScroll);
			if (window.focus) terms_win.focus();
			return false;
		}
	}

	// Yahoo! City Map
	var yahoo_map = newElement("yahooMap");
	if (yahoo_map) {
		// Create the map object
		var map = new YMap(yahoo_map,YAHOO_MAP_REG,new YSize(296,160));
		var location = yahoo_map.getAttribute("location").replace("John's","John%27s");
		// If location is False then remove
		if (location) {
			// Display map
			map.drawZoomAndCenter(location,7);
			// Add zoom +/- buttons
			map.addZoomShort();
			// Create marker for this city
			var marker = new YMarker(location);
			// Make marker auto expand on mouse events
			marker.addAutoExpand(yahoo_map.getAttribute("city"));
			// Add marker to map
			map.addOverlay(marker);
		} else if (yahoo_map = document.getElementById("areaMap")) yahoo_map.style.display = "none";
	}
	
	return true;
}