var slideshowTimer = null;
var mytime = 600;
var curelem = null;
var nextelem = null;
var tailelem = null;
var btncur = null;
var btnnext = null;
var h = 100;


$(document).ready(function(){

	// Main menu
	initMainMenu();
		
	$('ul.main-menu li a').click(function(){
		if ($(this).parent().hasClass('active')) return false;
		$('ul.main-menu li.active ul').animate({height: '0px'}, 500);
		$('ul.main-menu li.active ul li').fadeOut(300);
		//$('ul.main-menu li.active ul li').css('display', 'none');
		$('ul.main-menu li.active').removeClass('active');

		$(this).parent().addClass('active');
		var h = $('ul.main-menu li.active ul').attr('rel');
		$('ul.main-menu li.active ul').animate({height: h}, 500);
		$('ul.main-menu li.active ul li').fadeIn('normal');
		
		setTimeout("$('.scroll-pane').jScrollPane({scrollbarWidth:2});",800);

		
	});
	
	// List news
	$('ul.news li:first').css('top', '0');
	$('ul.news li:first').addClass('current');
	$('ul.news li.current').next().css('top', '100px');
	$('ul.news li.current').next().addClass('next');
	if ($('ul.news').children().size() > 2) {
		$('ul.news li:last').css('top', '-100px');
		$('ul.news li:last').addClass('tail');
	}
	
	slideshowTimer = setTimeout('doSlideshow(true);', 3000);
	
	$('a.link-up').click(function(){ doSlideshow(false); });
	
	$('a.link-down').click(function(){
		
		if ($('ul.news').children().size() < 3) return;
		
		clearTimeout(slideshowTimer);
		slideshowTimer = null;
		
		tailelem = $('ul.news li.tail');
				
		$('ul.news li.next').animate({top: '200px'}, mytime);
		$('ul.news li.current').animate({top: '100px'}, mytime);
		$('ul.news li.tail').animate({top: '0px'}, mytime);
		
		curelem = $('ul.news li.current');
		nextelem = $('ul.news li.next');
		
		tailelem.removeClass('tail');
		curelem.removeClass('current');
		tailelem.addClass('current');
		nextelem.removeClass('next');
		curelem.addClass('next');
		
		if ($('ul.news li:first').hasClass('current')) {
				$('ul.news li:last').addClass('tail'); 
			}
			else 
				{ 	
					$('ul.news li.current').prev().addClass('tail'); 
				};
		$('ul.news li.tail').animate({top: '-100px'}, 1);
		
	});
	
	// init scroll
	var win_h = parseInt($(window).height()) - 242;
	var scroll_h = win_h + 'px';
	$('.scroll-pane').css('height', scroll_h);
	$('.scroll-pane').jScrollPane({scrollbarWidth:2});
	setTimeout("$('.scroll-pane').jScrollPane({scrollbarWidth:2});", 500);
	
		$(window).bind('resize', function() {
			win_h = parseInt($(window).height()) - 242;
			scroll_h = win_h + 'px';
			$('.jScrollPaneContainer').css('height', scroll_h);
		});
		
	// init smoke
	setTimeout("initSmoke();", 500);
	
});  // end document ready

function initSmoke(){
	var doc_h = $('#page').height()
	
	if (doc_h > 863) doc_h = 863; //else doc_h -= 10;
	var smoke_h = doc_h + 'px';
	$('.smoke').css('height', smoke_h);
	
	if (!($('.smoke').hasClass('home-page'))) {
		var randomnumber = Math.floor(Math.random()*4);
		$('.smoke').addClass('ver-'+randomnumber);
	}
	$('.smoke').fadeIn(500);
}

function initMainMenu(){
	$('ul.main-menu li ul').attr('rel', $('ul.main-menu li ul').height());
	$('ul.main-menu li ul li').css('display', 'none');
	$('ul.main-menu li ul').css('height', '0px');
	
	var h = $('ul.main-menu li.active ul').attr('rel')+'px';
	$('ul.main-menu li.active ul').css('height', h);
	$('ul.main-menu li.active ul li').css('display', 'block');
}

function doSlideshow(start){
	
	if ($('ul.news').children().size() < 3) return;
	
	clearTimeout(slideshowTimer);
	slideshowTimer = null;
	
	
	$('ul.news li.tail').css('top', '200px');
		$('ul.news li.tail').removeClass('tail');
		
		curelem = $('ul.news li.current');
		if ($('ul.news li:last').hasClass('current')) {
			nextelem = $('ul.news li:first');
		} else {
			nextelem = curelem.next();
		}
		
		curelem.removeClass('current');
		nextelem.addClass('current');
		$('ul.news li.next').removeClass('next');
		curelem.addClass('tail');
		
		$('ul.news li.current').animate({top: '0px'}, mytime);
		
		
		if ($('ul.news li:last').hasClass('current')) {
			$('ul.news li:first').animate({top: '100px'}, mytime);
			$('ul.news li:first').addClass('next');
		} else {
			$('ul.news li.current').next().animate({top: '100px'}, mytime);
			$('ul.news li.current').next().addClass('next');
		}
		curelem.animate({top: '-100px'}, mytime);
	if (start)
		slideshowTimer = setTimeout('doSlideshow(true);', 3000);
}


