var advertising = {
	
	ad_call : null,
	
	// add call
	server : /^http\:\/\/ads\./,
	
	// empty ad test
	empty_ad : /((f{7}[a-e]{1})+|(AE[0-9]+\.gif))/g,
	
	render : function(value)
	{
		var invObj = 'INV' + value;
        var direct = false;
        var iframe = false;
        var code   = false;
        
		try {
			code = eval(value);
		} catch(er) { }
		
		if (typeof(code) == 'string') {
			var children = /\<([a-z]+)\s/i.exec(code);
			if (children[1] && ['iframe', 'a', 'img'].toString().indexOf(children[1].toLowerCase()) > -1) {
				direct = true;	
			}
            if (children[1] && ['iframe'].toString().indexOf(children[1].toLowerCase()) > - 1) {
                iframe = true;
            }
		}
		
		if (direct === true) {
            var container = $('div#' + value);
            if (iframe) {
                container.hide();
            }
            container.html(code);
            if (iframe) {
                if ($('iframe', container).length > 0) {
                    $('iframe', container)[0].src = $('div#' + value + ' iframe')[0].src;
                }
                container.show();
            }
			// INSERT
		} else {
			// COPY
			if (typeof(code) != 'undefined') {
				if (navigator.userAgent.indexOf('MSIE') > -1) {
					// IE
					document.getElementById(value).innerHTML =
						'<div id="TMP' + value + '" style="display:none">' +
						escape('<body><div id="adDiv">' + code + '</div>') +
						'</div><iframe name="' + invObj + '" width="0" height="0" frameborder="0" ' +
						'onload="advertising.relocateAd(this, \'' + value + '\');"></iframe>';
					window.frames[invObj].document.location = 'javascript:unescape(parent.document.getElementById(\'TMP' + value + '\').innerHTML)';
				} else {
					document.writeln('<div id="' + invObj + 
						'" style="display:none">' + code + 
						'<script type="text/javascript" defer="true">' +'document.getElementById(\'' + 
						value + 
						'\').innerHTML = ' + 
						'document.getElementById(\'' + 
						invObj + 
						'\').innerHTML;' + 
						'document.getElementById(\'' + invObj +'\').innerHTML = \'\';</script></div>');
				}
			}
		}
	},

	sponsor : function()
	{
		if (this.empty_ad.test(AAMB1) === false) {
            $('div#AAMB1').before(
                "<div>This printable version is sponsored by:</div>"
            );
		}
	},
	
	sponsor_text : function()
	{
		if (window.$ !== undefined){
			$(window).load(function(){
				$('div.searchTextAd').wrap('<div class="textAds"></div>').before('<h3>Sponsored Link</h3>');
				if (document.getElementById('main')) {
					// re-adjust column
					changeHeight('main');
				}
			});
		}
	},
	
	place : function()
	{
		for (aax=0; aax<AAMB.length; aax++) {
			if (this.empty_ad.test(eval(AAMB[aax])) === false && document.getElementById(AAMB[aax]) !== null) {
				// place ad
				this.render(AAMB[aax]);
				// force ad size
				if (/[4567]{1}$/.test(AAMB[aax]) === true) {
					// get set size
					aamsz = /[0-9]+X([0-9]+)/.exec(aamszArray[AAMB[aax]]);
					if (aamsz !== null) {
						document.getElementById(AAMB[aax]).parentNode.style.height = 'auto';
						document.getElementById(AAMB[aax]).style.height = aamsz[1] + "px";
					}
				}
			}
		}
	},
	
	relocateAd : function(iframeObj, divName) 
	{
		var iframeDoc, 
		allScripts; 
		iframeDoc = iframeObj.contentWindow.document; 
		allScripts = iframeDoc.getElementsByTagName('script'); 
		for (s = 0; s < allScripts.length; s++) { 
			if (allScripts[s].src) { 
			    allScripts[s].src = '';
            }
        }
		document.getElementById(divName).insertAdjacentElement('beforeEnd', iframeDoc.getElementById('adDiv')); 
	}
};
