/**
 ** Coremetrics for Bazaar Voice Q&A and Stories for Product Listin/Brand Pages
 **/
var lastBVTabID; 
function communityTabSelected(tabname)
{
	var throwtag = false;
	if (tabname !== lastBVTabID){
      	switch (tabname)
        	{
	      	case "Community Q&A":
	            	CM_PageIDBV = "COMMUNITY Q&A: " + CM_PageID + ": EXPAND";
	             	CM_CatIDBV = "COMMQA";
	             	throwtag = true;
				lastBVTabID = tabname;				
	             	break;
		
	       	case "Stories":
	             	CM_PageIDBV = "Stories: " + CM_PageID + ": EXPAND";
	             	CM_CatIDBV = "COMMQA";
	             	throwtag = true;
				lastBVTabID = tabname;
	             	break;
	  	}
	  	if(throwtag)
	  	{
	  		cmCreateManualPageviewTag(CM_PageIDBV, CM_CatIDBV);
	  	}
	}
}


function BVQADisplayed(questionCount, answerCount)
{
    var BVQAHeaderID_last;
    if (questionCount > 0) {
        var bvALPLink = document.getElementById("BVALPLinkContainer");
        if (bvALPLink) { bvALPLink.style.display = "block"; }
        
        //Adds coremetrics to Bazaar Voice customer images for Q&A
    	  $bv("img[@id^='BVQAQuestionPhotoThumbnailImage']").bind("click", function() {		
		CM_PageIDBV = "COMMUNITY Q&A: " + CM_PageID + ": 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 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 = "";
		}
		
       	if (BVQAHeader !== null && BVQAMain !== null)
		{
			if (!$bv(BVQAMain).is(':visible'))
			{
				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: " + CM_PageID + ": " + 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 Q&A search
    $bv("button[@id^='BVQASearchFormSubmitButtonID']").bind("click", function() {	
		var serchTextBox = document.getElementById("BVQASearchFormTextInputID");	
		CM_PageIDBV = "COMMUNITY Q&A: " + CM_PageID + ": " + serchTextBox.value;
	  	CM_CatIDBV = "COMMQA";
		cmCreateManualPageviewTag(CM_PageIDBV, CM_CatIDBV);
    });
	    

    //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: " + CM_PageID + ": " + this.innerHTML + ": EXPAND";
		    CM_CatIDBV = "COMMQA";
		    throwtag = true;
		}

        	switch (this.name) 
		{
		     case "BV_TrackingTag_QA_Display_AskQuestion":
		        CM_PageIDBV = "COMMUNITY Q&A: " + CM_PageID ;
		        CM_CatIDBV = "COMMQA";
		        throwtag = true;
                	  break;

		     case "BV_TrackingTag_QA_Display_BrowseTab":
		        CM_PageIDBV = "VIEW ALL Q&A: " + CM_PageID ;
		        CM_CatIDBV = "COMMQA";
		        throwtag = true;
                	  break;

		     case "BV_TrackingTag_QA_Display_HomeTab":
		        CM_PageIDBV = "Q&A Home: " + CM_PageID ;
		        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: " + CM_PageID ;
	         CM_CatIDBV = "COMMQA";
	         throwtag = true;
	         break;

	       case "PreviewQuestion":
	         CM_PageIDBV = "ASK A NEW QUESTION PREVIEW PAGE: " + CM_PageID ;
	         CM_CatIDBV = "COMMQA";
	         throwtag = true;
	         break;

	       case "SubmittedQuestion":
	         CM_PageIDBV = "ASK A NEW QUESTION THANK YOU PAGE: " + CM_PageID ;
	         CM_CatIDBV = "COMMQA";
	         throwtag = true;
	         break; 

	       case "EditAnswer":                 
	         CM_PageIDBV = "ANSWER THIS QUESTION FORM: " + CM_PageID ;
	         CM_CatIDBV = "COMMQA";
	         throwtag = true;
	         break;

	       case "PreviewAnswer":                  
	         CM_PageIDBV = "ANSWER THIS QUESTION PREVIEW PAGE: " + CM_PageID ;
	         CM_CatIDBV = "COMMQA";
	         throwtag = true;
	         break;

	       case "SubmittedAnswer":
	         CM_PageIDBV = "ANSWER THIS QUESTION THANK YOU PAGE: " + CM_PageID ;
	         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: " + CM_PageID ;
     			    CM_CatIDBV = "COMMQA";
	     		    throwtag = true;
     		    	}
	            break;

	       case "BVSYPreviewCommentButtonID":
     		    	if (BVSYSubmissionErrorID == null)
     		    	{
     			    CM_PageIDBV = "SHARE YOUR STORY POST A COMMENT PREVIEW: " + CM_PageID ;
     			    CM_CatIDBV = "COMMQA";
	     		    throwtag = true;
     		    	}
	            break;

	       case "BVSYEditCommentButtonID":
	             CM_PageIDBV = "SHARE YOUR STORY POST A COMMENT: " + CM_PageID ;
	             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: " + CM_PageID + ": " + 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: " + CM_PageID + ": 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: " + CM_PageID ;
		CM_CatIDBV = "COMMQA";
		cmCreateManualPageviewTag(CM_PageIDBV, CM_CatIDBV);
    });
								

    var throwtag = false;
    switch (jsonData.interactionType)
    {
	       case "editStory":
	       	CM_PageIDBV = "SHARE YOUR STORY FORM: " + CM_PageID ;
	         	CM_CatIDBV = "COMMQA";
	         	throwtag = true;
	         	break;

	       case "previewStory":
	         	CM_PageIDBV = "SHARE YOUR STORY PREVIEW PAGE: " + CM_PageID ;
	         	CM_CatIDBV = "COMMQA";
	         	throwtag = true;
	         	break;

	       case "submittedStory":
	         	CM_PageIDBV = "SHARE YOUR STORY THANK YOU PAGE: " + CM_PageID ;
	         	CM_CatIDBV = "COMMQA";
	        	throwtag = true;
	         	break;                    
     }
	
     if(throwtag)
     {
	    cmCreateManualPageviewTag(CM_PageIDBV, CM_CatIDBV);
     }
      
}
	