$(document).ready(function()
{
	// --- submenu
	$('ul#navigation li').hover(function()
	{
		//clearTimeout(menu_timer);
		var li_widths = new Array();
		
		$(this).find('ul.submenu li').each(function()
		{
			li_widths.push((parseInt($(this).find('a').text().length) * 7));
		});
		
		var abs_w = 0;
		for(key in li_widths)
		{
			if(li_widths[key] > abs_w)
				abs_w = li_widths[key];
		};
		
		if($.browser.msie)
		{
			var offset = $(this).offset();
			
			$(this)
			.addClass("current")
			.find('ul.submenu')
			.css
			({
				background: "#005DAB",
				opacity: 0.9,
				left: 0,
				width: (abs_w + 40)+"px"
			})
			.show();
		}
		else
			$(this).addClass("current").find('ul.submenu').css({width: (abs_w + 40)+"px"}).fadeIn(200);
		
	}, function()
	{
		$(this).removeClass("current").find('ul.submenu').hide();
	});

	//
	var header_fader = new HeaderFader();
	
	header_fader.loop();
	
	//
	var home_slider = new HomeSlider();
	
	home_slider.init();
});

var HEADER_PHOTO = "#photo_";
/*==========================*/
/*	HeaderFader
/*==========================*/
	/**
	 *
	 *
	 */
	function HeaderFader()
	{
		/*
		 */
		this.object = $('#header_photos');
	
		/*
		 */
		this.img_count   = (this.object.children().length - 1);
		this.current_img = 1;
		
		/*
		 */
		this.speed = 500;
		this.delay = 5000;
		
		/*
		 */
		this.timer = null;
		
		/**
		 *
		 */
		this.loop = function()
		{
			if(this.img_count > 1)
			{
				this.object.find('img.photo').fadeOut(this.speed);
			
				this.current_img++;
				
				if(this.current_img > this.img_count)
					this.current_img = 1;
				
				$(HEADER_PHOTO+this.current_img).fadeIn(this.speed);
				
				var _self = this;
				
				this.timer = setTimeout(function(){_self.loop()}, this.delay);
			};
		};
	};


var HOME_SLIDER_TEXT  = '#text_item_';
var HOME_SLIDER_PHOTO = '#photo_item_';
/*==========================*/
/*	HomeSlider
/*==========================*/
	/**
	 *
	 *
	 */
	function HomeSlider()
	{
		/*
		 */
		this.object = $('#slider');
	
		/*
		 */
		this.item_count   = this.object.find('img.item_backdrop').length;
		this.current_item = 1;
		
		/*
		 */
		this.speed = 500;
		this.delay = 5000;
		
		/**
		 *
		 */
		this.init = function()
		{
			$(HOME_SLIDER_TEXT+this.current_item).fadeIn(this.speed);
			$(HOME_SLIDER_PHOTO+this.current_item).fadeIn(this.speed);
			
			if(this.item_count > 1)
			{
				this.setTimer();
				
				this.setLinks();
			}
		};
		
		/**
		 *
		 */
		this.loop = function(n)
		{
			(n == null || n == "" ? this.current_item++ : this.current_item = n);
			
			if(this.current_item > this.item_count)
					this.current_item = 1;
		
			this.show(this.current_item);
			
			this.setTimer();
		};
		
		/**
		 *
		 */
		this.setLinks = function()
		{
			var _self = this;
		
			for(var i = 1; i <= this.item_count; i++)
			{
				this.object.find('#slider_links').append
				(
					$('<span></span>')
					.addClass('slider_link')
					.html(i)
					.addClass(""+(i == _self.current_item ? "current_slide" : "")+"")
					.bind('click', function(){clearTimeout(_self.timer);_self.loop($(this).html())})
				);
			};
		};
		
		/**
		 *
		 */
		this.setTimer = function()
		{
			var _self = this;
			
			this.timer = setTimeout(function(){_self.loop()}, this.delay);
		};
		
		/**
		 *
		 */
		this.show = function(n)
		{
			$('.item_backdrop').fadeOut(this.speed);
			$('#slider').find('p').fadeOut(this.speed);
			
			$(HOME_SLIDER_TEXT+this.current_item).fadeIn(this.speed);
			$(HOME_SLIDER_PHOTO+this.current_item).fadeIn(this.speed);
			
			$('span.current_slide').removeClass('current_slide');
			
			$('span.slider_link').each(function()
			{
				if($(this).html() == n)
					$(this).addClass('current_slide');
			});
		};
	};
