/**
 * @author gsandrigo
 * 
 */
function MoronJSON(){
   this.noCacheIE = '&noCacheIE=' + (new Date()).getTime();
   this.head = document.getElementsByTagName("head").item(0);
   this.scriptId = 'JscriptId' + MoronJSON.counter++;
}

MoronJSON.counter = 1;

MoronJSON.prototype.sendBegin = function(url, callback){      
   url += (url.indexOf("?") == -1) ? "?callback=" : "&callback=";
   url += callback;
   url += this.noCacheIE;
   this.script = document.createElement("script");
   this.script.setAttribute("type", "text/javascript");
   this.script.setAttribute("charset", "utf-8");
   this.script.setAttribute("src", url);
   this.script.setAttribute("id", this.scriptId);
   this.head.appendChild(this.script);
}

MoronJSON.prototype.sendEnd = function(){
   //this.head.removeChild(this.script);
}

function GoLogin(){ 
	var thisURL = location.href;  
	// document.location = "/login/?AppRedirect=" +thisURL; 
	document.location = "/login/"; 
}
function ratingCallBack(){
	document.getElementById("rtngMsg").innerHTML = "Thank you!";
}


function dump(o){
	var strData = "Object data:\n";
	       $H(o).each(function(pair) { 
		strData += pair.key + ": " + pair.value + "\n";
	  }
	);
	alert(strData);
}



function reSize()
{
	try{	
	var oBody	=	adview.document.body;
	var oFrame	=	document.all("adview");
		
	oFrame.style.height = oBody.scrollHeight + (oBody.offsetHeight - oBody.clientHeight);
	oFrame.style.width = oBody.scrollWidth + (oBody.offsetWidth - oBody.clientWidth);

		// v.style.width = width + 'px';
		// v.style.height = height + 'px';
	}
	//An error is raised if the IFrame domain != its container's domain
	catch(e)
	{
	window.status =	'Error: ' + e.number + '; ' + e.description;
	}
}



	var getFFVersion=navigator.userAgent.substring(navigator.userAgent.indexOf("Firefox")).split("/")[1];
	var FFextraHeight=parseFloat(getFFVersion)>=0.1? 16 : 0; //extra height in px to add to iframe in FireFox 1.0+ browsers

	function resizeCaller() {

		var iframeids=["adview"];

		// iframeids = 'adview';
		// iframeids = iframeids.split(",");
		

		var iframehide = "yes";
		var dyniframe=new Array();
		for (i=0; i<iframeids.length; i++){
			if (document.getElementById) resizeIframe(iframeids[i])



			//reveal iframe for lower end browsers? (see var above):


			if ((document.all || document.getElementById) && iframehide=="no"){
				var tempobj=document.all? document.all[iframeids[i]] : document.getElementById(iframeids[i])
				tempobj.style.display="block"
			}
		}
	}



	function resizeIframe(frameid){
		var currentfr=document.getElementById(frameid);
		if (currentfr && !window.opera){
			currentfr.style.display="block"
			if (currentfr.contentDocument && currentfr.contentDocument.body.offsetHeight){ //ns6 syntax
				currentfr.height = currentfr.contentDocument.body.offsetHeight+FFextraHeight; 
			}else if (currentfr.Document && currentfr.Document.body.scrollHeight){ //ie5+ syntax
				currentfr.height = currentfr.Document.body.scrollHeight;
			}
			if (currentfr.addEventListener){
				currentfr.addEventListener("load", readjustIframe, false)
			}else if (currentfr.attachEvent){
				currentfr.detachEvent("onload", readjustIframe); // Bug fix line
				currentfr.attachEvent("onload", readjustIframe);
			}
		}
	}

	function readjustIframe(loadevt) {
		var crossevt=(window.event)? event : loadevt;
		var iframeroot=(crossevt.currentTarget)? crossevt.currentTarget : crossevt.srcElement;
		if (iframeroot) resizeIframe(iframeroot.id);
	}

	function loadintoIframe(iframeid, url){
		if (document.getElementById) document.getElementById(iframeid).src=url;
	}





function ListResize(lst,maxsize){
	var out = new Array();
	var arr = lst.split(',');
	// alert(lst);
	for (var i=0; i<arr.length; i++){
		if((i+1) <= maxsize ){
			out[out.length] = arr[i];
		}
	}
	return out.join(',');
}


function ListRemoveNulls(lst){
	var out = new Array();
	var arr = lst.split(',');
	for (var i=0; i<arr.length; i++){
		if(arr[i] != ''){
			out[out.length] = arr[i];
		}
	}
	return out.join(',');
}


function adThing(){
	
	//private vars
	var that = this;

	// public vars
	this.contentId = 1;
	this.maxcats = 3;
	this.isUserLoggedIn = false;
	this.showText = false;
	this.isOwnerWatching = false;
	this.isRatingOn = true;
	this.thecode;
	this.theform;
	this.caller;
	this.a = init();

	// private methods
	function nothis(){ }


	function init(){
		var n=null;
		var r=null;
		var c=null;

		var x = new Object();

		x.user = $F('opt_user');
		x.cbid = $F('opt_cbid');

		x.folder = $F('opt_folder');
		x.ad_output = $F('opt_ad_output');
		x.rotate = $F('opt_rotate');

		c = $F('opt_cat');
		x.cat = c;

		// c = ListRemoveNulls(c);
		// c = ListResize(c,3);
		// alert(c);

		if(x.folder != ''){
			$('opt_cat').disable();
			x.cat = '';
		}else{
			$('opt_cat').enable();
		}


		x.tracking = $F('opt_tracking');


		x.show = ''; 
		x.cssid = ''; 

		if($('opt_show')){ x.show = $F('opt_show'); }
		if($('opt_cssid')){ x.cssid = $F('opt_cssid'); }



		x.kw = $F('opt_kw');
		x.ad_target = $F('opt_ad_target');
		x.ad_format = $F('opt_ad_format');

		r = x.ad_format.split("x");
		x.ad_width = r[0];
		x.ad_height = r[1];

		x.color_border = $F('opt_color_border');
		x.color_bg = $F('opt_color_bg');
		x.color_link = $F('opt_color_link');
		x.color_url = $F('opt_color_url');
		x.color_text = $F('opt_color_text');
		x.alternate_ad_url = $F('opt_alternate_ad_url');

		this.a = x;

		return x;
	}

	//public methods


	this.createID = function () {
		return new Date().getTime() + "" + 1; 
	}

	this.createAdCode = function () {
		var a = init();
		var s = '';
		var h = '';
		$H(a).each(function(pair) { 
			   if(a[pair.key] != ''){
				h += 'cbengine_'+pair.key+' = "' + a[pair.key] + '";' + '\n';
			   }
			}
		);
		s += '<script type="text/javascript"><!--' + '\n';
		s += h;
		s += '//-->' + '\n';
		s += '</script>' + '\n';
		s += '<script type="text/javascript" src="' + that.caller +'"></script>' + '\n';
		$('newcode').value = s;
		return s;
	}

	this.hideStartCode = function () {
		$('adcode').innerHTML = '';
		$('adcode').hide();
	}


	this.initFrame = function () {
		var a = init();
		var pars = that.a;
		var h = $H(pars);
		h['random'] = this.createID();
		var qs = h.toQueryString();
		// alert(qs); 

		var fsrc = that.caller.replace('show_ads.js','frame.cfm');
		fsrc += "?"+qs;

		this.hideStartCode();

		var width = Number(a.ad_width);
		var height = Number(a.ad_height);
		var height = Number(a.ad_height) + 50;
		var style = "width:"+width+"px; height:"+height+"px;";

		var v = $('adview');
		v.setAttribute('src',fsrc);
		v.style.width = width + 'px';
		v.style.height = height + 'px';


		// v.src = fsrc;
	}


	this.render = function () {
		this.createAdCode();
		this.initFrame();
		return false;
	}

}









