// Crated by Terapon Rodyam
// postdate 27-04-2009
/*-------------------------*/
// Default value

google_ad_client = 'pub-1044823792492543'; //Public ID
google_ad_output = 'js';
google_feedback = 'on';
google_adtest  = 'off'; //for test ad Default = 'off' 

/* Only insert this line for your first ad unit */ 
var google_adnum = 0; 

//Set value to default;
if(displayBorderTop==null){
	var displayBorderTop	 = false; 
}
if(displayLandScape==null){
	var displayLandScape = false; // false=Default, true=landscape *** if set Landscape not arrow ad type image
}
if(position_ad_detail==null){
	var position_ad_detail ='';//Set to Default
}

//Function google Ads
function google_ad_request_done (google_ads) {
	var s = '';
	var i;
	
	//main container div
	var container = document.createElement('div');//<div class="google-AdBox">...</div>
	container.className = 'google-AdBox';
	
	//chcek Display ad for landscape
	if(google_ads[0].type == 'text'){
		if(displayLandScape){
			container.id = 'Ad-landscap';	//for Ad Lanscape 
		}
	}
	//Check for Ad In content	
	if(position_ad_detail == 'in'){
		container.id = 'Ad-Intext'; //In Detail
	}else if(position_ad_detail=='under' || position_ad_detail == 'ad-Gogle-a3' || position_ad_detail == 'ad-300'){
		
		if(position_ad_detail=='under') container.id = 'Ad-UnderDetail'; //Under Detail
		else if(position_ad_detail == 'ad-Gogle-a3')container.id = 'ad-Gogle-a3';  //section page
		else if(position_ad_detail == 'ad-300')container.id = 'ad-300'; //list page
		
		if (google_ads[0].type == 'image' || google_ads[0].type == 'flash') {
			if(google_ads[0].image_width=='300'){
				container.style.margin="0 auto 10px";
				container.style.width =google_ads[0].image_width+"px";
				container.style.borderBottom = 0;
			}
		}	
	}

	//Create DIV Container 
	document.getElementById(ads_ID).appendChild(container); 
	
	if (google_ads.length == 0) {return;}

	//Create Ads By Google
	if (google_ads[0].type != "image"){ 
		var adBy = document.createElement('div'); //<div class="google-ads-by">...</div>
		adBy.className = 'google-ads-by';	
		headGoogle = '<a href="'+google_info.feedback_url+'" target="_blank">โฆษณาโดย Google</a>';
		adBy.innerHTML = headGoogle;
		container.appendChild(adBy);	
	}
	//-------------- Founction for Image Ads
	function google_ad_create_IMG_elem (google_ad){
		
		google_ad.url = google_ad.url.replace(/&amp;/g,"&");
		
		var ads = document.createElement('div'); //<div class="google-AdvertLnks">...</div>
	   		   ads.className = 'google-AdvertLnks';
			   ads.style.margin = 0;
			   ads.style.paddingTop = 0;
			   ads.style.paddingBottom = 0;
		var imgLink = document.createElement('a');
				imgLink.target = "_blank";
				imgLink.href = google_ad.url;
				imgLink.onmouseover = function () { window.status = google_ad.visible_url; return true; };
		var image =  document.createElement('img');
			   image.src= google_ads[0].image_url; 
			   image.width = google_ads[0].image_width;
			   image.height = google_ads[0].image_height;			   
			   imgLink.appendChild(image);
			   ads.appendChild(imgLink);
		return ads;	   
	}

//-------------- Founction for Text Ads
	function google_ad_create_elem (google_ad,count){
		// Google encodes "&"
		google_ad.url = google_ad.url.replace(/&amp;/g,"&");
		//		
		
		var ads = document.createElement('div'); //<div class="google-AdvertLnks">...</div>
				if(!displayBorderTop){	
					  ads.className = 'google-AdvertLnks';	
				}else{
					  ads.className = 'google-AdvertLnks '+'ad'+count;				
				}			
		
		var st = document.createElement('strong'); //<strong></strong>
		var Link1 = document.createElement('a');//<a></a>
				Link1.innerHTML = google_ad.line1;
				Link1.target = "_blank";
				Link1.href = google_ad.url;
				Link1.onmouseover = function () { window.status = google_ad.visible_url; return true; };
				st.appendChild(Link1);
				ads.appendChild(st);
				
		if(displayLandScape){				
			var span1 = document.createElement('span'); //<span></span>
			var Link2 = document.createElement('a');//<a></a>
					var i = document.createTextNode(google_ad.visible_url);
					Link2.target = "_blank";
					Link2.href = google_ad.url;
					Link2.onmouseover = function () { window.status = google_ad.visible_url; return true; };
					Link2.appendChild(i);
					span1.appendChild(Link2);
					ads.appendChild(span1);
	
			var span3 = document.createElement('span'); //<span></span>
				  span3.innerHTML = google_ad.line2 + ' ' + google_ad.line3; //show text description
				  ads.appendChild(span3);
		}else{
			var span3 = document.createElement('span'); //<span></span>
				  span3.innerHTML = google_ad.line2 + ' ' + google_ad.line3; //show text description
				  ads.appendChild(span3);
	
			var span1 = document.createElement('span'); //<span></span>
			var Link2 = document.createElement('a');//<a></a>
					var i = document.createTextNode(google_ad.visible_url);
					Link2.target = "_blank";
					Link2.href = google_ad.url;
					Link2.onmouseover = function () { window.status = google_ad.visible_url; return true; };
					Link2.appendChild(i);
					span1.appendChild(Link2);
					ads.appendChild(span1);	
		}			   
		return ads;	   
	}

//	alert('google_ads[0].type = '+google_ads[0].type);

	if (google_ads[0].bidtype == "CPC") {  /* insert this snippet for each ad call */
		google_adnum = google_adnum + google_ads.length;
	}
	
	if (google_ads[0].type == "image") {
		
		var ad_img;
		ad_img = google_ad_create_IMG_elem(google_ads[0]);
		container.appendChild(ad_img); 
		/*
		var ad = '';
		ad += '<a href=\"' +
google_info.feedback_url + '\" style="color:000000">Ads by Google</a><br> <a href="' +
google_ads[0].url + '" target="_top" title="go to ' +
google_ads[0].visible_url + '" onmouseout="window.status=\'\'" onmouseover="window.status=\'go to ' +
google_ads[0].visible_url + '\';return true"><img border="0" src="' +
google_ads[0].image_url + '"width="' +
google_ads[0].image_width + '"height="' +
google_ads[0].image_height + '"></a>';
		container.appendChild(ad);
		*/

	} else if (google_ads[0].type == "text") {
		//For Loop show Ad	
		var ad;
		var count=0;
		for (var i = 0; i < google_ads.length; i++)	{
			count = count+1;	
			ad = google_ad_create_elem(google_ads[i],count);
			container.appendChild(ad);
		}
		
		if(google_ads[0].bidtype == "cpc") {
			google_adnum = google_adnum + google_ads.length;	
	    }	
		
	}else if (google_ads[0].type == "flash") {
		var ad;
   		ad = '<div class=\"google-AdvertLnks\" style=\"margin: 0pt; padding-top: 0pt; padding-bottom: 0pt;\"><a href=\"' +
google_info.feedback_url + '\" style="color:#959595;font-weight:bold;font-size:11px;">โฆษณาโดย Google</a><br><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></div>';

		//container.appendChild(ad);
		document.getElementById(container.id).innerHTML = ad;

	}else if (google_ads[0].type == "html") {
	   var ad;
	   ad = '<div class=\"google-AdvertLnks\" style=\"margin: 0pt; padding-top: 0pt; padding-bottom: 0pt;\"><a href=\"' +
google_info.feedback_url + '\" style="color:#959595;font-weight:bold;font-size:11px;">โฆษณาโดย Google</a><br>' +
google_ads[0].snippet+'</div>';
	
		//container.appendChild(ad);
		document.getElementById(container.id).innerHTML = ad;
	}
	
}//End Function