function openPopUp( theURL, winName, features )
{
  var newWin = window.open( theURL, winName, features );
  newWin.focus();
  return false;
}

var pageid=Math.floor(Math.random()*1000000);

function injectBanner( sito, sezione, misura, pid, w, h, doc, pos )
{
  var now = new Date;
  var bumber = now.getTime();

  if ( !doc )
    doc = document;

  if ( !pos )
    pos = 1;

//  doc.write( '<scr' );
//  doc.write( 'ipt language="JavaScript1.1" type=text/javascript src=http://ads.dada.it/jserver/SITE=' + sito + '/AREA=' + sezione + '/AAMSZ=' + misura + '/ACC_RANDOM=' + bumber + '/PAGEID=' + pid + '/POS=' + pos + ' width=' + w + ' height=' + h + '></scr' );
//  doc.write( 'ipt>' );
}

function framedBanner( sito, sezione, misura, pid, w, h, frid, pos )
{
  if ( !pos )
    pos = 1;

  var now = new Date;
  var bumber = now.getTime();
  var burl = 'http://ads.dada.it/hserver/SITE=' + sito + '/AREA=' + sezione + '/AAMSZ=' + misura + '/ACC_RANDOM=' + bumber + '/PAGEID=' + pid + '/POS=' + pos;
  //document.write( "<iframe id='" + frid + "' name='" + frid + "' frameborder='0' marginwidth='0' marginheight='0' scrolling='no' width='" + w + "' height='" + h + "' src='" + burl + "'></iframe>" );
}

/* -----------------------------------*/
/* --->>> onDOMReady Extension <<<----*/
/* -----------------------------------*/


Object.extend(Event, {
	_domReady : function() {
		if(_isIE && !(document.readyState == "complete" || ($('g_chkend_body') && document.readyState == "interactive") ))
		{
			window.setTimeout(this._domReady.bind(this), 500);
			return;
		}
		if (arguments.callee.done || this._readyDone) return;
		arguments.callee.done = true;

		if (this._timer)  clearInterval(this._timer);

		for (var i=0;i < this._readyCallbacks.length;i++)
		{
			this._readyCallbacks[i]();
		}

		this._readyCallbacks = null;
		this._readyDone = true;
	},
	onDOMReady : function(f) {
		if(this._readyDone)
		{
			f();
			return;
		}

		if (!this._readyCallbacks) {
			var domReady = this._domReady.bind(this);
			
			if (document.addEventListener)
				document.addEventListener("DOMContentLoaded", domReady, false);

			if(_isIE)
			{
				document.write("<scr"+"ipt id=__ie_onload defer src=//0><\/scr"+"ipt>");
				document.getElementById("__ie_onload").onreadystatechange = function() {
					if (this.readyState == "complete") domReady(); 
				};
			}
			
			if (/WebKit/i.test(navigator.userAgent)) { 
				this._timer = setInterval(function() {
					if (/loaded|complete/.test(document.readyState)) domReady(); 
				}, 10);
			}
			
			Event.observe(window, 'load', domReady, false);
			Event._readyCallbacks =  [];
		}
		Event._readyCallbacks.push(f);
	}
});

function addLoadEvent( func, onlyWaitDOM )
{
	if(onlyWaitDOM)
	{
		Event.onDOMReady(func);
	}
	else
	{
		Event.observe( window, 'load', func, false );
  }
}

function addUnloadEvent( func )
{
  Event.observe( window, 'unload', func, false );
}

// it writes the right format number
function numFormat(num,type_us){
	try{
		num = String(num);
		if (type_us == 1){
		   num=num.replace(/\./g, ",")
		}
		return num;
	}
	catch(e){
	}
}
var JSON = {
	array: function (x) {
		var a = ['['], b, f, i, l = x.length, v;
		for (i = 0; i < l; i += 1) {
			v = x[i];
			f = JSON[typeof v];
			if (f) {
				v = f(v);
				if (typeof v == 'string') {
				   if (b) {
						a[a.length] = ',';
					}
					a[a.length] = v;
					b = true;
				}
			}
		}
		a[a.length] = ']';
		return a.join('');
	},
	'boolean': function (x) {
		return String(x);
	},
	'null': function (x) {
		return "null";
	},
	number: function (x) {
		return isFinite(x) ? String(x) : 'null';
	},
	object: function (x) {
		if (x) {
			if (x instanceof Array) {
				return JSON.array(x);
			}
			var a = ['{'], b, f, i, v;
			for (i in x) {
				v = x[i];
				f = JSON[typeof v];
				if (f) {
					v = f(v);
					if (typeof v == 'string') {
						if (b) {
							a[a.length] = ',';
						}
						a.push(JSON.string(i), ':', v);
						b = true;
					}
				}
			}
			a[a.length] = '}';
			return a.join('');
		}
		return 'null';
	},
	string: function (x) {
		var  m = {
			'\b': '\\b',
			'\t': '\\t',
			'\n': '\\n',
			'\f': '\\f',
			'\r': '\\r',
			'"' : '\\"',
			'\\': '\\\\'
		};
		if (/["\\\x00-\x1f]/.test(x)) {
			x = x.replace(/([\x00-\x1f\\"])/g, function(a, b) {
				var c = m[b];
				if (c) {
					return c;
				}
				c = b.charCodeAt();
				return '\\u00' +
				Math.floor(c / 16).toString(16) +
				(c % 16).toString(16);
			});
		}
		return '"' + x + '"';
	},
	
	toJSONString: function (obj) {
		var f = JSON[typeof obj];
		if (f)
			return(f(obj));
	},
	
	parseJSON: function (str) {
		try {
			return (/^("(\\.|[^"\\\n\r])*?"|[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t])+?$/.test(str)) &&
				eval('(' + str + ')');
		} catch (e) {
			return false;
		}
	}
};

function getImgGridPos(c) {
	var gSi = 64;
	var iSi = 18;
	var sta = {b: [0,0], o: [0,1], h: [0,2], d: [1,0]};

	//a_1_d
	c = String(c).toLowerCase();
	var tmp = c.split("_");
	var res = new Array(2);
	res[0] = -gSi*(tmp[0].charCodeAt(0)-97);	//Colonna parte da a
	res[1] = -gSi*(Number(tmp[1])-1);	//Riga parte da 1

	if(tmp[2] && sta[tmp[2]])
	{
		res[0] += -iSi*sta[tmp[2]][0];
		res[1] += -iSi*sta[tmp[2]][1];
	}

	return res;
};

function createImgGridPos(c) {
	var im = Builder.node('img', {'border': 0, 'class': 'psi', 'src': _imgDestDomain+'/general/pxl.gif'} );
	var ofs = getImgGridPos(c);
	im.style.backgroundPosition = ofs[0]+"px "+ofs[1]+"px";
	return im;
};

function setImgGridPos(im,c) {
	var ofs = getImgGridPos(c);
	im.style.backgroundPosition = ofs[0]+"px "+ofs[1]+"px";
};

function createButton(cColor, bLabel, small) {
	var foo, cClass, size, w, h;

	if(small)
	{
		size = 'bb';
		w = 7;
		h = 17;
	}
	else
	{
		size = 'ab';
		w = 9;
		h = 19;
	}

	switch(cColor)
	{
		case 'white':
			cClass = size+'W';
			break;

		case 'blue':
			cClass = size+'B';
			break;

		case 'blueAnim':
			cClass = size+'BA';
			break;

		case 'red':
		default:
			cClass = size+'R';
			break;
	}

	foo = document.createElement('a');
	foo.setAttribute(_isIE ? 'className' : 'class', (small ? 'barButton ' : 'addButton ')+cClass);
	
	var img = document.createElement('img');
	img.setAttribute('src',_imgDestDomain+"/general/buttons/"+cClass+"-left.gif");
	img.setAttribute('width',w);
	img.setAttribute('height',h);
	foo.appendChild(img);

	var spn = document.createElement('span');
//	spn.appendChild(document.createTextNode(String(chromeDictionary.getEntry(bLabel))));
	spn.appendChild(document.createTextNode(bLabel));
	foo.appendChild(spn);

	img = document.createElement('img');
	img.setAttribute('src',_imgDestDomain+"/general/buttons/"+cClass+"-right.gif");
	img.setAttribute('width',w);
	img.setAttribute('height',h);
	foo.appendChild(img);

	foo.style.cursor = _IEhack ? 'hand' : 'pointer';

	return(foo);
}

function createToolButton(bImg, bLabel) {
	var foo =  document.createElement('a');
	foo.setAttribute(_isIE ? 'className' : 'class','chromeTitleB');
	foo.setAttribute('style',"background-image: url("+_imgDestDomain+"/general/chrome/default/"+bImg+".gif);");
//	foo.setAttribute('title',String(chromeDictionary.getEntry(bLabel)));
	foo.setAttribute('title',bLabel);

	foo.style.cursor = _IEhack ? 'hand' : 'pointer';

	return(foo);
}

/***************************************
 * Cookie handling function
 ***************************************/

function getCookie(name) {
	var dc = document.cookie;
	var prefix = name + "=";
	var begin = dc.indexOf("; " + prefix);
	if (begin == -1) {
		begin = dc.indexOf(prefix);
		if (begin != 0) return null;
	} else {
		begin += 2;
	}
	var end = document.cookie.indexOf(";", begin);
	if (end == -1) {
		end = dc.length;
	}
	return decodeURIComponent(dc.substring(begin + prefix.length, end));
}

function setCookie(name, value, expires, path, domain, secure) {
	var now = new Date();
	var oExpires = new Date();
	oExpires.setTime(now.getTime() + expires*1000);
	document.cookie = name + "=" + encodeURIComponent(value) +
		((expires) ? "; expires=" + oExpires.toGMTString() : "") +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		((secure) ? "; secure" : "");
}

function delCookie(name, path, domain) {
	document.cookie = name + "=" +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		"; expires=Thu, 01-Jan-70 00:00:01 GMT";
}

function getCookieDomain() {
	if(_ISStaging)
	{
		console.log("call to getCookieDomain");
	}

	if(/dada\.net$/.test(window.location.hostname))
		return(".dada.net");
	else if(/dada\.it$/.test(window.location.hostname))
		return(".dada.it");
	else if(/playme\.com$/.test(window.location.hostname))
		return(".playme.com");
	else
		return(window.location.hostname);
}

function getURLParams() {
	var res = {};
	var s = String(window.location.search).substr(1);
	if(s)
	{
		var p = s.split("&");
		for(var i = 0; i<p.length; i++)
		{
			var q = p[i].split("=");
			res[q[0]] = decodeURIComponent(q[1]);
		}
	}
	return res;
}
