var fs; //fullscreen
var ie=document.all && !window.opera;
var IE6=ie && navigator.appVersion.indexOf("MSIE 6")!=-1;

var _=function(obj){
	if(typeof obj=="string" && document.getElementById) obj=document.getElementById(obj);
	return obj;
};


(AJAX=function(opt){this.init(opt)}).prototype={
	
	CACHE:{},
		
	init:function(opt){
		opt=opt || {};
		this.file=opt.file || "";
		this.query=opt.query || "";
		this.caching=opt.caching || false;
		this.hash=opt.hash || {};
		this.onready=opt.onready || function(){};
		if(opt.send) this.send();
	},
	
	send:function(){
		if(this.caching && this.CACHE[this.query]) return this.ondata(this.CACHE[this.query]);
		var SID=(new Date()).getTime()+Math.round(Math.random()*1000);
		var request, response;
		request=document.createElement("script");
		if(request){
			request.id=SID;
			var src=this.file+"?"+"_scriptID_="+request.id+"&"+this.query;
			//limit max GET query length {gesko:4Kb, msie:2kb}
			if(ie) request.src=src.substr(0,2048);
			else{request.src=src; request.src=request.src.substr(0,4096);}
			var _this=this;
			request.onreadystatechange=function(response){
				if(response && response.width!=undefined){
					_this.ondata(response);
					setTimeout(function(){request.parentNode.removeChild(request)},13);
				}
			};
			document.body.appendChild(request); //request send 
		}
	},
	
	ondata:function(response){
		if(this.caching || this.CACHE[this.query]) this.CACHE[this.query]=response; //cache response data
		this.response=response;
		this.onready(response);
	}
};


function create_bg(){
	var root=_("root");
	var fc=root.firstChild;
	var el;
	(root.insertBefore(document.createElement("div"), fc)).className="hdr_web";
	_("web").style.background="none";
	(root.insertBefore(document.createElement("div"), fc)).className="hdr_tech";
	_("tech").style.background="none";
	
	for(var i=1; i<=9; i++) (root.insertBefore(document.createElement("div"), fc)).className="vetka_"+i;
	
	var tech_a=_("tech").getElementsByTagName("a");
	(tech_a[1].insertBefore(el=document.createElement("div"), tech_a[1].firstChild)).className="vetka_3_1";
	(el.appendChild(document.createElement("div"))).className="vetka_3";
	(tech_a[2].insertBefore(el=document.createElement("div"), tech_a[2].firstChild)).className="vetka_3_2";
	(el.appendChild(document.createElement("div"))).className="vetka_3";

}


function init_mails(){
	var a=document.getElementsByTagName("a");
	for(var i=0, l=a.length; i<l; i++)
		if(a[i].lang)
			a[i].href="mailto:"+a[i].lang+"@"+a[i].href.replace(/http:\/\/(.*)\/$/,"$1");
}


function hover(obj, over){
	var img=obj.getElementsByTagName("img")[0];
	if(img){
		var color_img=obj.href.replace(/\/popup\//,"/color/");
		if(over==undefined){//preload color img 
			img.style.backgroundImage="url('"+color_img+"')";
			return false;
		}
		if(over){
			img.tmp=img.src;
			img.src=color_img;
		}
		else{
			img.src=img.tmp;
		}
	}
}


function init_links(root_id){
	var a=_(root_id).getElementsByTagName("a");
	for(var i=0, l=a.length; i<l; i++)
		if(a[i].href){ 
			hover(a[i]); //preload img
			a[i].onmouseover=function(){hover(this, 1)}
			a[i].onmouseout=function(){hover(this, 0)}
			a[i].onfocus=function(){this.blur()}
			a[i].onclick=function(){return fs.show(this);}
		}
}


function FullScreen(){
	var fs;
	this.init=function(){
		var _this=this;
		fs=document.createElement("div");
		fs.onclick=function(){_this.hide()};
		fs.className="fs";
		fs.appendChild(document.createElement("blockquote"));
		fs.appendChild(fs.block=document.createElement("div"));
		fs.block.appendChild(fs.content=document.createElement("span"));
		document.body.appendChild(fs);
		fs.scrollSize=this.scrollSize();
		if(IE6){
			window.onscroll=function(){this.IE6_centered()}
			this.IE6_centered();
		}
		return this;
	}
	this.IE6_centered=function(){
	}
	
	this.scrollSize=function(){ //calc srroll size
		var div=document.createElement("div");
		div.style.width="50px";
		div.style.height="50px";
		div.style.overflow="auto";
		div.style.visibility="hidden";
		div.style.position="absolute";
		(div.appendChild(document.createElement("div"))).style.height="100px";
		document.body.appendChild(div);
		var size=div.offsetWidth-div.clientWidth;
		document.body.removeChild(div);
		return size;
	}
	
	this.show=function(obj){
		this.write('',{width:200, height:200})
		var _this=this;
		fs.style.width=(_("root").offsetWidth+60)+"px";
		fs.style.height=_("root").offsetHeight+"px";
		fs.style.display="block";
		new AJAX({
			file:"site/index.php", 
			query:"sizeof="+obj.href,
			caching:true,
			onready:function(response){_this.write(obj.href,response)},
			send:1
		})
		return false;
	}
	this.hide=function(){
		fs.style.display="none";
	}
	this.write=function(url,size){
		if(!size.width || !size.height) return false;
		if(IE6) this.IE6_centered();
		fs.content.style.overflow="visible";
		var w=size.width+30;
		var h=size.height+30;
		fs.block.style.marginLeft=Math.round(document.documentElement.clientWidth/2-size.width/2+document.documentElement.scrollLeft)+"px";
		fs.block.style.marginTop=Number(document.documentElement.scrollTop+50)+"px";
		fs.content.style.width=Number(w-30)+"px";
		fs.content.style.height=Number(h-30)+"px";
		fs.content.innerHTML=url ? '<img src="'+url+'" width="'+size.width+'" height="'+size.height+'" />' : '';
	}
	return this.init();
}


document.onload=function(){
	create_bg();
	init_mails();
	init_links("web");
	init_links("tech");
	init_links("pict");
	init_links("foto");
	init_links("bann");
	init_links("logo");
	fs=FullScreen();
}


