$(function()
{
	fix_height();
	newsHandler();
	slider();
});

function fix_height()
{
	if($('#content-right').height() > $('#content-left').height())
	{
		$('#content').height($('#content-right').height());
	}
	if($.browser.msie && $.browser.version=="6.0")
	{
		$('#content h4 + p').css({'padding-top':'0px'});
		$('#background').height($(document).height());
	}
	if($('#background').height() > $('#website').height())
	{
		$('#website').height($('#background').height());
	}
}

function newsHandler()
{
	$("#scroller .i ul li .img:not(#scroller .i ul li.active .img)").fadeOut(1);
	$('#scroller ul.links a').mouseover(function()
	{
		if(!$(this).parents('li').hasClass('active'))
		{
			var index = $(this).parents('#scroller').find('ul.links li').index($(this).parents('li'));
			linkactive(index);
			itemactive(index, 200);
		}
	});
	$('#info .news ul li.media .l').mouseover(function()
	{
		window.newsstopscrolling = true;
	});
	$('#scroller').hover(function()
	{
		$(this).find('.i ul').addClass('hover');
	},
	function()
	{
		$(this).find('.i ul').removeClass('hover');
	});
	setTimeout (function(){ newsNext(); }, 2000);
}

function newsNext()
{
	if(window.newsstopscrolling != true)
	{
		if(!$('#scroller .i ul').hasClass('hover') && $('#scroller .i ul li').size() > 1)
		{
			if($("#scroller .i ul li.active").hasClass('last'))
			{
				var index = 0;
			}
			else
			{
				var index = $("#scroller .i ul li").index($('li.active')) + 1;
			}
			linkactive(index);
			itemactive(index, 1000);
			//$("#info .news ul li.active").removeClass('active');
			//$("#info .news ul li").eq(index).addClass('active');
			//$("#info .news ul li .l").eq(index).fadeIn(1000);
		}
		setTimeout (function(){ newsNext(); }, 5000);
	}
}

function linkactive(index)
{
	if(window.newsstopscrolling != true)
	{
		$('#scroller ul.links li.active').removeClass('active');
		$('#scroller ul.links li').eq(index).addClass('active');
	}
}

function itemactive(index, time)
{
	if(window.newsstopscrolling != true)
	{
		//window.newsstopscrolling = true;
		$("#scroller .i ul li.active .img").fadeOut(time, function()
		{
			window.newsstopscrolling = false;
			$(this).parent().removeClass('active');
		});
		$("#scroller .i ul li").eq(index).addClass('active').find('.img').fadeIn(time);
	}
}


function slider()
{
	var block = false;
	$('#refs .l').click(function()
	{
		$(this).blur();
		if(!block)
		{
			block = true;
			var slider = $(this).parents('#refs');
			var ul = slider.find('ul');
			var px = slider.find('.ii').width();
			var lipx =  parseInt(slider.find('li:first').width());
			var licount = slider.find('li').size();
			var margin = parseInt(ul.css('margin-left'));
			if((margin + px) <= 0)
			{
				ul.animate({marginLeft : (margin + px) + 'px'}, 500, function() { block = false; });
			}
		}
		return false;
	});
	
	$('#refs .r').click(function()
	{
		$(this).blur();
		if(!block)
		{
			block = true;
			var slider = $(this).parents('#refs');
			var ul = slider.find('ul');
			var px = slider.find('.ii').width();
			var lipx =  parseInt(slider.find('li:first').width());
			var licount = slider.find('li').size();
			var margin = parseInt(ul.css('margin-left'));
			if(((lipx * licount) - (lipx * licount) - (lipx * licount)) < (margin - px))
			{
				ul.animate({marginLeft : (margin - px) + 'px'}, 500, function() { block = false; });
			}
		}
		return false;
	});
}