//Version: 1.3 <16.06.2011>
//add setDomain

function myGoogle(oWrapper,oConf,sLocalConfData)
{
    this.oConf=oConf;
    this.oWrapper=oWrapper;
    this.sLocalConfData=sLocalConfData;

    this.index = function ()
    {
        this.oLocalConfData=this.loadConf();

        this.initial();
        this.oWrapper.registerFunctions("ga","trackPageView",this.trackPageView);
        this.oWrapper.registerFunctions("ga","submit",this.submit);

        this.oWrapper.registerFunctions("ga","trackEvent",this.trackEvent);
        this.oWrapper.registerFunctions("ga","trackError",this.trackError);
        this.oWrapper.registerFunctions("ga","trackAddToCart",this.trackAddToCart);
        this.oWrapper.registerFunctions("ga","trackSale",this.trackSale);
        this.oWrapper.registerFunctions("ga","trackSaleProduct",this.trackSaleProduct);
        this.oWrapper.registerFunctions("ga","trackProductView",this.trackProductView);
        this.oWrapper.registerFunctions("ga","trackSearch",this.trackSearch);
        this.oWrapper.registerFunctions("ga","trackTeaserClick",this.trackTeaserClick);

        //this.oWrapper.registerFunctions("ga","trackForm",this.trackForm);

        this.oWrapper.registerFunctions("ga","trackUserVar",this.trackUserVar);
        this.oWrapper.registerFunctions("ga","trackSessionVar",this.trackSessionVar);
        this.oWrapper.registerFunctions("ga","trackPageVar",this.trackPageVar);

        this.oWrapper.registerFunctions("ga","trackGoal",this.trackGoal);
		
		this.oWrapper.registerFunctions("ga","trackWatchList",this.trackWatchList);
		this.oWrapper.registerFunctions("ga","trackDeleteFromCart",this.trackDeleteFromCart);
		
		
		this.isTrackTrans = false;
    }

    this.initial = function ()
    {
        this.sendTrackPageView=false;

        window._gaq = window._gaq || [];
        _gaq.push(['_setAccount', this.oLocalConfData["id"]]);
		//1.3: setDomain hinzugefügt
		_gaq.push(['_setDomainName', this.oLocalConfData["sDomain"]]);

		
        (function() {
        var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
        ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
        })();
		
        this.sPageTitle=window.location.pathname;
    }

    this.loadConf = function()
    {
		/*1.2
        //Create the XML request
        xmlReq = null;
        if(window.XMLHttpRequest) xmlReq = new XMLHttpRequest();
        else if(window.ActiveXObject) xmlReq = new ActiveXObject("Microsoft.XMLHTTP");
        if(xmlReq==null) return false; // Failed to create the request

        // Make the request
        xmlReq.open ('GET', this.sLocalConfData, false);
        xmlReq.send (null);

        //json Daten interpretieren
        eval("oData="+xmlReq.responseText+";");
		*/
		
		//<+1.2
		eval("oData="+this.sLocalConfData);
		//+>
		
		
        /*
        var oFinalData=new Object();
        for(var Key in oData)
        {
            var aData = oData[Key].split(":");
            oFinalData[Key]= new Object();
            oFinalData[Key]["path"]=aData[0];
            oFinalData[Key]["object"]=aData[1];
        }
        */
        //Daten zurückgeben
        return oData;
    }

//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//+++Tracker Functions+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    this.trackPageView = function (sPageTitle,aPageGroup)
    {

		
		
        if(sPageTitle.length>0)
        {
            //Wenn ein Page Title angegeben ist.
            if(typeof(aPageGroup)=="object")
            {
                var sPageTitleFinal="";
                for(var sKey in aPageGroup)
                {
                    sPageTitleFinal+=aPageGroup[sKey]+"/";
                }

                sPageTitle=sPageTitleFinal+sPageTitle;
            }


            //_gaq.push(['_trackPageview', sPageTitle]);
            this.sendTrackPageView=sPageTitle;
            this.sPageTitle=sPageTitle;
        }
        else
        {
            //_gaq.push(['_trackPageview']);
            this.sendTrackPageView=true;
            this.sPageTitle= window.location.pathname;
        }
    }

    this.submit = function ()
    {
        if(this.sendTrackPageView==true)
        {
            _gaq.push(['_trackPageview']);
        }
        else if(this.sendTrackPageView!=false)
        {
            _gaq.push(['_trackPageview', this.sendTrackPageView]);
        }
		
		if(this.isTrackTrans===true)
		{
			_gaq.push(['_trackTrans']);
		}
    }

    this.trackEvent = function (sGroup,sName,sValue,oPar)
    {
        _gaq.push(['_trackEvent', sGroup, sName, sValue]);
    }

    this.trackError = function (sError)
    {
	//conf
        //var iVarIndex=1;
        var iVarIndex=this.oLocalConfData["iErrorVarIndex"];

        _gaq.push(["_setCustomVar", iVarIndex, "Error", sError, 3]);
    }


    this.trackSaleProduct = function (sOrderID,sProductID,sProductName,oCategory,sProductCost,iProductQuantity,oProductbundles,oPar)
    {
	
        for(sKey in oCategory)
        {
            var sCategory=oCategory[sKey];
            break;
        }

        _gaq.push(['_addItem',sOrderID,sProductID,sProductName,sCategory,sProductCost,iProductQuantity]);
    }


    this.trackSale = function (sOrderID, sTotal, sTax, sShipping, sCity, sState, sCountry)
    {

        //Im oPar wird die Kaufart übergeben - > diese in Affiliation tracken.
        var sAffiliation="";

        //letzten - entfernen
        if(sAffiliation.length>0)
        {
            sAffiliation=sAffiliation.substr(0,sAffiliation.length-3);
        }
        
        _gaq.push(['_addTrans',sOrderID, sAffiliation, sTotal,  sTax, sShipping, sCity, sState, sCountry]);
        this.isTrackTrans=true;
		
		
		//Adwords Conversion Codes ausgeben
		/*var oDiv=document.createElement("div");
		oDiv.innerHTML="<img height=\"1\" width=\"1\" style=\"border-style:none;\" alt=\"\" src=\"http://www.googleadservices.com/pagead/conversion/1044679853/?label=xx57CKegmwIQrZmS8gM&guid=ON&script=0\"/>";
		oDiv.innerHTML+="<img height=\"1\" width=\"1\" style=\"border-style:none;\" alt=\"\" src=\"http://www.googleadservices.com/pagead/conversion/1039683698/?label=MYRuCOirgAIQ8qDh7wM&guid=ON&script=0\"/>";
		document.getElementsByTagName("body")[0].appendChild(oDiv);
		*/
	}

    //this.trackProductView = function(sProductID,sProductGroup, aVars)
    //{
        //_gaq.push('_trackEvent', "Sale", "productView", sProductID);
    //}

    this.trackSearch = function (sSearchGroup, sSearchString, iSearchResult)
    {
       var aCluster=this.oLocalConfData["aCluster"];

        sCluster=false;

        for(var i =0;i<aCluster.length;i++)
        {
            if(aCluster[i]["min"] < iSearchResult && aCluster[i]["max"] > iSearchResult)
            {
                sCluster=aCluster[i]["min"]+"-"+aCluster[i]["max"];
                break;
            }
        }

        if(sCluster==false)
        {
            sCluster=aCluster[aCluster.length-1]["max"]+" +";
        }

        _gaq.push(['_trackEvent', sSearchGroup, sCluster, sSearchString]);

    }

    this.trackTeaserClick = function(sTeaserSite,sTeaserArea,sTeaserElement)
    {
        //this.sPageTitle
		
        _gaq.push(['_trackEvent', sTeaserSite, sTeaserArea, sTeaserElement]);
    }

    this.trackUserVar = function(sUserKey,sUserValue)
    {
        var iVarIndex=this.oLocalConfData["aUserVarSlots"][sUserKey];

        _gaq.push(['_setCustomVar', iVarIndex, sUserKey, sUserValue, 1]);
    }

    this.trackPageVar = function(sPageKey,sPageValue)
    {
        var iVarIndex=this.oLocalConfData["aPageVarSlots"][sPageKey];

        _gaq.push(['_setCustomVar', iVarIndex, sPageKey, sPageValue, 3]);
    }

    this.trackSessionVar = function(sSessionKey,sSessionValue)
    {
        var iVarIndex=this.oLocalConfData["aSessionVarSlots"][sSessionKey];

        _gaq.push(['_setCustomVar', iVarIndex, sSessionKey, sSessionValue, 2]);
    }
	
	
	//1.4: neue Funktion
    this.trackGoal = function(sGoalId)
    {
        var sGoal=this.oLocalConfData["aGoals"][sGoalId];

		_gaq.push(['_trackPageview', "goals/"+sGoal]);
    }
	

    this.trackAddToCart = function(sProductId,sProductGroup,sProductName,iProductQuantity,sProductCost)
	{
        _gaq.push(['_trackEvent', "addToCart", sProductGroup, sProductName]);	
	}
	
    this.trackWatchList = function(sProductId,sProductGroup,sProductName,sProductCost)
	{
        _gaq.push(['_trackEvent', "addToWatchlist", sProductGroup, sProductName]);		
	}

    this.trackDeleteFromCart = function(sProductId,sProductGroup,sProductName,sProductQuantity,sProductCost)
	{
        _gaq.push(['_trackEvent', "deleteFromCart_Groups", sProductGroup, sProductQuantity]);			
        _gaq.push(['_trackEvent', "deleteFromCart_Products", sProductName, sProductQuantity]);			
	}

	this.trackProductView = function(sProductID, sProductGroup, sProductName, sProduktpreis)	
	{
        _gaq.push(['_trackEvent', "productView", sProductGroup, sProductName]);			
	}
	
    this.index();
}
