var sponsoredLinksContainerID = 'nLinkDiv_1';
var sponsoredLinksPartnerID = 'excite_myway_email_js';

function fiSponLinksPresentationAction(globHtmlWriteSponLinksObj){
	if (!globHtmlWriteSponLinksObj) {
		var globHtmlWriteSponLinksObj = new Object;
		globHtmlWriteSponLinksObj.mode = 'return';
	}
	var fiDefaultDiv = sponsoredLinksContainerID;
	var fiDivLinksMapping = new Array();
	var fiDivNames = new Array();
	var fiDivObjs = new Array();

	fiDivNames[0] =  fiDefaultDiv;
	fiDivObjs[0] = globHtmlWriteSponLinksObj;
	fiDivLinksMapping[fiDefaultDiv] = new Array();
	var thisDiv = '';
	var max = 0;

	if (typeof(gcaMaxNumAds) != 'undefined') {
		max = gcaMaxNumAds;
	} else {
		max = 3;
	}
	
	/* Left below alone to ensure nothing breaks, can probably be very simplified */
	for (i=0;i<max;i++) {
		if ((typeof(fiSponLinkTarget) != 'undefined') && (typeof(fiSponLinkTarget[i]) != 'undefined')) {
			thisDiv = (fiSponLinkTarget[i] == '') ? fiDefaultDiv : fiSponLinkTarget[i][0];
			if (typeof(fiDivLinksMapping[thisDiv]) == 'undefined') {
				fiDivNames[fiDivNames.length] = thisDiv;
				fiDivLinksMapping[thisDiv] = new Array();
				if (typeof(fiSponLinkTarget[i][1]) != 'undefined') {
					fiDivObjs[fiDivObjs.length] = fiSponLinkTarget[i][1];
					fiDivObjs[fiDivObjs.length-1].mode = 'return';
				} else {
					fiDivObjs[fiDivObjs.length] = globHtmlWriteSponLinksObj;
				}
			}
			fiDivLinksMapping[thisDiv][fiDivLinksMapping[thisDiv].length] = i;
		} else {
			fiDivLinksMapping[fiDefaultDiv][fiDivLinksMapping[fiDefaultDiv].length] = i;			
		}
	 }
	var count = 0;
		
	for (i = fiDivNames.length-1; i >= 0; i--) {
		if (!fiSponLinksDivAvailable(fiDivNames[i])){
			gcaMaxNumAds -= fiDivLinksMapping[fiDivNames[i]].length;
		}
	}
	for	(i=fiDivNames.length-1;i>=0;i--) {
		if (fiSponLinksDivAvailable(fiDivNames[i])){
			if (count++ == 0) {		
				if(fiSponLinksDataAvailable()){
					gcaNumAdsReturned=google_ads.length;
				} else {
					gcaNumAdsReturned=-1;
				}
				if(fiSponLinksDataAvailable()){
					 if(google_ads.length==0){
						//fiLoadDefaultSponLinks();
					}
				} else {
					 //fiLoadDefaultSponLinks();
				}
				if (getGcaPosition() != 'bot')				
					fiSponLinksTrackImpression();
			}
			globHtmlWriteSponLinksToDiv(fiDivObjs[i], fiDivNames[i], fiDivLinksMapping[fiDivNames[i]]);
		}
	}
}

function fiSponLinksDivAvailable(){
	if (document.getElementById(sponsoredLinksContainerID))
		return true;
	else return false;
}

function fiSponLinksDataAvailable(){
	if ((typeof google_ads) != 'undefined') return true;
	else return false;
}

function fiSponLinksTrackImpression() {
	var thisImgNme='m.gif';
	if ((typeof fiSponLinksRetainImgRand) != 'undefined') var thisImgRand = fiSponLinksRetainImgRand;
	else var thisImgRand = Math.round(Math.random() * 10000000000);
	var thisImgUrl = 'http://imgfarm.com/images/nocache/tr/gca/' + thisImgNme
		+ '?rand=' + thisImgRand 
		+ '&a=' + getGcaAdClient() 
		+ '&c=' + getGcaAdChannel()
		+ '&st=' + getGcaSearchType()
		+ '&k=' + escape(getGcaKeyword()) 
		+ '&w=' + getGcaNumAdsRequested()
		+ '&r=' + getGcaNumAdsReturned() 
		+ '&u=' + getDocumentUrl() 
		+ '&v=' + fiSponLinkTrackVal_v()
		+ '&g=' + fiSponLinkTrackVal_g()
		+ '&s=' + fiSponLinkTrackVal_s()
		+ '&h=' + fiSponLinkTrackVal_h();
	image1 = new Image();
	image1.src = thisImgUrl;
}

function fiSponLinksTrackClick(url,p){
	var p=p+1;   // ordinal
	var htmlEsc=new Array();
  	htmlEsc["&amp;"]="&";
  	htmlEsc["&quot;"]="\"";
  	htmlEsc["&lt;"]="<";
  	htmlEsc["&gt;"]=">";
  	for (var tok in htmlEsc) {
	  	var re=new RegExp(tok,"g");
	  	url=url.replace(re,htmlEsc[tok]);
	}
	
    var thisHrefUrl = getBaseRedirectUrl() 
	    + '&tp=' + getGcaPosition()
        + '&dod=' + getDod()
        + '&pg=' + getPg()
        + '&hlc=' + getHlc()
        + '&plp=' + (p-1)
        + '&tpr=' + getTpr()
		+ '&ptb=' + getPtb()
        + '&redirect=' + escape(url)
        + '&pr=' + getPr()
        + '&ct=GX';

    return thisHrefUrl;
}
function getPn(){
	if ((typeof pn) != 'undefined') return pn;
	else return '';
}
function getSs(){
	if ((typeof ss) != 'undefined') return ss;
	else return '';
}
function getQid(){
	if ((typeof qid) != 'undefined') return qid;
	else return '';
}
function getPtb(){
	if ((typeof ptb) != 'undefined') return ptb;
	else return '';
}
function getPr(){
	if ((typeof pr) != 'undefined') return pr;
	else return '';
}
function getRedirect(){
	if ((typeof redirect) != 'undefined') return redirect;
	else return '';
}
function getBaseRedirectUrl(){
	if ((typeof baseRedirectUrl) != 'undefined') return baseRedirectUrl;
	else return 'redirect.jhtml';
}
function getTpr(){
	if ((typeof tpr) != 'undefined') return tpr;
	else return '';
}
function getPlp(){
	if ((typeof plp) != 'undefined') return plp;
	else return '';
}
function getHlc(){
	if ((typeof hlc) != 'undefined') return hlc;
	else return '';
}
function getPg(){
	if ((typeof pg) != 'undefined') return pg;
	else return '';
}
function getDod(){
	if ((typeof dod) != 'undefined') return dod;
	else return '';
}
function getGcaSearchType(){
	if ((typeof gcaSearchType) != 'undefined') return gcaSearchType;
	else return '';
}
function getGcaPosition(){
	if ((typeof gcaPosition) != 'undefined') return gcaPosition;
	return '';
}
function getGcaAdClient(){
	if ((typeof gcaAdClient) != 'undefined') return gcaAdClient;
	return '';
}
function getGcaAdChannel(){
	if ((typeof gcaAdChannel) != 'undefined') return gcaAdChannel;
	return '';
}
function fiSponLinkTrackVal_g(){
	if ((typeof fiSponLinkSetTrackVal_g) != 'undefined') return fiSponLinkSetTrackVal_g;
	return '';
}
function fiSponLinkTrackVal_h(){
	if (((typeof google_hints_trk) != 'undefined')) return google_hints_trk;
	else return '';
}
function getGcaKeyword(){
	if ((typeof gcaKwd) != 'undefined') return gcaKwd;
	else return '';
}
function getGcaNumAdsReturned(){
	if ((typeof gcaNumAdsReturned) != 'undefined') return gcaNumAdsReturned;
	return -1;
}
function fiSponLinkTrackVal_s(){
	if ((typeof fiSponLinkSetTrackVal_s) != 'undefined') return fiSponLinkSetTrackVal_s;
	return '';
}
function getDocumentUrl(){
	return escape(document.location);
}
function fiSponLinkTrackVal_v(){
	return '';
}
function getGcaNumAdsRequested(){
	if ((typeof gcaMaxNumAds) != 'undefined') return gcaMaxNumAds;
	else return 3;
}

function globHtmlWriteSponLinksToDiv (globHtmlWriteSponLinksObj, divName, links) {
	if ((typeof globHtmlWriteSponLinks) == 'function') {
		var linkHtml = globHtmlWriteSponLinks(globHtmlWriteSponLinksObj, links);
		document.getElementById(divName).innerHTML = linkHtml;
	}
}
function globHtmlTruncLongText(text, width) {
	if (width == null) return text;
	var words = text.split(" ");
	var pixels_per_char = 7;
	maxCharsPerLine=Math.round(width/pixels_per_char);
	for (var i=0;i<words.length;i++) {
		if (words[i].length > maxCharsPerLine) {
			words[i] = words[i].substring(0, maxCharsPerLine);
		}
	}
	return words.join(" ");
}

function globHtmlWriteSponLinks(obj, links_to_write){
	var link_search_str = "";
	if (links_to_write != null) {
		link_search_str = ',' + links_to_write.join(',') + ',';
	}
    if(obj!=null){
    	var mode=obj.mode;
		var type=obj.type;
		var botvspace=obj.botvspace;
		var clrBg=obj.clrBg;
		var clrBdr=obj.clrBdr;
		var clrTtlBg=obj.clrTtlBg;
		var clrTtlTxt=obj.clrTtlTxt;
		var clrTtlUl=obj.clrTtlUl;
		var clrLine1=obj.clrLine1;
		var clrLine2=obj.clrLine2;
		var clrLine3=obj.clrLine3;
		var noHeader=obj.noHeader;
		var modWidth=obj.width;
    }
    if(mode==null)mode='docwrite';
	if(type==null)type=0;
	if(botvspace==null)botvspace='<font size=1><br></font>';
	if(clrBg==null||clrBg=='')clrBg='#ffffff';
	if(clrBdr==null||clrBdr=='')clrBdr='#6699FF';
	if(clrTtlBg==null||clrTtlBg=='')clrTtlBg='#3366CC';
	if(clrTtlTxt==null||clrTtlTxt=='')clrTtlTxt='white';
	if(clrTtlUl==null||clrTtlUl=='')clrTtlUl='#6699FF';
	if(clrLine1==null||clrLine1=='')clrLine1='#0033cc';
	if(clrLine2==null||clrLine2=='')clrLine2='#000000';
	if(clrLine3==null||clrLine3=='')clrLine3='#008000';
	if(noHeader==null||noHeader=='')noHeader=false;
	if(modWidth==null||modWidth=='')modWidth=210;

	if(((typeof fiSponLinkBdyLnkForce)!='undefined') && (fiSponLinkBdyLnkForce!=''))var clrLine3=fiSponLinkBdyLnkForce;
	
	if(!noHeader)var useHeader=true;
	else useHeader=false;
	if ((typeof links_to_write ) != 'undefined') count = links_to_write.length;
	else if((typeof gcaMaxNumAds)!='undefined') count = gcaMaxNumAds;
	else count=3;
	
	if (((typeof NAV_NS) != 'undefined') && ((typeof NAV_VER) != 'undefined')) {
		if(NAV_NS && NAV_VER < 5) {
		} else {
			rValInterLnkHeight='<font st'+'yle="line-height:6px;">&nbsp;<br></font>';
		}
	}
	
	if((typeof fiSponLinksDivHgt)=='undefined')fiSponLinksDivHgt=170;
	fiSponLinksInnerTbleHgt=fiSponLinksDivHgt-16;
	
	rVal='';
	these_google_ads = new Array();
	if (typeof(google_ads) == "undefined" || google_ads.length == 0) {
		fiLoadDefaultSponLinks();
	}
	for (var i=0;i<google_ads.length;i++) {
		if ((link_search_str.indexOf(',' + i + ',') > -1) || (link_search_str == '')) {
			these_google_ads[these_google_ads.length] = google_ads[i]; }
	}
	
    if(fiSponLinksDataAvailable()){
		//if (typeof sponsoredLinksTitleText != "undefined") rVal += globHtmlWriteSponLinksTtl();
		rVal+='<table cellpadding=0 cellspacing=0 border=0><tr><td>';
		
		var i;
		for(i = 0; i < these_google_ads.length; ++i) {
			if(i<count){
				var onMouseHtml='onMouseOver="(window.status=\'go to '+these_google_ads[i].visible_url+'\'); return true;" ';
				onMouseHtml+=	'onMouseOut="window.status=\'\'; return true;"';
				if(typeof(providerBranded)=='undefined' || providerBranded==null){
					
					
					rVal+='<div class="spLinkCon"><div class="sponlink">';
					rVal+='<a href="'+fiSponLinksTrackClick(these_google_ads[i].url,i)+'" target="_parent" style="text-decoration: none;">';
					rVal+='<span class="pseudolink">'+these_google_ads[i].line1+'</span><br/>';
					rVal+='</a>';
					rVal+='<span class="nDesc">'+these_google_ads[i].line2+' '+these_google_ads[i].line3+'</span><br/>';
					rVal+='<a href="'+fiSponLinksTrackClick(these_google_ads[i].url,i)+'" target="_parent" style="text-decoration: none;">';
					rVal+='<span class="durl">'+these_google_ads[i].visible_url + '</span>';
					rVal+='</a>';
					rVal+='</div></div>';
					
				}
				else{
					
					rVal+='<div class="spLinkCon"><div class="nBox">';
					rVal+='<a href="'+fiSponLinksTrackClick(these_google_ads[i].url,i)+'" target="_parent" class="nDesc">';
					rVal+='<span class="pseudolink">'+these_google_ads[i].line1+'</span><br/>';
					rVal+='</a>';
					rVal+='<span class="nDesc">'+these_google_ads[i].line2+' '+these_google_ads[i].line3+'</span><br/>';	
					rVal+='<a href="'+fiSponLinksTrackClick(these_google_ads[i].url,i)+'" target="_parent" class="nDesc">';
					rVal+='<span class="uri">'+these_google_ads[i].visible_url + '</span>';
					rVal+='</a>';
					rVal+='</div></div>';
					
				}
					
				if(count==2 && i==0)
					rVal+='<table cellpadding=0 cellspacing=0 border=0 height=1><tr><td>';
			}
		}
		rVal+='</td></tr></table>';
    }
	rVal+='';
	return rVal;
}

function globHtmlWriteSponLinksTtl() {
	//if (typeof sponsoredLinksTitleText != "undefined")
	return  '<div id="sectionHdr">' +
			'<a class="sectionTitle" href="adsByGoogle.html" target="_blank"><fmt:message key="title.sponsored_links"/></a>' + 
			'</div>';
}

function google_ad_request_done(google_ads){
	return;
}


function fiLoadDefaultSponLinks() {
	var x=0;
	google_ads = new Array();
	
	google_ad = new Object();
	google_ad.n = 1;
	google_ad.url = "http://poll.myway.com";
	google_ad.visible_url = "http://poll.myway.com";
	google_ad.line1 = "Voice Your Opinion ";
	google_ad.line2 = "Take today's My Way Poll, featuring a new topic daily.";
	google_ad.line3 = "";
	google_ads[0] = google_ad;
	
	google_ad = new Object();
	google_ad.n = 2;
	google_ad.url = "http://games.myway.com";
	google_ad.visible_url = "http://games.myway.com";
	google_ad.line1 = "Play Free Online Games";
	google_ad.line2 = "Collapse, Bounce Out, Jigsaw, Crossword, Mah Jong and tons more.";
	google_ad.line3 = "";
	google_ads[1] = google_ad;
	
	google_ad = new Object();
	google_ad.n = 3;
	google_ad.url = "http://today.myway.com";
	google_ad.visible_url = "http://today.myway.com";
	google_ad.line1 = "Cartoons, Tips, Fun Facts";
	google_ad.line2 = "Make 'My Way Today' a part of your day - filled with trivia, comics, photos and more.";
	google_ad.line3 = "";
	google_ads[2] = google_ad;
	
	google_ad = new Object();
	google_ad.n = 4;
	google_ad.url = "http://weather.myway.com";
	google_ad.visible_url = "http://weather.myway.com";
	google_ad.line1 = "5-Day Weather Forecast";
	google_ad.line2 = "Check local weather conditions on My Way before making plans.";
	google_ad.line3 = "";
	google_ads[3] = google_ad;
	
	google_ad = new Object();
	google_ad.n = 5;
	google_ad.url = "http://sports.myway.com/mlb/stats.html";
	google_ad.visible_url = "http://sports.myway.com/mlb/stats.html";
	google_ad.line1 = "Over 150 Baseball Stats ";
	google_ad.line2 = "Updated daily in My Way Sports for avid fans like you.";
	google_ad.line3 = "";
	google_ads[4] = google_ad;
	
	if (window.google_ad_request_done) {
		google_ad_request_done(google_ads);
  	}
}

	function fiSponLinksAdminMode(){return 1;}
	if(fiSponLinksDivAvailable()){
		google_ad_client = gcaAdClient;
		google_ad_channel = gcaAdChannel;
		google_ad_output = 'js';
		google_encoding = 'utf8';
		google_safe = 'medium';
		if (getGcaSearchType().toLowerCase()  == 'dns')
			google_safe = 'high';
		
		if ((typeof gcaAdTestMode)!='undefined')
			google_adtest = gcaAdTestMode;
		else
			google_adtest = 'off';
		
		if ((typeof gcaMaxNumAds)!='undefined')
			google_max_num_ads = gcaMaxNumAds;
		else 
			google_max_num_ads = 3;

		if ((typeof gcaKwd)!='undefined') {
			google_kw = gcaKwd;
			google_kw_type = 'broad';
		}
		//	google_hints = gcaKwd;
		
		document.write('<scr'+'ipt language="JavaScript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></scr'+'ipt>');
	}
