window.addEvent('domready', function() {
									 
	Cufon.replace($$("#sidebar .right li a, #top .left h4 a, #content h2, #content h3, #content .tekst, #contents_wrapper h3, #content.subpage .left a"), { fontFamily: 'Arial Narrow', hover: true });
	Cufon.now();
	new FadingThroughForTop($$("#top .right")[0], $$("#images_wrapper img"), 6000, $("left_arrow"), $("right_arrow"), $$("#contents_wrapper h3"));
	new MenuBorderBottom();
});

var Rotating = new Class({
						 
	initialize: function(elements, time){
		this.elements = elements;
		this.current_el = 0;
		this.time = time;
	},
	
	to_forward: function(){
		var old_current_el = this.current_el;
		if(this.current_el==this.elements.length-1){
			this.current_el = 0;
		}
		else {
			this.current_el+=1;
		}
		
		this.rotate(old_current_el, this.current_el);
	},
	
	to_backward: function(){
		var old_current_el = this.current_el;
		if(this.current_el==0){
			this.current_el = this.elements.length-1;
		}
		else {
			this.current_el-=1;
		}
		
		this.rotate(old_current_el, this.current_el);
	}
});

var FadingThroughForTop = new Class({
	Extends: Rotating,
	initialize: function(main, elements, time, left_arrow, right_arrow, headers){
		this.parent(elements, time);
		
		this.main = main;
		this.left_arrow = left_arrow;
		this.right_arrow = right_arrow;
		this.headers = headers;
		
		this.elements.fade("hide");
		this.elements[0].fade("show");
		this.headers.fade("hide");
		this.headers[0].fade("show");
		
		this.add_events();
		this.function_rotate = this.to_forward.periodical(this.time, this);
		
	},
	
	add_events: function(){
		var oThis = this;
		
		this.left_arrow.addEvent("click", function(event){
			var event = new Event(event);
			event.preventDefault();
			oThis.to_backward();
			$clear(oThis.function_rotate);
			oThis.main.removeEvents();
		});
		
		this.right_arrow.addEvent("click", function(event){
			var event = new Event(event);
			event.preventDefault();
			oThis.to_forward();
			$clear(oThis.function_rotate);
			oThis.main.removeEvents();
		});
		
		this.main.addEvent("mouseover", function(){
			$clear(oThis.function_rotate);
		});
		
		this.main.addEvent("mouseout", function(){;
			oThis.function_rotate = oThis.to_forward.periodical(oThis.time, oThis);
		});
	},
	
	rotate: function(old_current_el, new_current_el){
		this.elements[old_current_el].fade("out");
		this.elements[new_current_el].fade("in");
		this.headers[old_current_el].fade("out");
		this.headers[new_current_el].fade("in");
	},
	
	set_element: function(index){
		this.rotate(this.current_el, index);
		this.current_el = index;
	}
	
});

var MenuBorderBottom = new Class({
	initialize: function(menu){
		this.menu = menu;
		this.hover = $("slider");
		this.current = $$("#sidebar li.current")[0];
		
		this.set_first_to_position(this.current);
		this.add_events();
	},
	
	add_events: function(){
		var oThis = this;
		$$("#sidebar li").addEvent("mouseover", function(){
			oThis.set_to_position(this);
		});
		
		$$("#sidebar li").addEvent("mouseout", function(){
			oThis.set_to_position(oThis.current);
		});

	},
	
	set_first_to_position: function(el){
		var position = el.getCoordinates();
		var width = position["width"];
		var x = position["left"];
		if(Browser.Engine.trident){
			var y = position["top"]+24;
		}
		else {
			var y = position["top"]+26;
		}
		
		var to_add = el.offsetWidth/2;
		
		if(Browser.Engine.gecko){
			var to_remove = 6;
		}
		else {
			var to_remove = 10;
		}
		
		x = x+to_add.toInt()-to_remove;
		
		this.hover.setPosition({x: x, y: y});
	},
	
	set_to_position: function(el){
		var position = el.getCoordinates();
		var width = position["width"];
		var x = position["left"];
		
		var to_add = el.offsetWidth/2;
		
		if(Browser.Engine.gecko){
			var to_remove = 6;
		}
		else {
			var to_remove = 10;
		}
		x = x+to_add.toInt()-to_remove;
		
		this.hover.morph({"left": x});
	}
});

