/************************************
|			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.cnn.com/weboffers/weboffers.jsp' : 'http://searchapp.cnn.com/weboffers/weboffers.jsp';
	//var url = (isProd) ? 'http://searchapp.cnn.com/weboffers/weboffers.jsp' : 'http://arya.turner.com:8080/weboffers/weboffers.jsp';
	var args = (isProd) ? 'itype=cnn&cid=cnn&text='+cnnSearchTerm+'&domains=cnn.com|turner.com' : 'cid=cnn&itype=wo_cnn_q&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;
		try{
			CSIManager.getInstance().callObject(conf);
		} catch(e){}
	}
	
	this.display = function(obj){
		var conf = WebOffers.getInstance().conf;
		var id = conf.domId;
		var results = obj.terms;
		var content = "";
		if(results.length > 0){
			var content = '<p class="cnnSeeMoreSponLinks"><span class="cnnBlackText">See more sponsored links for: </span>';
			for(var i = 0; i < results.length; i++){
				content += '<a href=\"'+ obj.terms_urls[i] +'\" target="_blank">'+ obj.terms_bold[i] +'';
				if(i < results.length - 1){ content +=", ";	}
				content += '</a>';
			}
			content +="</p>";
			CNN_WO_AVAILABLE = true;
		} 
		cnnLatch.prototype.called('wOffers', obj);
		return content;
	};
	
};

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, cnnIsIntl){
		
		var page = 1;
		var selectedLink;
		
		//determine page
		$('cnnSrchPgn').select('div').each(function(val) {
			if (val.style.display === 'block') {
				selectedLink = val.select('a#selectedPage')[0];
				if (typeof selectedLink !== 'undefined') {
					page = parseInt(selectedLink.innerHTML);
				}				
			}
		});
						
		if (pagetype == 'video'){
			params.snum='3';
		}
		else{
			params.snum='8';
		}
				
		if (!cnnIsIntl) {
			params.squery=cnnEscapeQuery(cnnSearchTerm);
			params.sch='cnn';
			params.cua = navigator.userAgent;
			params.ssch='cnn_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;
			
			get_links();								//fetch the links
			this.hasSponsoredLinks = false;
			this.hasSponsoredLinksBottom = false;
			this.countSponsoredLinks = 0;
		} else {
			
			var url = (isProd) ? 'http://searchapp.cnn.com/gwebsearch/gwebsearch.jsp' : 'http://srchp1refws1.turner.com/gwebsearch/gwebsearch.jsp';
			var args = 'start=1&end=11&text='+cnnSearchTerm+'&webClientId=cnn-int&intl=true&domains=cnn.com|turner.com';
			this.confIntl = {
				url:url,
				args:args,
				domId: ['cnnSearchSponsoredSearches'],
				funcObj:this.displayIntl,
				link:''
			};
			
			try{
				CSIManager.getInstance().callObject(this.confIntl);
			} catch(e){}
			
		}
		
		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.displayIntl = function(obj){
		var sl = parseInt(obj.islMetaResults.isl);
		var content = '';
		var total = sl;
		if (sl > 0) {
			document.getElementById('cnnSearchSponsoredVideoLinks').innerHTML = buildIntl(obj,0,3,cnnSearchSponsorBoxTopId,'video');
			CNN_VIDEO_ADS_AVAILABLE = true;
			
			document.getElementById('cnnSearchSponsoredLinks').innerHTML = buildIntl(obj,0,3,cnnSearchSponsorBoxTopId,'top');
			CNN_TOP_ADS_AVAILABLE = true;
			
			if( sl > 3 ){
				document.getElementById('cnnSearchSponsoredLinksBottom').innerHTML = buildIntl(obj,3,sl,cnnSearchSponsorBoxBottomId,'side');
				CNN_SIDE_ADS_AVAILABLE = true;
			}
			document.getElementById('cnnSearchSponsoredLinksAll').innerHTML = buildIntl(obj,0,sl,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);
		return content;
	};
	
	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="cnnSponLabel">Sponsored Links</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 "side":
						case "top":

							html.push('<ul class="cnnSponLinksItem" >');
							if(result.title){
								html.push('<li class="cnnSponLinksResultTitle"><a href="' + result.redirect_url + '"  target="_blank" >' + result.title.replace(theregExp," <b>$1</b> ") + '</a></li>');
							}
							if(result.d1 || result.url){
								html.push('<li class="cnnSponLinksResultURL">');
								if(result.url){
									html.push('<a href="' + result.redirect_url + '"  target="_blank" >' + result.url +'&nbsp;-&nbsp;');
									html.push('</a>');
								}
								if ( result.d1) {
									html.push('<a href="' + result.redirect_url + '"  target="_blank" >');
									html.push('<span class="cnnBlackText">');
									html.push(result.d1.replace(theregExp," <b>$1</b> ")); 
									
								}
								html.push('&nbsp;');
								if ( result.d2) { 
									html.push(result.d2.replace(theregExp," <b>$1</b> ")); 
								}
								html.push('</span>');
								html.push('</a>');
								html.push('</li>');
							}
							html.push('</ul>');
						
						break;
						case "video":
							html.push('<ul class="cnnSponLinksItem cnnSponLinksItemHorz" >');
							if(result.title){
								html.push('<li class="cnnSponLinksResultTitle"><a href="' + result.redirect_url + '"  target="_blank">' + result.title.replace(theregExp," <b>$1</b> ") + '</a></li>');
							}
								
							if ( result.d1) { 
								html.push('<li class="cnnSponLinksResultText">');
								html.push('<a href="' + result.redirect_url + '"  target="_blank"><span class="cnnBlackText">');
								html.push(result.d1.replace(theregExp," <b>$1</b> ")); 
							}
								html.push('&nbsp;');
								if ( result.d2) { 
									html.push(result.d2.replace(theregExp," <b>$1</b> ")); 
								}							
								html.push('</span></a></li>');
								if(result.d1 || result.url){
								html.push('<li class="cnnSponLinksResultURL">');
								if(result.url){
									html.push('<a href="' + result.redirect_url + '"  target="_blank">' + result.url +'</a>&nbsp;</li>');
								}
								
							}
							html.push('</ul>');
						break;
						default:
							html.push('<ul class="cnnSponLinksItem' + firstItemClass + '" >');
							if(result.title){
								html.push('<li class="cnnSponLinksResultTitle"><a href="' + result.redirect_url + '"  target="_blank" >' + result.title.replace(theregExp," <b>$1</b> ") + '</a></li>');
							}
							if(result.d1 || result.url){
						
								if ( result.d1) { 
								html.push('<li class="cnnSponLinksResultText">');
								html.push('<a href="' + result.redirect_url + '"  target="_blank"><span class="cnnBlackText">');
								html.push(result.d1.replace(theregExp," <b>$1</b> ")); }
								html.push('&nbsp;');
								if ( result.d2) { html.push(result.d2.replace(theregExp," <b>$1</b> ")); }
								html.push('</span></a>');
								html.push('</li>');
								html.push('<li class="cnnSponLinksResultURL">');
								if(result.url){
									html.push('<a href="' + result.redirect_url + '"  target="_blank" >' + result.url +'</a>&nbsp;</li>');
								}
							}
							html.push('</ul>');
					}
				}
			}
		}
		return html.join('');
	};
	
	function buildIntl (obj,start,end,wrapperId,adType){
		var html = new Array();
		var countTmp = 0;
		var theregExp = new RegExp("\\b("+ cnnSearchTerm +")\\b","ig");
		var results = obj.islResults;
		
		html.push('<div class="cnnSponLabel">Sponsored Links</div>');
		for (var count = start; count < end; count++) {
			if (results && results[count]) {
				var result = results[count];
				if (result.url) {
					if(countTmp == 0){
					}
					countTmp++;
					var firstItemClass = '';
					if(countTmp==1){ 
						firstItemClass = ' cnnSponLinksItem1';
					} 
					this.countSponsoredLinks++;
					
					switch(adType){
						case "top":
	
							html.push('<ul class="cnnSponLinksItem" >');
							if(result.title){
								html.push('<li class="cnnSponLinksResultTitle"><a href="' + result.url + '"  target="_blank" >' + result.title.replace(theregExp," <b>$1</b> ") + '</a></li>');
							}
							if(result.dispText || result.dispUrl){
								html.push('<li class="cnnSponLinksResultURL">');
								if(result.dispUrl){
									html.push('<a href="' + result.url + '"  target="_blank" >' + result.dispUrl +'&nbsp;-&nbsp;');
									html.push('</a>');
								}
								if ( result.dispText) {
								html.push('<a href="' + result.dispUrl + '"  target="_blank" >');
								html.push('<span class="cnnBlackText">');
								html.push(result.dispText.replace(theregExp," <b>$1</b> ")); }
								html.push('&nbsp;');
								html.push('</span>');
								html.push('</a>');
								html.push('</li>');
							}
							html.push('</ul>');
						
						break;
						case "video":
							html.push('<ul class="cnnSponLinksItem cnnSponLinksItemHorz" >');
							if(result.title){
								html.push('<li class="cnnSponLinksResultTitle"><a href="' + result.url + '"  target="_blank">' + result.title.replace(theregExp," <b>$1</b> ") + '</a></li>');
							}
								
							if ( result.dispText) { 
								html.push('<li class="cnnSponLinksResultText">');
								html.push('<a href="' + result.url + '"  target="_blank"><span class="cnnBlackText">');
								html.push(result.dispText.replace(theregExp," <b>$1</b> ")); }
								html.push('&nbsp;');
								html.push('</span></a></li>');
								if(result.url){
								html.push('<li class="cnnSponLinksResultURL">');
									if(result.dispUrl){
										html.push('<a href="' + result.url + '"  target="_blank">' + result.dispUrl +'</a>&nbsp;</li>');
									}
								
							}
							html.push('</ul>');
						break;
						default:
							html.push('<ul class="cnnSponLinksItem' + firstItemClass + '" >');
							if(result.title){
								html.push('<li class="cnnSponLinksResultTitle"><a href="' + result.url + '"  target="_blank" >' + result.title.replace(theregExp," <b>$1</b> ") + '</a></li>');
							}
							if(result.dispText || result.dispUrl){
						
								if ( result.dispText) { 
								html.push('<li class="cnnSponLinksResultText">');
								html.push('<a href="' + result.url + '"  target="_blank"><span class="cnnBlackText">');
								html.push(result.dispText.replace(theregExp," <b>$1</b> ")); }
								html.push('&nbsp;');
								html.push('</span></a>');
								html.push('</li>');
								html.push('<li class="cnnSponLinksResultURL">');
								if(result.dispUrl){
									html.push('<a href="' + result.url + '"  target="_blank" >' + result.dispUrl +'</a>&nbsp;</li>');
								}
							}
							html.push('</ul>');
					}
					
				
				}
			}
		}
		return html.join('');
	}
}


SponsoredLinks.__instance__ = null; 

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