/*
=========================================================================

FileName	:	intl_autos_dapad.js

Functions Included
FunctionName:	extractValues(shares,category,make,count);
Description	:	To extract the diffrent values seperated by delimiter [|]

FunctionName:	showDapad(pg,ap,width,height);
Description	:	To show the Dapad

FunctionName:	setAutoMake(autoMake,autoCategory);
Description	:	To set the automake and category for the page.
=========================================================================*/


/*
 		-------------------------------------------
		   Define the required global variable 
		-------------------------------------------
*/
var sponserShare = new Array();
var categories = new Array();
var makes = new Array();
var weightedShare=new Array(); 
var weightedCategories = new Array();
var adShown = null;
var pageContentMediaMake = null;
var pageContentMediaCategory = null;
/*
		---------------------------------------------------------
		Function to extract the delimiter seperated values and 
		populate the global variable accordingly11y
		---------------------------------------------------------
*/
function extractValues(shares,category,make,count){
	var shareIndex;
	var categoryIndex;
	var makeIndex;
	// Extract the delimiter seperated values and populate in respective arrays.
		for (i=0; i < count; i++){
				// If the value to be extracted is last
				if(i == count-1){
					shareIndex = shares.length;
					categoryIndex = category.length;
					makeIndex = make.length;
				}
				// Else process the delimter for extracting values
				else
				{
					shareIndex = shares.indexOf('|');
					categoryIndex = category.indexOf('|');
					makeIndex = make.indexOf('|');
				}
				// Populate the Arrays
				sponserShare[i]= shares.substring(0,shareIndex);
				categories[i]= category.substring(0,categoryIndex);
				makes[i]= make.substring(0,makeIndex);
				
				//Adjust the indexes
				shares = shares.substring(shareIndex+1,shares.length);
				category = category.substring(categoryIndex+1, category.length );
				make = make.substring(makeIndex+1, make.length);
		}
}
/*
		------------------------------------------------------
					Function to show the DapAd 
		------------------------------------------------------
*/
function showDapad(pg,ap,width,height){
 // Display the Dap Ad related to page content, if make is set
	makeFlag = false;
	for(i=0; i < makes.length; i++){
		if(pageContentMediaMake != null && makes[i] == pageContentMediaMake)
		{
			makeFlag = true;
			break;
		}
	}
	if(pageContentMediaMake != null &&  makeFlag != false )
	{
	dap("&PG=" + pg + "&AP=" + ap + "&CATEGORY=" + pageContentMediaCategory + "&MAKE=" + pageContentMediaMake,width,height);
	}
	else
	{
 			// If no Ad is appearing on the page 
		if(adShown == null){
			var totalNumber = 0;
			// Calculate the total share of the sponsers
			for (i=0; i < sponserShare.length ; i++){
				totalNumber += parseInt(sponserShare[i]);
			}
			// Loop through all sponsers Make values
			var currentSponser=0;
			while (currentSponser < makes.length){ 
			// populate the wighted arrays depending on the share of the sponsers
			for (i=0; i < sponserShare[currentSponser]; i++)
				{
					weightedShare[weightedShare.length] = makes[currentSponser];
					weightedCategories[weightedCategories.length] = categories[currentSponser];
				}
				currentSponser++;
			}
			//Generate the random number and call the dap() function for DapAd display
			var randomnumber=Math.floor(Math.random()*totalNumber);
			dap("&PG=" + pg + "&AP=" + ap + "&CATEGORY=" +weightedCategories[randomnumber]+ "&MAKE=" + weightedShare[randomnumber],width,height);
				// Set the adShown value to the current position of the Make in the Weigthted Arrays
				adShown = randomnumber;
			}
			else
				//else some Ad is already appearing on the page then display the Ad with same make and category values
			{
				dap("&PG=" + pg + "&AP=" + ap + "&CATEGORY=" +weightedCategories[adShown]+ "&MAKE=" + weightedShare[adShown],width,height);
			}
	}
}
/*
		------------------------------------------
		Function to set the auto make on the page
		------------------------------------------
*/
function setAutoMake(autoMake,autoCategory)
{
  //Check if there is make value
  if(autoMake != '')
  {
	pageContentMediaMake = autoMake;
	if(autoCategory == null)
		pageContentMediaCategory = "NONE";
	else
		pageContentMediaCategory = autoCategory;
  }
}
