// create the main object, to become the namespace
var electricity = {};

/* vars relevant for tracking */
electricity.s_account = "audiag227880"; // per microsite
electricity.pagetitle = "Audi > Microsite > F09";

electricity.basename = "audi";
electricity.defLang = "com";
electricity.defCountry = "com";
electricity.site = "microsite_f09_0809";
electricity.locale = "";

electricity.trackActive = true;
electricity.showDebug = true;

/* size of flash movie */
electricity.minW = 1016;
electricity.minH = 552;

/* ------------------- do not modify anything below --------------------------- */
electricity.onloads = []; // new Array();

electricity.addOnLoad = function(funcstr) {
	electricity.onloads.push(funcstr);
};

electricity.addOnLoad("electricity.correctPNG()");

onload = function() {
	for ( var i in electricity.onloads) {
		eval(electricity.onloads[i]);
	}
};

/* ---------- start of tracking functions ------------ */
electricity.setLocale = function() {
	if (window.location.search.indexOf('locale=') > -1) {
		var _locale = (window.location.search.split('locale=')[1]).split('&')[0];
		if (_locale.indexOf('_') > -1) {
			electricity.defLang = _locale.split('_')[0].toLowerCase();
			electricity.defCountry = _locale.split('_')[1].toLowerCase();
			electricity.locale = _locale;
		} else {
			electricity.locale = "";
		}
	}
};

/**
 * wrapper for old calls
 */
electricity.track_sendInfo = function(pageid, _country, _lang, isError) {
	if (electricity.locale === "") {
		electricity.setLocale();
	}
	electricity.track(pageid, _country ? _country.toLowerCase()
			: electricity.defCountry, _lang ? _lang.toLowerCase()
			: electricity.defLang, isError);
};

/**
 * wrapper for old calls
 */
electricity.track_sendEvent = function(pageid, _country, _lang) {
	if (electricity.locale === "") {
		electricity.setLocale();
	}
	electricity.trackLink(pageid, _country ? _country.toLowerCase()
			: electricity.defCountry, _lang ? _lang.toLowerCase()
			: electricity.defLang);
};

/**
 * track whole page
 */
electricity.track = function(pageid, _country, _lang, isError) {
	var s = electricity.createCommonSettings(pageid, _country, _lang);
	s.charSet = "UTF-8";
	s.currencyCode = "EUR";
	s.cookieDomainPeriods = 3;
	s.server = electricity.getURLParts(window.location).hostname;
	if (isError) {
		s.pageType = "errorPage";
	}
	if (electricity.getQuerystring("csref") !== null) {
		s.campaign = electricity.getQuerystring("csref");
	} // catch csref if exists

	if (typeof s.t == "function" && electricity.trackActive) {
		s.t();
	}
	if (electricity.showDebug) {
		electricity.log(s, "page");
	}
};

/**
 * track non-page views
 */
electricity.trackLink = function(pageid, _country, _lang) {
	var s = electricity.createCommonSettings(pageid, _country, _lang);

	if (typeof s.tl == "function" && electricity.trackActive) {
		s.tl();
	}
	if (electricity.showDebug) {
		electricity.log(s, "event");
	}
};

electricity.createCommonSettings = function(pageid, _country, _lang) {
	var userLinked = (pageid.indexOf("user_linked_to") > -1);

	var settings = s_gi(electricity.s_account);
	settings.pageName = userLinked ? "" : electricity.combinePageName(pageid,
			_country, _lang);
	settings.prop1 = electricity.pagetitle;
	settings.prop2 = userLinked ? "" : electricity.combinePageName(pageid);
	settings.prop3 = userLinked ? electricity.combinePageName(pageid) : "";
	settings.eVar15 = settings.prop15 = _country;
	settings.eVar16 = settings.prop16 = _lang ? _lang : electricity.defLang;
	settings.prop17 = settings.prop15 + "-" + settings.prop16;
	return settings;
};

electricity.combinePageName = function(pageid, country, lang) {
	var combined = electricity.basename;
	if (country) {
		combined += "/" + country;
	}
	combined += "/" + electricity.site;
	if (lang) {
		combined += "/" + lang;
	}
	combined += "/" + pageid;
	return combined;
};

electricity.getURLParts = function(location) {
	var params = location;
	return params;
};

electricity.log = function(s, type) {
	if (typeof console != "object") {
		return;
	}
	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 ------------ */

electricity.correctPNG = function() // 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
electricity.getQuerystring = function(key, default_) {
	if (default_ == null)
		default_ = "";
	key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
	var regex = new RegExp("[\\?&]" + key + "=([^&#]*)");
	var qs = regex.exec(window.location.href);
	if (qs == null)
		return default_;
	else
		return qs[1];
}

electricity.getColorDepth = function() {
	var d;
	d = screen.pixelDepth;
	if (d !== undefined && d !== null) {
		return d;
	}
	d = screen.colorDepth;
	if (d !== undefined && d !== null) {
		return d;
	}
	return 16;
};

electricity.addArgumentsToLink = function(id) {
	var link = document.getElementById(id);
	var url = link.href;
	var params = electricity.attachParams();
	var sep = "";
	if (params.length > 0) {
		sep = ((url.indexOf('?') > -1) ? '&' : '?');
	}
	link.href = url + sep + params;
};

electricity.openFSWindow = function(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 = electricity.attachParams();
	var sep = "";
	if (params.length > 0) {
		sep = ((url.indexOf('?') > -1) ? '&' : '?');
	}
	electricity.openWindow(url + sep + params, w, h, 'yes', 'no', 'yes', 0, 0,
			'no', 'Q5MS');
	electricity.correctPNG();
};

electricity.openWindow = function(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);
};

electricity.attachParams = function() {
	var paramString = window.location.search;
	var paramStringBuff = paramString.slice(1, paramString.lastIndexOf(""));
	return paramStringBuff;
};

electricity.addAllQueryParams = function(targetObj) {

	// parsing variables out of the url
	var paramString = window.location.search;
	var paramStringBuff = paramString.slice(1, paramString.lastIndexOf(""));
	var variable = [];
	var parameter = [];
	var paraSplit = [];

	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];
			targetObj[variable[i]] = paraSplit[i][1];

		}
	}// else alert('no parameters - no "?" ')
};

electricity.hideScroll = function() {
	document.getElementsByTagName('html')[0].style.overflow = 'hidden';
};

electricity.whichBrs = function() {
	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;
	}
};

electricity.styleChange = function() {
	switch (electricity.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;

	}
};

electricity.checkSize = function() {

	var w;
	var h;
	if (electricity.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';
	}
};


electricity.share = function(type, contentLink, contentText, contentSubject,
		additional) {
	var link = '';
	if (typeof additional == 'undefined') {
		additional = '';
	}
	if (type === 'twitter') {
		// twitter-link erstellen und öffnen
		link = 'http://twitter.com/home?&status=' + encodeURIComponent(contentLink);
		window.open(link, 'twitter', 'width=800,height=550,scrollbars=yes');
	} else if (type === 'facebook') {
		// facebook-link erstellen und öffnen
		link = 'http://www.facebook.com/share.php?src=bm&v=4&u='
				+ encodeURIComponent(contentLink) + '&t='
				+ encodeURIComponent(contentText);
		window.open(link, 'facebook', 'width=626,height=436,scrollbars=yes');
	} else if (type === 'delicious') {
		// delicious-link erstellen und öffnen
		link = 'http://delicious.com/save?v=5&noui&jump=close&url='
				+ encodeURIComponent(contentLink) + '&title='
				+ encodeURIComponent(contentText);
		window.open(link, 'delicious', 'width=550,height=550,scrollbars=yes');
	} else if (type === 'posterous') {
		// simply send as mail with receipient
		return electricity.share('email', contentLink, contentText,
				contentSubject, 'post@posterous.com');
	} else if (type === 'email') {
		// mailto erstellen und öffnen

		link = 'mailto:' + encodeURIComponent(additional) + '?subject='
				+ encodeURIComponent(contentSubject) + '&body='
				+ encodeURIComponent(contentText + '\n\n' + contentLink);
		window.location = link;
	}
};



