/// 
/// Costco Javascript Analytics proxy variables and functions.  
///
/// These allow Costco to switch Analytics providers with minimal pain.   
/// In the ideal conversion you would add the new provider's javascript library functions to the solution and
/// reference the functions here.  All existing Costco pages should make calls to functions in this file.
///
/// In practice, there might be new functions that might need to be created and new attributes to be passed. 
/// So they should be added to this file.
///

/// Analytics variables may be set by the code behind or Costco page specific scripts.
/// CostcoPage.cs sets the environment, default PageID, and default category variables.
//	var analyticsSubDomain;     /// Assumes this will be a sub domain under Costco.com or .ca
//	var analyticsSiteCountry;   /// .COM or .CA
//	var analyticsAcctNo;        /// Coremetrics ClientID used to separate reports by BC, BD, and CA.
//	var analyticsEnvironment;   /// Test or Production.

// Tab Tagging Flag Variables
var tabProductFlag;
var tabShippingFlag;
var tabReviewsFlag;

/// Functions will normally be called by the Costco pages specific scripts.
/// Uses JQuery and Coremetrics libraries.
//<script type='text/javascript' src='/Scripts/JQuery.js'></script>
//<script type='text/javascript' src='/Scripts/Coremetrics/eluminate.js'></script>
//<script type='text/javascript' src='/Scripts/Coremetrics/cmDataTagUtils.js'></script>

/// When the page is loaded, initialize the environment so tags can be sent at the appropriate time.
function AnalyticsPageSetup() {
    var clientID = $("input:hidden[name='analyticsAcctNo']").val();
    var firstParty = false; 
    var costcoFirstPartyDomain = $("input:hidden[name='analyticsDomain']").val();
	cmSetClientID( clientID, firstParty, costcoFirstPartyDomain);
	
	if (costcoFirstPartyDomain.length > 0)
	{
	  cmSetProduction();
	}
	AddActionClickTags();
	CheckForRegisteredUser();
}
 
 /// Adds action tags to Click events assoicated with images.  
 /// Examples: Watch Video, View PDF...
 function AddActionClickTags() {
	// Video and Image Tagging
	
	$("[src*='_Analytics_']").click(function(){
		//View_Analytics_Images_MoreViews.gif
		  wholeurl = this.src;
		  x = wholeurl.length;
		  while((wholeurl.substring(x,x-1)) != "."){ x--; } clipend = x;
		  while((wholeurl.substring(x,x-1)) != "/"){ x--; } clipstart = x;
		  var srcFile= unescape(wholeurl.substring(clipend-1,clipstart));	
		  var elementID=srcFile.substring(srcFile.lastIndexOf("_")+1);
		  srcFile=srcFile.substring(0,srcFile.lastIndexOf("_"));
		  var elementCat=srcFile.substring(srcFile.lastIndexOf("_")+1);
		  attributes=cG7.cM0[cm_ClientID];
		  cmCreatePageElementTag(elementID,elementCat,attributes); 
		  
		  // If the user came from an email, is on a product group page, and clicks on the details link, 
		  // set a cookie to allow Email to be sent as an attribute on the productViewTag.
		  if ( cmExtractParameter("cm_mmc",document.location.href) 
		       && cmExtractParameter("cm_mmc",document.location.href).toUpperCase().indexOf("EMAIL")>-1 
		       && this.src.toUpperCase().indexOf("VIEW_ANALYTICS_PRODUCTGROUP_PRODUCTDETAILS.GIF")>-1  ){  
			document.cookie = "cmEMAILFlag='Y'; path=/";
		  }
	});

	//pdf tagging
	$("[href*='.pdf']").click(function(){
		var pgID=cG7.cM0[cm_ClientID];
		var catID="PDF_Other";
		if (pgID.toUpperCase().indexOf("PRODUCT:")>-1){
			catID="PDF_Product";
		}
		   wholeurl = this.href;
		   x = wholeurl.length;
		   while((wholeurl.substring(x,x-1)) != "/"){ x--; } clipstart = x;
		   var pdfPage= unescape(wholeurl.substring(wholeurl.length,clipstart));
		   cmCreateManualPageviewTag(pdfPage,catID,this.href,window.location.href);
	});

//tab tagging

	$("[href='#ProductInfoTabs_ProductDetailsTab']").click(function(){ 
			if (tabProductFlag!=1){
				tabProductFlag=1;
				var attributes=cG7.cM0[cm_ClientID];
				cmCreatePageElementTag("PRODUCT DETAILS", "TABS",attributes); 
			}		
		});

	$("[href='#ProductInfoTabs_ShippingInfoTab']").click(function(){
			if (tabShippingFlag!=1){
				tabShippingFlag=1;
				var attributes=cG7.cM0[cm_ClientID];
				cmCreatePageElementTag("SHIPPING AND TERMS", "TABS",attributes); 
			}		
		});

	$("[href='#ProductInfoTabs_ReviewTab']").click(function(){ 
			if (tabReviewsFlag!=1){
				tabReviewsFlag=1;
				var attributes=cG7.cM0[cm_ClientID];
				cmCreatePageElementTag("PRODUCT REVIEWS", "TABS",attributes); 
			}		
		});
 }
 
 /// Send a Product page tag: ItemNo, SignDesc1, Category.   
 /// 
 function AnalyticsSendProductPage() {
	var prodID =   $("[name='AnalyticsProductID']").val();
	var prodName = $("[name='AnalyticsProductName']").val();
	var category = $("[name='AnalyticsCategory']").val();
	cmCreateProductviewTag( prodID,	prodName, category );
 }
 
 //
 //  Once the user logs in, the coremetrics registered user cookie is set.  
 //  This allows the next page, to send the coremetrics registered user ID 
 //  without cluttering the query string or complicating server side logic.
 //
 function CheckForRegisteredUser() {
	var regUserID = $.cookie('CoremetricsRegisteredUser');
	if (regUserID.length > 1)  // IE does not always clear regUserID
	{
		cmCreateRegistrationTag( regUserID );
		$.cookie('CoremetricsRegisteredUser', null, { expires:-1, path: '/' } );  //Clear the cookie so it is only sent once per login.
	}
}