$(document).ready(function(){
	/* make regional jump menu submit work */
	$("#tjSubmit").click(function(){
		var url = $("#Terminal_DropDown1_ddTerminals option:selected").val();
		if (url != "") {
			window.open(url,"_top");
		}
	});

	/* locate a terminal control*/
	if (window.location.href.split("//")[1].split("/")[1] == "") {
		$("#flash_map_colorbox").click(function(){
			$("#flash_map").toggle();
			$("#flash_banner").toggle();
		});
	}
	else {
		if (typeof $.flash != 'undefined' && typeof $.flash == 'function'){
			$("#flash_map_colorbox_content").flash({
				swf:'/_assets/swf/apmHeader_main.swf',
				height: 435,
				width: 940
			});
			$("#flash_map_colorbox").colorbox({inline:true, href:"#flash_map_colorbox_content",transition:"elastic"});
		}
	}
	
	/* Creates new popup window for flash movies */
	$("a").each(function() {
		if($(this).attr("href")) {
			var href = $(this).attr("href");
			var dot = href.lastIndexOf(".");
			var extension = href.substr(dot,href.length).toLowerCase();
			if (extension == ".flv") {
				var newhref = "/viewer.aspx?id=" + href;
				$(this).attr("href",newhref);
				$(this).attr("target", "");
				$(this).colorbox({iframe:true,transition:"elastic",width:560,height:420});
			}
		}
	});

	/* Opens linked images in a colorbox */
	$("a").each(function() {
		if($(this).attr("href")) {
			var href = $(this).attr("href");
			var dot = href.lastIndexOf(".");
			var extension = href.substr(dot,href.length).toLowerCase();
			if (extension == ".jpg" || extension == ".png" || extension == ".gif" || extension == ".jpeg") {
				var bodyHeight = $('body').height();
				var bodyWidth = $('body').width();

				$(this).attr("target", "").addClass("colorbox").colorbox({transition:"elastic",maxWidth:'100%',maxHeight:'100%'});
			}
		}
	});

	/* Overrides onclick javacsript windows and opens images in a colorbox instead */	
	$("a").each(function() {
		if($(this).attr("onclick") && !$(this).attr("contentEditable")) {
			if($('> img',this).length > 0) {
				var $href = String($(this).attr('onclick')).split(',')[0].replace("function onclick(event) {\njavascript:\n    window.open(\"", '').replace('"', '');

				$(this).attr("onclick", "").attr("href", $href).colorbox({iframe:false,transition:"elastic"});
			}
		}
	});

	/* Opens all external links in new window */
	$('a[href^=http]').not('.colorbox, [href*=mindcomet.net], [href*=/apmterminals.com], [href*=/www.apmterminals.com], [href*=apmterminals.net]').bind("click", function(){
		return !window.open(this.href);
	});

	/* main nav dropdowns and hover states */
	$("#nav > span > ul > li").hover(function(e) {
		if (e = this && $('> ul', this).length > 0) {
			$('> a', this).addClass("hover");
			$(this).addClass("sfhover");
		}
	}, function(e) {
		if (e = this && $('> ul', this).length > 0)
		{
			$('> a', this).removeClass("hover");
			$(this).removeClass("sfhover");
		}
	});

	/* removes empty ul lists under nav items */
	$("#nav > span > ul > li > ul").each(function() {
		if($(this).children().length == 0){
			$(this).remove();
		}
	});

	/* active state based on current document */
	$("#nav").find("a","nav").each(function(){
		var url = window.location.href.split("//")[1].split("/");
		var path = '';
		if (url.length > 2) {
			path = "/"+url[1] +"/"+url[2].split("?")[0];
		}
		else {
			path = "/"+url[1].split("?")[0];
		}
		if($(this).attr("href").split("?")[0] == path){
			$(this).css("color","#22AFCB");
		}
	});

	/* remove apdding on first element in nav */
	$("#nav > span > ul > li:first > a").css("padding-left","0px");

	/* active states on side nav */
	$("#terminal_side_nav").find("a","terminal_side_nav").each(function(){
		var url = window.location.href.split("//")[1].split("/");
		var path = "/"+url[1];
		if (url.length > 2) {
			path += "/"+url[2];
		}
		if($(this).attr("href") == path){
			$(this).addClass("active");
		}
	});
 });

//  sfHover = function() {
// 	var sfEls = document.getElementById("nav").getElementsByTagName("LI");
// 	for (var i=0; i<sfEls.length; i++) {
// 		sfEls[i].onmouseover=function() {
// 			this.className+=" sfhover";
// 		}
// 		sfEls[i].onmouseout=function() {
// 			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
// 		}
// 	}
// }
// if (window.attachEvent) window.attachEvent("onload", sfHover);

//pop function 
// openwindow('http://www.mindcomet.com','600','600','location=yes,status=yes,toolbar=yes,top=2,left=2,scrollbars=yes')
function openwindow(varSite,varWidth,varHeight,varOther)
{
	var varOptions = "height="+varHeight
	varOptions = varOptions+",width="+varWidth+","
	varOptions = varOptions+varOther
	newWin = window.open(varSite,"newwin",varOptions)
	newWin.focus()
}

//function
function returnURL() {
	var url = String(window.location.href).split("/");
	var urlLength = url.length;
	var urlString = '';

	for(i=3;i<=urlLength-2;i++) {
		urlString += url[i] + "/"
	}
	return urlString
}


function sendTo(urlValue) {
	var thisURL = returnURL();

	window.location.href='/redirect.aspx?url=' + thisURL + '&id=' + urlValue
}

