function init(){
	if (document.getElementById("help")){
		setupHelp();
	}	
};



function addLoadListener(fn)
{
	if (typeof window.addEventListener != 'undefined')
	{
		window.addEventListener('load', fn, false);
	}
	else if (typeof document.addEventListener != 'undefined')
	{
		document.addEventListener('load', fn, false);
	}
	else if (typeof window.attachEvent != 'undefined')
	{
		window.attachEvent('onload', fn);
	}
	else
	{
		return false;
	}
	
	return true;
};




function attachEventListener(target, eventType, functionRef, capture)
{
    if (typeof target.addEventListener != "undefined")
    {
        target.addEventListener(eventType, functionRef, capture);
    }
    else if (typeof target.attachEvent != "undefined")
    {
        target.attachEvent("on" + eventType, functionRef);
    }
    else
    {
        return false;
    }

    return true;
};


/* RESOLUTION SWITCHER */
checkBrowserWidth();
attachEventListener(window, "resize", checkBrowserWidth, false);

function checkBrowserWidth()
{
	var theWidth = getBrowserWidth();
	
	if (theWidth == 0)
	{
		var resolutionCookie = document.cookie.match(/(^|;)sita_layout[^;]*(;|$)/);

		if (resolutionCookie != null)
		{
			setStylesheet(unescape(resolutionCookie[0].split("=")[1]));
		}
		
		addLoadListener(checkBrowserWidth);
		
		return false;
	}

	if (theWidth > 990)
	{
		setStylesheet("normal");
		document.cookie = "sita_layout=" + escape("normal");
	}
	else
	{
		setStylesheet("");
		document.cookie = "sita_layout=";
	}
	
	return true;
};




function getBrowserWidth()
{
	if (window.innerWidth)
	{
		return window.innerWidth;
	}
	else if (document.documentElement && document.documentElement.clientWidth != 0)
	{
		return document.documentElement.clientWidth;
	}
	else if (document.body)
	{
		return document.body.clientWidth;
	}
	
	return 0;
};




function setStylesheet(styleTitle)
{
	var currTag;

	if (document.getElementsByTagName)
	{
		for (var i = 0; (currTag = document.getElementsByTagName("link")[i]); i++)
		{
			if (currTag.getAttribute("rel").indexOf("style") != -1 && currTag.getAttribute("title"))
			{
				currTag.disabled = true;

				if(currTag.getAttribute("title") == styleTitle)
				{
					currTag.disabled = false;
				}
			}
		}
	}
	
	return true;
};


// FORM HELP
function setupHelp(){
	document.getElementById("help").className = "access";
	
	var h = new Array();
	var targ = new Array();
	
	for(i = 0; i < help.length; i++){
		j = i + 1;
		h[i] = crteHelp(help[i] + "H",j);	
		targ[i] = document.getElementById(help[i]);
		targ[i].appendChild(h[i]);
	}
};


function crteHelp(f,num){
	//Grab text from help divs
	var src = document.getElementById("help"+num+"");
	var headers = src.getElementsByTagName("h3");
	var head = headers[0].innerHTML;
	var paras = src.getElementsByTagName("p");
	var p = paras[0].innerHTML;
	
	//Create Div tag
	var hT = document.createElement("div");
	hT.id = f;
	hT.className = "infopanel help access";
	
	//Create H3 tag
	var hTh = document.createElement("h3");
	hTh.innerHTML = head;
	
	//Create P tag
	var hTp = document.createElement("p");
	hTp.appendChild(document.createTextNode(p));
	
	//Put it all together
	hT.appendChild(hTh);
	hT.appendChild(hTp);
	
	return hT;
}

function showHelp(lyr, btn){
	var h;
	
	if (btn){
		//alert(btn.parentNode.id);
	}
	
	if (document.getElementById(lyr)){
		h = document.getElementById(lyr);
		if (h.className === "infopanel help"){
			for (i = 0; i < help.length; i++){
				hideHelp(help[i]+"H");
			}
		}else{
			for (i = 0; i < help.length; i++){
				hideHelp(help[i]+"H");
			}
			h.className = "infopanel help";
		}
	}
};

function hideHelp(lyr){
	if (document.getElementById(lyr)){
		document.getElementById(lyr).className="infopanel help access"
	}
};

// CLEAR FORM VALUES
function clearText(thefield){
if (thefield.defaultValue==thefield.value)
thefield.value = ""
}



/*@cc_on
   /*@if (@_jscript_version <= 5.5)   
   @else @*/
		$(function () {
			(function () {
				// Text-mode menu fixes
				var menu_items = $("#nav > ul.nav-text > li");
				if( menu_items.length )
				{
					menu_items.each( function() {
						var submenu = $(this).children('ul');
						submenu.css( 'width','150px' );
						submenu.css( 'top', ($(this).height()-1) + 'px' );
					});
				}
				
				// Hovers
				if ($("#nav > ul > li").length) {
					$("#nav > ul > li").hover(
						function () {
							$(this).addClass("hover");
							$(this).children("ul").show();
							
							/*nav05 requires a lower z-index than nav06 when nav06 is hovered, so the top blue border shows*/
							if ($(this).attr("id") == "nav06") {
								$("#nav #nav05").css("z-index","1");
							}
						},
						function () {
							$(this).removeClass("hover");
							$(this).children("ul").hide();
							
							/*ensure nav05's z-index has reverted to 6*/
							$("#nav #nav05").css("z-index","6");
						}
					);
				}
			})();
			
			(function () {
				if ($(".quicklinks > ul > li").length) {
					$(".quicklinks > ul > li").hover(
						function () {
							$(this).addClass("hover");
							$(this).children("div").show();
							$(".quicklinks > h2 > img").each(function () {
								this.style.visibility = "hidden";
							})
						},
						function () {
							$(this).removeClass("hover");
							$(this).children("div").hide();
							$(".quicklinks > h2 > img").each(function () {
								this.style.visibility = "visible";
							})
						}
					);
				}
			})();
		
			(function () {
				if ($("table.calendar").length) {
					var __arr = document.getElementsByTagName("div");
					var __active = [];
					
					for (var i = Number(0); i < __arr.length; i++) {
						if (__arr[i].className == "event") {
							__arr[i].onmouseover = function () {this.className = "event hover"};
							__arr[i].onmouseout = function () {this.className = "event"};
						}
					}
				}
			})();
			
		});
   /*@end
@*/

window.onload = init;

// Handle file URLs and add tracking URL
$(document).ready( function()
{
	// var prefix = this.location.protocol + '//' + '(('+this.location.host.replace('.','\\.')+')|(sita\\.aero))' + config.base_url;
	var re1 = new RegExp( 'sita\\.aero/files/resources/(.+)' );
	var re2 =  new RegExp( 'sita\\.aero/file/[0-9]+' );
	var re3 = new RegExp( '/file/526/' );
	
	// alert( prefix );
	
	try
	{
		console.log('Hello');
	}
	catch( e )
	{
		console = { log: function(){} }
	}
	
	// alert( pageTracker );
	// console.log( pageTracker );
	
	var count = 0;
	$("a[href]").each( function(i)
	{
		// $('#dbg').append( this.href + '<br />' );
		if( this.href.match( re1 ) ||
			this.href.match( re2 )
		)
		{
			/*
			if( this.href.match( re3 ) )
			{
				alert( this.href );
				alert( $(this).attr('target') );
				alert( $(this).attr('onclick') );
			}
			*/
			
			$(this).attr( 'g_tracked','1' );
			if( !$(this).hasClass( 'allow-new-window' ) )
			{
				$(this).attr( 'target','' );
				$(this).attr( 'onclick','' );
				$(this).attr( 'onkeypress','' );
			}
			
			// $('#dbg').append( '   MATCHED' );
			$(this).click( function()
			{
				try
				{
					pageTracker._trackPageview( $(this).attr('href') );
				}
				catch( e )
				{}
			});
			count++;
		}
	});
});

// Handle FLV video wrappers
$(document).ready( function()
{
	var url_player = '/video/player/player-viral.swf';
	$("a[href].flv").each( function(i)
	{
		var ie = !!(window.attachEvent && !window.opera);
		var image = $(this).children('img')[0];
		var width = parseInt( image.width );
		var height = parseInt( image.height );
		
		var url_video = this.getAttribute('href');
		var url_video_preview = url_video.replace(/(\.[a-z0-9]+$)/,'.jpg');

		var s =
		( !ie ?
			// If not Internet Explorer
			'<object type="application/x-shockwave-flash" data="'+url_player+'" width="'+width+'" height="'+height+'">'+
				'<param name="allowscriptaccess" value="always" />'
			:
			// If Internet Explorer
			'<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" '+
				'codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" '+
				'width="'+width+'" height="'+height+'">'+
				'<param name="movie" value="'+url_player+'" />'+
				'<param name="allowscriptaccess" value="never" />'
		)
			+
				'<param name="allowfullscreen" value="true" />'+
				'<param name="flashvars" value="file='+url_video+'&image='+url_video_preview+'" />'+
				'<p><a href="http://get.adobe.com/flashplayer">Get Flash</a> to see this player.</p>'+
		'</object>';
	
		$(this).replaceWith( s );

	});
});
