﻿window.addEvent('domready', function() {
	
	var uls = Array();
	$$("#top_menu ul li").each(function(el){
		if(el.getParent().getParent().get("tag")!="li"){
			if(el.getElement("a")){
				uls.include(el.getElement("a"));
			}
			else {
				uls.include(el);
			}
			
		}
	});
	
	Cufon.replace($$("h1, h2, h3 a"), {
		fontFamily: "Aller",
		hover: true
	});
	
	Cufon.replace(uls, {
		fontFamily: "Aller",
		hover: true
	});
	
	Cufon.now();
	
	new Menu();
	if($("banners")){ new FadingThrough($$("#banners a"), 4500); }
	new MenuBorderBottom($$("#top_menu ul")[0]);
	
	$$("#footer .right a").addEvent("click", function(event){
		var event = new Event(event);
		event.preventDefault();
		new Fx.Scroll($(document.body)).toTop();
	});
	
	var first_level_lis = Array();
	
	$$("#right_menu li").each(function(el){
		if(el.getElements("ul").length > 0){
			first_level_lis.include(el);
		}
	});
	
	/*first_level_lis.each(function(el){
		el.addEvent("click", function(event){ event = new Event(event); event.preventDefault(); }); 
	});
	
	var accordion = new Fx.Accordion(first_level_lis, $$("#right_menu ul"), {
		
		onActive: function(toggler){
			toggler.addClass("active")
		}, 
		
		onBackground: function(toggler, element){
			toggler.removeClass("active")
		}
	});*/
	
});




var Menu = new Class({
	initialize: function(){
		var oThis = this;
		
		this.lis= $$("#top_menu ul li");
		this.first_level_lis = Array();
		this.second_level_uls = $$("#top_menu ul ul");
		this.lis.each(function(el){
			if(el.getParent().getParent().get("tag") == "li"){}
			else {
				oThis.first_level_lis.push(el);
			}
		});
		
		this.init();
	},
	
	init: function(){
		var oThis = this;
		
		$$("#top_menu ul li").each(function(el){
			if(el.getElements("ul").length > 0){
				var ul = el.getElement("ul");
				
				ul.fade("hide");
				
				var pos = el.getPosition();
				ul.setPosition({ x: pos.x-10, y: (pos.y+el.offsetHeight+17)});
			}
		});
		
		$$("#top_menu ul li").each(function(el){
			el.addEvent("mouseover", function(){
				if(el.getElements("ul").length > 0){
					var ul = this.getElement("ul");
					ul.fade("in")
				}
			})
			
			el.addEvent("mouseout", function(){
				if(el.getElements("ul").length > 0){
					var ul = this.getElement("ul");
					ul.fade("out")
				}
			})
		});
	}
})


var FadingThrough = new Class({
	initialize: function(elements, time){
		this.elements = elements;
		this.elements.fade("hide");
		this.elements[0].fade("show");
		this.current_el = $random(0, this.elements.length-1);
		this.time = time;
		this.set_image();
		this.function_rotate = this.rotate.periodical(this.time, this);
	},
	
	rotate: function(){
		this.elements[this.current_el].fade("out");
		if(this.current_el==this.elements.length-1){
			this.current_el = 0;
		}
		else {
			this.current_el+=1;
		}
		this.elements[this.current_el].fade("in");
		
	},
	
	set_image: function(){
		this.elements.fade("hide");
		this.elements[this.current_el].fade("show");
	}
});

var MenuBorderBottom = new Class({
	initialize: function(menu){
		this.menu = menu;
		this.hover = $("slider");
		this.current = $$("#top_menu li.current a")[0];
		
		if(!$$("#top_menu li.current a")[0])
		{
			this.current = $$("#top_menu li.trail a")[0];
		}
		
		var hover_position = this.menu.getPosition();
		this.hover_y = hover_position["y"];
		this.set_first_to_position(this.current);
		this.add_events();
	},
	
	add_events: function(){
		var oThis = this;
		$$("#top_menu li a").addEvent("mouseover", function(){
			oThis.set_to_position(this);
		});
		
		$$("#top_menu li a").addEvent("mouseout", function(){
			oThis.set_to_position(oThis.current);
		});
		
		$$("#top_menu ul ul li a").removeEvents();
	},
	
	set_first_to_position: function(el){
		var position = el.getCoordinates();
		var width = position["width"];
		var x = position["left"];
		
		var pl = el.getStyle("padding-left").toInt();
		var pr = el.getStyle("padding-right").toInt();
		
		if(pl > 0 && pr > 0){
			var to_add = (pr+pl)/2
		}
		else if (pl > 0){
			var to_add = pl;
		}
		else if (pr > 0){
			var to_add = 0;
		}		
		
		this.hover.setPosition({x: x+to_add});
		this.hover.setStyle("width", width-(pr+pl));
	},
	
	set_to_position: function(el){
		var position = el.getCoordinates();
		var width = position["width"];
		var x = position["left"];
		
		var pl = el.getStyle("padding-left").toInt();
		var pr = el.getStyle("padding-right").toInt();
		
		if(pl > 0 && pr > 0){
			var to_add = (pr+pl)/2
		}
		else if (pl > 0){
			var to_add = pl;
		}
		else if (pr > 0){
			var to_add = 0;
		}
		
		this.hover.morph({"width": width-(pr+pl), "left": x+to_add});
	}
	
})

