// minimum page width for scrolling
var globalMinThreshold = 970;

// Major version of Flash required
//var requiredMajorVersion = 9;
// Minor version of Flash required
//var requiredMinorVersion = 0;
// Minor version of Flash required
//var requiredRevision = 0;
    
// ----------------------------------------------------------------------------------------------------------------------------------------------------------

var objArray = new Array();
var global100Divs = new Array();

//var hasReqestedVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);
var hasReqestedVersion = swfobject.hasFlashPlayerVersion("9.0.115");
var layers = new Array();
layers[0] = 1;

var windowResizeOverride = false;
var uniqueLoadID = Math.floor(Math.random()*10000);

var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();

var Window = {	
	//Returns an integer representing the width of the browser window (without the scrollbar).
	getWindowWidth : function() {
	return (document.layers||(document.getElementById&&!document.all)) ? window.outerWidth : (document.all ? document.body.clientWidth : 0);
	},
 
	//Returns an integer representing the height of the browser window (without the scrollbar).
	getWindowHeight : function() {	
	return window.innerHeight ? window.innerHeight :(document.getBoxObjectFor ? Math.min(document.documentElement.clientHeight, document.body.clientHeight) : ((document.documentElement.clientHeight != 0) ? document.documentElement.clientHeight : (document.body ? document.body.clientHeight : 0)));
	},	
 
	//Returns an integer representing the scrollWidth of the window. 
	getScrollWidth : function() {
	return document.all ? Math.max(Math.max(document.documentElement.offsetWidth, document.documentElement.scrollWidth), document.body.scrollWidth) : (document.body ? document.body.scrollWidth : ((document.documentElement.scrollWidth != 0) ? document.documentElement.scrollWidth : 0));
	},
 
	//Returns an integer representing the scrollHeight of the window. 
	getScrollHeight : function(){		
		return document.all ? Math.max(Math.max(document.documentElement.offsetHeight, document.documentElement.scrollHeight), Math.max(document.body.offsetHeight, document.body.scrollHeight)) : (document.body ? document.body.scrollHeight : ((document.documentElement.scrollHeight != 0) ? document.documentElement.scrollHeight : 0));
	},			
 
	//Returns an integer representing the scrollLeft of the window (the number of pixels the window has scrolled from the left).
	getScrollLeft : function() {
		return document.all ? (!document.documentElement.scrollLeft ? document.body.scrollLeft : document.documentElement.scrollLeft) : ((window.pageXOffset != 0) ? window.pageXOffset : 0);
	},
 
	//Returns an integer representing the scrollTop of the window (the number of pixels the window has scrolled from the top).
	getScrollTop : function() {
		return document.all ? (!document.documentElement.scrollTop ? document.body.scrollTop : document.documentElement.scrollTop) : ((window.pageYOffset != 0) ? window.pageYOffset : 0);
	}
}

// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
//fullscreen popup
function openFullWindow(url) {
	var windowprops = 'top=0,left=0,toolbar=no,location=no,directories=no,status=no,scrollbars=yes,menubar=no,resizable=yes';
	var popup = window.open(url,'NewAirplanePop',windowprops);
	var intWidth = screen.availWidth? screen.availWidth : screen.width;
	var intHeight = screen.availHeight? screen.availHeight: screen.height-30;
    if(popup == null){
        alert("Please disable your popup blocker to view this portion of the site."); 
    }else{
	    popup.resizeTo(intWidth, intHeight);
        popup.outerWidth = intWidth;
	    popup.outerHeight = intHeight;
    }
}

window.onfocus = function(){ //start swfs back up
    if(hasReqestedVersion){
		for(i=0;i<objArray.length;i++){
			if(objArray[i][5]){
				document.getElementById(objArray[i][0]).startSwf();
			}
		} 
    }
}

//pop up that takes different parameters
// this will center the window within the browser and bring the window to focus
//javascript:popUpProps('test.html','winName','400','300',',menubar=0,resizable=0,scrollbars=0,titlebar=0,toolbar=0');
var winProps = null;
function popUpProps(url, windowName, width, height, props)
{
	var left = (screen.width - width) / 2;
	var top = (screen.height - height) / 2;
	var propsCentered = "width=" + width + ",height=" + height + ",left=" + left + ",top=" + top + props;

	if(winProps == null || winProps.closed) {
		winProps = window.open(url, windowName, propsCentered);
		winProps.focus();
	} else {	
		winProps.close();
		winProps = window.open(url, windowName, propsCentered);
		winProps.focus();
	}
}

function surveyNov08()
{
	/*
	var counter = 1;
	if (readCookie("surveyNov08Counter")){
		counter = readCookie("surveyNov08Counter");
	}
	
	if (!readCookie("surveyNov08") && counter >= 5){
		createCookie("surveyNov08","1",999999);
		eraseCookie("surveyNov08Counter");
		popUpProps('http://www.newairplane.com/survey/nov08/Boeing Survey.html','surveynov08','400','280',',menubar=0,resizable=0,scrollbars=0,titlebar=0,toolbar=0');
	}
	*/
}

function checkSurvey()
{
	/*
	var counter = 1;
	if (readCookie("surveyNov08Counter")){
		counter = readCookie("surveyNov08Counter");
		eraseCookie("surveyNov08Counter");
		counter++;
	}
	
	createCookie("surveyNov08Counter",counter,999999);
	*/
}


function swfObjectResize(swfObject, swfObjectNewWidth, swfObjectNewHeight){
    var swfObjectEmbed = document.getElementById(swfObject);
    if(swfObjectEmbed != null){
		var objArrayID = -1;
		for(i=0;i<objArray.length;i++){
		    if(swfObject == objArray[i][0]){
		        objArrayID = i;
		    }
		}
        if(swfObjectNewWidth != null){
			if(true || BrowserDetect.browser == "Firefox" || BrowserDetect.browser == "Safari"){
				swfObjectEmbed.parentNode.style.width = swfObjectNewWidth;
			}
            swfObjectEmbed.style.width = swfObjectNewWidth;
            objArray[objArrayID][3] = swfObjectNewWidth.replace("px", "");
        }
        if(swfObjectNewHeight != null && swfObjectNewHeight != ""){
			if(true || BrowserDetect.browser == "Firefox" || BrowserDetect.browser == "Safari"){
				swfObjectEmbed.parentNode.style.height = swfObjectNewHeight;
			}
            swfObjectEmbed.style.height = swfObjectNewHeight;
            objArray[objArrayID][4] = swfObjectNewHeight.replace("px", "");
        }
    }
	if(!windowResizeOverride){
		windowResize();
	}
}


function windowOnload(){
    if(hasReqestedVersion){
		//window.scrollTo(0,0);

        document.getElementById("mainWrapper").style.visibility = "visible";
 
	    global100Divs = YAHOO.util.Dom.getElementsBy(checkDiv, "div");
	    if(BrowserDetect.browser != "Explorer"){
		    for(i=0;i<objArray.length;i++){
			
			    //var pos = YAHOO.util.Dom.getXY(document.getElementById(objArray[i][0])); 	
				//alert("test "+document.getElementById(objArray[i][0]));
				
				if(BrowserDetect.browser == "Safari" && navigator.userAgent.indexOf("10_6") != -1 && navigator.userAgent.indexOf("4.0.3") != -1){
					//safari 4.0.3 on snow leopard breaks the plugin and crashes the browser
				}else{
					document.getElementById(objArray[i][0]).style.width = "1px";
					document.getElementById(objArray[i][0]).style.height = Window.getScrollHeight();
					document.getElementById(objArray[i][0]).style.position = "absolute";

				}
				
			    var newPos = Array(0,0);
			    //YAHOO.util.Dom.setXY(document.getElementById(objArray[i][0]), newPos); 
				
				if(BrowserDetect.browser != "Safari"){
					document.getElementById(objArray[i][0]).style.left = newPos[0];
					document.getElementById(objArray[i][0]).style.top = newPos[1];
				}
    			
		    }
	    }
	    windowBackgroundScale();
    	
	    //safari anchor div fix
	    if(BrowserDetect.browser == "Safari"){	
	        if(window.location.href.match(/#(\w.+)/))
	        {
                strAnchor = window.location.href.match(/#(\w.+)/)[1];
                if(document.getElementById(strAnchor)){
                    window.scrollTo(0,document.getElementById(strAnchor).offsetTop);
                }
	        }
        }
    }else{
        document.getElementById("divFlashDetectionFailed").style.visibility = "visible";
    } 
}

window.onunload = function(){}; 

function swfRendered(object, width, height){

	if(BrowserDetect.browser == "Explorer"){
		var pos = YAHOO.util.Dom.getXY(document.getElementById(object).parentNode); 
		
		pos[0] = Math.round(pos[0]);
		pos[1] = Math.round(pos[1]);
		
		document.getElementById(object).style.left = pos[0];
		document.getElementById(object).style.top = pos[1];
		
		document.getElementById(object).style.position = "absolute";
	}
	
	if(width.indexOf("%") != -1){
		if(true || BrowserDetect.browser == "Firefox" || BrowserDetect.browser == "Safari"){
			document.getElementById(object).parentNode.style.width = width;
		}
		document.getElementById(object).style.width = width;
	}else{
		document.getElementById(object).style.width = width + "px";
	}
	if(height.indexOf("%") != -1){
		if(true || BrowserDetect.browser == "Firefox" || BrowserDetect.browser == "Safari"){
			document.getElementById(object).parentNode.style.height = height;
		}
		document.getElementById(object).style.height = height;
	}else{
		document.getElementById(object).style.height = height + "px";
	}
	

	if(BrowserDetect.browser == "Safari" && navigator.userAgent.indexOf("10_6") != -1 && navigator.userAgent.indexOf("4.0.3") != -1){
		//bug... seems to crash on resize of Safari Snow Leapord		
	}else{
		var pos = YAHOO.util.Dom.getXY(document.getElementById(object).parentNode); 
		pos[0] = Math.round(pos[0]);
		pos[1] = Math.round(pos[1]);
		
		document.getElementById(object).style.left = pos[0];
		document.getElementById(object).style.top = pos[1];
	}
	
	
	
	//fix: the following seems to fix a Firefox3.x issue with not redrawing the flash embed
	document.getElementById(object).parentNode.style.zIndex = 2;
	document.getElementById(object).style.zIndex = 2;
	document.getElementById(object).parentNode.style.zIndex = 1;
	document.getElementById(object).style.zIndex = 1;
	//fix: end
	
	footerPusher();

}

function windowResize(){
	windowBackgroundScale();

	
    if(hasReqestedVersion){
	    for(i=0;i<objArray.length;i++){
		    if(Window.getWindowWidth() >= objArray[i][3]){
			    if(objArray[i][1].indexOf("%") != -1 && document.getElementById(objArray[i][0]).style.width.indexOf("%") == -1){
				    document.getElementById(objArray[i][0]).style.width = objArray[i][1];
				    document.getElementById(objArray[i][0]).parentNode.style.width = "100%";
			    }
		    }
		    if(true || BrowserDetect.browser == "Firefox" || BrowserDetect.browser == "Safari"){
			
				if(BrowserDetect.browser == "Safari" && navigator.userAgent.indexOf("10_6") != -1 && navigator.userAgent.indexOf("4.0.3") != -1){
					//bug... seems to crash on resize of Safari Snow Leapord
				}else{
					var pos = YAHOO.util.Dom.getXY(document.getElementById(objArray[i][0]).parentNode); 
					
					pos[0] = Math.round(pos[0]);
					pos[1] = Math.round(pos[1]);				
					
					document.getElementById(objArray[i][0]).style.left = pos[0];
					document.getElementById(objArray[i][0]).style.top = pos[1];
				}
									
		    }
	    }
    }
	
	footerPusher();
}


function findPosX(obj)
  {
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
  }

  function findPosY(obj)
  {
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
  }


function checkDiv(object){
	if(object.style.width == "100%"){
		return true;
	}else{
		return false;
	}
}

function layerUp(object, _top, height){
	windowResizeOverride = true;
	flashObject = document.getElementById(object);
	layers = layers.sort();
	if(_top == "true"){
		layers.push(layers[layers.length-1]++);
		//flashObject.parentNode.style.zIndex = layers[layers.length-1];
		//flashObject.style.zIndex = layers[layers.length-1];
		console.log(flashObject.style.zIndex);
		flashObject.parentNode.style.zIndex = 1000;
		flashObject.style.zIndex = 1000;
		console.log(flashObject.style.zIndex);
	}else{
		if(layers.length == 1){
			layers[1] = 2;
		}
		flashObject.parentNode.style.zIndex = layers[1];
		flashObject.style.zIndex = layers[1];
	}
	flashObject.style.height = height;
}

function layerDown(object, height){
	flashObject = document.getElementById(object);
	flashObject.style.zIndex = 1;
	flashObject.style.height = height;
	windowResizeOverride = false;
}

function shiftFlashModule(shiftDif, object){
	flashObject = document.getElementById(object);
	flashObject.style.top = (parseInt(flashObject.style.top.substring(0, flashObject.style.top.indexOf("px"))) + parseInt(shiftDif)) + "px";
	alert(flashObject);
	flashObject.wootest("asf");
}

function test(){
	document.getElementById('swfModule500').wootest("asf");
}

function windowBackgroundScale(){
	for(i=0;i<global100Divs.length;i++){
		if(Window.getWindowWidth() < globalMinThreshold){
			global100Divs[i].style.width = globalMinThreshold+"px";
		}else{
			global100Divs[i].style.width = "100%";
		}
	}
}

function addToObjArray(objToAdd, widthToAddEmbedOrig, heightToAddEmbedOrig, widthToAddStageOrig, heightToAddStageOrig){
	objArray.push(new Array(objToAdd, widthToAddEmbedOrig, heightToAddEmbedOrig, widthToAddStageOrig, heightToAddStageOrig));
}

function anchor(tag){
    if(BrowserDetect.browser == "Safari"){
     /* 
		var form = document.createElement('form');
		form.style.display = 'none';

		form.action = rawURL + '#' + tag; 

		var link = document.createElement('input');
		link.type = 'submit';
		form.appendChild(link);

		document.body.appendChild(form);
		link.click();
		
		form.parentNode.removeChild(form);
		*/
	    window.location = rawURL + '#' + tag;
	}else{
		location.hash = tag;
	}

}


//scroll flicker for PC firefox
function flicker(){
	document.getElementById("mainWrapper").style.backgroundColor = "transparent";
}

var d = new Date();
var scrollStart = 0;
var waitForScroll = false;
window.onscroll = function(){
	if(BrowserDetect.browser == "Firefox" || BrowserDetect.browser == "Safari"){
		d = new Date();
		scrollStart = d.getTime();
		waitForScroll = true;
	}
}

function scrollLoop(){
	d = new Date();
	if(waitForScroll && scrollStart < (d.getTime() - 250)){
		waitForScroll = false;
		document.getElementById("mainWrapper").style.backgroundColor = "#ffffff";
		setTimeout(flicker, 10);
	}else{
	
	}
	setTimeout(scrollLoop, 50);
}

if(BrowserDetect.browser == "Firefox" || BrowserDetect.browser == "Safari"){
	scrollLoop();
}

function footerPusher(){
	if(BrowserDetect.browser != "Safari"){
		if (document.getElementById("footerPusher") != null){
			var divver = document.getElementById("footerPusher");
			var middle = YAHOO.util.Dom.getRegion("middleContainer");
			var header = YAHOO.util.Dom.getRegion("header");
			var footer = YAHOO.util.Dom.getRegion("footer");
			
			var totalHeight = (header.bottom-header.top)+(middle.bottom-middle.top)+(footer.bottom-footer.top);
			
			if(Window.getWindowHeight() > totalHeight){
				divver.style.height = (Window.getWindowHeight() - totalHeight) + "px";
			}else{
				divver.style.height = "0px";
			}
			//divver.style.height = (((Window.getWindowHeight()-(header.bottom-header.top))-(middle.bottom-middle.top))-(footer.bottom-footer.top))+"px";
		}
	}

}
function footerPusher2(){
	var divver = document.getElementById("footerPusher");
	var body = YAHOO.util.Dom.getRegion("divFlashDetectionFailed");
	divver.style.height = Window.getWindowHeight() - (body.bottom-body.top);
}
// ----------------------------------------------------------------------------------------------------------------------------------------------------------
// cookie
// ----------------------------------------------------------------------------------------------------------------------------------------------------------
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000)); //days
		//date.setTime(date.getTime()+(hours*60*60*1000));  //hours
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
function eraseCookie(name) {
	createCookie(name,"",-1);
}