/**
 ** Coremetrics for Bazaar Voice Q&A and Stories for Product Detail Pages
 ** Change History
 ** Date        Modified BY          Description
 ** 20100114    Usman Farooq         Changed to throw Manual Page View Tag instead of Product View Tag
 ** 20100412    Usman Farooq	     Removed @ sign (as is depricated after JQuery V 3) from JQuery injections into 
 **    				     Bazaar Voice contents. As BV changed JQuery Version at there end.
 **/
var lastBVTabID;
var BVQAHeaderID_last;
var BVQAAnswerClick = false;

function communityTabSelected(tabname)
{
	var throwtag = false;
        if (tabname !== lastBVTabID) {
        	switch (tabname)
        	{
	   		case "Community Q&A":
	     			CM_PageIDBV = "COMMUNITY Q&A: " + BVProdID + " - " + BVShortDesc + ": EXPAND";
	     			CM_CatIDBV = "COMMQA";
	     			throwtag = true;
				lastBVTabID = tabname;
	     			break;

	   		case "Stories":
	     			CM_PageIDBV = "Stories: " + BVProdID + " - " + BVShortDesc + ": EXPAND";
	     			CM_CatIDBV = "COMMQA";
	     			throwtag = true;
				lastBVTabID = tabname;
	     			break;   
		
			default:
				lastBVTabID = tabname;
	     			break;  			                  
 
		}
		if(throwtag)
		{
			cmCreateManualPageviewTag(CM_PageIDBV, CM_CatIDBV);
		}
	}
}


function BVQADisplayed(questionCount, answerCount)
{	
    if (questionCount > 0) {

        var bvALPLink = document.getElementById("BVALPLinkContainer");
        if (bvALPLink) { bvALPLink.style.display = "block"; }

        //Adds coremetrics to Bazaar Voice customer images
    	$bv("img[id^='BVQAQuestionPhotoThumbnailImage']").bind("click", function() {		
            	CM_PageIDBV = "COMMUNITY Q&A: " + BVProdID + " - " + BVShortDesc + ": CUSTOMER IMAGES";
	    	CM_CatIDBV = "COMMQA";
	    	cmCreateManualPageviewTag(CM_PageIDBV, CM_CatIDBV);
	});

	//Adds coremetrics to Bazaar Voice Question Expand
	//This code runs after user expands Question for first time as Bazaar Voice removes id/name from the link
	// and places on click for expand/collapse of question on BVQAQuestionAndAnswers div 
    	$bv("div[id^='BVQAQuestionAndAnswers']").bind("click", function() {

		var BVQAID = this.id;
		var bvIDCharlength = 22; //BVQAQuestionAndAnswers has length 22 and after that there is Q&A ID
                var BVQAHeaderID = "BVQAQuestionHeader" + BVQAID.substr(bvIDCharlength ,BVQAID.length);
		var BVQAMain = "#BVQAQuestionMain" + BVQAID.substr(bvIDCharlength,BVQAID.length);	
		var BVQAHeader =  document.getElementById(BVQAHeaderID);
		

		if (BVQAHeaderID_last == null) {
			BVQAHeaderID_last = "";
		}
		else {
			var rExp = /BV_TrackingTag_QA_Display_HomteTab_QuestionSelect.+/gi;
			if (BVQAHeaderID_last.search(rExp) != -1)
			{
				BVQAHeaderID_last = "BVQAQuestionHeader" + BVQAHeaderID_last.substr(BVQAHeaderID_last.lastIndexOf("_") + 1, BVQAHeaderID_last.length);
			}
		}

		if (BVQAAnswerClick){ 
					BVQAHeaderID_last = BVQAHeaderID;
					BVQAAnswerClick = false;
		}

		if (BVQAHeader !== null && BVQAMain !== null)
		{

			if (!$bv(BVQAMain).is(':visible') && (BVQAHeaderID  == BVQAHeaderID_last))
			{
				BVQAHeaderID_last = "";
			}


			if ((BVQAHeader.innerHTML.length > 0) && ($bv(BVQAMain).is(':visible')) && (BVQAHeaderID !==  BVQAHeaderID_last))
			{
		    		var rExp = new RegExp(/\#.+?\s?>.+<\/a>/gi); //to get the value inside hyperlink
		    		var BVQAHREF = rExp.exec(BVQAHeader.innerHTML);
		    		var throwtag = false;
        			BVQAHeaderID_last = BVQAHeaderID;
				
				if (BVQAHREF !== null)
				{
		    			var BVQATitle = BVQAHREF[0].substr(BVQAHREF[0].indexOf(">") +  1,BVQAHREF[0].lastIndexOf("<") - (BVQAHREF[0].indexOf(">") + 1));
		    			CM_PageIDBV = "COMMUNITY Q&A: " + BVProdID + " - " + BVShortDesc + ": " + BVQATitle +  ": EXPAND";
		    			CM_CatIDBV = "COMMQA";
		    			throwtag = true;
				}
		
				if(throwtag)
				{
					cmCreateManualPageviewTag(CM_PageIDBV, CM_CatIDBV);
				}
		 	}
		}
	});
    }
 
    // Other custom items leveraging these values.
    // TODO: insert Web Analytics tracking here
    
    //Adds coremetrics to Bazaar Voice Links
    $bv("a[name^='BV_TrackingTag_QA_Display']").bind("click", function() {
		var throwtag = false;
		var rExp = /BV_TrackingTag_QA_Display_HomteTab_QuestionSelect.+/gi;
		if (this.name.search(rExp) != -1)
		{
		    CM_PageIDBV = "COMMUNITY Q&A: " + BVProdID + " - " + BVShortDesc + ": " + this.innerHTML + ": EXPAND";
		    CM_CatIDBV = "COMMQA";
		    throwtag = true;
		    BVQAHeaderID_last = this.name;
		}

                switch (this.name) 
		{
		 case "BV_TrackingTag_QA_Display_AskQuestion":
		    CM_PageIDBV = "COMMUNITY Q&A: " + BVProdID + " - " + BVShortDesc;
		    CM_CatIDBV = "COMMQA";
		    throwtag = true;
		    BVQAAnswerClick = true;
                    break;

		 case "BV_TrackingTag_QA_Display_BrowseTab":
		    CM_PageIDBV = "VIEW ALL Q&A: " + BVProdID + " - " + BVShortDesc;
		    CM_CatIDBV = "COMMQA";
		    throwtag = true;
                    break;

		 case "BV_TrackingTag_QA_Display_HomeTab":
		    CM_PageIDBV = "Q&A Home: " + BVProdID + " - " + BVShortDesc;
		    CM_CatIDBV = "COMMQA";
		    throwtag = true;
                    break;
		}
		
		if(throwtag)
		{
			cmCreateManualPageviewTag(CM_PageIDBV, CM_CatIDBV);
		}
    });
}

function BVQASubmissionPageDisplayed(pageName,pageStatus)
{	
	var throwtag = false;
        switch (pageName)
        {

	   case "EditQuestion":
	     CM_PageIDBV = "ASK A NEW QUESTION FORM: " + BVProdID + " - " + BVShortDesc;
	     CM_CatIDBV = "COMMQA";
	     throwtag = true;
	     break;

	   case "PreviewQuestion":
	     CM_PageIDBV = "ASK A NEW QUESTION PREVIEW PAGE: " + BVProdID + " - " + BVShortDesc;
	     CM_CatIDBV = "COMMQA";
	     throwtag = true;
	     break;

	   case "SubmittedQuestion":
	     CM_PageIDBV = "ASK A NEW QUESTION THANK YOU PAGE: " + BVProdID + " - " + BVShortDesc;
	     CM_CatIDBV = "COMMQA";
	     throwtag = true;
	     break; 

	   case "EditAnswer":                 
	     CM_PageIDBV = "ANSWER THIS QUESTION FORM: " + BVProdID + " - " + BVShortDesc;
	     CM_CatIDBV = "COMMQA";
	     throwtag = true;
	     break;

	   case "PreviewAnswer":                  
	     CM_PageIDBV = "ANSWER THIS QUESTION PREVIEW PAGE: " + BVProdID + " - " + BVShortDesc;
	     CM_CatIDBV = "COMMQA";
	     throwtag = true;
	     break;

	   case "SubmittedAnswer":
	     CM_PageIDBV = "ANSWER THIS QUESTION THANK YOU PAGE: " + BVProdID + " - " + BVShortDesc;
	     CM_CatIDBV = "COMMQA";
	     throwtag = true;
	     break;
 		
         }
	 
	if(throwtag)
	{
		cmCreateManualPageviewTag(CM_PageIDBV, CM_CatIDBV);
	}
}



function BVSYCommentsCoremetrics(){

	$bv("input[id^='BVSYPreviewCommentButtonID']").bind("click", function() {
     		setTimeout("BVSYCommentsThrowTag('BVSYPreviewCommentButtonID')",3000);
     		setTimeout("BVSYCommentsCoremetrics()", 3000);
	});

	$bv("input[id^='BVSYSubmitCommentButtonID']").bind("click", function() {
     		setTimeout("BVSYCommentsThrowTag('BVSYSubmitCommentButtonID')",3000);
     		setTimeout("BVSYCommentsCoremetrics()", 3000);
	});

	$bv("input[id^='BVSYEditCommentButtonID']").bind("click", function() {
     		setTimeout("BVSYCommentsThrowTag('BVSYEditCommentButtonID')",3000);
     		setTimeout("BVSYCommentsCoremetrics()", 3000);
	});

}

function BVSYCommentsThrowTag(name){

	var throwtag = false;
     	var BVSYSubmissionErrorID = document.getElementById("BVSYSubmissionErrorID");
        switch (name)
        {
	   case "BVSYSubmitCommentButtonID":
     		if (BVSYSubmissionErrorID == null)
     		{
     			CM_PageIDBV = "SHARE YOUR STORY POST A COMMENT THANK YOU: " + BVProdID + " - " + BVShortDesc;
     			CM_CatIDBV = "COMMQA";
	     		throwtag = true;
     		}
	     	break;

	   case "BVSYPreviewCommentButtonID":
     		if (BVSYSubmissionErrorID == null)
     		{
     			CM_PageIDBV = "SHARE YOUR STORY POST A COMMENT PREVIEW: " + BVProdID + " - " + BVShortDesc;
     			CM_CatIDBV = "COMMQA";
	     		throwtag = true;
     		}
	    	break;

	   case "BVSYEditCommentButtonID":
	     	CM_PageIDBV = "SHARE YOUR STORY POST A COMMENT: " + BVProdID + " - " + BVShortDesc;
	     	CM_CatIDBV = "COMMQA";
	     	throwtag = true;
	     	break;                    
         }
	
	if(throwtag)
	{
		cmCreateManualPageviewTag(CM_PageIDBV, CM_CatIDBV);
	}
}


function BVSYTitleCoremetrics(){
	var BVSYTitle = getElementsByClass('BVSYValue BVSYStoryContentTitle' ,  document.getElementById('BVSYDisplayContentBodyID') , 'span');
      	BVSYTitle = BVSYTitle[0].innerHTML;
	if (BVSYTitle !== null)
	{
		CM_PageIDBV = "Stories: " + BVProdID + " - " + BVShortDesc + ": " + BVSYTitle + ": EXPAND";
		CM_CatIDBV = "COMMQA";
		cmCreateManualPageviewTag(CM_PageIDBV, CM_CatIDBV);
	}
}

function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");//To search the class name
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			break;
		}
	}
	return classElements;
}

function BVSYAnalytics(jsonData) {
// TODO: insert Web Analytics tracking here

	//Adds coremetrics to Bazaar Voice customer images for stories
	$bv("img[id^='BVSYStoryPhotoThumbnailImage']").bind("click", function() {		
	    CM_PageIDBV = "Stories: " + BVProdID + " - " + BVShortDesc + ": CUSTOMER IMAGES";
	    CM_CatIDBV = "COMMQA";
	    cmCreateManualPageviewTag(CM_PageIDBV, CM_CatIDBV);
    	});

	$bv("a[class^='BVSYStoriesImageLink BVSYStoriesReadThisLink BVSYStoriesReadThisBottomLink']").bind("click",  function() {
		setTimeout("BVSYTitleCoremetrics()",3000);
    	});

	$bv("a[class^='BVSYStoriesTitleLink']").bind("click", function() {
		setTimeout("BVSYTitleCoremetrics()",3000);
    	});

	$bv("a[class^='BVSYGridStoryReadMoreTextLink']").bind("click", function() {
		setTimeout("BVSYTitleCoremetrics()",3000);
    	});

    	$bv("a[name^='BV_TrackingTag_Story_Display_NextPage']").bind("click", function() {
		setTimeout("BVSYTitleCoremetrics()",3000);
    	});

    	$bv("a[name^='BV_TrackingTag_Story_Display_PageNumber']").bind("click", function() {
		setTimeout("BVSYTitleCoremetrics()",3000);
    	});

    	$bv("a[name^='BV_TrackingTag_Story_Display_PrevPage']").bind("click", function() {
		setTimeout("BVSYTitleCoremetrics()",3000);
    	});


	$bv("a[name^='BV_TrackingTag_Story_Display_PostStoryComment']").bind("click", function() {
		CM_PageIDBV = "SHARE YOUR STORY POST A COMMENT: " + BVProdID + " - " + BVShortDesc;
		CM_CatIDBV = "COMMQA";
		cmCreateManualPageviewTag(CM_PageIDBV, CM_CatIDBV);
    	});
								

	var throwtag = false;
        switch (jsonData.interactionType)
        {
	   case "editStory":
	     CM_PageIDBV = "SHARE YOUR STORY FORM: " + BVProdID + " - " + BVShortDesc;
	     CM_CatIDBV = "COMMQA";
	     throwtag = true;
	     break;

	   case "previewStory":
	     CM_PageIDBV = "SHARE YOUR STORY PREVIEW PAGE: " + BVProdID + " - " + BVShortDesc;
	     CM_CatIDBV = "COMMQA";
	     throwtag = true;
	     break;

	   case "submittedStory":
	     CM_PageIDBV = "SHARE YOUR STORY THANK YOU PAGE: " + BVProdID + " - " + BVShortDesc;
	     CM_CatIDBV = "COMMQA";
	     throwtag = true;
	     break;                    
         }
	
	if(throwtag)
	{
		cmCreateManualPageviewTag(CM_PageIDBV, CM_CatIDBV);
	}
      
}
