var isProd = (location.hostname.indexOf('turner.com') > 0) ? false : true; //production environment flag
var cnnSearchSponsorBoxTopId = 'cnnSearchSponsorBoxTop';
var cnnSearchSponsorBoxBottomId = 'cnnSearchSponsorBoxBottom';

/************************************
|			SPONSORED LINKS			|
************************************/
function SponsoredLinks(){
	this.initialize = function(pagetype){

		var page = 1;
		var selectedLink;

/*		try{
			if(cnnPrimaryType == 'mixed'){
				page=SPAGE[0];	
			}
			else if(cnnPrimaryType == 'article'){
				page=SPAGE[1];
			}
			else{
				page=SPAGE[2];
			}
		}catch(e){}
*/

		var url = (isProd) ? 'http://searchapp.cnn.com/gwebsearch/gwebsearch.jsp' : 'http://srchp1refws1.turner.com/gwebsearch/gwebsearch.jsp';
		var webClientID = 'cnn-money';
		//http://arya:8080/gwebsearch/gwebsearch.jsp?query=obama&type=web&start=1&npp=5&numAds=w8&s=alltype=all&webClientId=cnn&domains=cnn.com|turner.com&csiID=csi2
		//'start=1&end=11&text='+cnnSearchTerm+'&webClientId=cnn-int&intl=true&domains=cnn.com|turner.com';
		var args = 'start=1&end=11&text='+cnnEscapeQuery(cnnSearchTerm)+'&numAds=w8&webClientId='+webClientID+'&intl='+'true'+'&domains=cnn.com|turner.com&getWeb=false';
		this.conf = {
			url:url,
			args:args,
			domId: ['cnnSearchSponsoredSearches'],
			funcObj:this.display,
			link:''
		};

		try{
			CSIManager.getInstance().call(this.conf.url,this.conf.args,this.conf.domId,this.conf.funcObj);
		} 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){
		var sl = parseInt(obj.islMetaResults.isl);
		var content = '';
		if (sl > 0) {
			document.getElementById('cnnSearchSponsoredVideoLinks').innerHTML = build(obj,0,3,cnnSearchSponsorBoxTopId,'video');
			CNN_VIDEO_ADS_AVAILABLE = true;
			
			document.getElementById('cnnSearchSponsoredLinks').innerHTML = build(obj,0,3,cnnSearchSponsorBoxTopId,'top');
			CNN_TOP_ADS_AVAILABLE = true;
			
			if( sl > 3 ){
				document.getElementById('cnnSearchSponsoredLinksBottom').innerHTML = build(obj,3,8,cnnSearchSponsorBoxBottomId,'side');
				CNN_SIDE_ADS_AVAILABLE = true;
			}
			document.getElementById('cnnSearchSponsoredLinksAll').innerHTML = build(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;
	};

}

	function build(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="moneyResultHeader">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('<div class="ResultDivide">');
							if(result.title){
								html.push('<a href="'+result.url+'"  target="_blank" >'+result.title.replace(theregExp," $1 ")+'</a><br>');
							}
							if(result.dispText || result.dispUrl){
								if ( result.dispText) 
								{
									html.push(result.dispText.replace(theregExp," $1 "));
									html.push('&nbsp;'); 
								}
								if(result.dispUrl){
									html.push('<div class="moneyDisplayURL"><a href="'+result.url+'"  target="_blank" >'+result.dispUrl+'</a></div>');
								}
							}
							html.push('</div>');
						
						break;
						case "video":
							html.push('<div class="ResultDivide">');
							if(result.title){
								html.push('<a href="'+result.url+'"  target="_blank" >'+result.title.replace(theregExp," $1 ")+'</a><br>');
							}			
							if(result.dispText || result.dispUrl){
								if ( result.dispText) 
								{
									html.push(result.dispText.replace(theregExp," $1 "));
									html.push('&nbsp;'); 
								}
								if(result.dispUrl){
									html.push('<div class="moneyDisplayURL"><a href="'+result.url+'"  target="_blank" >'+result.dispUrl+'</a></div>');
								}
							}
							html.push('</div>');
						break;
						default:
							html.push('<div class="ResultDivide">');
							if(result.title){
								html.push('<a href="'+result.url+'"  target="_blank" >'+result.title.replace(theregExp," $1 ")+'</a><br>');
							}
							if(result.dispText || result.dispUrl){
								if ( result.dispText) 
								{
									html.push(result.dispText.replace(theregExp," $1 "));
									html.push('&nbsp;'); 
								}
								if(result.dispUrl){
									html.push('<div class="moneyDisplayURL"><a href="'+result.url+'"  target="_blank" >'+result.dispUrl+'</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		|
************************************/



