var OAS_app = "magazine"; //not used !
var OAS_domain = "http://oascentral.lavalife.com/RealMedia/ads/adstream_jx.ads/";
var OAS_site = null;
var OAS_page = null;
var OAS_rn = new String(Math.random());
var OAS_rns = OAS_rn.substring (2, 11);
var OAS_keys = "";

var adPageRef = null;
var adPage = null;
var needSite = true;
var needPage = true;
var needKeys = true;

//En-LavalifeMagazine-Dev

/* Sites available */
var adSites = [
{site:"En-LavalifeMagazine",locales:"*",products:"*",brands:"*",pageRef:"adPages"}
];

/* Pages available. Order matters:first url match is used; leave "home" at the end - this way we cover the root url */
var adPages = [

{page:"linkLove",urls:"partnerLayout,linkLove",
 tag300x250_2:"@Position1!Position1"},

{page:"archive",urls:"dating/archive", cmty:"d",
 tag120x90:"@Right,Right1,Right2,Right3,Top,TopRight!TopRight",
 tag728x90:"@Right,Right1,Right2,Right3,Top,TopRight!Top",
 tag160x600_R:"@Right,Right1,Right2,Right3,Top,TopRight!Right",
 tag160x600_2R:"@Right,Right1,Right2,Right3,Top,TopRight!Right1",
 tag120x90_google:"@Left!Left",
 tag160x600_L:"@Right,Right1,Right2,Right3,Top,TopRight!Right2",
 tag160x600_2L:"@Right,Right1,Right2,Right3,Top,TopRight!Right3"},

{page:"feature",urls:"dating", cmty:"d",
 tag120x90:"@Right,Right1,Right2,Right3,Top,TopRight!TopRight",
 tag728x90:"@Right,Right1,Right2,Right3,Top,TopRight!Top",
 tag160x600_R:"@Right,Right1,Right2,Right3,Top,TopRight!Right",
 tag160x600_2R:"@Right,Right1,Right2,Right3,Top,TopRight!Right1",
 tag120x90_google:"@Left!Left",
 tag160x600_L:"@Right,Right1,Right2,Right3,Top,TopRight!Right2",
 tag160x600_2L:"@Right,Right1,Right2,Right3,Top,TopRight!Right3"},

{page:"archive",urls:"relationship/archive", cmty:"r",
 tag120x90:"@Right,Right1,Right2,Right3,Top,TopRight!TopRight",
 tag728x90:"@Right,Right1,Right2,Right3,Top,TopRight!Top",
 tag160x600_R:"@Right,Right1,Right2,Right3,Top,TopRight!Right",
 tag160x600_2R:"@Right,Right1,Right2,Right3,Top,TopRight!Right1",
 tag120x90_google:"@Left!Left",
 tag160x600_L:"@Right,Right1,Right2,Right3,Top,TopRight!Right2",
 tag160x600_2L:"@Right,Right1,Right2,Right3,Top,TopRight!Right3"},

{page:"feature",urls:"relationship", cmty:"r",
 tag120x90:"@Right,Right1,Right2,Right3,Top,TopRight!TopRight",
 tag728x90:"@Right,Right1,Right2,Right3,Top,TopRight!Top",
 tag160x600_R:"@Right,Right1,Right2,Right3,Top,TopRight!Right",
 tag160x600_2R:"@Right,Right1,Right2,Right3,Top,TopRight!Right1",
 tag120x90_google:"@Left!Left",
 tag160x600_L:"@Right,Right1,Right2,Right3,Top,TopRight!Right2",
 tag160x600_2L:"@Right,Right1,Right2,Right3,Top,TopRight!Right3"},

{page:"archive",urls:"intimate/archive", cmty:"i",
 tag120x90:"@Right,Right1,Right2,Right3,Top,TopRight!TopRight",
 tag728x90:"@Right,Right1,Right2,Right3,Top,TopRight!Top",
 tag160x600_R:"@Right,Right1,Right2,Right3,Top,TopRight!Right",
 tag160x600_2R:"@Right,Right1,Right2,Right3,Top,TopRight!Right1",
 tag120x90_google:"@Left!Left",
 tag160x600_L:"@Right,Right1,Right2,Right3,Top,TopRight!Right2",
 tag160x600_2L:"@Right,Right1,Right2,Right3,Top,TopRight!Right3"},

{page:"feature",urls:"intimate", cmty:"i",
 tag120x90:"@Right,Right1,Right2,Right3,Top,TopRight!TopRight",
 tag728x90:"@Right,Right1,Right2,Right3,Top,TopRight!Top",
 tag160x600_R:"@Right,Right1,Right2,Right3,Top,TopRight!Right",
 tag160x600_2R:"@Right,Right1,Right2,Right3,Top,TopRight!Right1",
 tag120x90_google:"@Left!Left",
 tag160x600_L:"@Right,Right1,Right2,Right3,Top,TopRight!Right2",
 tag160x600_2L:"@Right,Right1,Right2,Right3,Top,TopRight!Right3"},

{page:"archive",urls:"sex-advice/archive", cmty:"i",
 tag120x90:"@Right,Right1,Right2,Right3,Top,TopRight!TopRight",
 tag728x90:"@Right,Right1,Right2,Right3,Top,TopRight!Top",
 tag160x600_R:"@Right,Right1,Right2,Right3,Top,TopRight!Right",
 tag160x600_2R:"@Right,Right1,Right2,Right3,Top,TopRight!Right1",
 tag120x90_google:"@Left!Left",
 tag160x600_L:"@Right,Right1,Right2,Right3,Top,TopRight!Right2",
 tag160x600_2L:"@Right,Right1,Right2,Right3,Top,TopRight!Right3"},

{page:"sexadvice",urls:"sex-advice", cmty:"i",
 tag120x90:"@Right,Right1,Right2,Right3,Top,TopRight!TopRight",
 tag728x90:"@Right,Right1,Right2,Right3,Top,TopRight!Top",
 tag160x600_R:"@Right,Right1,Right2,Right3,Top,TopRight!Right",
 tag160x600_2R:"@Right,Right1,Right2,Right3,Top,TopRight!Right1",
 tag120x90_google:"@Left!Left",
 tag160x600_L:"@Right,Right1,Right2,Right3,Top,TopRight!Right2",
 tag160x600_2L:"@Right,Right1,Right2,Right3,Top,TopRight!Right3"},

{page:"archive",urls:"fetish/archive", cmty:"i",
 tag120x90:"@Right,Right1,Right2,Right3,Top,TopRight!TopRight",
 tag728x90:"@Right,Right1,Right2,Right3,Top,TopRight!Top",
 tag160x600_R:"@Right,Right1,Right2,Right3,Top,TopRight!Right",
 tag160x600_2R:"@Right,Right1,Right2,Right3,Top,TopRight!Right1",
 tag120x90_google:"@Left!Left",
 tag160x600_L:"@Right,Right1,Right2,Right3,Top,TopRight!Right2",
 tag160x600_2L:"@Right,Right1,Right2,Right3,Top,TopRight!Right3"},

{page:"fetish",urls:"fetish", cmty:"i",
 tag120x90:"@Position1,Position2,Right2,Right3,Top,TopRight!TopRight",
 tag728x90:"@Position1,Position2,Right2,Right3,Top,TopRight!Top",
 tag300x250:"@Position1,Position2,Right2,Right3,Top,TopRight!Position1",
 tag300x250_2:"@Position1,Position2,Right2,Right3,Top,TopRight!Position2",
 tag120x90_google:"@Left!Left",
 tag160x600_L:"@Position1,Position2,Right2,Right3,Top,TopRight!Right2",
 tag160x600_2L:"@Position1,Position2,Right2,Right3,Top,TopRight!Right3"},

{page:"archive",urls:"he-said-she-said/archive",
 tag120x90:"@Right,Right1,Right2,Right3,Top,TopRight!TopRight",
 tag728x90:"@Right,Right1,Right2,Right3,Top,TopRight!Top",
 tag160x600_R:"@Right,Right1,Right2,Right3,Top,TopRight!Right",
 tag160x600_2R:"@Right,Right1,Right2,Right3,Top,TopRight!Right1",
 tag120x90_google:"@Left!Left",
 tag160x600_L:"@Right,Right1,Right2,Right3,Top,TopRight!Right2",
 tag160x600_2L:"@Right,Right1,Right2,Right3,Top,TopRight!Right3"},

{page:"hesaysshesays",urls:"he-said-she-said", 
 tag115x45:"@Middle1!Middle1",
 tag120x90:"@Position1,Position2,Right2,Right3,Top,TopRight!TopRight",
 tag728x90:"@Position1,Position2,Right2,Right3,Top,TopRight!Top",
 tag300x250:"@Position1,Position2,Right2,Right3,Top,TopRight!Position1",
 tag300x250_2:"@Position1,Position2,Right2,Right3,Top,TopRight!Position2",
 tag120x90_google:"@Left!Left",
 tag160x600_L:"@Position1,Position2,Right2,Right3,Top,TopRight!Right2",
 tag160x600_2L:"@Position1,Position2,Right2,Right3,Top,TopRight!Right3"},

{page:"archive",urls:"film-wine-pairings/archive",
 tag120x90:"@Right,Right1,Right2,Right3,Top,TopRight!TopRight",
 tag728x90:"@Right,Right1,Right2,Right3,Top,TopRight!Top",
 tag160x600_R:"@Right,Right1,Right2,Right3,Top,TopRight!Right",
 tag160x600_2R:"@Right,Right1,Right2,Right3,Top,TopRight!Right1",
 tag120x90_google:"@Left!Left",
 tag160x600_L:"@Right,Right1,Right2,Right3,Top,TopRight!Right2",
 tag160x600_2L:"@Right,Right1,Right2,Right3,Top,TopRight!Right3"},

{page:"filmsommelier",urls:"film-wine-pairings",
 tag120x90:"@Position1,Position2,Right2,Right3,Top,TopRight!TopRight",
 tag728x90:"@Position1,Position2,Right2,Right3,Top,TopRight!Top",
 tag300x250:"@Position1,Position2,Right2,Right3,Top,TopRight!Position1",
 tag300x250_2:"@Position1,Position2,Right2,Right3,Top,TopRight!Position2",
 tag120x90_google:"@Left!Left",
 tag160x600_L:"@Position1,Position2,Right2,Right3,Top,TopRight!Right2",
 tag160x600_2L:"@Position1,Position2,Right2,Right3,Top,TopRight!Right3"},

{page:"archive",urls:"profile-doctor/archive",
 tag120x90:"@Right,Right1,Right2,Right3,Top,TopRight!TopRight",
 tag728x90:"@Right,Right1,Right2,Right3,Top,TopRight!Top",
 tag160x600_R:"@Right,Right1,Right2,Right3,Top,TopRight!Right",
 tag160x600_2R:"@Right,Right1,Right2,Right3,Top,TopRight!Right1",
 tag120x90_google:"@Left!Left",
 tag160x600_L:"@Right,Right1,Right2,Right3,Top,TopRight!Right2",
 tag160x600_2L:"@Right,Right1,Right2,Right3,Top,TopRight!Right3"},

{page:"profiledoctor",urls:"profile-doctor",
 tag120x90:"@Right,Right1,Right2,Right3,Top,TopRight!TopRight",
 tag728x90:"@Right,Right1,Right2,Right3,Top,TopRight!Top",
 tag160x600_R:"@Right,Right1,Right2,Right3,Top,TopRight!Right",
 tag160x600_2R:"@Right,Right1,Right2,Right3,Top,TopRight!Right1",
 tag120x90_google:"@Left!Left",
 tag160x600_L:"@Right,Right1,Right2,Right3,Top,TopRight!Right2",
 tag160x600_2L:"@Right,Right1,Right2,Right3,Top,TopRight!Right3"},

/* NOT USED - check the urls when putting them back
{page:"dinnerandmovie",urls:"dinnerandmovie",
 tag120x90:"@Position1,Position2,Right2,Right3,Top,TopRight!TopRight",
 tag728x90:"@Position1,Position2,Right2,Right3,Top,TopRight!Top",
 tag300x250:"@Position1,Position2,Right2,Right3,Top,TopRight!Position1",
 tag300x250_2:"@Position1,Position2,Right2,Right3,Top,TopRight!Position2",
 tag120x90_google:"@Left!Left",
 tag160x600_L:"@Position1,Position2,Right2,Right3,Top,TopRight!Right2",
 tag160x600_2L:"@Position1,Position2,Right2,Right3,Top,TopRight!Right3"},

{page:"whatyoursays",urls:"whatyoursays",
 tag120x90:"@Right,Right1,Right2,Right3,Top,TopRight!TopRight",
 tag728x90:"@Right,Right1,Right2,Right3,Top,TopRight!Top",
 tag160x600_R:"@Right,Right1,Right2,Right3,Top,TopRight!Right",
 tag160x600_2R:"@Right,Right1,Right2,Right3,Top,TopRight!Right1",
 tag120x90_google:"@Left!Left",
 tag160x600_L:"@Right,Right1,Right2,Right3,Top,TopRight!Right2",
 tag160x600_2L:"@Right,Right1,Right2,Right3,Top,TopRight!Right3"},
*/

{page:"poll",urls:"poll", 
 tag115x45:"@Middle1!Middle1",
 tag120x90:"@Position1,Position2,Right2,Right3,Top,TopRight!TopRight",
 tag728x90:"@Position1,Position2,Right2,Right3,Top,TopRight!Top",
 tag300x250:"@Position1,Position2,Right2,Right3,Top,TopRight!Position1",
 tag300x250_2:"@Position1,Position2,Right2,Right3,Top,TopRight!Position2",
 tag120x90_google:"@Left!Left",
 tag160x600_L:"@Position1,Position2,Right2,Right3,Top,TopRight!Right2",
 tag160x600_2L:"@Position1,Position2,Right2,Right3,Top,TopRight!Right3"},

{page:"horoscope",urls:"horoscopes",
 tag120x90:"@Right,Right1,Right2,Right3,Top,TopRight!TopRight",
 tag728x90:"@Right,Right1,Right2,Right3,Top,TopRight!Top",
 tag160x600_R:"@Right,Right1,Right2,Right3,Top,TopRight!Right",
 tag160x600_2R:"@Right,Right1,Right2,Right3,Top,TopRight!Right1",
 tag120x90_google:"@Left!Left",
 tag160x600_L:"@Right,Right1,Right2,Right3,Top,TopRight!Right2",
 tag160x600_2L:"@Right,Right1,Right2,Right3,Top,TopRight!Right3"},

/*
{page:"editorletter",urls:"editorletter",
 tag120x90:"@Right2,Right3,Top,TopRight!TopRight",
 tag728x90:"@Right2,Right3,Top,TopRight!Top",
 tag120x90_google:"@Left!Left",
 tag160x600_L:"@Right2,Right3,Top,TopRight!Right2",
 tag160x600_2L:"@Right2,Right3,Top,TopRight!Right3"},
*/

{page:"editorblog",urls:"editors-blog",
 tag120x90:"@Position1,Position2,Right2,Right3,Top,TopRight!TopRight",
 tag728x90:"@Position1,Position2,Right2,Right3,Top,TopRight!Top",
 tag300x250:"@Position1,Position2,Right2,Right3,Top,TopRight!Position1",
 tag300x250_2:"@Position1,Position2,Right2,Right3,Top,TopRight!Position2",
 tag120x90_google:"@Left!Left",
 tag160x600_R:"@Right,Right1,Right2,Right3,Top,TopRight!Right",
 tag160x600_2R:"@Right,Right1,Right2,Right3,Top,TopRight!Right1",
 tag160x600_L:"@Position1,Position2,Right2,Right3,Top,TopRight!Right2",
 tag160x600_2L:"@Position1,Position2,Right2,Right3,Top,TopRight!Right3"},

{page:"sponsoredarticle1",urls:"sponsoredarticle1",
 tag120x90:"@Right,Right1,Right2,Right3,Top,TopRight!TopRight",
 tag728x90:"@Right,Right1,Right2,Right3,Top,TopRight!Top",
 tag160x600_R:"@Right,Right1,Right2,Right3,Top,TopRight!Right",
 tag160x600_2R:"@Right,Right1,Right2,Right3,Top,TopRight!Right1",
 tag120x90_google:"@Left!Left",
 tag160x600_L:"@Right,Right1,Right2,Right3,Top,TopRight!Right2",
 tag160x600_2L:"@Right,Right1,Right2,Right3,Top,TopRight!Right3"},

{page:"sponsoredarticle2",urls:"sponsoredarticle2",
 tag120x90:"@Right,Right1,Right2,Right3,Top,TopRight!TopRight",
 tag728x90:"@Right,Right1,Right2,Right3,Top,TopRight!Top",
 tag160x600_R:"@Right,Right1,Right2,Right3,Top,TopRight!Right",
 tag160x600_2R:"@Right,Right1,Right2,Right3,Top,TopRight!Right1",
 tag120x90_google:"@Left!Left",
 tag160x600_L:"@Right,Right1,Right2,Right3,Top,TopRight!Right2",
 tag160x600_2L:"@Right,Right1,Right2,Right3,Top,TopRight!Right3"},

{page:"sponsoredarticle3",urls:"sponsoredarticle3",
 tag120x90:"@Right,Right1,Right2,Right3,Top,TopRight!TopRight",
 tag728x90:"@Right,Right1,Right2,Right3,Top,TopRight!Top",
 tag160x600_R:"@Right,Right1,Right2,Right3,Top,TopRight!Right",
 tag160x600_2R:"@Right,Right1,Right2,Right3,Top,TopRight!Right1",
 tag120x90_google:"@Left!Left",
 tag160x600_L:"@Right,Right1,Right2,Right3,Top,TopRight!Right2",
 tag160x600_2L:"@Right,Right1,Right2,Right3,Top,TopRight!Right3"},

{page:"sponsoredarticle4",urls:"sponsoredarticle4",
 tag120x90:"@Right,Right1,Right2,Right3,Top,TopRight!TopRight",
 tag728x90:"@Right,Right1,Right2,Right3,Top,TopRight!Top",
 tag160x600_R:"@Right,Right1,Right2,Right3,Top,TopRight!Right",
 tag160x600_2R:"@Right,Right1,Right2,Right3,Top,TopRight!Right1",
 tag120x90_google:"@Left!Left",
 tag160x600_L:"@Right,Right1,Right2,Right3,Top,TopRight!Right2",
 tag160x600_2L:"@Right,Right1,Right2,Right3,Top,TopRight!Right3"},

{page:"sponsoredarticle5",urls:"sponsoredarticle5",
 tag120x90:"@Right,Right1,Right2,Right3,Top,TopRight!TopRight",
 tag728x90:"@Right,Right1,Right2,Right3,Top,TopRight!Top",
 tag160x600_R:"@Right,Right1,Right2,Right3,Top,TopRight!Right",
 tag160x600_2R:"@Right,Right1,Right2,Right3,Top,TopRight!Right1",
 tag120x90_google:"@Left!Left",
 tag160x600_L:"@Right,Right1,Right2,Right3,Top,TopRight!Right2",
 tag160x600_2L:"@Right,Right1,Right2,Right3,Top,TopRight!Right3"},

{page:"home",urls:"*",
 tag120x90:"@Position1,Position2,Right2,Right3,Top,TopRight!TopRight",
 tag728x90:"@Position1,Position2,Right2,Right3,Top,TopRight!Top",
 tag300x250:"@Position1,Position2,Right2,Right3,Top,TopRight!Position1",
 tag300x250_2:"@Position1,Position2,Right2,Right3,Top,TopRight!Position2",
 tag120x90_google:"@Left!Left",
 tag120x90_poll:"@x01!x01",
 tag160x600_R:"@Right,Right1,Right2,Right3,Top,TopRight!Right",
 tag160x600_2R:"@Right,Right1,Right2,Right3,Top,TopRight!Right1",
 tag160x600_L:"@Position1,Position2,Right2,Right3,Top,TopRight!Right2",
 tag160x600_2L:"@Position1,Position2,Right2,Right3,Top,TopRight!Right3"}
];

/*
* Find a 'site' based on the locale, product and brand; 
* The site identifies the ad server domain OAS_site and the array that defines site pages
*/
function setAdSite() {
   needSite = false;
   if (!this.adLoc) adLoc = "en"; // locale - default "en"
   if (!this.adProd) adProd = "a" // product - default "Lavalife"
   if (!this.adBrand) adBrand = "rr";
   for (var i = 0; i < adSites.length; i++) {
      var s = adSites[i];
      if ( matchArrayVal(s.locales, adLoc) && 
          matchArrayVal(s.products, adProd) && 
          matchArrayVal(s.brands, adBrand)) {
        var pr = eval("this."+s.pageRef);
        if (pr) {
   	     OAS_site = s.site;
           adPageRef = pr;
           break;
	  }
      }
   }
}
/*
* Find a particular ad page for the list available for a give site.
*/
function setAdPage() {
   needPage = false;
   // did we find a list of pages?
   if (!adPageRef) return;
   var lhref = location.href;
   // Do we enforce a given page (i.e. 'adTemplate' present)? 
   // If not, check the urls
   for (var i = 0; i < adPageRef.length; i++) {
      var a = adPageRef[i];
      var urls = a.urls;
      if ((this.adTemplate && (matchArrayVal(urls, adTemplate))) || (checkArrayVal(urls, lhref))) {
         OAS_page = a.page;
         adPage = a;
         if (!this.adCmty && a.cmty) adCmty = a.cmty;
         break;
      }
   }
}

/*
* Build the script that connects to the ad server and download the ads.
*/
function getAd(position) {
  if (needSite) setAdSite();
  if (!OAS_site) return;
  if (needPage) setAdPage();
  if (!adPage) return;
  if (needKeys) buildKeys();
  var OAS_pos = adPage[position];
  if (!OAS_pos) return;
  var OAS_path = OAS_domain+OAS_site +((OAS_page)?('/'+OAS_page):'')+'/1'+OAS_rns+OAS_pos+OAS_keys;
  document.write('<scr'+'ipt LANGUAGE="JavaScript1.1" SRC="'+OAS_path+'"></scr'+'ipt>');
}

function buildKeys() {
  needKeys = false;
  var k = "";
  k += "?prod=" + (this.adProd?adProd:"");
  k += "&cmty=" + (this.adCmty?adCmty:"");
  k += "&brand=" + (this.adBrand?adBrand:"");
  k += "&locale=" + (this.adLoc?adLoc:"");
  k += "&ut=" + ((this.adUt == "G" || this.adUt == "F" || this.adUt == "P")?adUt:"");
  k += "&pt=" + ((this.adPt == "C" || this.adPt == "S" || this.adPt == "O")?adPt:"");
  k += "&se=" + ((this.adSe == "M" || this.adSe == "F")?adSe:"");
  k += "&ag=" + ((this.adAg && this.adAg.length > 0 && this.adAg != "0")?adAg:"");
  k += "&sm=" + (this.adSm?adSm:"");
  k += "&dr=" + (this.adDr?adDr:"");
  k += "&ic=" + (this.adIc?adIc:"");
  k += "&in=" + (this.adIn?adIn:"");
  k += "&sp=" + (this.adSp?adSp:"");
  k += "&co=" + (this.adCo?adCo:"");
  k += "&st=" + (this.adSt?adSt:"");
  k += "&ct=" + (this.adCt?adCt:"");
  k += "&zp=" + (this.adZp?adZp:"");
  k += "&ety=" + (this.adEty?adEty:"");
  k += "&bty=" + (this.adBty?adBty:"");
  k += "&oa=" + (this.adOa?adOa:"");
  k += "&spp=" + (this.adSpp?adSpp:"");
  k += "&spw=" + (this.adSpw?adSpw:"");
  
  //get article id from url
  var aurl = location.href;
  var i = aurl.indexOf("/p1");
  if (i < 0) i = aurl.indexOf("/p2");
  if (i >= 0 ) {
    aurl = aurl.substring(0,i);
    k += "&articleId=" + aurl.substring(aurl.lastIndexOf("/")+1);
  }

  OAS_keys = k;
}

function matchArrayVal(ar, val) {
  if (!ar || !val) return false;
  if (ar == "*") return true;
  var ars = ar.split(",");
  for (var i = 0; i < ars.length; i++) {
	if (ars[i] == val) return true;
  }
  return false;
}

function checkArrayVal(ar, val) {
  if (!ar || !val) return false;
  if (ar == "*") return true;
  var ars = ar.split(",");
  for (var i = 0; i < ars.length; i++) {
	if (val.indexOf(ars[i])>-1) return true;
  }
  return false;
}

adScriptLoaded = true;
