// Copyright 2005-2008 Enzon
// design by Enzon.cz [http://www.enzon.cz]

function preload(file)
{
	img = new Image();
	img.src = file;
}
function bodypreload()
{
	
}
function confirmMessage(text){
	if(!confirm(text)){
		return false;
	}		
}

SZN.Bavykomenu = SZN.ClassMaker.makeClass({
	NAME: "Bavykomenu",
	VERSION: "1.0",
	CLASS: "class"
});

SZN.Bavykomenu.prototype.$constructor = function(menuclasss) {
	//this.debug("Bavykomenu inicializovano pro odkazy s tridou: "+menuclasss);
	this.menuclasss = menuclasss;
	this.a = Array();
	this.lock = true;
	this.timeout = false;
	
	
	this.logo = SZN.gEl('mHeader-home');
	this.logoDefault = 130;
	this.logo.style.left = this.logoDefault+'px';
	this.header = SZN.gEl("mHeader");
	this.active = false;
	this.location = false;
	
	var inpath = false;
	var anchors = document.getElementsByTagName('a');
	for(i=0;i<anchors.length;i++){
		var aClass = anchors[i].className;
		if(aClass && aClass.indexOf(this.menuclasss)!=-1){
			this.a.push(anchors[i]);
			//this.debug('Pridan odkaz s tridou: '+aClass);
			if(SZN.Dom.hasClass(anchors[i],"active")) this.active = anchors[i];
			if(SZN.Dom.hasClass(anchors[i],"inpath")) inpath = anchors[i];
		}
		
	}
	if(!this.active) this.active = inpath;
	
	this.eventsCache = [];
	for(i=0;i<this.a.length;i++){
		this.eventsCache.push(SZN.Events.addListener(this.a[i],"click",this,"aClick",false));
		this.eventsCache.push(SZN.Events.addListener(this.a[i],"mouseover",this,"aMouseover",false));
		this.eventsCache.push(SZN.Events.addListener(this.a[i],"mouseout",this,"aMouseout",false));
	}
	
	this.interpolator = new SZN.CSSInterpolator(this.logo, 500, {});
	
	this.toActive();
	
}

SZN.Bavykomenu.prototype.debug = function(message) {
	//if(SZN.Browser.client == 'gecko') console.log(message);

}
SZN.Bavykomenu.prototype.bavykoloc = function() {
	document.location.href = this.location;
}
SZN.Bavykomenu.prototype.aClick = function(e,elm) {
	SZN.Events.cancelDef(e);
	this.toElement(e,elm,true);
	this.lock = true;
}
SZN.Bavykomenu.prototype.toActive = function() {
	var elmPos = SZN.Dom.getFullBoxPosition(this.active);
	var headerPos = SZN.Dom.getFullBoxPosition(this.header);
	var newLeft = parseInt((elmPos.left-headerPos.left)+(this.active.offsetWidth/2)-20);
	this.setLogoLeft(newLeft);
	this.logo.style.top = '-110px';
	this.logo.style.display = 'block';
	
	var i = new SZN.CSSInterpolator(this.logo, 500, {});
	i.addProperty('top', parseInt(this.logo.style.top), 32, 'px');
	i.start();
	this.lock = false;
}

SZN.Bavykomenu.prototype.toElement = function(e,elm,go) {
	this.timeout = false;
	var elmPos = SZN.Dom.getFullBoxPosition(elm);
	var headerPos = SZN.Dom.getFullBoxPosition(this.header);
	var newLeft = parseInt((elmPos.left-headerPos.left)+(elm.offsetWidth/2)-20);
	if(!this.lock){
		this.interpolator.stop();
		if(go){
			this.location = elm.href;
			var bavykoloc = SZN.bind(this,this.bavykoloc);
			this.interpolator = new SZN.CSSInterpolator(this.logo, 500, {endCallback: bavykoloc});
		}
		this.interpolator.addProperty('left', parseInt(this.logo.style.left), newLeft, 'px');
		this.interpolator.start();
	}	
}

SZN.Bavykomenu.prototype.aMouseover = function(e,elm) {
	if(this.timeout) clearTimeout(this.timeout);
	var toElement = SZN.bind(this,this.toElement);
	this.timeout = setTimeout(function(){ toElement(e,elm)}, 1000);

}
SZN.Bavykomenu.prototype.aMouseout = function(e,elm) {
	if(this.timeout) clearTimeout(this.timeout);
	this.toElement(e,this.active);
}
SZN.Bavykomenu.prototype.setLogoLeft = function(left) {
	//this.debug(left);
	if(isFinite(left)) this.logo.style.left = left+'px';
}


