moteris = {};
moteris.initialize = function() {
	mainmenu.initialize();
	var nodes = $$(".branding_handle");
	if (nodes.length > 0) {
		var body1 = document.body;
		var body2 = document.documentElement;
		var height = Math.max(
			Math.max(body1.scrollHeight, body2.scrollHeight), 
			Math.max(body1.offsetHeight, body2.offsetHeight),
			Math.max(body1.clientHeight, body2.clientHeight)
		);
		nodes[0].style.height = height + "px";	
	}
	Event.observe("f_form_submit", "click", function(e) {
		$('bc_code').value = $('s_code').value;
		$('f_form').submit();
		Event.stop(e);
	});
}

function showHideSpecialist(event, id_prefix) {
	var el = Event.element(event);
	var show = el.src.indexOf("/plus.gif") !== -1;
	el.src = "images/" + (show ? "min" : "pl") + "us.gif";
	$(id_prefix + "_ds").style.display = $(id_prefix + "_ts").style.display = show ? "none" : "";
	$(id_prefix + "_d").style.display = $(id_prefix + "_t").style.display = !show ? "none" : "";
}

slideshow = function() {
	slideshow.instance = this;
	this.show(1);
}

slideshow.instance = null;
slideshow.__slide_timeout = null;
slideshow.__active_slide__ = 1;
slideshow.__old_efect__ = null;
slideshow.__prefix = "slideviewer";

slideshow.prototype.show = function(n) {
	if (slideshow.__old_efect__ !== null) {
		return false;
	}
	if (!$("slideviewer_" + n)) {
		n = 1;
	}	
	this.showHandle(n, slideshow.__active_slide__);
	this.showInfo(n);
	this.showImage(n, slideshow.__active_slide__);
	slideshow.__active_slide__ = n;	
}

slideshow.prototype.showImage = function(n, old) {
	try {
		clearTimeout(slideshow.__slide_timeout);
	} catch (e) {}
	this.hideBackgrounds();
	var nodeViewer = $(slideshow.__prefix);
	nodeViewer.src = $(slideshow.__prefix + "_" + n).src;
	nodeViewer.uri = $(slideshow.__prefix + "_" + n).getAttribute("uri");
	nodeViewer.onclick = function(){
		location.href = this.uri;
	};
	$(slideshow.__prefix + "_" + old).style.display = "";
	nodeViewer.setOpacity(0);
	slideshow.__old_efect__ = new Effect.Opacity(
		nodeViewer.id, {
			from: 0, to: 1, duration: 0.4, afterFinish : function() {slideshow.instance.afterFinish()}
		}
	);
	slideshow.__slide_timeout = setTimeout("slideshow.instance.show(" + (n + 1) + ")", 9000);
}

slideshow.prototype.showHandle = function(n, old) {
	var node = $(slideshow.__prefix + "_handles");
	if (node) {
		var nodes = node.getElementsByTagName("A");
		nodes[old - 1].removeClassName("active");
		nodes[n - 1].addClassName("active");
	}
}

slideshow.prototype.showInfo = function(n) {
	var el = $(slideshow.__prefix + "_" + n);
	var elLink = $(slideshow.__prefix + "_link");
	var nodesSpan = elLink.getElementsByTagName("SPAN");
	elLink.href = el.getAttribute("uri");
	nodesSpan[0].innerHTML = el.getAttribute("title");
	nodesSpan[1].innerHTML = el.getAttribute("alt");
}

slideshow.prototype.afterFinish = function() {
	slideshow.__old_efect__ = null;
	this.hideBackgrounds();
}

slideshow.prototype.hideBackgrounds = function() {
	for (var i = 1; ; i++) {
		var el = $(slideshow.__prefix + "_" + i);
		if (!el) {
			break;
		}
		el.style.display = "none";
	}
}

__vote_timeout__ = null;
function voteDuel(uri) {
	var listener = {}
	listener.onComplete = function(response) {
		var parts = response.responseText.split(",");
		$("__votes_warning__").style.display = parts.length === 2 ? "none" : "";
		if (__vote_timeout__ !== null) {
			clearTimeout(__vote_timeout__);
			__vote_timeout__ = null;
		}
		if (parts.length !== 2) {
			__vote_timeout__ = setTimeout("$('__votes_warning__').style.display='none'", 5000);
			return false;
		}
		for (var i = 1; i <= 2; i++) {
			var prefix = "__votes_member" + i;
			var el = $(prefix + "_label__");
			el.innerHTML = $(prefix + "__").style.width = parts[i - 1] + "%";
			el.style.display = "";
			$(prefix + "_handle__").style.display = "none";
		}
	}
	new Ajax.Request(uri, {onComplete: listener.onComplete.bind(listener)});
}

__marquee_timeout__ = null;
__marquee_n__ = 1;
function autorunMarquee(uri, instant) {
	var duration = 0.25;
	var timeout = 5;
	var el = $('marquee');
	if (!instant) {
		el.fade({duration: duration, from: 0, to: 1});
		el.style.display = "";
		__marquee_timeout__ = setTimeout("autorunMarquee('" + uri + "', true)", (duration + timeout) * 1000);
		return false;
	}	
	if (__marquee_timeout__ !== null) {
		clearTimeout(__marquee_timeout__);
		__marquee_timeout__ = null;
	}
	var listener = {"uri" : uri, "el" : el, response : ""};
	listener.load = function() {
		this.el.innerHTML = this.response;
		autorunMarquee(this.uri);
	}	
	listener.onComplete = function(response) {
		this.response = response.responseText;
		this.el.fade({duration: duration, from: 1, to: 0.01, afterFinish : this.load.bind(this)});
	}
	new Ajax.Request(uri + ",n." + (__marquee_n__++), {onComplete: listener.onComplete.bind(listener)});
}

mainmenu = {};
mainmenu.active = null;
mainmenu.initialize = function() {
	Event.observe(window, "mousemove", mainmenu.mousemove);
	bannerfixed.initialize();
}
mainmenu.mousemove = function(event) {
	if (mainmenu.active) {
		var node = mainmenu.active;
		var nodeTarget = $(node.getAttribute("targetsub"));
		if (nodeTarget.getAttribute("mouseover") == "0" && node.getAttribute("mouseover") == "0") {
			mainmenu.out();
		}
	}
}
mainmenu.getEventNode = function(event) {
	var node = Event.element(event);
	return $(node.nodeName === "IMG" ? node.parentNode : node);
}
mainmenu.out = function() {
	var node = mainmenu.active;	
	if (!node) {
		return false;
	}	
	var nodeTarget = $(node.getAttribute("targetsub"));
	if (node.getAttribute("img")) {
		node.getElementsByTagName("IMG")[0].src = node.getAttribute("img");
	}
	if (nodeTarget) {
		nodeTarget.style.display = "none";
	}
	mainmenu.active = null;
}
mainmenu.overitem = function(node) {
	$(node).setAttribute("mouseover", 1);
}
mainmenu.outitem = function(node) {
	$(node).setAttribute("mouseover", 0);
}
mainmenu.over = function(event) {
	mainmenu.out();
	var node = mainmenu.getEventNode(event);	
	mainmenu.active = node;	
	var nodeTarget = $(node.getAttribute("targetsub"));
	if (node.getAttribute("img_active")) {		
		node.getElementsByTagName("IMG")[0].src = node.getAttribute("img_active");
	}
	node.setAttribute("mouseover", 1);
	if (nodeTarget) {
		nodeTarget.setAttribute("mouseover", 0);
		nodeTarget.style.left = (node.cumulativeOffset().left - node.parentNode.cumulativeOffset().left) + "px";
		nodeTarget.style.display = "";
	}
}

bannerfixed = function(container) {
	this.timeAppear = 5;
	this.timeHide = 5;
	this.container = container;
	this.nodeTitle = this.container.getElementsBySelector(".bannerfixed_title")[0];
	this.hidden = true;
	this.hiddenHeight = 44;
	this.fullHeight = null;	
	this.container.getElementsBySelector(".bannerfixed_close")[0]
		.observe("click", this.handleClose.bind(this));	
	this.nodeTitle.observe("click", this.handleHide.bind(this));
	setTimeout(this.appear.bind(this), this.timeAppear * 1000);
}

bannerfixed.prototype.appear = function() {
	this.setVisible(true);
	this.setHidden(false);
}

bannerfixed.prototype.autoHide = function() {
	this.setHidden(true);
}

bannerfixed.prototype.handleHide = function() {
	this.setHidden(!this.hidden);
}

bannerfixed.prototype.setHidden = function(hide, instant) {
	this.hidden = hide;
	var height = (this.hidden ? this.hiddenHeight : this.fullHeight) + "px";
	if (instant) {
		this.container.style.height = height;
	} else {
		this.container.morph("height: " + height, {duration: 1, transition: Effect.Transitions.sinoidal}); 
	}
	this.nodeTitle.className = this.nodeTitle.className.split("bannerfixed_collapsed").join("") + 
		(this.hidden ? " bannerfixed_collapsed" : "");
	if (this.timeout) {
		clearTimeout(this.timeout);
	}
	if (!this.hidden) {
		this.timeout = setTimeout(this.autoHide.bind(this), this.timeHide * 1000);
	}
}

bannerfixed.prototype.handleClose = function() {
	this.setVisible(false);
}

bannerfixed.prototype.setVisible = function(show) {	
	var className = this.container.className;
	className = className.split("bannerfixed_shown").join("");
	if (this.timeout) {
		clearTimeout(this.timeout);
	}
	if (show) {
		className += " bannerfixed_shown";
		if (this.fullHeight === null) {
			var nodeBanner = this.container.getElementsBySelector(".bannerfixed_content_banner img")[0];
			if (!nodeBanner) {
				nodeBanner = this.container.getElementsBySelector(".bannerfixed_content_banner object")[0];
			}
			this.container.style.width = (Number(nodeBanner.getAttribute("width")) + 28) + "px";
			this.container.style.height = (Number(nodeBanner.getAttribute("height")) + 54) + "px";
			this.fullHeight = this.container.getHeight();			
		}
		this.setHidden(this.hidden, true);
	}
	this.container.className = className;
}

bannerfixed.initialize = function() {
	var el = $("bannerfixed");
	if (el) {
		new bannerfixed(el);
	}
}

Event.observe(window, "load", moteris.initialize);
