// Kuoni tracking script
Valtech.Namespace.create("Kuoni.Style.Ga");

Kuoni.Style.Ga = (function () {
	
	var gaCurrentURL = document.location.pathname;
		
	var init = function () {
		      //Print page tagging
          $("#page-tools .print-page").click(function() {
            pageTracker._trackPageview("/ce/print-page/"+gaCurrentURL+"/");
            pageTracker._trackEvent("page tools", "print page", gaCurrentURL);
          });
          
          //add to wishlist tagging
          $("#content-tools .addtowishlist a[href='#removefromwishlist']").click(function() {
            var gaCmpID = $("#content-tools").metadata().wishlistItem.CmpId;
            pageTracker._trackPageview("/ce/remove-from-wish-list-page-click/");
            pageTracker._trackEvent("page tools", "remove from wish list - page", gaCmpID);
          });
          
          $("#content-tools .addtowishlist a[href='#addtowishlist']").click(function() {
            var gaCmpID = $("#content-tools").metadata().wishlistItem.CmpId;
            pageTracker._trackPageview("/ce/add-to-wish-list-click/");
            pageTracker._trackEvent("page tools", "add to wish list", gaCmpID);
          });
          
          $("#my-wishlist a.remove").click(function() {
            pageTracker._trackPageview("/ce/remove-from-wish-list-member-click/");
            pageTracker._trackEvent("page tools", "remove from wish list - member", '');
          });
          
           $("#my-profile-change-pass a.delete-profile").click(function() {
            pageTracker._trackPageview("/ce/delete-membership-click/");
            pageTracker._trackEvent("page tools", "delete membership", '');
          });
          
          
          
  
          //External links tagging
          $("#content a").each(function() {
            var $aex = $(this);
            var hrefex = $aex.attr('href');
            if (!hrefex) return;
            
            // see if the link is external
            if ( (hrefex.match(/^http/)) && (! hrefex.match(document.domain)) ) {
              // if so, add the GA tracking code
              $aex.click(function() {
                pageTracker._trackPageview('/ce/exturl/' + hrefex);
                pageTracker._trackEvent('external link', 'click', "link:"+hrefex +" page:"+ gaCurrentURL);
              });
            }
          });
          
          //download links tagging
          $("a[href$='.pdf'],a[href$='.jpg'],a[href$='.doc'],a[href$='.gif'],a[href$='.zip'],a[href$='.exe']").click(function() {
            var $adl = $(this);
            var hrefdl = $adl.attr('href');
            pageTracker._trackPageview('/ce/file-download/'+hrefdl+"/page/"+ gaCurrentURL);
            pageTracker._trackEvent('downloads', 'click', "file:"+hrefdl +" - page:"+gaCurrentURL);
          });

            //user logged in setting cookie
            var gaUserInfo = document.userContext;
            if(typeof gaUserInfo !== 'undefined'){
              if(document.userContext.IsUserLoggedIn == 'true'){
                pageTracker._setVar("logged-in");
               }  
            }
            
            //layers + light boxes
           $("#content a.snippetbox").click(function() {
                  var lightboxPopup = $(this).attr('href').substring(1);
                  //alert(lightboxPopup);
                  pageTracker._trackPageview("/ce/popup/"+lightboxPopup+gaCurrentURL+"/");
                  pageTracker._trackEvent("popup", lightboxPopup, gaCurrentURL);
           });

	};
	
	var gaFormHandler = function (trackingType, fromId, fieldId) {
		if(trackingType == 'FormSubmit'){
			pageTracker._trackPageview("/ce/form/submitted/"+fromId+"/");
			pageTracker._trackEvent("forms", "submitted", fromId);
		}
		else if(trackingType == 'FormFieldError'){
			pageTracker._trackPageview("/ce/form/field-error/"+fromId+"/"+fieldId);
			pageTracker._trackEvent("forms", "error", fromId+" - "+fieldId);
			
		}
		else{}
		
	};
	return {
		init : init,
		gaFormHandler : gaFormHandler
	}
})();
     

$(window).load(Kuoni.Style.Ga.init);