// $Id: adSwap.js,v 1.3 2005/04/21 06:16:11 jhedley Exp $
// 2006/12/5 AJB Added remove_* behaviour into the specified element, place the given html
// 2008/10/30 dfinnecy Added mapping to translate ptype to human readable target - in cshedPtype and ptype
// http://fdimages.fairfax.com.au/crtvs/code/countryshed/adSwap.js
// 2009/09/14 AL Added AdTest Module - code by dfinnecy

// into the specified element, place the given html

//***START  This is the Adtest Module that helps test ads via parameters passed via URLs***
function getVal(zMSmyStr,zMStagline)
{
	zMSmyStr = zMSmyStr+"=";
	var zMSind = zMStagline.indexOf(zMSmyStr)+zMSmyStr.length;
	if(zMSind==(zMSmyStr.length-1)) //test for myStr not found
		return "";
	zMStagline += "&";
	return zMStagline.substring(zMSind,zMStagline.indexOf("&",zMSind));
}


//TEST MODULE
//adtest=true&adtestkeyword=k3yw0rd&adtestsize=468x60_300x250
//testMods(target,testKeyword,testSizes);

function testMods(testTarg,testKey,testSize){
	//PROCESS TESTSIZES
	testSizeMatch = 0;
	testSizeList = 0;

	if(testSize){
		testSize = testSize.split("_");
		testSizeList=1;
		}

	//CHECK TESTSIZES AGAINST AAMSZ IN TESTTARG
	for(i=0;i<testSize.length;i++){
		if(testTarg.match("AAMSZ="+testSize[i])) testSizeMatch=1;
		}

	// IF THERE IS NO TESTSIZE OR IF THE SIZE MATCHES, THEN RUN REPLACEMENTS
	if(!testSizeList || testSizeMatch){
		//CHANGE TARGET SITE TO ZZ.ADTEST
		testTarg = testTarg.replace(/SITE=[A-Z\.]+\//gi, "SITE=ZZ.ADTEST/");

		//CHANGE KEYWORD OR INSERT KEYWORD
		if(testTarg.indexOf("/KEYWORD=")!=-1) testTarg = testTarg.replace(/\/KEYWORD=[A-Z\.]+\//gi, "/KEYWORD="+testKey+"/");
		else testTarg = testTarg.replace(/SITE=ZZ.ADTEST\//gi, "SITE=ZZ.ADTEST/KEYWORD="+testKey+"/");
		}

	return(testTarg);
}
//***END This is the Adtest Module that helps test ads via parameters passed via URLs***

function getVal(zMSmyStr,zMStagline)
{
	zMSmyStr = zMSmyStr+"=";
	var zMSind = zMStagline.indexOf(zMSmyStr)+zMSmyStr.length;
	if(zMSind==(zMSmyStr.length-1)) //test for myStr not found
		return "";
	zMStagline += "&";
	return zMStagline.substring(zMSind,zMStagline.indexOf("&",zMSind));
}

function ffx_swap(elName, html) {
if(html.indexOf("ffxcam")!=-1){
framit = html;
if(typeof(pageid)=="undefined")
 pageid = Math.floor(1000000*Math.random());

framit = framit.replace(/&amp;/gi,"&");
framitbackup = framit;
start1 = framit.indexOf(".ng")+4;
end = framit.indexOf("\"",start1);
end2 = framit.indexOf("'",start1);
end3 = framit.indexOf("?",start1);
wid=0;
hei=0;
if(end==-1)
 end=9999;
if(end2==-1)
 end2=9999;
if(end3==-1)
 end3=9999;
if(end2<end && end2<end3)
 end = end2;
else if(end3<end2 && end3<end)
 end = end3;
target1 = framit.substring(start1,end);
drivevariables = target1.split("&");
var cat,subcat = "";
cat = getVal("cat",target1);
subcat = getVal("subcat",target1);
pos = getVal("loc",target1);
if(pos.length!=0)
  pos = "/POS="+pos.toUpperCase();
subsite="";
if(getVal("site",target1)=="drivebikes")
 subsite="BIKES";
if(cat.length == 0)
 cat = "OTHER";
 
var ptype = "";
var cshedPtype = {
"B":"Bikes","V":"Caravans","GC":"Compressors","GK":"Construction","GD":"Conveyors","GR":"Cultivation","GA":"Engines",
"GZ":"Everything","GF":"Farming","GJ":"Feeding","GX":"Garages","GE":"Engine","GV":"Wanted","GG":"Harvesting","GH":"Hay",
"GI":"Lawn","GL":"Machinery","OANY":"Marine","O":"Marine","GW":"Pumps","GM":"Silos","GN":"Slashers","GO":"Spraying","GQ":"Storage",
"GS":"Tools","GU":"TractorParts","GT":"Tractors","GB":"Trailers","T":"Trucks"
};

ptype = getVal("ptype",target1);
ptype = ((cshedPtype[ptype])?cshedPtype[ptype]:"NONE").toUpperCase();
for (i in drivevariables){
		if (drivevariables[i].match("ptype")){
			drivevariables[i]="";
	}	}

if(cat == "NEWSREVIEWS"||cat == "ARTICLEDETAIL")
{
 temp = subcat;
 subcat = cat;
 cat = temp;
}
subsite="";
if(getVal("site",target1)=="drivebikes")
 subsite="BIKES";
if(subcat.length != 0)
 subcat = "."+subcat;
usertype = getVal("usertype",target1);
cat = getVal("cat",target1);
if(usertype.length!=0)
 usertype = "."+usertype;
if(cat.length==0&&usertype.length==0)
 cat = "OTHER";
target = "SITE=ONL.CL.COUNTRYSHED.CLASSIFIEDS/AREA=CLASSIFIEDS.COUNTRYSHED."+cat.toUpperCase().replace(/-|\s|%/gi,"")+subcat.toUpperCase().replace(/-|\s|%/gi,"")+pos+"/pageid="+pageid+"/"+"PTYPE="+ptype+"/";
for(i in drivevariables)
{
if(i!=""&&i.length<30&&drivevariables[i].toString().length<30&&i.indexOf(" ")==-1){
	if(drivevariables[i].toString().indexOf("=")!=drivevariables[i].toString().length-1&&drivevariables[i].toString().indexOf("site=")==-1&&drivevariables[i].toString().indexOf("area=")==-1)
	    target+=unescape(drivevariables[i].toString()).toUpperCase().replace(/\+/gi,"_").replace(/\&|\-|\//gi,"").replace(/areaname=/i,"LOCAREA=").replace(/section=/i,"SECT=").replace(/min=/i,"MINIMUM=").replace(/max=/i,"MAXIMUM=")+"/";
}
}

change=target.match(/ADSPACE=([^\/]*)/);
if(!/\d+x\d+/i.test(change[1]))
{
	if(framit.toLowerCase().indexOf("adspace=networkad")!=-1)
	{
	target +="AAMSZ=1X4/";
	}
		else if(framit.toLowerCase().indexOf("adspace=domaintext")!=-1)
		{
		target +="AAMSZ=0X"+getVal("loc",target1)+"/";
		}else if(framit.toLowerCase().indexOf("adspace=domainresizable")!=-1)
		{
		target +="AAMSZ=2X"+getVal("loc",target1)+"/";
		}else if(framit.toLowerCase().indexOf("adspace=iwos")!=-1)
		{
		target +="AAMSZ=3X"+getVal("loc",target1)+"/";
		}else if(framit.toLowerCase().indexOf("adspace=text")!=-1)
		{
		target +="AAMSZ=0X1/";
		}else if(framit.toLowerCase().indexOf("adspace=dhtml")!=-1)
		{
		target +="AAMSZ=1X1/";
		}else if(framit.toLowerCase().indexOf("adspace=resizable")!=-1)
		{
		target +="AAMSZ=638X68/";
	}else if(framit.toLowerCase().indexOf("iframe")!=-1){
		wid=framit.match(/\swidth\s?=["'\s]+([^'"\s]+)["'\s]/i)[1];
		hei=framit.match(/\sheight\s?=["'\s]+([^'"\s]+)["'\s]/i)[1];
		target +="AAMSZ="+wid+"X"+hei+"/";}
}
else
{
if(framit.toLowerCase().indexOf("adspace=140x35")!=-1)
 target = target.replace(/ADSPACE=([^/]*)/gi,"ADSPACE=140X25/AAMSZ=140X25");
else
 target = target.replace(/ADSPACE=([^/]*)/gi,"ADSPACE=$1/AAMSZ=$1");
 }
 target = target.replace(/=MULTILINK/i,"=295X60");
framit = framit.replace(/ffxcam[^/]*\/html\.ng/,"direct.fairfax.com.au/hserver");
framit = framit.replace(/ffxcam[^/]*\/js\.ng/,"direct.fairfax.com.au/jserver");
if(framit.indexOf("http://ffxcam")!=-1&&wid=="468"&&hei=="60")
{
	framit = framit.substring(0,framit.indexOf("http://direct.fairfax.com.au"))+"http://www.drive.com.au/ffxd/intermediatoryV2.html?ad="+framit.substring(framit.indexOf("http://ffxcam"),framit.length);
}

//***START  This is the Adtest Module that helps test ads via parameters passed via URLs***
//TEST MODULE - ADDS A PASSED KEYWORD VALUE AND CHANGES SITE VALUE WHEN KEYVALUES ARE PASSED IN THE URL
//adtest=true&adtestkeyword=k3yw0rd&adtestsize=468x60_300x250

target = target.toUpperCase();
var urlStr = window.location.search.toUpperCase();
if(urlStr.indexOf("ADTEST=TRUE")!=-1 && urlStr.indexOf("ADTESTKEYWORD=")!=-1){
	testKeyword = getVal("ADTESTKEYWORD",urlStr);
	testSizes = getVal("ADTESTSIZE", urlStr);
	target = testMods(target,testKeyword,testSizes);
	}
//***END  This is the Adtest Module that helps test ads via parameters passed via URLs***

html = framit.substring(0,framit.indexOf("direct.fairfax.com.au")+30)+target+framitbackup.substring(end,framitbackup.length).replace(/\?ord=([0-9]*)/gi,"acc_random=$1");
}
    var d = document;
    if (!d.getElementById) {
        return;
    }
    var el = d.getElementById(elName);
    if (!el) {
        return;
    }
    var remove = false;
    if (typeof ffxAdExclusionList != "undefined") {
      var thisPage = window.location.pathname;
      for (var i=0; i<ffxAdExclusionList.length; i++) {
        var current = ffxAdExclusionList[i];
        if (current == 'remove_all_ads'){
          return;
        }
        else if (current == 'remove_article_ads'){
          if (thisPage.match(/\/news\/[a-zA-Z0-9-]+\/[a-zA-Z0-9-]+\/[0-9]{4}\/[0-9]{2}\/[0-9]{2}\/[0-9]+.html/) || thisPage.match(/\/articles\/.*?/)){
            remove = true;
          }
        }
        else if (current == 'remove_index_ads'){
          if  (thisPage.match(/\/index.html$/) || !thisPage.match(/.html$/)){
            remove = true;
          }
        }
        else if (current == thisPage) {
          return;
        }
      }
    }
    if (remove == true) {return;}
    el.innerHTML = html;
}

// shuffles a list of html snippets into an element
function ffx_shuffle_swap(elName) { // and a list of html snippets
    // get the array of html from the arguments (not a real array)
    var snippets = new Array();
    for (var i = 1; i < arguments.length; i++) { snippets[i-1] = arguments[i]; } // skip the first el
    ffx_shuffle(snippets);
    var html = snippets.join("<br />"); // maybe should pass in?
    ffx_swap(elName, html);
}



// inline Fisher Yates shuffles the given array of objects
function ffx_shuffle(list) {
    for (var i = list.length -1; i >= 0; --i) {
        var j =  Math.floor(Math.random() * (i + 1));
        if (i == j) continue;
        var temp = list[i];
        list[i] = list[j];
        list[j] = temp;
    }
}



