// the variable to hold the flash name
var flashName;
var seedID = Math.floor(Math.random()*11111);
/*Turn on rendering div*/
function showFlashBox(argSwf,argElement,argFlashVars,overrideLowBand){
if($(argElement)) {
	// if we have main content lets temporarily clone it for the print stylesheet
	if(argElement == "main_content") {
		if ($(argElement).getFirst() && $('promo_background')){
			var clone = $(argElement).getFirst().clone().injectAfter('promo_background');
			clone.addClass('print');
			clone.setProperty('id', 'clone');
			var myArray = $$('#clone li');
			for(var i=0; i<myArray.length; i++) {
				myArray[i].setProperty('id', '');
			}
			myArray = $$('#clone a');
			for(var i=0; i<myArray.length; i++) {
				myArray[i].setProperty('id', '');
			}
			myArray = $$('#clone div');
			for(var i=0; i<myArray.length; i++) {
				myArray[i].setProperty('id', '');
			}
			myArray = $$('#clone ul');
			for(var i=0; i<myArray.length; i++) {
				myArray[i].setProperty('id', '');
			}
			myArray = $$('#clone p');
			for(var i=0; i<myArray.length; i++) {
				myArray[i].setProperty('id', '');
			}
			myArray = $$('#clone span');
			for(var i=0; i<myArray.length; i++) {
				myArray[i].setProperty('id', '');
			}
		}
	}

	if(argElement == "model_image") {
		var clone = $(argElement).getFirst().clone().injectAfter('colors');
		clone.addClass('print');
	}

	// cache buster for IE7 redraw issues
	if(argElement == "promos") {
		argSwf+= "?r=" + new Date().getTime();
	}

	var individualID = Math.floor(Math.random()*11111);
	/*
	    Prepare to load in the Flash rendering engine.
	    Uses Unobtrusive Flash Objects by Bobby van der Sluis
	    http://www.bobbyvandersluis.com/ufo/
	*/

	var isSafari = "";
 	navigator.userAgent.indexOf("Safari")>= 0 ? isSafari = true : isSafari = false;

	var isLTOpera92 = "";
 	navigator.userAgent.indexOf("Opera") >= 0 && parseFloat(navigator.appVersion) < 9.2 ? isLTOpera92 = true : isLTOpera92 = false;

	//Should Flash even be shown? (does user want it / can their browser support Flash 8 functionality)


	if ((userData.bandwidth != "low" && isLTOpera92 == false) || overrideLowBand ) {

    var site_config = {
    	'host':location.host,
        'src':location.href,
        'title':document.title,
    	'isSafari':isSafari,
    	'isLTOpera92':isLTOpera92,
    	'asset_path':'en',
    	'EOF':'EOF'
    };

    if(typeof(userData.zipcode) != "undefined") {
    	site_config['zip'] 			= userData.zipcode;
    }
    if( (typeof(cur_year) !="undefined" && cur_year != '') && typeof(cur_vehicle) !="undefined") {
    	site_config['cur_vehicle']	= vehicle_data[cur_year][cur_vehicle].apps.trilogy;
    }
    if(typeof(brand) != "undefined") {
    	site_config['brand'] 		= brand
    }
    if(typeof(embedded_disclaimers) != "undefined") {
    	site_config['embedded_disclaimers'] = Json.toString(embedded_disclaimers);
    }

	



	var flash_vars = '';
	for(k in site_config) {
		if (k != "toJSONString") {
    		flash_vars += "&" + k + "=" + encodeURIComponent(site_config[k]);
		}
	}

	// deep link & query strings
	if(location.href.indexOf("?") != -1) {
		// if we have a src in the query we need to strip it out to avoid overwrite
		if(location.href.indexOf("src=") != -1) {
			// split on the src
			var queryCleanedArray = location.href.split("src=");
			// if we have additional values past the src value
			if(queryCleanedArray[1].indexOf("&") != -1) {
				// remove the src altogether
				queryCleanedArray[1] = queryCleanedArray[1].substring(queryCleanedArray[1].indexOf("&")+1, queryCleanedArray[1].length);
			} else {
				// set the val to empty
				queryCleanedArray[1] = "";
			}
			// join on empty
			var cleanedVersion = queryCleanedArray.join("");
			flash_vars += "&" + cleanedVersion.substr(location.href.indexOf("?")+1);
		} else {
			flash_vars += "&" + location.href.substr(location.href.indexOf("?")+1);
		}
	}

	// add local connect sound vars
	flash_vars += "&localconnect_seed=" + seedID;
	flash_vars += "&localconnect_id=" + individualID;
	if(userData.audiopref == "on"){
		flash_vars += "&audio_pref=on";
	}else if(userData.audiopref == "off"){
		flash_vars += "&audio_pref=off";
	}
	//else{
		//flash_vars += "&audio_pref=off";
	//}
	// add text size preference
	if(typeof(userData.featureTextSize) == "number"){
		flash_vars += "&text_size="+userData.featureTextSize;
	}

	//flash_vars += "&argMLC=" + hbx.mlc;
	if ( $(argElement) ) {
		var render_foo = document.getElementById(argElement);
		render_foo.style.display = 'block';
	}

	// set the name fo the flash based on the div
	flashName = argElement +  '_flash';
	
	// pass the containing div height and width
	flash_vars += "&stageWidth="+parseInt($(argElement).getStyle('width'))+"&stageHeight=" + parseInt($(argElement).getStyle('height')); 

	if(argFlashVars != null) {
		flash_vars += argFlashVars;
	}
	var debug_html = getParameter('flash');
	var flash_version;
	if(debug_html == "false" && debug_html !=  ""){
		flash_version = 10;
	}else{
		flash_version = 8;
	}
	var FO = {
	    movie:        argSwf,
	    width:        "100%",
	    height:       "100%",
	    name:	  	   flashName,
	    id:		  flashName,
	    allowscriptaccess:"always",
	    bgcolor:      "#FFFFFF",
	    scale:        "noscale",
	    wmode:        "transparent",
	    play:         "true",
	    quality:      "high",
	    menu:         "false",
	    majorversion: flash_version,
	    build:        "0",
	    xi:           "false",
	    flashvars:    flash_vars
	};
	UFO.create(FO, argElement);

	//these 2 lines are handled in the UFO.create function.
	//var renderElement = document.getElementById(argElement);
	//renderElement.style.visibility = 'visible';
	} else {
		$(argElement).style.visibility = 'visible';
		// the promo div had this class initially, but older browsers would improperly place the div when it conatined Flash.
		if ($('promos')){
			$('promos').style.overflow = 'auto';
			//setlowbanddaalink();
			}
		}
	}
}

// External Interface hack for IE out of memory error
function nullFlashLoopFunction() { __flash_savedUnloadHandler = null; }
window.onbeforeunload = nullFlashLoopFunction;

function hasFlashMin(){
	var debug_html = getParameter('flash');
	var flash_version;
	if(debug_html == "false" && debug_html !=  ""){
		flash_version = 10;
	}else{
		flash_version = 8;
	}
	return(UFO.hasFlashVersion(flash_version,0));
}

/*Universal function to get parameter from url srting */
function getParameter(aP){var qS = new String(location.search.substring(1,location.search.length));var p = qS.split("&");var val = "";if(aP){for(i=0;i<p.length;i++){if(p[i].split( "=" )[0] == aP){val = p[i].split( "=" )[1];}}return val;}}

//
//	iePngFix()
//	Allows transparent PNG images to be display correctly in IE 5.5 and 6
//  http://homepage.ntlworld.com/bobosola. Updated 18-Jan-2006.
//  CSS backround PNGs not supported, use filter:progid:DXImageTransform.Microsoft.AlphaImageLoader in CSS for PNG backgrounds.
//

function iePngFix() {
	for(var i=0; i<document.images.length; i++) {
		var img = document.images[i]
		var imgName = img.src.toUpperCase()
		if (imgName.substring(imgName.length-3, imgName.length) == "PNG") {
			var imgID = (img.id) ? "id='" + img.id + "' " : ""
			var imgClass = (img.className) ? "class='" + img.className + "' " : ""
			var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
			var imgStyle = "display:inline-block;" + img.style.cssText
			if (img.align == "left") imgStyle = "float:left;" + imgStyle
			if (img.align == "right") imgStyle = "float:right;" + imgStyle
			if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
			var strNewHTML = "<span " + imgID + imgClass + imgTitle
			+ " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
			+ "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
			+ "(src=\'" + img.src + "\', sizingMethod='image');\"></span>"
			img.outerHTML = strNewHTML
			i = i-1
		}
	}
}
