/* vars relevant for tracking */
var s_account	= "audiag227821"; // per microsite
var pagetitle 	= "Audi > Microsite > EAspore";

var basename	= "audi";
var defLang		= "com";
var defCountry	= "com";
var site 		= "microsite_easpore_0109";
var locale		= "";

var trackActive	= true;
var showDebug	= true; 

/* size of flash movie */
var minW 		= 1024;
var minH 		= 600;

/* ------------------- do not modify anything below ---------------------------*/
var onloads		= new Array();

function addOnLoad(funcstr){
	onloads.push(funcstr);
}

addOnLoad("correctPNG()");

onload = function(){
	for (var i in onloads){
		eval(onloads[i]);
	}
}


/* ---------- start of tracking functions ------------*/
function setLocale(){
	if (window.location.search.indexOf('locale=') > -1){
		var _locale = (window.location.search.split('locale=')[1]).split('&')[0];
		if (_locale.indexOf('_') > -1){
			defLang 	= _locale.split('_')[0].toLowerCase();
			defCountry 	= _locale.split('_')[1].toLowerCase();
			locale = _locale;
		} else {
			locale = "";
		}
	}
}

/**
 * wrapper for old calls
 */
function track_sendInfo(pageid, _country, _lang, isError){
	if (locale === "") setLocale();
	track(pageid, _country?_country.toLowerCase():defCountry,_lang, isError);
}

/**
 * wrapper for old calls
 */
function track_sendEvent(pageid, _country, _lang) 	{
	if (locale === "") setLocale();
	trackLink(pageid, _country?_country.toLowerCase():defCountry,_lang);
}

/**
 * track whole page
 */
function track(pageid,  _country, _lang, isError) {
	var s = createCommonSettings(pageid,  _country, _lang);
	s.charSet = "UTF-8";
	s.currencyCode = "EUR";
	s.cookieDomainPeriods = 3;
	s.server = getURLParts(window.location).hostname;
	if (isError) s.pageType="errorPage";
	if (getQueryParamValue("csref") != null) s.prop5 = getQueryParamValue("csref"); // catch csref if exists

	if (typeof s.t == "function" && trackActive) {
		s.t();
	} 
	if (showDebug) {
		log(s, "page");
	}
}

/**
 * track non-page views
 */
 function trackLink(pageid, _country, _lang) {
 	var s = createCommonSettings(pageid,  _country, _lang);
 	
	if (typeof s.tl == "function" && trackActive) {
		s.tl();
	} 
	if (showDebug) {
		log(s, "event");
	}
 }
 
function createCommonSettings (pageid,  _country, _lang){
 	var userLinked = (pageid.indexOf("user_linked_to") > -1);
 	
 	var settings = s_gi(s_account);
 	settings.pageName = userLinked ? "" :combinePageName(pageid,_country,_lang);
 	settings.prop1 = pagetitle;
	settings.prop2 = userLinked ? "" : combinePageName(pageid); 
	settings.prop3 = userLinked ? combinePageName(pageid) : "";
	settings.eVar15 = settings.prop15 = _country;
	settings.eVar16 = settings.prop16 = _lang?_lang:defLang;
	settings.prop17= settings.prop15 + "-" + settings.prop16;
	return settings;
 }
 
function combinePageName(pageid, country, lang){
	var combined = basename;
	if (country)  combined += "/" + country;
	combined += "/" + site;
	if (lang) combined += "/" + lang;
	combined += "/" + pageid;
	return combined;
}
 
function getURLParts(location){
	var params = location;
	return params;
}

function log(s, type){
 	if (typeof console.log != "function") return;
 	console.log("track", type, "pageName: " + s.pageName);
 	var showProps = ["prop1","prop2", "prop3"];
 	for (var i in s){
 		if (showProps.indexOf(i) > -1){
		 	console.info(i + ": " + s[i]);
		 }
	 }
 }
/* ---------- end of tracking functions ------------*/

function correctPNG() // correctly handle PNG transparency in Win IE 5.5 & 6.
{
   var arVersion = navigator.appVersion.split("MSIE")
   var version = parseFloat(arVersion[1])
   if ((version >= 5.5 && (version < 7)) && (document.body.filters)) 
   {
      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='scale');\"></span>" 
            img.outerHTML = strNewHTML
            i = i-1
         }
      }
   }    
}

// toggle scrollbars depending on window size


function getColorDepth() {
  var d;
  d = screen.pixelDepth;
  if(d != undefined && d != null) {
    return d;
  }
  d = screen.colorDepth;
  if(d != undefined && d != null) {
    return d;
  }
  return 16;
}



function addArgumentsToLink(id){
	var link = document.getElementById(id);
	var url = link.href;
	var params = attachParams();
	var sep = "";
	if (params.length > 0) sep = ((url.indexOf('?') > -1)?'&':'?');
	link.href = url + sep + params;
}


function openFSWindow(url){
	var w = screen.availWidth;
	var h = screen.availHeight;
	if (document.layers){
		var tmp1 = window.outerWidth - window.innerWidth;
		var tmp2 = window.outerHeight - window.innerHeight;
		w -= tmp1;
		h -= tmp2;
	}
	var params = attachParams();
	var sep = "";
	if (params.length > 0) sep = ((url.indexOf('?') > -1)?'&':'?');
	openWindow(url + sep + params, w, h, 'yes', 'no', 'yes', 0, 0, 'no', 'Q5MS');
	correctPNG();
}
		
function openWindow(url, w, h, re, st, sc, le, to, center, type) {
	if (center == 'yes'){
		le = screen.width/2 - w/2;
	    to = screen.height/2 - h/2;
	}
	var params = 'width='+w+',height='+h+',scrollbars='+sc+',status='+re+',resizable='+re;
	newWin = window.open(url, type, params);
	newWin.moveTo(le, to);
	window.setTimeout("newWin.focus()", 250);
}

function attachParams(){
	var paramString = window.location.search;
	var paramStringBuff = paramString.slice(1,paramString.lastIndexOf(""));
	return paramStringBuff;
}
			
function addAllQueryParams(fo){
	
	//parsing variables out of the url
	var paramString = window.location.search;
	var paramStringBuff = paramString.slice(1,paramString.lastIndexOf(""))
	var variable = new Array();
	var	parameter = new Array();
	var paraSplit = new Array();
	
	if(paramString.charAt(0) == '?'){
		//alert("found parameters")
		parameter = paramStringBuff.split("&");
		for(var i = 0; i < parameter.length; i++){
			paraSplit[i] = parameter[i].split("=");
			variable[i] = paraSplit[i][0];

			//add variables to SWFObject (fo)	
			fo.addVariable(variable[i], getQueryParamValue(variable[i]));		
			//alert(variable[i] + "=" +  getQueryParamValue(variable[i]));				
			
		}
	}//else alert('no parameters - no "?" ')
}

function hideScroll(){
	document.getElementsByTagName('html')[0].style.overflow = 'hidden';
}

function whichBrs() {
	var agt=navigator.userAgent.toLowerCase();
	if (agt.indexOf("opera") != -1) return 'Opera';
	if (agt.indexOf("staroffice") != -1) return 'Star Office';
	if (agt.indexOf("webtv") != -1) return 'WebTV';
	if (agt.indexOf("beonex") != -1) return 'Beonex';
	if (agt.indexOf("chimera") != -1) return 'Chimera';
	if (agt.indexOf("netpositive") != -1) return 'NetPositive';
	if (agt.indexOf("phoenix") != -1) return 'Phoenix';
	if (agt.indexOf("firefox") != -1) return 'Firefox';
	if (agt.indexOf("safari") != -1) return 'Safari';
	if (agt.indexOf("skipstone") != -1) return 'SkipStone';
	if (agt.indexOf("msie") != -1) return 'Internet Explorer';
	if (agt.indexOf("netscape") != -1) return 'Netscape';
	if (agt.indexOf("mozilla/5.0") != -1) return 'Mozilla';
	if (agt.indexOf('\/') != -1) {
	if (agt.substr(0,agt.indexOf('\/')) != 'mozilla') {
	return navigator.userAgent.substr(0,agt.indexOf('\/'));}
	else return 'Netscape';} else if (agt.indexOf(' ') != -1)
	return navigator.userAgent.substr(0,agt.indexOf(' '));
	else return navigator.userAgent;
}

function styleChange(){
	switch(whichBrs()){
		case 'Internet Explorer':
			document.getElementById("upper").style.width = '1px';
			document.getElementById("container").style.left = '0px';
		break;
		case 'Safari':
			document.getElementById("upper").style.width = '0px';
			document.getElementById("container").style.left = '0px';
		break;
		case 'Opera':
			document.getElementById("upper").style.width = '0px';
			document.getElementById("container").style.left = '0px';
		break;
		case 'Firefox':
			document.getElementById("upper").style.width = '0px';
			document.getElementById("container").style.left = '0px';
		break;
			
	}
}

function checkSize() {
	
	if( whichBrs()!="Firefox" ) {
	
		var w;
		var h;
		
		if (whichBrs()=="Internet Explorer") {
			w = document.body.offsetWidth;
			h = document.body.offsetHeight;	
		} 
		else {
			w = window.innerWidth;
			h = window.innerHeight;
		}
		
		
		if (h<minH || w<minW) {
			document.getElementsByTagName('html')[0].style.overflow = 'auto';
		} else {
			document.getElementsByTagName('html')[0].style.overflow = 'hidden';
		}
	}
}

