$(document).ready(function(){
	initMenu();
	initCategory();
	initNews();
});
function initMenu(){
	
	$('#horiz-menu > ul > li').hover(
		function(){$(this).addClass('hover')},
		function(){$(this).removeClass('hover')}
	);
	
	$('#horiz-menu ul li ul').each(function(i,ul){
		var width_ul=0;
		$(ul).find('li a').each(function(j,link){
			if (($(link).width()+22)>width_ul) { 
				width_ul=$(link).width()+22;
			}
		});
		$(ul).css('width',width_ul);
	});
/*	$('#news-rotator div.controls').hover(
		function(){ $(this).animate({opacity: '1',visibility:'visible' },500);},
		function(){$(this).animate({opacity: '0',visibility:'hidden' },500);}
	);*/	
	$('#news-rotator div.controls').css('display','none');
}
function explode( delimiter, string, limit ) {
    // Splits a string on string separator and return array of components. If limit is positive only limit number of components is returned. If limit is negative all components except the last abs(limit) are returned.  
    // 
    // version: 810.114
    // discuss at: http://phpjs.org/functions/explode
    // +     original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +     improved by: kenneth
    // +     improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +     improved by: d3x
    // +     bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: explode(' ', 'Kevin van Zonneveld');
    // *     returns 1: {0: 'Kevin', 1: 'van', 2: 'Zonneveld'}
    // *     example 2: explode('=', 'a=bc=d', 2);
    // *     returns 2: ['a', 'bc=d']
 
    var emptyArray = { 0: '' };
    
    // third argument is not required
    if ( arguments.length < 2
        || typeof arguments[0] == 'undefined'
        || typeof arguments[1] == 'undefined' )
    {
        return null;
    }
 
    if ( delimiter === ''
        || delimiter === false
        || delimiter === null )
    {
        return false;
    }
 
    if ( typeof delimiter == 'function'
        || typeof delimiter == 'object'
        || typeof string == 'function'
        || typeof string == 'object' )
    {
        return emptyArray;
    }
 
    if ( delimiter === true ) {
        delimiter = '1';
    }
    
    if (!limit) {
        return string.toString().split(delimiter.toString());
    } else {
        // support for limit argument
        var splitted = string.toString().split(delimiter.toString());
        var partA = splitted.splice(0, limit - 1);
        var partB = splitted.join(delimiter.toString());
        partA.push(partB);
        return partA;
    }
}
function initCategory(){
	if ($('#main-section ul.category').get(0)) {
		$('#main-section ul.category li').each(function(i,item){
			var string = explode('(', $(item).html());
			var count =explode(')', string[1]);
			$(item).html(string[0]+'<span>('+count[0]+' articles)</span>');
		});	
		
	}
}
var count_news = 2;
var curent_news = 0;
var time=7000;
var pause=true;
function initNews(){
	if ($('#news-rotator').get(0)) {
		if (!pause) setTimeout('initRotate()',0);
		$('#news-rotator div.controls #play-stop').click(function(){
			if ($(this).hasClass('control-stop')) {
				pause=true;
				$(this).removeClass();
				$(this).addClass('control-play');
			} else
			if ($(this).hasClass('control-play')) {
				
				setTimeout('initRotate()',time);
				pause=false;
				$(this).removeClass();
				$(this).addClass('control-stop');
			}
			//initNews();
		});
		$('#news-rotator div.controls .control-next').click(function(){
			if (pause) {
				if (curent_news < count_news) curent_news++; else curent_news=0; 		
			}
			initSlide();
		});
		$('#news-rotator div.controls .control-prev').click(function(){
			if (curent_news !=0) curent_news--; else curent_news=count_news-1;
			initSlide();
		});
	}
}
function initSlide(){
	var change = curent_news+1;
	if (change==count_news) change=0;
	$('#news-rotator div.image div.item:eq('+curent_news+')').fadeOut(800,function(){
		$('#news-rotator div.image div.item:eq('+change+')').fadeIn(800);
	});
	$('#news-rotator div.story div.padding:eq('+curent_news+')').fadeOut(800,function(){
		$('#news-rotator div.story div.padding:eq('+change+')').fadeIn(800);
	}); 
} 	
function initRotate(){
	initSlide();
	curent_news++;		
	if (curent_news > count_news) curent_news=0;
	if (!pause) setTimeout('initRotate()',time); else return false;
}


