(function( $ ){
	

	$.fn.promo = function(speed,duration){
		var $this = $(this);
		$('.switcher li').clearQueue();
		$('.switcher li').unbind('click');

		var i = $this.index();
		//alert(i);
	   
		var $activeSlide = $('.slider li.active');
		var $thisSlide = $('.slider li').eq(i);
		$activeSlide.attr('class','');
		$thisSlide.attr('class','active');

		var offset = $activeSlide.position().left - $thisSlide.position().left;
		if (offset >= 0){
			$('.slider').animate({right: "-="+offset }, 700, 'swing');
		} else {
			$clone = $thisSlide.clone();
			$clone.insertAfter($activeSlide).attr('class','clone');
			offset = $activeSlide.position().left - $clone.position().left;
			$('.slider').animate({right: "-="+offset }, 700, 'swing', function(){
				$('.slider').css('right',($thisSlide.position().left-$('.slider').width())+$thisSlide.width());
				$('.slider li.clone').remove();
			});
		}
		
		$this.attr('class', 'selected');
		$('div.gallery-box').animate({height: "0px"}, 700, 'swing' , function(){
			$(this).delay(400).animate({height: '340px'}, 700, 'swing', function(){
				$('.switcher li').bind('click', $.fn.promo);
			});
			
			$('.switcher li.active').attr('class','');
			$('.switcher li.selected').attr('class','active');
			
			var i = $('.switcher li.active').index();
			$('.holder li.active').attr('class','');
			$('.holder li').eq(i).attr('class','active');
		});
		
		var next;
		(i == $this.siblings().length)? next=0 : next= (i+1);
		
		$this.delay(9000).queue(function() {
		$('.switcher li').eq(next).promo(speed,duration);
		$this.dequeue(); 
		return $this;
		});
		
	};
})( jQuery );


$(document).ready(function(){
	$('.switcher li').click(function(){
		//$(this).promo(500,9000);
		$(this).promo(1000,9000);
        $(this).blur();
    });
	
    $('div.cloud').mouseenter(function(){
        $this = $(this);
        $this.animate({left: '+=6'}, 200, 'swing').animate({left: '-=10'}, 200, 'swing').animate({left: '+=4'}, 200, 'swing');
    });
    
    $('.switcher li').eq(1).delay(9000).queue(function(){
		//$(this).promo(500,9000);
		$(this).promo(1000,9000);
		//$(this).dequeue();
	});

    if ($("#sidebar .current_page_item").has("ul.children").length<= 0){
        $("#sidebar .current_page_item").addClass('active').removeClass('current_page_item');
    }
	$('.menu li.active').has('ul').addClass('issubmenu');
	
});


