/************************************
|			WEB OFFERS				|
************************************/
var isProd = (location.hostname.indexOf('turner.com') > 0) ? false : true; //production environment flag
var cnnSearchSponsorBoxTopId = 'cnnSearchSponsorBoxTop';
var cnnSearchSponsorBoxBottomId = 'cnnSearchSponsorBoxBottom';

function WebOffers(){

	//MERGE top - QAI / bottom -dev
	//var url = (isProd) ? 'http://searchapp.money.cnn.com/money-search/webOffers.jsp' : 'http://srchp1devws1.turner.com:8080/weboffers/weboffers.jsp';
	var url = (isProd) ? 'http://searchapp.money.cnn.com/weboffers/weboffers.jsp' : 'http://srchp1devws1.turner.com:8080/weboffers/weboffers.jsp';
	var args = (isProd) ? 'itype=cnnmoney_wo&cid=time-cnnmoney&text='+cnnSearchTerm+'&domains=cnn.com|turner.com' : 'cid=time-cnnmoney&itype=cnnmoney_wo&text='+cnnSearchTerm+'&domains=cnn.com|turner.com';
	this.initialize = function(){
		this.conf = {
			url:url,
			args:args,
			domId: 'cnnSearchSponsoredSearches',
			funcObj:this.display,
			link:''
		};
		this.fetch();
	};
	this.fetch = function(){
		var conf = this.conf;
		CSIManager.getInstance().callObject(conf);
	}
	
	this.display = function(obj){
		try{
			var conf = WebOffers.getInstance().conf;
			var id = conf.domId;
			var results = obj.terms;
			var content = "";
			if(results.length > 0){
				var content = 'See more sponsored links for: ';
				for(var i = 0; i < results.length; i++){
					try{
							content += '<a href=\"'+ obj.terms_urls[i] +'\" target="_blank">'+ obj.terms_bold[i] +'';
							if(i < results.length - 1){ content +=", ";	}
							content += '</a>';
						}catch(e){}
				}
			} 
			CNN_WO_AVAILABLE = true;
			cnnLatch.prototype.called('wOffers', obj);
			return content;
		}catch(e){}
	};
	
};

WebOffers.__instance__ = null; 

WebOffers.getInstance = function ()
{
	if (this.__instance__ == null) {	this.__instance__ = new WebOffers();	}
	return this.__instance__;
}
/************************************
|			end: WEB OFFERS			|
************************************/

/************************************
|			SPONSORED LINKS			|
************************************/
function SponsoredLinks(){
	this.initialize = function(pagetype){

		//try{		var start = readCookie("searchStartAt")[0];	} 
		//catch(e){		var start = parseInt(O.startAt);	}
		//var max	= parseInt(O.maxResults);	
		//var page  = ((start-1)/max)+1;

		//snum is the number of ads requested
		params.snum='6';
		params.squery=cnnEscapeQuery(cnnSearchTerm);
		params.sch='time-cnnmoney';
		params.ssch='time_cnnmoney_sl';
		params.of='js'; 
		params.callback='SponsoredLinks.getInstance().display';	//called when results are returned 
		params.rv='1.2';
		params.stest=(isProd)?'off':'on';                      				//Set this to "off" for production
		//params.spage=page;
		try{
			if(cnnPrimaryType == 'mixed'){
				params.spage=SPAGE[0];	
			}
			else if(cnnPrimaryType == 'article'){
				params.spage=SPAGE[1];
			}
			else{
				params.spage=SPAGE[2];
			}
		}catch(e){}
		params.cua=navigator.userAgent;
		get_links();								//fetch the links
		this.hasSponsoredLinks = false;
		this.hasSponsoredLinksBottom = false;
		this.countSponsoredLinks = 0;
		CNN_VIDEO_ADS_AVAILABLE = false;
		CNN_TOP_ADS_AVAILABLE = false;
		CNN_SIDE_ADS_AVAILABLE = false;
		CNN_ALL_ADS_AVAILABLE = false;
		this.domId = ['cnnSearchSponsoredLinks','cnnSearchSponsoredVideoLinks','cnnSearchSponsoredLinksBottom','cnnSearchSponsoredLinksAll'];
		this.pageType = pagetype;
	};
	
	this.display = function(obj){
		if(obj.length > 0) {
			var total = obj.length;
			document.getElementById(this.domId[1]).innerHTML = this.build(obj,0,3,cnnSearchSponsorBoxTopId,'video');
			CNN_VIDEO_ADS_AVAILABLE = true;
			
			document.getElementById(this.domId[0]).innerHTML = this.build(obj,0,3,cnnSearchSponsorBoxTopId,'top');
			CNN_TOP_ADS_AVAILABLE = true;
			
			if( total > 3 ){
				document.getElementById(this.domId[2]).innerHTML = this.build(obj,3,total,cnnSearchSponsorBoxBottomId,'side');
				CNN_SIDE_ADS_AVAILABLE = true;
			}
			document.getElementById(this.domId[3]).innerHTML = this.build(obj,0,total,cnnSearchSponsorBoxBottomId,'side');
			CNN_ALL_ADS_AVAILABLE = true;
		}
		else{
			CNN_VIDEO_ADS_AVAILABLE = false;
			CNN_TOP_ADS_AVAILABLE = false;
			CNN_SIDE_ADS_AVAILABLE = false;
			CNN_ALL_ADS_AVAILABLE = false;
		}
		cnnLatch.prototype.called('sLinks', obj);
	};
	
	this.build = function(obj,start,end,wrapperId,adType){
		var html = new Array();
		var countTmp = 0;
		var theregExp = new RegExp("\\b("+ cnnSearchTerm +")\\b","ig");
		html.push('<div class="moneyResultHeader">SPONSORED RESULTS</div>');
		for (var count = start; count < end; count++) {
			if (obj[count]) {
				var result = obj[count];		
				if (result.redirect_url) {
					if(countTmp == 0){
					}
					countTmp++;
					var firstItemClass = '';
					if(countTmp==1){ 
						firstItemClass = ' cnnSponLinksItem1';
					} 
					this.countSponsoredLinks++;
					
					switch(adType){
						case "top":
							html.push('<div class="ResultDivide">');
							if(result.title){
								html.push('<a href="'+result.redirect_url+'"  target="_blank" >'+result.title.replace(theregExp," $1 ")+'</a><br>');
							}
							if(result.d1 || result.url){
								if ( result.d1) 
								{
									html.push(result.d1.replace(theregExp," $1 "));
									html.push('&nbsp;'); 
								}
								if ( result.d2) 
								{ 
									html.push(result.d2.replace(theregExp," $1 ")); 
								}
								if(result.url){
									html.push('<div class="moneyDisplayURL"><a href="'+result.redirect_url+'"  target="_blank" >'+result.url+'</a></div>');
								}
							}
							html.push('</div>');
						
						break;
						case "video":
							html.push('<div class="ResultDivide">');
							if(result.title){
								html.push('<a href="'+result.redirect_url+'"  target="_blank" >'+result.title.replace(theregExp," $1 ")+'</a><br>');
							}			
							if ( result.d1) { 
									html.push(result.d1.replace(theregExp," $1 ")); 
									html.push('&nbsp;');
							}
							if ( result.d2) { 
									html.push(result.d2.replace(theregExp," $1 ")); 
							}							
							if(result.url){
								html.push('<div class="moneyDisplayURL"><a href="'+result.redirect_url+'"  target="_blank" >'+result.url+'</a></div>');
							}
							html.push('</div>');
						break;
						default:
							html.push('<div class="ResultDivide">');
							if(result.title){
								html.push('<a href="'+result.redirect_url+'"  target="_blank" >'+result.title.replace(theregExp," $1 ")+'</a><br>');
							}
							if(result.d1 || result.url){
						
								if ( result.d1) { 
									html.push(result.d1.replace(theregExp," $1 ")); 
									html.push('&nbsp;');
								}
								if ( result.d2) { 
									html.push(result.d2.replace(theregExp," $1 ")); 
								}
								if(result.url){
									html.push('<div class="moneyDisplayURL"><a href="'+result.redirect_url+'"  target="_blank" >'+result.url+'</a></div>');
								}
							}
							html.push('</div>');
					}
				}
			}
		}
		return html.join('');
	};
}


SponsoredLinks.__instance__ = null; 

SponsoredLinks.getInstance = function ()
{
	if (this.__instance__ == null) {	this.__instance__ = new SponsoredLinks();	}
	return this.__instance__;
}
/************************************
|		end: SPONSORED LINKS		|
************************************/



