/*Work type */
var workType = '';

/*Check whether function exists*/
function functionExists(funcName) {
	return (window[funcName] && typeof window[funcName] == 'function');
}

/*Define google_ad_request_done if function does not exist*/
if (!functionExists('google_ad_request_done')) {
	google_ad_request_done = function(google_ads) {
		document.write(getGoogleAdsHtml(google_ads));		
		return;
	}
}

/*Define google_afs_request_done if function does not exist*/
if (!functionExists('google_afs_request_done')) {
	// define it
	google_afs_request_done = function(google_ads) {
		document.write(getGoogleAdsHtml(google_ads));
		return;
	}
}

/*Display fetched Google advertisements*/
function showGoogleAfc(type) {
        if('Top' == type) {
                workType = 'Top';
        }
        if('Bot' == type) {
                workType = 'Bot';
        }
        if('w' == type) {
                workType = 'AFCw';
        }
        if('w' != type && 'Top' != type && 'Bot' != type){
                workType = 'AFC';
        }	
	document.write('<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>');
}

function showGoogleAfs(type) {
	if (!type) {
		workType = 'AFS';
		document.write('<script type="text/javascript" src="http://www.google.com/afsonline/show_afs_ads.js"></script>');
	}
	if ('posting_top' == type) {
		workType = 'AFSpt';
		document.write('<script type="text/javascript" src="http://www.google.com/afsonline/show_afs_ads.js"></script>');
	}	
	if ('posting_bottom' == type) {
		workType = 'AFSpb';
		document.write('<script type="text/javascript" src="http://www.google.com/afsonline/show_afs_ads.js"></script>');
	}
}

/*Get HTMl code do display ads*/
function getGoogleAdsHtml(google_ads) {
	var html = '';
        //console.log([google_ads, workType]);
		//window.alert(workType); 
        if ('Top' == workType || 'AFSpt' == workType) {
			if (google_ads.length > 0) {
			var feedback_url = (typeof(google_info) != 'undefined') ? google_info.feedback_url : 'http://services.google.com/feedback/online_hws_feedback';			
			var local_link = '<a href="' + feedback_url + '" >' + ads_by_google_local + '</a>';
			var fontSize = (google_ads.length == 1) ? ' class="fs14"' : '';
			var rowclass = '';
	
//TOP OF AD PAGES	
			var html = '<div class="header">' +
					'<span><a href="' + feedback_url + '" >' + ads_by_google_local + '</a></span>' +
						'</div>';
	        var border_class;
            if ('Top' == workType) {
                border_class = 'post_page_border';
            } else {
                border_class = 'post_border';
            }
			for (i = 0; i < google_ads.length; ++i) {
				rowclass = 'postingRow0';			
				if (google_ads[i].type != 'image') {
					html += '<div class="ad">' +		
							'<div class="gholder">' +
							'<a href="' + google_ads[i].url + '" >' + google_ads[i].line1 + '</a>' +
                            '&nbsp;' + (google_ads[i].line2 ? (google_ads[i].line2 + ' ') : '') +
                            (google_ads[i].line3 ? (google_ads[i].line3) : '') + '' +							
							'&nbsp;<a href="' + google_ads[i].url + '" >' + google_ads[i].visible_url + '</a>' +	
						'</div></div>';
				}	
			}	
		}
        }
		
        if ('Bot' == workType || 'AFSpb' == workType) {
                if (google_ads.length > 0) {
			var feedback_url = (typeof(google_info) != 'undefined') ? google_info.feedback_url : 'http://services.google.com/feedback/online_hws_feedback';			
			var local_link = '<a href="' + feedback_url + '" >' + ads_by_google_local + '</a>';
			var fontSize = (google_ads.length == 1) ? ' class="fs14"' : '';
			var rowclass = '';

//BOTTOM OF AD PAGES
			var html = '<div class="header">' +
					'<span><a href="' + feedback_url + '" >' + ads_by_google_local + '</a></span>' +
						'</div>';
            var border_class;
            if ('Bot' == workType) {
                border_class = 'post_page_border';
            } else {
                border_class = 'post_border';
            }
			for (i = 0; i < google_ads.length; ++i) {
				rowclass = 'postingRow0';			
				if (google_ads[i].type != 'image') {

					html += '<div class="ad">' +						
							'<div class="gholder">' +
							'<a href="' + google_ads[i].url + '" >' + google_ads[i].line1 + '' + '</a>' +
                            '&nbsp;' + (google_ads[i].line2 ? (google_ads[i].line2 + ' ') : '') +
                            (google_ads[i].line3 ? (google_ads[i].line3) : '') +
							'&nbsp;<a href="' + google_ads[i].url + '" >' + google_ads[i].visible_url + '</a>' +	
							'</div>' +
						'</div>';
				}	
			}
		}
        }
//LISTING PAGES
        if ('AFS' == workType || 'AFCw' == workType) {
		if (google_ads.length > 0) {
			var feedback_url = (typeof(google_info) != 'undefined') ? google_info.feedback_url : 'http://services.google.com/feedback/online_hws_feedback';			
			var local_link = '<div><a href="' + feedback_url + '" >' + ads_by_google_local + '</a></div>';
			var fontSize = (google_ads.length == 1) ? ' class="fs14"' : '';
			var rowclass = '';
			var html = '<div class="header abscorners lightyellow">' +
							'<span><a href="' + feedback_url + '" >' + ads_by_google_local + '</a></span>' +
							'<div class="tl"></div>' +
							'<div class="tr"></div>' +
							'<div class="bl"></div>' +
							'<div class="br"></div>' +
						'</div>';
			for (i = 0; i < google_ads.length; ++i) {
				rowclass = 'postingRow0';				
				if (google_ads[i].type != 'image') {
					html += '<div class="ad">' +
								'<div class="gholder">' +
									'<a href="' + google_ads[i].url + '" >' + google_ads[i].line1 + '</a>' +	
									(google_ads[i].line2 ? ('<div>' + google_ads[i].line2) + ' ': '') +
									'<a href="' + google_ads[i].url + '" >' + google_ads[i].visible_url + '</a></div>' +
								'</div>' +
							'</div>';
				}
			}
		}
	}
	if ('AFC' == workType) {
		if (google_ads.length > 0) {
			var feedback_url = (typeof(google_info) != 'undefined') ? google_info.feedback_url : 'http://services.google.com/feedback/online_hws_feedback';
                        if (typeof(ads_by_google_local) == 'undefined') {
                                ads_by_google_local = 'Google';
                        }
			var local_link = '<a href="' + feedback_url + '" >' + ads_by_google_local + '</a>';
			var fontSize = (google_ads.length == 1) ? ' class="fs14"' : '';
			var rowclass = '';
                        if (typeof(margin_top) == 'undefined') {
                                margin_top = '0px';
                        }
			var html = '<table style="width: 160px; max-width: 160px; margin-top: ' + margin_top + '">';
			for (i = 0; i < google_ads.length; ++i) {
				rowclass = ((i % 2) == 1) ? 'postingRow1' : 'postingRow0';			
				if (google_ads[i].type != 'image') {
					html += '<tr class="' + rowclass + '">' +
							'<td align="left">' +
							'<a href="' + google_ads[i].url + '" >' + google_ads[i].line1 + '</a><br />' +
							(google_ads[i].line2 ? (google_ads[i].line2 + ' ') : '') +
							(google_ads[i].line3 ? (google_ads[i].line3) : '') + '<br />' +
							'<span><a href="' + google_ads[i].url + '" >' + google_ads[i].visible_url + '</a></span>' +
							'</td>' +
						'</tr><tr><td>&nbsp;</td></tr>';
				}
			}
		        html += '<tr class="rowLevel1" style="text-align: right;">' +
					'<td><a style="text-decoration: none;" href="' + feedback_url + '" >' + ads_by_google_local + '</a></td>' +
					'</tr>';
			html += '</table>';
		}
	}
	return html;
}