/*
 * In this file are effects / functions which are used on mainpage
 */

function listSlider (parent_node) {
	this.init(parent_node);
}
listSlider.prototype._slideInterval = 5000;		/* Interval between changing items */
listSlider.prototype._firstElement = null;		/* Reference to the first element in the list */
listSlider.prototype._elementHeight = 0;		/* Height of the element */
listSlider.prototype._elementCount = 0;			/* Element count in the list */
listSlider.prototype._currentElement = 0;		/* Currently visible element */
listSlider.prototype._timer = null;				/* Timer */
/* Slide next item */
listSlider.prototype.next = function () {
	this._currentElement++;
	
	var self = this;
	var pos = this._currentElement * this._elementHeight;
	
	this._firstElement.stop().animate({marginTop: - pos + 'px'}, function () {
		if (self._currentElement == self._elementCount)
		{
			self._currentElement = 0;
			self._firstElement.css({marginTop: '0px'});
		}
	});
};
/* Initialize slider */
listSlider.prototype.init = function (parent_node) {
	if (parent_node.length == 0) return;
	
	var self = this;
	var childs = parent_node.children();
	var child = $(childs).eq(0);
	
	if (child[0])
	{
		var h = child.height() + parseInt(child.css('paddingTop')) + parseInt(child.css('paddingBottom'));
		
		this._elementHeight = h;
		this._firstElement = child;
		this._elementCount = childs.length;
		
		parent_node.css({overflow: 'hidden', height: h + 'px'});
		
		child.clone(true).appendTo(parent_node);
		this._timer = setInterval(function () { self.next(); }, this._slideInterval );
	}
};



/**
 * Initialize sub menu
 */
function submenu_init() {
	if ($('#sub-menu td').length == 0) return;
		
	/* Fix for "clicking on images nothing happens" */
	if ($.browser.msie)
	{
		$('#sub-menu a').click(function () {
			this.click();
		});
	}
	
	$('#sub-menu td').hover(
		function () {
			$(this).addClass('active');
			var popup = $('.sub-menu-popup span', this);
			popup.stop()
				 .css({opacity: 0, marginTop: '121px'})
				 .animate({opacity: 1, marginTop: '0px'}, 250)
				 .parent().css({'visibility':'visible','z-index':1});
		},
		function () {
			var popup = $('.sub-menu-popup span', this);
			popup.stop()
				 .animate({ opacity: 0, marginTop: '121px'}, 250,
				 	function () {
						$(this).parent().css({visibility: 'hidden','z-index':-1})
							   .parent().parent().removeClass('active');
					}
				 );
		}
	).find('.sub-menu-popup').css('z-index',-1);
};

/**
 * Fixes promo table
 */
function promo_table_fix() {
	var td = $('table.promo td'), max_h = 0;

	/* Calculate max height */
	for(var i=0,j=td.length; i<j; i++)
		max_h = Math.max(max_h, td.eq(i).height());
	
	/* Assign height */
	$('table.promo td').each(function () {
		$('div.block-1', this).css({height: max_h + 'px'});
	});
	
	$('table.promo .button').hover(function () {
		$(this).addClass('hover');
	}, function () {
		$(this).removeClass('hover');
	});
}

$(window).bind('load', function () {
	submenu_init();
	
	new listSlider($('div.news-latest ul'));
	
	promo_table_fix();
});
