window.onload = function () {
	var cssstyles=document.styleSheets;
	var gc=0;
	for(var k=0;k< document.styleSheets.length;k++) {
		if (document.styleSheets[k].href && document.styleSheets[k].href.indexOf('global.css')>-1) gc++;
	}
	if (!gc) {
		var fileref=document.createElement("link");
		fileref.setAttribute("rel", "stylesheet");
		fileref.setAttribute("type", "text/css");
		fileref.setAttribute("href", "/css/global.css");
		document.getElementsByTagName("head")[0].appendChild(fileref);
	
	}
}


function openPopup(u,w,h)
{
  popleft = Math.round(screen.availWidth/2)  - Math.round(w/2);
  poptop  = Math.round(screen.availHeight/2) - Math.round(h/2);
  popup = window.open(u,'popup_window','width='+w+',height='+h+',location=0,status=1,toolbar=0,resize=0,scrollbars=1,top='+poptop+',left='+popleft);
  popup.focus();
}
var w_t;
var h_t;
var prepic;
var prevkeyhandler;

function windowsize()
{
	
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
	dscroll=document.body.style.overflow;
	document.body.style.overflow='hidden';
	pre_w=document.body.clientWidth;
	document.body.style.overflow=dscroll;
	w_scroll=pre_w-document.body.clientWidth;
    myWidth = window.innerWidth-w_scroll;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }

	return {width:myWidth,height:myHeight};
}

function keyhandle(ev){

ev=ev||event||document.getElementById('poupdiv_container').contentWindow.event;
if(ev.keyCode==27){
closediv();
return false;
}
}

function createCSS(selector, declaration) {
	// test for IE
	var ua = navigator.userAgent.toLowerCase();
	var isIE = (/msie/.test(ua)) && !(/opera/.test(ua)) && (/win/.test(ua));

	// create the style node for all browsers
	var style_node = document.createElement("style");
	style_node.setAttribute("type", "text/css");
	style_node.setAttribute("media", "screen"); 

	// append a rule for good browsers
	if (!isIE) style_node.appendChild(document.createTextNode(selector + " {" + declaration + "}"));

	// append the style node
	document.getElementsByTagName("head")[0].appendChild(style_node);

	// use alternative methods for IE
	if (isIE && document.styleSheets && document.styleSheets.length > 0) {
		var last_style_node = document.styleSheets[document.styleSheets.length - 1];
		if (typeof(last_style_node.addRule) == "object") last_style_node.addRule(selector, declaration);
	}
};

function openDiv(u,w,h, show_control) {

	w=parseInt(w);
	h=parseInt(h);
	w_t=w;
	h_t=h;
	
	// by default we need true, we cannot use || trick here becouse 2 param false  
	if (typeof show_control == "undefined") {
    	show_control = true;
  	}
	
	if (!show_control) {
		var popupdivclass = 'donotshow';	
	}
	
	if (typeof show_control == "object") {
		var popupdivclass = show_control['div'];
		var popupbuttonclass = show_control['button'];
	}


	
	var cdiv = document.createElement('div');
	cdiv.setAttribute('id', 'popupdiv');

	popleft = Math.round(windowsize().width/2)  - Math.round(w/2);
	poptop  = Math.round(windowsize().height/2) - Math.round(h/2);
	cdiv.setAttribute("style", 'width:'+w+'px; height:'+h+'px; top:'+poptop+'px; left:'+popleft+'px');
	cdiv.style.cssText = 'width:'+w+'px; height:'+h+'px; top:'+poptop+'px; left:'+popleft+'px';
	if (popupdivclass) {
		cdiv.className = popupdivclass;	
	}	
	eval_js = false;
	prevkeyhandler=document.onkeypress;
	document.onkeypress=keyhandle;

	
	// check that 1 param is URL
	if(u.substr(0,1) == '?' || u.substr(0,1) == '/' || u.substr(0,4) == 'http'){
		var link = document.createElement('iframe');
		link.setAttribute('src', u);
		link.setAttribute('frameBorder', '0');
		link.setAttribute('border', '0');
		link.setAttribute('id', 'poupdiv_container');
		
	}else{
		var link = document.createElement('div');
		link.innerHTML = u;
		eval_js = true;
	}
	
	if(show_control !== false){
		

		// init top top-coltrol bar
		var topdiv = document.createElement('a');
		topdiv.setAttribute('id', 'popupdiv_close');
		topdiv.setAttribute('href', 'javascript:closediv();');
		if (popupbuttonclass) {
			topdiv.className = popupbuttonclass;	
		}

	}else{
		control_bar_height = 0;
	}
	
	// default styles
	link.setAttribute("style", 'border:0 none ;height:'+h+'px; width:'+w+'px;');
	link.style.cssText = 'border:0 none;height:'+h+'px; width:'+w+'px;';	

	var bgdiv = document.createElement('div');
	bgdiv.setAttribute('id', 'bgdiv');
	bgdiv.setAttribute("style", 'position: fixed; top: 0; left: 0; overflow: hidden; padding: 0; margin: 0; background-color: #000; filter: alpha(opacity=60); opacity: 0.5; display: block; z-index: 1000; width: 100%; height: 100%');
	bgdiv.style.cssText = 'position: fixed;  top: 0; left: 0; overflow: hidden; padding: 0; margin: 0; background-color: #000; filter: alpha(opacity=60); opacity: 0.5; display: block; z-index: 1000; width: 100%; height: 100%';

	IE6=(navigator.userAgent.toLowerCase().indexOf('msie 6') != -1) &&(navigator.userAgent.toLowerCase().indexOf('msie 7') == -1);

	if (IE6) {
		document.documentElement.scrollTop=0; 
		//var ddivpos=0; 
		window.attachEvent("onscroll", scrollingdivs);
		bgdiv.style.position='absolute';
		cdiv.style.position='absolute';
		document.body.style.height='100%';
	}
	document.body.appendChild(bgdiv);
	
	document.body.appendChild(cdiv);
// Create div for X image preload

	prepic  = document.createElement('div');
	prepic.setAttribute('id', 'popup_rollover_preloader');
	
	document.body.appendChild(prepic);
	prepic.style.display='none';

	if(show_control){
		cdiv.appendChild(topdiv);
		
		topdiv.style.cssText = 'top:'+(1*(poptop-topdiv.offsetHeight))+'px; left:'+(1*(popleft+w-topdiv.offsetWidth))+'px';
		topdiv.setAttribute("style", 'top:'+(1*(poptop-topdiv.offsetHeight))+'px; left:'+(1*(popleft+w-topdiv.offsetWidth))+'px');

	}
	cdiv.appendChild(link);	

if (typeof(link.contentDocument) == 'object') link.contentDocument.onkeypress=keyhandle;
if (typeof(link.contentWindow) == 'object') link.onload=function() {link.contentWindow.document.onkeypress=keyhandle};
if (typeof(link.contentWindow) == 'object') link.contentWindow.document.onkeypress=keyhandle;
	// eval JS scripts for all browsers exept Firefox and non url request
	if (eval_js && !/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent)){
		var scripts = link.getElementsByTagName("script");
		var script;
		for (var i = 0; script = scripts[i]; i++)
		{
			eval(script.innerHTML); 
		}		
	}

}

function scrollingdivs() { 

	if (document.getElementById('bgdiv')&&document.getElementById('popupdiv')) { 
		document.documentElement.scrollTop=0; 
	} 
} 

function closediv() {
pdiv=document.getElementById('bgdiv');
document.body.removeChild(pdiv);
popdiv=document.getElementById('popupdiv');
document.body.removeChild(popdiv);
document.onkeypress=prevkeyhandler;

}

window.onresize=function (e) {
	if (document.getElementById('bgdiv')&&document.getElementById('popupdiv')) {
		popleft = Math.round(windowsize().width/2)  - Math.round(w_t/2);
		poptop  = Math.round(windowsize().height/2) - Math.round((h_t)/2);
		var popupdiv = document.getElementById('popupdiv')
		popupdiv.style.left=popleft+'px';
		popupdiv.style.top=poptop+'px';
	}
	if (document.getElementById('popupdiv_close')) {
		popleft = Math.round(windowsize().width/2)  - Math.round(w_t/2);
		poptop  = Math.round(windowsize().height/2) - Math.round((h_t)/2);
		var popupdiv_close = document.getElementById('popupdiv_close')
		popupdiv_close.style.top=poptop-popupdiv_close.offsetHeight+'px';
		popupdiv_close.style.left=popleft+w_t-popupdiv_close.offsetWidth+'px';
	}

}
	