/* introduced under CAS-09264/65/66 */
var lastPDFURLProcessed;
var isinInCookiesArray = new Array(); 	
var thisSiteName = '/';

thisSiteName= thisSiteName.replace("/","");
thisSiteName= thisSiteName.replace("/","");
thisSiteName = thisSiteName.toLowerCase()


function FundXplorerTabRecord(tabName,FundType)
{
	FundType=	getProperFundTypeName(FundType);
	tabName =	getProperTabName(tabName);
    scFundXplorerTabRecord(tabName,FundType);
    gaFundXplorerTabRecord(tabName,FundType);
    
    
	/*This order should be fix. First global event then fund explorer
everytime tab refresh, event needs to rebind.
*/
	attachGlobalPDFclickEvent();
	attachFundExplorerPDFclickEvent();
	attachFundExplorerFundclickEvent();
	

}

function fepdfclk(anchorTag,DocType)
{
	
}

function recordOnPDFClick(PDFName)
{
		scRecordOnPDFClick(PDFName);	
		gaRecordOnPDFClick(PDFName);
}

function getProperTabName(tabName)
{
	if(tabName == undefined) { return tabName;}
	tabName = tabName.toLowerCase();

	switch(tabName)
	{
		case 'docs':
		tabName = 'documents'
		break;

		case 'vals':
		tabName = 'quickvaluations'
		break;
		
		default:
		break;

	}//end of switch

	return tabName;
}



function getProperFundTypeName(FundType)
{
	if(FundType == undefined) { return FundType;}
	FundType = FundType.toLowerCase();

	switch(FundType)
	{
		case 'it':
		FundType = 'investmenttrusts'
		break;

		case 'lux':
		FundType = 'sicav'
		break;
		
		case 'lifeon':
		FundType = 'life'
		break;
		
		default:
		break;

	}//end of switch

	return FundType;
}


function attachGlobalPDFclickEvent()
{
	$("a[@href$=pdf]").click(function()
	{
		var urlOnAnchorTag;
		urlOnAnchorTag=$(this).attr("href");
		
		if(urlOnAnchorTag ==lastPDFURLProcessed ) {return;}
		
		lastPDFURLProcessed = urlOnAnchorTag;
		
		if(urlOnAnchorTag == undefined ||urlOnAnchorTag =="") {return;}

		 var eachUrlOnAnchorTag=urlOnAnchorTag.split("/");
		 var pdfNameInURL;
		 if(eachUrlOnAnchorTag.length==0) {return;}
		 
		 pdfNameInURL = eachUrlOnAnchorTag[eachUrlOnAnchorTag.length-1];
		 pdfNameInURL = unescape(pdfNameInURL);
		 recordOnPDFClick(pdfNameInURL);
		 
	});

}


function attachFundExplorerPDFclickEvent()
{
	$('.documents a,.docs a').click(function() 
	{
		var tdObj;
		var cellNo;
		var urlOnAnchorTag;
		urlOnAnchorTag=$(this).attr("href")
		if(urlOnAnchorTag == undefined ||urlOnAnchorTag =="") {return;}
		
		if(urlOnAnchorTag.indexOf(".pdf")>0) {return;} //exit if pdf is valid URL as attachGlobalPDFclickEvent already serving for purpose.
		
		if(urlOnAnchorTag ==lastPDFURLProcessed ) {return;}
		lastPDFURLProcessed = urlOnAnchorTag;
		
		
		
		tdObj=$(this).parent().get(0); ///get parent TD
		cellNo = tdObj.cellIndex; // find cell index object of TD
		
		var trObj;
		var fundName;
		trObj = $(tdObj).parent().get(0); // get parent TR
		fundName = $(trObj).attr("fund"); // collect fund name
		
		
		var tableObj;
		
		tableObj = $(this).parents("table").get(0); // get table of the TD
		var headercell= $(tableObj).find("thead>tr>th:nth-child("+(parseInt(cellNo)+1)+")");
		var headername;
		headername = headercell.html()

		fundName = fundName + ' - ' + headername;
		fundName = fundName.capitalize();
		
		recordOnPDFClick(fundName);	
		
		
	});
	

}


/*function to work on capitalize of string*/
String.prototype.capitalize = function(){
   return this.replace( /(^|\s)([a-z])/g , function(m,p1,p2){ return p1+p2.toUpperCase(); } );
  };
  

function getProperPDFTypeName(PDFCode)
{
	if(PDFCode == undefined) { return 'unknown';}
	PDFCode = PDFCode.toLowerCase();

	switch(PDFCode)
	{
		case 'MF':
		PDFCode = 'Monthly Factsheet';
		break;

		case 'SP':
		PDFCode = 'Simplifiled Prospectus';
		break;
		
		case 'P':
		PDFCode = 'Prospectus';
		break;

		case 'AR':
		PDFCode = 'Annual Reports';
		break;
		
		case 'HR':
		PDFCode = 'Half Year Reports';
		break;

		case 'ITP':
		PDFCode = 'Investment Trust Profiles';
		break;
		
		case 'QF':
		PDFCode = 'Quaterly Factsheet';
		break;
		
		default:
		break;

	}//end of switch

	return PDFCode;
}

function recordWatchListEvents(eventType,FundName)
{
	if(eventType == undefined) {return;}
	
	eventType = eventType.toLowerCase();
	
	if(eventType == 'add') { ga_WatchList_Add(FundName); }
	
	if(eventType == 'remove') {ga_WatchList_Remove() ;}
	
	
}



function attachFundExplorerFundclickEvent()
{

	$('.fundname a').click(function() 
	{
		var tdObj;
		var cellNo;
		var urlOnAnchorTag;
		urlOnAnchorTag=$(this).attr("href")
		if(urlOnAnchorTag == undefined ||urlOnAnchorTag =="") {return;}
		
		if(urlOnAnchorTag.indexOf(".pdf")>0) {return;} //exit if pdf is valid URL as GA Tracking already serving for purpose.
		
		
		tdObj=$(this).parent().get(0); ///get parent TD
				
		var trObj;
		var fundCode;
		trObj = $(tdObj).parent().get(0); // get parent TR
		fundCode = $(trObj).attr("isin"); // collect fund name
		
		storeFundVisitToCookies(fundCode);
	});
	

}

	
function storeFundVisitToCookies(isin)
{	
	var cookieName;
	var isinInCookies;
	
	if(isin == null || isin == undefined) {return ;}
	if(isin.length <=8) {return ;}

		
	cookieName = thisSiteName + 'fundvisits'
	isinInCookies = $.cookie(cookieName);
	//isinInCookies = isinInCookies + '1,2,3,4,4,5,5,6,6,6,7,2'
	if(isinInCookies!=null || isinInCookies!=undefined) {
                isinInCookies = isinInCookies.replace(";",",");
		isinInCookiesArray = isinInCookies.split(",");
	}
	
	addISINInVisitedList(isin);
	
	 if(isinInCookiesArray.length>0) {
			$.cookie(cookieName, isinInCookiesArray.join(','), { expires: 90 , path: '/' });
        }
}

function addISINInVisitedList(isin)
	{
		var isPresent;
		isPresent = isISINRecordAlreadyPresent(isin);
		
		if(!isPresent)
		{
			var thisIsin = new Array();
			
			if( isinInCookiesArray.length >=7 ) { 
					isinInCookiesArray.pop(); 
				}
			thisIsin[0] = isin;
			if(isinInCookiesArray.length>0) {	
				isinInCookiesArray = thisIsin.concat(isinInCookiesArray);
			}else {
				isinInCookiesArray=thisIsin;
			}
		}
}
	
	
function isISINRecordAlreadyPresent(checkISIN)
	{
	
		if(isinInCookiesArray.lenght <=0) {return false;}
		
		var lenghtOfArr=0;
		lenghtOfArr = isinInCookiesArray.lenght;
		
		
		for(i=0;i<lenghtOfArr;i++)
		{
				if(checkISIN==isinInCookiesArray[i])
				{	return true;
				}
		}
		return false;
	}
	


$(document).ready(function(){

attachGlobalPDFclickEvent();
attachFundExplorerPDFclickEvent();
attachFundExplorerFundclickEvent();
});
