$(function(){
	
	if($('select.custom-select').length){
		$('select.custom-select').selectbox();
	}
	
	$("#navigation ul li").hover(function() {
		if(!($(this).find(".dd").is(':animated')))
		{
			$(this).find(".dd").slideDown();
		}
			
		}, function(e) {
		 
		$(this).find(".dd").slideUp();
	});
	/*
	$('.sidebar .links a').hover(function(){
		$(this).animate({
			height : '125px'
		},{duration: 200, queue: false});
	},function(){
		$(this).animate({
			height : '31px'
		},{duration: 200, queue: false});
	});
	*/
	$('.blink').focus(function () {
		if ($(this).val() == $(this).attr('title')) {
			$(this).val('');
		}
	});
	
	$('.blink').blur(function () {
		if ($(this).val() == '') {
			$(this).val($(this).attr('title'));
		}
	});
	
	
	init_slider();
	
	$('a.size-edit').click(function(){
		var currSize = $('body').css('font-size');
		currSize = currSize.substring(0, currSize.length-2);
		
		if($(this).hasClass('size-plus')){
			currSize++;
			if(currSize<15){
				$('body').css('font-size', currSize);
			}
		}
		
		if($(this).hasClass('size-minus')){
			currSize--;	
			if(currSize>8){
				$('body').css('font-size', currSize);
			}
		}
		
		return false;
	})
	
	$('.inner-menu ul li a').click(function(){
		var target = $(this).attr('href');
		var topOffset = $(target).offset().top;
		
		$("html,body").animate({
			scrollTop: topOffset
		}, "slow");
		
		return false;
	});
	
	$('.publication').hover(function(){
		$(this).find('.img').animate({
			'top': '-5'
		}, { duration: 200, queue: false });
	},function(){
		$(this).find('.img').animate({
			'top': '0'
		}, { duration: 200, queue: false });
	});
	
	$('.sidebar ul li a').live('click',function(){
		if($(this).parent('li').find('ul').length){
			$(this).parent('li').find('ul:eq(0)').slideToggle();
			return false;
		}
	});
	
});


function init_slider() {
	// big slide selectors
	var big_slides = $('.slider-container li');
	var big_ul = big_slides.parent();
	var big_width = big_slides.eq(0).outerWidth();
	
	var dots = $('.slider-nav a');
	
	var first_li = big_ul.find('li:first').clone();
	var last_li = big_ul.find('li:last').clone();
	
	big_ul.append(first_li);
	big_ul.prepend(last_li);
	
	big_slides = $('.slider-container li');
	
	// indexes
	var num_slides = big_slides.length, interval;
	
	var index = 1;
	
	
	// set width
	big_slides.parent().css('width', big_width * num_slides);
	
	// set left
	big_slides.parent().css('left', -1 * index * big_width);
	
	// append overlays
	var overlay_left = $('<span class="overlay" style="left:0"></span>').appendTo($('.slider-container')).css('opacity', 0.8);
	var overlay_right = $('<span class="overlay" style="right:0"></span>').appendTo($('.slider-container')).css('opacity', 0.8);
	
	set_overlay_width();
	$(window).resize(set_overlay_width);
	
	show_big_slide();
	start_auto();
	
	function get_overlay_width() { 
		return ($(window).width() - big_width) / 2;
	}
	function set_overlay_width(){
		$('.slider-container .overlay').css('width', get_overlay_width());
	}
	
	// thumbs click events
	dots.live('mouseup', function(){
		var last_index = index;
		index = $(this).parent().find('a').index(this)+1;
		stop_auto();
		show_big_slide();
		start_auto();
		
		return false;
	});
	
	// show next slides
	function next_slide(){
		index++;
		if(index == num_slides-1) {
			index = 1;
		}
		show_big_slide();
	}
	
	function start_auto() {
		interval = window.setInterval(function(){
			next_slide();
		}, 8000);
	}
	function stop_auto(){
		window.clearInterval(interval);
	}
	
	// move big slides
	function show_big_slide(){
		if($.browser.msie && $.browser.version==6)
		{
			$('.slider-container li').eq(index-1).find('.slider-cnt').hide();
			$('.slider-container li').eq(index).find('.slider-cnt').show();
			$('.slider-container li').eq(index+1).find('.slider-cnt').hide();
		} else {
			$('.slider-container li').eq(index-1).find('.slider-cnt').fadeOut();
			$('.slider-container li').eq(index).find('.slider-cnt').fadeIn();
			$('.slider-container li').eq(index+1).find('.slider-cnt').fadeOut();
		}
		
		dots.removeClass('active');
		dots.eq(index-1).addClass('active');
		big_slides.parent().stop(true, true).animate({ 'left' : -1 * index * big_width });
		
	}
}
