var VW = function () {

	// bust out of any framesets before we do anything else
	if (top.location != self.location) {
		top.location.replace(self.location);
	}

	var mediaCom = {
		"/enter": 		        "type=gtipr798;cat=enter512;",
        "/door":	            "type=gtipr798;cat=doord108;",
        "/start":	            "type=gtipr798;cat=start922;",
        "/demo":	            "type=gtipr798;cat=democ888;",
        "/racenow":	            "type=gtipr798;cat=racen501;",
        "/racenow/details":	    "type=gtipr798;cat=racen526;",
		"/racenow/gti/details":	    "type=gtipr798;cat=racen526;",
		"/racenow/gtd/details":	"type=gtipr798;cat=racen292;",
        "/racenow/entername":	"type=gtipr798;cat=racen206;",
        "/racenow/howtoplay":	"type=gtipr798;cat=racen768;",
        "/race":	            "type=gtipr798;cat=raceh117;",
        "/leaderboards":	    "type=gtipr798;cat=leade581;",
        "/leaderboards/search":	"type=gtipr798;cat=leade638;",
        "/leaderboards/create":	"type=gtipr798;cat=leade793;",
        "/leaderboards/invite":	"type=gtipr798;cat=leade866;",
		"/howtoplay":	        "type=gtipr798;cat=howto641;",
        "/winagti":	            "type=gtipr798;cat=winag434;"
    };

	var trackData = {},
		campaignId = "",
		referrer = "";


    function initFlash() {
        $(document).ready(function () {

            // league magic
            var league = window.name.match(/league:([^,]+)/);
            if (league && league[1]) {
                league = league[1];
            }

            // email magic
            var email = window.name.match(/email:([^,]+)/);
            if (email && email[1]) {
                email = email[1];
            }

            var assetBasePath = "assets/";
            var mainSwfPath = "assets/swf/GTIProject.swf"
            var cdn = "http://cdn.digivault.co.uk/volkswagen/gtiproject/"

            // CDN magic
            if (window.location.hostname == "www.gtiproject.com") {
                assetBasePath = cdn + assetBasePath;
                mainSwfPath = cdn + mainSwfPath;
            }

            var flashVars = {
		    	email: email,
			    league: league,
				netStreamPrependPath:"../../", 
				assetBasePath: assetBasePath,
                amfChannelUrl: "http://" + document.location.host + "/messagebroker/amf"
            };

            var params = {
                quality: "high",
                align: "middle",
                allowScriptAccess: "always",
                swliveconnect: "true",
                scale: "noscale",
                allowFullScreen: "true",
                bgcolor: "#000000"
            }

            var attributes = {
                id: 'vw'
            }

            if (swfobject.hasFlashPlayerVersion("9.0.115")) {
                $("#flash").empty().append('<div id="vw"></div>');
                swfobject.embedSWF(
                    mainSwfPath,
                    "vw",
                    "100%", "100%", "9.0.115", "/assets/swf/expressInstall.swf",
                    flashVars, params, attributes);
                }

        });
    }

    function doLandingRedirect(league, email) {

        window.name = "league:" + league
        window.name += ",email:" + email

        doRedirect('/')
    }

    function doRedirect(url) {
		if ($.browser.safari) { // safari
			window.location.assign(url);
		} else {
			window.location.replace(url);
		}
	}

	/* external links in new window */
	$(document).ready(function () {
		$("a[@href^='http']").not("[@href^='http://"+location.hostname+"']").attr("target", "_blank");
		$("a.ext").attr("target", "_blank");
    });

	/* Tracking */

	function track(url) {

        log("tracking: " + url);

        // omniture
		doOmnitureTracking(url);

		// media com tracking tags
		if (mediaCom[url]) {
			log("mediacom: " + url + ' = "' + mediaCom[url] + '"');
			var axel = Math.random() + "";
			var a = axel * 10000000000000;
			$("body").append('<iframe src="//fls.doubleclick.net/activityi;src=1822532;' + mediaCom[url] + 'ord=1;num='+ a + '?" width="1" height="1" frameborder="0"></iframe>');
		}

        if ("/leaderboards" == url) {
            $("body").append("<img src='http://smp.adviva.net/track/v=4;m=1;t=3912;ts=" + new Date().getTime() + "' width='0' height='1' border='0' />");
        }
    }

	function doOmnitureTracking(url, configDetails) {

		s.pageName = url;
		s.trackFormList = false;
        s.g = document.location.hostname + url

        setOmnitureProperties(url, configDetails);

		var s_code = s.t();
		if (s_code) $("body").append(s_code);
	}

	function setOmnitureProperties(url, configDetails) {

        // channel
        s.channel = url;

        // server
        s.server = document.location.hostname;
    }

	function log(str) {
		if (window.location.hostname != "www.gtiproject.com" && typeof console != "undefined" && typeof console.log == "function") console.log(str);
	}

	return {
		initFlash: initFlash,
        doLandingRedirect: doLandingRedirect,
        track: track,
		log: log
	};
}();

try { /* IE6 flickering image fix */
  document.execCommand("BackgroundImageCache", false, true);
} catch(err) {}
