var _w	= window;
var _d	= document;
function forbid_link_focus()
{
	var links	= _d.getElementsByTagName("a");
	for(var i=0; i<links.length; i++) {
		links[i].onfocus	= function() { this.blur(); };
	}
}

function ajax_init()
{
	var req = false;
	if (_w.XMLHttpRequest) {
		req = new XMLHttpRequest();
		if (req.overrideMimeType) { req.overrideMimeType("text/plain"); }
	} else if (_w.ActiveXObject) {
		try { req = new _w.ActiveXObject("MSXML3.XMLHTTP"); } catch(e) {
		try { req = new _w.ActiveXObject("MSXML2.XMLHTTP.3.0"); } catch(e) {
		try { req = new _w.ActiveXObject("Msxml2.XMLHTTP"); } catch(e) {
		try { req = new _w.ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {
		}}}}
	}
	return req;
}

function keep_session()
{
	var req = ajax_init();
	if( ! req ) {
		return false;
	}
	req.onreadystatechange = function() {};
	req.open("GET", "/ajax/noop.php?r="+Math.round(Math.random()*1000), true);
	req.send("");
}

function ie_fix_some_hovers()
{
	if( navigator.appName != "Microsoft Internet Explorer" ) {
		return;
	}
	if( ! _d.getElementById("centercol") ) {
		return;
	}
	var dv	= _d.getElementById("centercol").getElementsByTagName("DIV");
	for(var i=0; i<dv.length; i++) {
		if( dv[i].className != "thmbdvhvr" ) {
			continue;
		}
		dv[i].onmouseover	= function() { this.className = "thmbdvhvr_on"; };
		dv[i].onmouseout	= function() { this.className = "thmbdvhvr"; };
	}
}

function preload_editor()
{
	if( ! _d.body ) { return; }
	var req = ajax_init();
	if( ! req ) { return; }
	req.onreadystatechange = function() {
		if( req.readyState!=4 || !req.responseText || req.responseText=="LOADED" ) { return; }
		var ifr	= _d.createElement("IFRAME");
		ifr.style.display	= "none";
		ifr.style.zIndex	= "-1";
		ifr.style.width	= "0px";
		ifr.style.height	= "0px";
		var ifrlocation	= "/editor/site_preload.php?r="+Math.round(Math.random()*1000);
		var ifrinitbug	= navigator.userAgent.toLowerCase().indexOf("opera 8.") != -1;
		if( ifrinitbug ) {
			ifr.src	= ifrlocation;
		}
		var frnm	= _w.frames.length;
		_d.body.appendChild(ifr);
		if( ! ifrinitbug ) {
			_w.frames[frnm].location	= ifrlocation;
		}
	};
	req.open("GET", "/ajax/preload_editorfiles.php?r="+Math.round(Math.random()*1000), true);
	req.send("");
}
function preload_editor_done()
{
	var req = ajax_init();
	if( ! req ) { return; }
	req.onreadystatechange = function() {};
	req.open("GET", "/ajax/preload_editorfiles.php?loaded=1&r="+Math.round(Math.random()*1000), true);
	req.send("");
}

_w.onload	= function()
{
	ie_fix_some_hovers();
	forbid_link_focus();
//	preload_editor();
	setInterval( keep_session, 120000);
};

function preload()
{
	var tmp	= [];
	for(var i=0; i<arguments.length; i++) {
		tmp[i]	= new Image();
		tmp[i].src	= arguments[i];
	}
}

var edt;
var edttm	= false;
function init_editor(div_id, submit_url, config, seltab, tpl_n, tpl_w, tpl_h, action, extraconf)
{
	if( ! edttm ) {
		edttm	= new Date().getTime();
	}
	if( new Date().getTime()-edttm > 10000 ) {
		location.reload(true);
		return false;
	}
	var Obj	= false;
	try { Obj = Editor; } catch(e) {}
	if( ! Obj ) {
		setTimeout( function() { init_editor(div_id, submit_url, config, seltab, tpl_n, tpl_w, tpl_h, action, extraconf); }, 0 );
		return false;
	}
	edt	= new Obj(tpl_n, tpl_w, tpl_h);
	if( action !== undefined ) {
		edt.action	= action;
	}
	edt.INIT(div_id, submit_url, config, seltab, extraconf);
}

function get_screen_scroll()
{
	var x=0, y=0;
	if( typeof( window.pageYOffset ) == 'number' ) {
		x	= window.pageXOffset;
		y	= window.pageYOffset;
	} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		x	= document.body.scrollLeft;
		y	= document.body.scrollTop;
	} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		y	= document.documentElement.scrollTop;
		x	= document.documentElement.scrollLeft;
	}
	return [x, y];
}

function show_banner(bid, bsize, e)
{
	if( ! document.body ) {
		return;
	}
	if( ! document.getElementById("banner_preview") ) {
		var dv	= document.createElement("DIV");
		dv.id	= "banner_preview";
		dv.style.display	= "none";
		dv.style.backgroundColor	= "#ebebeb";
		dv.onmouseover	= hide_banner;
		document.body.appendChild(dv);
	}
	bsize	= bsize.split("x");
	var w	= parseInt(bsize[0], 10);
	var h	= parseInt(bsize[1], 10);
	var ifr	= document.createElement("IFRAME");
	ifr.style.width	= w+"px";
	ifr.style.height	= h+"px";
	ifr.style.border	= "0px solid";
	ifr.style.margin	= "0px";
	ifr.style.padding	= "0px";
	ifr.setAttribute("width",	w);
	ifr.setAttribute("height",	h);
	ifr.setAttribute("frameBorder",	"0");
	ifr.setAttribute("scrolling",	"no");
	ifr.src	= "http://banner.sedem.bg/test.php?bnr_id="+bid;
	
	with( document.getElementById("banner_preview") ) {
		while(firstChild) { removeChild(firstChild); }
		appendChild(ifr);
		style.position	= "absolute";
		style.border	= "5px double #4d4d4d";
		style.width	= w + "px";
		style.height	= h + "px";
		var scr	= get_screen_scroll();
		style.left	= (e.clientX + 20 + scr[0]) + "px";
		style.top	= (e.clientY + 15 + scr[1]) + "px";
		if( e.clientY + h + 100 > screen.height ) {
			style.top	= Math.max(50, (screen.height-h-200+scr[1])) + "px";
		}
		style.display	= "";
	}
}
function hide_banner()
{
	if( ! document.getElementById("banner_preview") ) {
		return;
	}
	with( document.getElementById("banner_preview") ) {
		style.display	= "none";
		while(firstChild) { removeChild(firstChild); }
	}
}