var GOOGLE = 1,MERLIN = 2;

var adsManager = 
{
	pushScript: function(elmID, JSsrc) 
	{	
		var newScript = new Element('script', { id: elmID+'JS', type: 'text/javascript' });
		
		$(elmID).insert(newScript);
		
		$(elmID+'JS').src = JSsrc;
		$(elmID+'JS').remove();
				
	},
	
	init: function(googleAdChannel, adID, partnerID,paramAll)
	{
		this.adID = adID;
		this.partnerID = partnerID;
		this.paramAll = paramAll;
		this.googleAdChannel = googleAdChannel;		
		
		//this.adType = Math.ceil(Math.random()*100000)%2 + 1;
		this.adType = GOOGLE;
		
		if(this.adType==GOOGLE)
		{
			this.initGoogle();
		}		
		else
		{
			this.initMerlin();
		}
	},

	initGoogle: function(_googleAdChannel)
	{		
		if(_googleAdChannel) this.googleAdChannel = _googleAdChannel;
			
		var oTmp = $$('#googleAds pre')[0];
		this.tmplt = oTmp.innerHTML;
		oTmp.remove();	
		
		var oFeedback = $$('#googleAds h5')[0];
		
		this.feedback = oFeedback.innerHTML;
		oFeedback.remove();									
									
		window.google_ad_request_done = function(google_ads) 
		{
			/*
			* This function is required and is used to display
			* the ads that are returned from the JavaScript
			* request. You should modify the document.write
			* commands so that the HTML they write out fits
			* with your desired ad layout.
			*/
			var s = '';
			var i;
			
			/*
			* Verify that there are actually ads to display.
			*/
			if (google_ads.length == 0)
			{
				return;
			}
			
			/*
			* If an image or flash ad is returned, display that ad.
			* Otherwise, build a string containing all of the ads and
			* then use a document.write() command to print that string.
			*/
			
			var tmplt = adsManager.tmplt;
			var feedback = '<h5>' 
			+	adsManager.feedback.interpolate({
							ifeedback_url: ' href="' + google_info.feedback_url + '"'
						}) 
						+ '</h5>';	
			
			if (google_ads[0].type == 'flash') 
			{	
				s += feedback +	'<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"' +
				' codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" WIDTH="' +
				google_ad.image_width + '" HEIGHT="' +
				google_ad.image_height + '"> <PARAM NAME="movie" VALUE="' +
				google_ad.image_url + '">' +
				'<PARAM NAME="quality" VALUE="high">' +
				'<PARAM NAME="AllowScriptAccess" VALUE="never">' +
				'<EMBED src="' +
				google_ad.image_url + '" WIDTH="' +
				google_ad.image_width + '" HEIGHT="' +
				google_ad.image_height +
				'" TYPE="application/x-shockwave-flash"' +
				' AllowScriptAccess="never" ' +
				' PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer"></EMBED></OBJECT>';		
			} 
			else if (google_ads[0].type == 'image') 
			{
				s += feedback +
				 '<a href="' +google_ads[0].url + '" target="_top" title="go to ' +
				google_ads[0].visible_url.split('/')[0] + 
				'" onmouseout="window.status=\'\'" onmouseover="window.status=\'go to ' +
				google_ads[0].visible_url.split('/')[0] + '\';return true"><img border="0" src="' +
				google_ads[0].image_url + '"width="' +
				google_ads[0].image_width + '"height="' +
				google_ads[0].image_height + '"></a>';
			}
			else if (google_ads[0].type == 'html') 
			{
				s += feedback +	google_ads[0].snippet;
			} 
			else 
			{						
				s += feedback;				
				
				if (google_ads.length == 1)
				{
					var curAd = google_ads[0];
				
					s += tmplt.interpolate({
						iclass: '',
						iurl: ' href="' + curAd.url + '" ',
						visible_url:curAd.visible_url.split('/')[0],
						ititle: curAd.line1,
						content: curAd.line2 + '<br/>' + curAd.line3					
					});				
					
				} 
				else if (google_ads.length > 1) 
				{								
					/*
					* For text ads, append each ad to the string.
					*/
					for(i = 0; i < google_ads.length; ++i) 
					{
						var curAd = google_ads[i];
		
						var Iclass = (i==1)?'middle':(i==google_ads.length-1)?'last':''; 
						
						s += tmplt.interpolate({
							iclass: Iclass,
							iurl:' href="' + curAd.url + '" ',
							visible_url:curAd.visible_url.split('/')[0],
							ititle: curAd.line1,
							content:  curAd.line2 + '<br/>' + curAd.line3				
						});					
					}
				}
		   	}
		
			$(google_type).update('<div>'+s+'</div>');
			$(google_type).show();
			
		    return;
		}	
	},	
	
	showGoogle: function(elmID, adCount)
	{
		google_ad_client = 'pub-5867894179059439';
		var googleAdSlot = '';
		
		if(elmID=='googleAds')
		{
			googleAdSlot = '6826311856';
			google_ad_type = 'text';
			google_skip = null;
		}
		else if(elmID=='fillSideAds')
		{
			googleAdSlot = '3146123322';
			google_ad_type = 'text+image';
			google_image_size = '250x250';
			// skip the first 3 ads			
			google_skip = 3;
		}		
		else if(elmID=='fillMainAds')
		{
			googleAdSlot = '3146123322';			
			google_image_size = '200x200';
			google_ad_type = 'text+image';
			// skip the first 3 ads
			google_skip = 3;		
		}

		google_ad_channel = this.googleAdChannel + ','+googleAdSlot;
		
		google_ad_output = 'js';
		google_max_num_ads = adCount;
		 //'text,image,flash,html'
		google_feedback = 'off';
		google_language ='iw';
		//google_skip = ;
		google_type = elmID;
		/*
		* The JavaScript returned from the following page uses
		* the parameter values assigned above to populate an array
		* of ad objects. Once that array has been populated,
		* the JavaScript will call the google_ad_request_done
		* function to display the ads.
		*/		
	
		var scr_src = 'http://pagead2.googlesyndication.com/pagead/show_ads.js?';		
		
		//document.write.target = elmID;
		//alerT('elmID='+elmID);
		document.write('<scr' +	'ipt src="' + scr_src + '"></scr' +'ipt>');
		//document.write.target = null;
	},
	
	fillHoles: function()
	{
		var $sideCol = $$('.mako_main_portlet_group_container_td.side_bar_width')[0];
		var $mainCol = $$('.mako_main_portlet_group_container_td.main_column_width')[0];
	
		var maxHeight = $sideCol.getHeight();
	
		var $sideAds = new Element('div', {id:'fillSideAds'});//.setStyle({background:'yellow'});
		var $mainAds = new Element('div', {id:'fillMainAds'});//.setStyle({background:'red'});
		
		$sideCol.insert($sideAds);
		$mainCol.insert($mainAds);		
		
		// test side bar first
		var newHeight = $sideCol.offsetHeight - $sideAds.offsetTop;
		
		if (newHeight >= 70) 
		{
			$sideAds.setStyle({
				height: newHeight + 'px'
			});
			
			// fill right space 
			this.showGoogle('fillSideAds', Math.floor( newHeight*1/70)  );
		}
		else
		{
			newHeight = $mainCol.offsetHeight - $mainAds.offsetTop;
			
			if (newHeight >= 200) 
			{
				$mainAds.setStyle({
					height: newHeight + 'px'
				});
				
				// fill main space		
				this.showGoogle('fillMainAds',   Math.floor( newHeight*1/200)*2 );
			}		
		}
	},		
	
	initMerlin: function()
	{
		if( !window.merlin_start_offset ) 	window.merlin_start_offset = 0;
	
		function fnMerlinAddUrl(qStr,val) 
		{
			try{return('&' + qStr + '=' + val)}
			catch(e){}
		}
		
		//params
		merlin_partnerid = this.partnerID;
		merlin_search = 'q'+this.adID;
		merlin_search_result = '3';
		//merlin_start_offset= 0;
		merlin_cols = '3';
		merlin_lang='heb';
		merlin_width = '90%';
		merlin_url = '';
		merlin_AdType='merlin_mako1';
		merlin_Iframe_Unique_Index=this.adID;
		merlin_default = '1';
		
		merlin_decodeKeyword = escape($F('merlin_q'+this.adID));
		
		var path='http://www.merlin.co.il/merlin_searchUTF/merlin_search.asp';
		merlin_url=path+"?utf=0&fixed=category&all="+this.paramAll+"&modaa=mo"+fnMerlinAddUrl("partnerid",merlin_partnerid);
		merlin_url+=fnMerlinAddUrl('lang',merlin_lang);
		merlin_url+=fnMerlinAddUrl('AdType',merlin_AdType);
		merlin_url+=fnMerlinAddUrl('qtyResult',merlin_search_result);
		merlin_url+=fnMerlinAddUrl('ScriptIndex',merlin_Iframe_Unique_Index);
		merlin_url+=fnMerlinAddUrl('start_offset',merlin_start_offset);
		merlin_url+=fnMerlinAddUrl('cols',merlin_cols);
		merlin_url+=fnMerlinAddUrl('merlin_default',merlin_default);
		merlin_url+='&q=' +merlin_decodeKeyword;
		
		this.buildMerlin(merlin_url);
	},	
	
	buildMerlin: function(merlin_url)
	{			
		//var MerlinSS_OBJ=document.getElementsByTagName('HEAD');
		//if(!MerlinSS_OBJ)document.createElement('HEAD');
		LazyJs.load(merlin_url, function(){
			$('googleAds').remove();
			$('merlinAds').show();	
			window.merlin_start_offset += 3;			
		});					
	}
};