//Modif 07/10/07 Add DART
//Modif 11/13/06 Add QA
//MNS 12.29.06 version 15 - created encoding function in order to be reused
function encodeHBX(str_Obj){
// MNS 12.27.06 version 14 - The following replaces are (incomplete) restricted character types not allowed by HitBox
	str_Obj = str_Obj.replace(/\'/g, "");
	str_Obj = str_Obj.replace(/&/g, " and ");
	str_Obj = str_Obj.replace(/!/g, "");
	str_Obj = str_Obj.replace(/ /g, "+");
	str_Obj = str_Obj.replace(/Lifetimetv.com:/gi, "");
	return str_Obj;
}

var str_PageName= encodeHBX(location.pathname);
var astr_PageName = str_PageName.split("/");
var str_PagePath = "";

if (astr_PageName.length > 1) {
	str_PageName = astr_PageName[astr_PageName.length-1];
	str_PagePath = astr_PageName.slice(1,astr_PageName.length-1).join('/');

	if (str_PageName == '') {
		str_PageName = 'index.php';
	}
}
else {
	str_PageName = 'index.php';
}

var loc = new String(location);
//configuration
var OAS_url  = 'http://oascentral.lifetimetv.com/RealMedia/ads/';
var DART_url = 'http://ad.doubleclick.net/adj/';
var DART_site = "";
var DART_zone = "";
var DART_page = "";
var DART_size = "";
var DART_tile = 1;
var DART_rn = new String(Math.random()); 
var DART_rns = DART_rn.substring(2,11);

DART_zone = str_PagePath;
DART_page = str_PageName;

if (top!=self) {
	var hostString = new String("http://" + location.hostname);
	var hostlen = hostString.length;
	var filepath = loc.substring(hostlen,loc.length);
} 
else {
	var filepath = new String(parent.window.location.pathname);
}

var OAS_listpos = 'Top1,Right1';

//switch domain for OAS if i come across Lifetimemag.com
var thisDomain = "www.lifetimetv.com";

//make all boards paths = /boards
var onBoards = (location.hostname.indexOf('boards.lifetimetv.com')!=-1);
if (onBoards) { filepath="/boards"; }

if (onBoards)
	{ 
	OAS_listpos = 'Top1';
}

if (loc.contains('.lovespringinternational.com')) { thisDomain="www.lovespringinternational.com"; }

if (
	(loc.indexOf('lifetimemag.com')!=-1) ||
	(loc.indexOf('lifetimetv.com/lmn')!=-1) ||
	(loc.indexOf('lifetimetv.com/lrw')!=-1)
	)
	{ 
	OAS_listpos = 'Top,Right1';
	
	//also...
	if (loc.indexOf('lifetimemag.com')!=-1) {
		thisDomain="www.lifetimemag.com"; 
	}
}

//BASE POSITIONS for these sites: (meaning these positions should always be available)
if (loc.contains('.com/games/')) { OAS_listpos = 'Top1' }
if (loc.contains('.com/games/sudoku/')) { OAS_listpos = 'Top1,Right1' }
//if (loc.contains('.com/sweeps/index.html')) { OAS_listpos = 'Top1' }
//if (loc.contains('.com/movies/index.php')) { OAS_listpos = 'Top1' }
if (loc.contains('.com/shows/cheerleader/')) { OAS_listpos = 'Top1' }
if (loc.contains('.com/extra/nextstop/')) { OAS_listpos = 'Top1,x01,x50' }
if (loc.contains('.com/breastcancer/')) { OAS_listpos = 'Top1' }
//if (filepath=='/index.php') { { OAS_listpos = 'Top1,x50' } }

//HARD CODED LISTPOS - i did it, tfc! :-)
//mns 04/03/2007 - Moving lower in code so that it actually is a "final" override case.  Will still be appended to by other rules, though.
if (typeof OAS_listpos_hard != 'undefined') 
{
	OAS_listpos = OAS_listpos_hard;
}


//topright ad - gets Top, Top Right
var topright_ad = [
	'/games/realarcade/ra_moregames.php',
	'/games/gemmys',
	'/games/games_more.php'
	];

//bottomright ad - gets Top, Bottom Right
var bottomright_ad = [
	//'/games/index.php'
	];
	
//GAMES RULES - km102406 
var gamesright1_ad = [
	'/games/dressup',
	'/games/trivia',
	'/games/card',
	'/games/mahjong',
	'/games/puzzle',
	'/games/lifetime'
	];
	
var gamesx50_ad = [
	'/games/index.php',
	'/games/reviews',
	'/games/dressup',
	'/games/trivia',
	'/games/card',
	'/games/mahjong',
	'/games/puzzle',
	'/games/realarcade',
	'/games/multiplayer',
	'/games/areyougame',
	'/games/lifetime/allltgames.php'
	];

for (u in topright_ad) {
	if (loc.contains(topright_ad[u])) { OAS_listpos += ",TopRight" }
}

for (u in bottomright_ad) {
	if (loc.contains(bottomright_ad[u])) { OAS_listpos += ",BottomRight" }
}

for (u in gamesright1_ad) {
	if (loc.contains(gamesright1_ad[u])) { OAS_listpos += ",Right1" }
}

for (u in gamesx50_ad) {
	if (loc.contains(gamesx50_ad[u])) { OAS_listpos += ",x50" }
}

//games homepage without the index.php
if (loc == domain + '/games/') { OAS_listpos += ",x50" }


//if the last character in the path is '/', convert to '/index.html'
if (filepath.substring(filepath.length-1,filepath.length) == "/") { 
	filepath+="index.html";
}


var OAS_sitepage = thisDomain + filepath;

//if this is a cgi produced thank you page, use the hard-coded
//url embedded at the top of the html page

if (typeof OAS_sitepage_hard != 'undefined') 
{
	OAS_sitepage = OAS_sitepage_hard;
}

// Check if running on qa.lifetime for QA testing and change www to qa.
// This overrides the OAS_sitepage variable.
if (loc.indexOf('qa.lifetime')!=-1)
{
    // alert("OAS_sitepage (befor): " + OAS_sitepage);
    OAS_sitepage = OAS_sitepage.replace(/^www\./, "qa."); 
    // alert("OAS_sitepage (after): " + OAS_sitepage);
}

// For DART 7/11/07 Remove 'Top1', 'x50', and 'Right1'
OAS_listpos = OAS_listpos.replace(/,?Top1/, "");
OAS_listpos = OAS_listpos.replace(/,?x50/, "");
OAS_listpos = OAS_listpos.replace(/,?Right1/, "");
OAS_listpos = OAS_listpos.replace(/^,+|,+$/g, "");       // Get rid of leading and trailing commas

// alert("OAS_listpos (after): " + OAS_listpos);


/*****************************************************************/

var OAS_query = 'QUERY';
var OAS_target = '_top';
var OAS_rn = new String(Math.random()); 
var OAS_rns = OAS_rn.substring(2,11);
var OAS_version = 11;

if (navigator.userAgent.indexOf('Mozilla/3') != -1 || 
	navigator.userAgent.indexOf('Mozilla/4.0 WebTV') != -1) { 
	OAS_version = 10; 
}
if (OAS_version>=11) { 
    // Can't use next line because of some page incompatibilies   
    // if (OAS_version>=11 && OAS_listpos != "") {     // 07/11/07 if OAS_listpos is blank don't call OAS at all

    //  Removed 3/18/09
    //	document.write('<SCR' + 'IPT LANGUAGE=JavaScript1.1 SRC="' + OAS_url + 'adstream_mjx.ads/' + OAS_sitepage + '/1' + OAS_rns + '@' + OAS_listpos + '?' + OAS_query + '"><\/SCRIPT>');
    //  document.write('');
} 

function OAS_NORMAL(pos) {
	var normalTag = '<A HREF="' + OAS_url + 'click_nx.ads/' + OAS_sitepage + '/1' + OAS_rns + '@' + OAS_listpos + '!' + pos + '?' + OAS_query + '"TARGET=' + OAS_target + '>' +
	'<IMG SRC="' + OAS_url + 'adstream_nx.ads/' + OAS_sitepage + '/1' + OAS_rns + '@' + OAS_listpos + '!' + pos + '?' + OAS_query + '"BORDER=0></A>';	
        //  Removed 3/18/09
        // document.write(normalTag);
}

function OAS_AD(pos) {

    // Trap for DART
    if (pos == 'Top1' || pos == 'x50' || pos == 'Right1') {
        DART_AD(pos);
        return;
    }

	if (OAS_version>=11) OAS_RICH(pos);
	else OAS_NORMAL(pos);
}

// New 07/11/07 for DART Serving
function DART_AD(pos) {


    DART_site = DART_site_set(OAS_sitepage);
	DART_zone = DART_zone_set(OAS_sitepage);
	DART_page = DART_page_set(OAS_sitepage);

    DART_size = DART_size_set(pos);
    // if (pos == 'Top1')  return;   // Skip Top1 position

    var DART_url_complete = DART_url + DART_site + DART_zone + ";page=" + DART_page + ";tile=" + DART_tile + ";sz=" + DART_size + ";ord=" + DART_rns + "?";

    // remove following comment for debug info
    if ( loc.search(/testad/) >= 0)  {
		alert('DART_zone: ' + DART_zone);
		alert('DART_page: ' + DART_page);		
        alert('DART_url_complete: ' + DART_url_complete);
    }

	document.write('<SCRIPT LANGUAGE="JavaScript1.1" SRC="' + DART_url_complete + '"><\/SCRIPT\>');

    DART_tile++;        // Increment DART Tile
}

function DART_page_set(OAS_sitepage) {
/*   
    DART_page = "";
    if (loc.contains('game=')) {
        DART_page = loc.replace(/^.*game=(\w*)/, "$1"); 
    } 
*/
    return DART_page;
}

function DART_zone_set(OAS_sitepage) {
    
/*    if (loc.contains('.com/games'))   DART_zone = 'games/'; 
    if (loc.contains('.com/sweeps')) {     // DART_zone = 'sweeps/'; 
        DART_zone = loc.replace(/^.*com\/sweeps\/(.*)\/.*$/, "sweeps/$1");      // Gets additional Game names and puts them in DART_page
    }
    if (loc.contains('.com/photos')) { 
        DART_zone = loc.replace(/^.*com\/photos\/(.*)\/.*$/, "photos/$1");      // Gets additional contest names and puts them in DART_page
	}
	
    if (loc.contains('.com/games/lifetime/'))           { DART_zone += 'lifetime/'; }
    else if (loc.contains('.com/games/realarcade/'))    { DART_zone += 'realarcade/'; }
    else if (loc.contains('.com/games/multiplayer/'))   { DART_zone += 'multiplayer/'; }
    else if (loc.contains('.com/games/reviews/'))       { DART_zone += 'reviews/'; }
    else if (loc.contains('.com/games/cover-girl'))       { DART_zone += 'cover-girl/'; }	
	else if (loc.contains('.com/games/')) {
        DART_page = loc.replace(/^.*com\/games\/(.*)\/.*$/, "$1");      // Gets additional Game names and puts them in DART_page
    }

    if (loc.contains('.com/hcorner'))  DART_zone = 'hcorner/'; 
    if (loc.contains('.com/livinghealthy'))  DART_zone = 'livinghealthy/'; 
    if (loc.contains('.com/breastcancer'))  DART_zone = 'breastcancer/'; 
    if (loc.contains('.com/shows'))  DART_zone = 'shows/'; 
    if (loc.contains('.com/extra/poise'))  DART_zone = 'extra/poise/'; 
*/
    return DART_zone;
}

function DART_site_set(OAS_sitepage) {

    DART_site = "home";
    if (loc.contains('.com/games'))   DART_site = 'fun-games/'; 
    if (loc.contains('.com/sweeps'))  DART_site = 'fun-games/'; 

    if (loc.contains('.com/photos'))  DART_site = 'fun-games/'; 
    if (loc.contains('.com/hcorner'))  DART_site = 'health/'; 
    if (loc.contains('.com/livinghealthy'))  DART_site = 'health/'; 
    if (loc.contains('.com/breastcancer'))  DART_site = 'community/'; 
    if (loc.contains('.com/shows'))  DART_site = 'on-tv/'; 
    if (loc.contains('.com/extra/poise'))  DART_site = 'health/'; 
    if (loc.contains('.com/feedback'))  DART_site = 'about-us/'; 
    
    return "ltv." + DART_site;
}

function DART_size_set(pos) {

    if (pos == 'Right1') {
        DART_size = "160x600";
    }
    if (pos == 'x50') {
        DART_size = "300x250";
    }
    if (pos == 'Top1') {
        DART_size = "728x90";
    }

    return DART_size;
}

function DisplayAds(position,width,height) {


    // Direct to DART
    if (position == 'Middle1') {
        DART_AD('x50');
        return;
    }

	var OASpage = OAS_sitepage + '/1' + OAS_rns + '@' + position;
	
        //  Removed 3/18/09
        /*
	if (OAS_version<11) {
		document.write('<A HREF="' + OAS_url + 'click_nx.ads/' + OASpage + '" TARGET="_top"><IMG SRC="' + OAS_url + 'adstream_nx.ads' + OASpage + '" BORDER="0" WIDTH="' + width + '" HEIGHT="' + height + '"><\/a>');
	}
	else {
		document.write('<SCRIPT LANGUAGE="JavaScript1.1" SRC="' + OAS_url + 'adstream_jx.ads/' + OASpage + '"><\/SCRIPT\>');
	}
	*/
}

/*****************************************************************/

function drawBanner(width,height,tilenum,layernum) {
	DisplayAds('Bottom!Bottom',468,60);
}

function drawHREFAd(width,height,tilenum) { }

function drawAdLayer(width,height,tile,layernum,addX,addY) { }

/*******************************************************************/
/** This function is used to updated companion ads upon a flash
/** event. The flash application would call this function by passing 
/** the companion position to update and the html to use.
/**
/********************************************************************/
// <![CDATA[
function draw_oas_HTML(position,positionHtml) {
        var src="";
        var adPosition="";

        //Flash=Html OAS position mapping:
        //Flash has to call different positions b/c it needs the ad asset without the accomponying html
        //The positions in the page must be surrounded by specified div tags so the function knows
        //where to update the ad. The position_map array represents the div tag names by flash position.
        //x90=Top1
        //x91=x15
        //x95=Right1
        //x96=x50
        var position_map = new Array();

        position_map["x90"] = "companionad_Top1";
        position_map["x92"] = "companionad_Top2";
        position_map["x91"] = "companionad_x15";
        position_map["x94"] = "companionad_x19";
        position_map["x95"] = "companionad_Right1";
        position_map["x96"] = "companionad";

        src=positionHtml;
//alert("draw_oas_HTML: escaped:"+src);
//alert("draw_oas_HTML: ad position:"+position);
        clean_src=unescape(src);
//alert("draw_oas_HTML: positionHTML:"+clean_src);

        adPosition=position_map[position];
//alert("draw_oas_HTML: div tag:"+adPosition);

        document.getElementById(adPosition).innerHTML='';
        document.getElementById(adPosition).innerHTML=clean_src;
        return false;
}

// ]]>
