/*
* READ COOKIE FUNCTION
*/
function getCookie(cookieName){
    var nameEQ = cookieName + "=";
	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;
}



/*
* ----------------------------------------------------------------------
* GOOGLE ANALYTICS TRACKING
* ----------------------------------------------------------------------
*/


/* 
* CALL GLOBAL tracking - On every page load
*/
try {
    var pageTracker = _gat._getTracker("UA-10604611-1");
    pageTracker._trackPageview();
} catch (err) {
    
}


/*
* CALL LOCAL tracking
*/
var cookieName = "ecm";//Set cookie name

var stringCookieValues = getCookie(cookieName);
var arrayCookieValues = stringCookieValues.split("&")

for (var i = 0; i < arrayCookieValues.length; i++) {
    name = arrayCookieValues[i].split('=')[0];
    if (name == "SiteLanguage") {
        value = arrayCookieValues[i].split('=')[1];
    }
}
var siteLanguage = value;

//Holds all locale's analytics ID's
var aCountryId = new Array();
aCountryId["10249"] = "UA-10604611-7"; // en-ROW
aCountryId["3081"] = "UA-10604611-6"; // en-AU
aCountryId["1040"] = "UA-10604611-3"; // it-IT
aCountryId["1033"] = "UA-10604611-10"; // en-US
aCountryId["1031"] = "UA-10604611-11"; // de-DE
aCountryId["1049"] = "UA-10604611-14"; // ru-RU
aCountryId["1034"] = "UA-10604611-15"; // es-ES
aCountryId["2057"] = "UA-10604611-9"; // en-GB
aCountryId["2055"] = "UA-10604611-13"; // de-CH
aCountryId["2064"] = "UA-10604611-16"; // it-CH
aCountryId["4108"] = "UA-10604611-17"; // fr-CH
aCountryId["3079"] = "UA-10604611-12"; // de-AT
aCountryId["6153"] = "UA-10604611-8"; // en-IE
aCountryId["1043"] = "UA-10604611-18"; // nl-NL
aCountryId["2060"] = "UA-10604611-19"; // fr-BE
aCountryId["1030"] = "UA-10604611-20"; // da-DK
aCountryId["1026"] = "UA-10604611-4"; // bg-BG
aCountryId["1029"] = "UA-10604611-5"; // cs-CZ
aCountryId["17417"] = "UA-10604611-21"; // en-MY
aCountryId["3076"] = "UA-10604611-22"; // zh-HK
aCountryId["5129"] = "UA-10604611-23"; // en-NZ
aCountryId["13321"] = "UA-10604611-24"; // en-PH
aCountryId["18441"] = "UA-10604611-25"; // en-SG
aCountryId["1038"] = "UA-10604611-26"; // hu-HU
aCountryId["2070"] = "UA-10604611-27"; // pt-PT
aCountryId["1048"] = "UA-10604611-28"; // ro-RO
aCountryId["11274"] = "UA-10604611-29"; // es-AR
aCountryId["2052"] = "UA-10604611-30"; // zh-CN
aCountryId["1028"] = "UA-10604611-31"; // zh-TW
aCountryId["1041"] = "UA-10604611-32"; // ja-JP

//loop through array to get locale analytics ID
for (var i in aCountryId) {

    var key = i;

    if (key == siteLanguage) {
        var googleTrackingCode = aCountryId[i];
        break;
    } else {
        var googleTrackingCode = "UA-10604611-7"; // en-ROW
    }
}

//Call analytics using locale analytics ID
try {
    var pageTrackerLocal = _gat._getTracker(googleTrackingCode);
    pageTrackerLocal._trackPageview();
} catch (err) {
    
}


/*
* Legacy tracking code
* Need to keep the function alive to avoid js errors, but do not use.
* The SWF for both Flavours and Story sections use the arTrackPageview() function
*/
function arTrackClick(page, click, link) {
//    try {
//        if (document.getElementById("legacyTracking") != null) {
//            if (document.getElementById("legacyTracking").value == "story") {
//                //alert(googleTrackingCode + "OurStory/" + click);
//                var pageTrackerLegacyClick = _gat._getTracker(googleTrackingCode);
//                pageTrackerLegacyClick._trackPageview("OurStory/" + page);
//            }
//            else if (document.getElementById("legacyTracking").value == "flavours") {
//                //alert(googleTrackingCode + " Flavours/" + click);
//                var pageTrackerLegacyClick = _gat._getTracker(googleTrackingCode);
//                pageTrackerLegacyClick._trackPageview("Flavours/" + page);
//            }
//        } else {
//            //alert("string = /" + link + "   -------   " + googleTrackingCode);
//            var pageTrackerLegacyClick = _gat._getTracker(googleTrackingCode);
//            pageTrackerLegacyClick._trackPageview(googleTrackingCode + "string = /" +   );
//        }
//    } catch (err) {
//        
//    }
}

/*
* Legacy tracking for the Flavours and Story SWF's
*/
function arTrackPageview(page, category) {
    try {
        if (document.getElementById("legacyTracking") != null) {
            if (document.getElementById("legacyTracking").value == "story") {
                //alert(googleTrackingCode + " OurStory/" + page);
                var pageTrackerLegacyPageView = _gat._getTracker(googleTrackingCode);
                pageTrackerLegacyPageView._trackPageview("OurStory/" + page);
            }
            else if (document.getElementById("legacyTracking").value == "flavours") {
                //alert(googleTrackingCode + " Flavours/" + page);
                var pageTrackerLegacyPageView = _gat._getTracker(googleTrackingCode);
                pageTrackerLegacyPageView._trackPageview("Flavours/" + page);
            }
        } else {
            //alert("string = /" + page + "   -------   " + googleTrackingCode);
            var pageTrackerLegacyPageView = _gat._getTracker(googleTrackingCode);
            pageTrackerLegacyPageView._trackPageview(page);
        }
    } catch (err) {

    }
}


/*
* Drinks and Cocktails Recipe view
* Called when a specific Receipe is selected 
* (Call is made from recipe.js)
*/
function occasionTracking() {
    var drinkName = document.getElementsByTagName("h2")[0].innerHTML;
    var pageTracker = _gat._getTracker(googleTrackingCode);
    pageTracker._trackPageview("Occasions/Recipe/" + drinkName);
}


/*
* Home/LTYL/Drinks and Cocktails
* Should be called when ever a user clicks on a share this link
*/
function addthisTracking() {
    var pageTracker = _gat._getTracker(googleTrackingCode);
    pageTracker._trackPageview("addThis")
}


/*
* Drinks and Cocktails Recipe view
* Called when the print this link is clicked on a specific Receipe 
* (Call is made from recipe.js)
*/
function printThisTracking() {
    var pageTracker = _gat._getTracker(googleTrackingCode);
    var pageH2Tag = document.getElementsByTagName("h2")[0].innerHTML;
    pageTracker._trackPageview("Print Recipe - " + pageH2Tag);
}




