adOpsLocalFlag = (typeof adOpsLocalFlag == 'undefined')?false:adOpsLocalFlag;

function tiffDateToString(date) {
var yyyy = date.getYear();
var mm = date.getMonth() + 1;
var dd = date.getDate();
var hour = date.getHours();
var min = date.getMinutes();

if (mm < 10) mm = "0"+mm;
if (dd < 10) dd = "0"+dd;
if (hour < 10) hour = "0"+hour;
if (min < 10) min = "0"+min;
return ''+mm+dd+hour+min;
}

function estOffset(dateObj)
{
	var mo = dateObj.getMonth()+1;
	if (mo < 2 || mo > 10) return 300;
	if (mo > 2 && mo < 10) return 240;
	var last_sunday_index =   ( ( dateObj.getDate() - dateObj.getDay() - 1 ) / 7) + 1;
	if(mo==2)
	{
	  return ( last_sunday_index > 2 || (last_sunday_index == 2 && dateObj.getHours() >= 2))?240:300
	}
	else
	{
	 return ( last_sunday_index > 1 || (last_sunday_index == 1 && dateObj.getHours() >= 2))?300:240;
	}
}

if(typeof estNow == 'undefined' || typeof estNowWithYear == 'undefined')
{
	var estNow = new Date();
	var estNowInMillis = estNow.getTime();
	var millisFromEST = (estNow.getTimezoneOffset() - estOffset(estNow)) * 60000;
	var estNow = new Date( estNowInMillis + millisFromEST )
	var estNowWithYear = estNow.getYear();
	var estNowWithYear = (estNowWithYear < 1900 )?estNowWithYear + 1900:estNowWithYear;
	estNowWithYear = estNowWithYear.toString() + dateToString(estNow).toString() ;
}



var doTiffany = false;
var tiffWidth = '300';
var tiffHeight = '45';
var tiffOnTheFly = '';
var ord=Math.floor(Math.random()*10000000000);
var tiffDelivery = 'adi';
var broadNode = new Array('politics','business','opinion');
var classifieds = new Array('weather','cars','cityguide','jobs','cars','shopping');
var jrsArray = new Array('jobs','rentals','shopping');


function tiffContCategories(_commercialNode,_targCat)
{
	if(_commercialNode.charAt(_commercialNode.length-1) != '/')
	{
		_commercialNode += '/';
	}
	tiffCatArray = [];
	//tiffCatArray['metro'] = ['metro'];
	//tiffCatArray['sports'] = ['sports'];
	tiffCatArray['health'] = ['health','education'];
	tiffCatArray['classifieds'] = ['jobs','cars','realestate','rentals'];
	tiffCatArray['entertainment'] = ['artsandliving/movies','artsandliving/television','artsandliving/entertainmentnews','artsandliving/dvdhomevideo'];
	tiffCatArray['artsandliving'] = ['artsandliving/adviceandrelationships','artsandliving/books','artsandliving/comics','artsandliving/crosswords','artsandliving/fashionandbeauty','artsandliving/foodanddining','artsandliving/homeandgarden','artsandliving/museums','artsandliving/music','artsandliving/pets','artsandliving/style','artsandliving/theater','artsandliving/horoscopes','travel'];
	tiffCatArray['execnews'] = ['opinion','nation','politics','world','print','liveonline','technology'];
	tiffCatArray['local'] = ['cityguide','weather','cars','shopping','shoppingNEW','shoppingOLD','jobs'];

	var tiffCatLength = tiffCatArray[_targCat.toLowerCase()].length;
	if(!tiffCatArray[_targCat])
	{
		return false;
	}
	for(var a=0;a<tiffCatLength;a++)
	{
		var re = RegExp('^'+tiffCatArray[_targCat][a]+'/','gi');
		if (_commercialNode.match(re))
		{
			return true;
		}
	}
	return false;
}

//super pages 
function superLabel(){
	var str ='<style type="text\/css">';
str+='#hp #wp-nav-box { background:#1D406B url(http:\/\/www.washingtonpost.com\/wp-srv\/images\/homepage\/hp-superpages-gradient.gif) no-repeat scroll 0 0;height:30px;width:970px }';
str+='#hp div.nav-item div.nav-btn a { border-left:1px solid #cc9600;border-right:1px solid #febb00;border-top:1px solid #cc9600;color:#000 }';
str+='<\/style>';
	document.write(str)
	if(document.getElementById('tiffTD') && document.getElementById('tiffTD').parentNode.getElementsByTagName('IMG')[0])
	{
		var tiffLabel = document.getElementById('tiffTD').parentNode.getElementsByTagName('IMG')[0];
		tiffLabel.style.visibility = 'hidden';
		tiffLabel.src = 'http://www.washingtonpost.com/wp-adv/superpages/images/sp_label_left.gif';
		tiffLabel.width = 89;
		tiffLabel.height = 48;
		tiffLabel.border = 0;
		tiffLabel.alt = '';
		setTimeout ("document.getElementById('tiffTD').parentNode.getElementsByTagName('IMG')[0].style.visibility = 'visible'",1000);
	}
}


var doTiffCheck = false;

//commented out by TM 5:35 6/17/08--why is this here?
if(!doTiffany && typeof commercialNode != 'undefined' && commercialNode)
{
	doTiffCheck = true;
}

function cnm(a)
{
	var nodes = a.split(';');
	for (var b = 0; b < nodes.length; b++)
	{
		
if(commercialNode.match(nodes[b])) return true;
	}
	return false;
}


//11087-JM
if(doTiffCheck && commercialNode == 'washingtonpost.com' && (estNowWithYear >= '200910010000' && estNowWithYear <= '200910192359'))
{
	tiffWidth = "290";
	tiffHeight = "60";
	doTiffany = true;
	tiffDelivery = "adi";	
}


//11231-AL-218771136
if(doTiffCheck && tiffContCategories(commercialNode,'local') && estNowWithYear <= '200910302359')
{
	tiffWidth = "300";
	tiffHeight = "45";
	doTiffany = true;
	tiffDelivery = "adi";	
}

//11281-MB-Order#3967936
if(doTiffCheck && tiffContCategories(commercialNode,'execnews'))
{
	var sprint_exec_array = [ ['1019'], ['1102'], ['1116'],['1214'] ];
	for(var k in sprint_exec_array)
	{
	
			if(sprint_exec_array[k].length > 1 && (estNowWithYear >= '2009' + sprint_exec_array[k][0] + '0000' && estNowWithYear <= '2009' + sprint_exec_array[k][1] + '2359' ) || ( estNowWithYear.substring(4,8) == sprint_exec_array[k] ))
			{
				tiffWidth = "300";
				tiffHeight = "45";
				doTiffany = true;
				tiffDelivery = "adi";
			}
	}

	
}

//10951-MB-218142591
if(doTiffCheck && commercialNode.match('business') && estNowWithYear <= '200911302359')
{
	tiffWidth = "300";
	tiffHeight = "45";
	doTiffany = true;
	tiffDelivery = "adi";	
}


//11413-JM-219032687
if(doTiffCheck && commercialNode == 'washingtonpost.com' && (estNowWithYear >= '200910280000' && estNowWithYear <= '200910282359'))
{
	tiffWidth = "200";
	tiffHeight = "60";
	doTiffany = true;
	tiffDelivery = "adi";	
}

//11089-AL
if(doTiffCheck && tiffContCategories(commercialNode,'execnews') && ((estNowWithYear >= '200911030000' && estNowWithYear <= '200911092359') || (estNowWithYear >= '200911170000' && estNowWithYear <= '200911222359')))
{
	tiffWidth = "300";
	tiffHeight = "45";
	doTiffany = true;
	tiffDelivery = "adi";	
}

//11561-AL-219458757
if(doTiffCheck && tiffContCategories(commercialNode,'local') && estNowWithYear <= '200911172359')
{
	tiffWidth = "300";
	tiffHeight = "45";
	doTiffany = true;
	tiffDelivery = "adi";	
}

//11487-MB-218518263
if(doTiffCheck && commercialNode=='washingtonpost.com' && (estNowWithYear >= '200911120000' && estNowWithYear <= '200911122359'))
{
	tiffWidth = "290";
	tiffHeight = "60";
	doTiffany = true;
	tiffDelivery = "adi";	
}


if(location.href.match('test_tiff'))
{
	tiffWidth = getQSValue('test_tiff').split('x')[0];	
	tiffHeight = getQSValue('test_tiff').split('x')[1];
	doTiffany = true;
	tiffDelivery = "adi";
}

//testing code//
/*if(doTiffCheck && location.href.match('test_ads=newtiff'))
{
	tiffWidth = '200';
	tiffHeight = '60';
	doTiffany = true;
	tiffDelivery = 'adi';
}*/



//10027-Email-a-friend
if( location.href.match('emailafriend') && estNowWithYear <= '200912312359' )
{
	doTiffany = false;
}


if( ( doTiffany  && !location.href.match('demoAds') && !location.href.match('no_ads') ) || ( urlCheck('demoAds',{'type':'variable'}) && urlCheck('demoAds',{'type':'variable'}).match('tiffanytile') ) )
{
	
	//trigger demoAds
	var commercialNode = ( urlCheck('demoAds',{'type':'variable'}) && urlCheck('demoAds',{'type':'variable'}).match('tiffanytile') ) ? 'test' : commercialNode;
	
	var commercial_node = commercialNode;
	commercial_node += ( commercialNode == 'washingtonpost.com' && location.href.match('reload=true') ) ? '/tiffrefresh' : '';
	
	//testing code//
	 tiffOnTheFly += (getQSValue('test_ads'))?('kw=test_' + getQSValue('test_ads') + ';'):'';
	//testing code//
	 tiffDiv = (document.getElementById('slug_tiffany_tile'))?document.getElementById('slug_tiffany_tile'):document.getElementById('adTiff');
	 tiffDiv.style.display = 'block';
	 tiffDiv.style.position = 'absolute';
	 //tiffDiv.style.left = '';
	 //tiffDiv.style.top = '';
	 tiffDiv.style.right = '0px';
	 tiffDiv.style.bottom = '0px';
	 tiffDiv.style.marginBottom = '2px';
	 tiffDiv.parentNode.style.position = 'relative';

	 if(tiffDelivery == 'adi')
	 {
	  tiffIframe = document.createElement('iframe');
	  tiffIframe.src= 'http://ad.doubleclick.net/' + tiffDelivery + '/wpni.' + commercial_node + ';ad=tiff;sz=200x60,200x80,234x60,290x60,300x45;beta=n;' + tiffOnTheFly + 'ord=' + ord + '?';
	  tiffIframe.width = tiffWidth;
	  tiffIframe.height = tiffHeight;
	  tiffIframe.frameBorder = 0;
	  tiffIframe.marginHeight = 0;
	  tiffIframe.marginWidth = 0;
	  tiffIframe.scrolling = "no";
	  if(!document.getElementById('tiffTD'))
	   {
		document.write('<div id="tiffTD"></div>')
	   }
	  document.getElementById('tiffTD').appendChild(tiffIframe);
	 }
	 else if(tiffDelivery == 'hc' && typeof tiffRef != 'undefined' && typeof tiffSrc != 'undefined')
	 {
		tiffAnchor = document.createElement('a');
		tiffImg = document.createElement('img');
		tiffAnchor.href = tiffRef;
		tiffAnchor.target = '_blank';
		tiffImg.src = tiffSrc;
		tiffImg.width = tiffWidth;
	  	tiffImg.height = tiffHeight;
		tiffImg.alt = '';
		tiffImg.border = '0';
		if(!document.getElementById('tiffTD'))
		{
			document.write('<div id="tiffTD"></div>')
		}
		tiffAnchor.appendChild(tiffImg);
		document.getElementById('tiffTD').appendChild(tiffAnchor);
	 }
	 else
	 {
	 	 placeAd('SECTION',commercialNode,14,'',true)
	 }
// We had to remove the placeAd and use an iframe so that the ad will function properly in channelNav.js
	
}


