jQuery(document).ready(function($) {
	(function($){
	$('#round_max, #round_min').css({'width':25, 'height':26, 'margin-left':12, 'margin-top':37});
	 $('#sign_min, #sign_max').rotate({
         duration:1,
         angle: 0, 
         animateTo:-180,
         callback: function() {
        	 $('#sign_min').css({'top':42, 'left':16});
        	  $('#sign_max').css({'top':41, 'left':15});
        	 
         }
       });
	
	
	$('#sign_max').css({'top':41, 'left':16});
	
	$('#slider_max, #slider_min').hover(
		function () {
			$(this).find('.round').animate({ 
			        width: 30, 
			        height: 30,
			        marginLeft: 10 ,
			        marginTop:35
			        }, 50 );
			      
	    }, function () {
	    	$(this).find('.round').animate({ 
			        width: 26, 
			        height: 26,
			        marginLeft: 12 ,
			        marginTop:37
			        }, 50 );
	    }); 
	
	var slider_items = $('#slider_ul').children('li').size();
	var current_position = 0;
	var	max_position = slider_items - 5;
	var step = $('#slider_ul').children('li').eq(0).outerWidth();
	var autoslide_interval_value = parseInt($('.autoslide_settings').find('.autoslide_interval').html());
	
	$('#slider_ul').css('width', slider_items*180);
	var $slider_content = $('#sl_content');
	
	function move_slider_to_position( new_position ) {
		step = $('#slider_ul').children('li').eq(0).outerWidth();
		//console.log( 'step:' + step );
		//console.log( new_position );
		if( slider_items <= 5 ) return;
		$slider_content.stop();
		if( new_position > max_position ) {
			new_position = max_position;
			var current = step*new_position;
			//alert( current );
			$slider_content.animate({marginLeft: -current - 20}, 300, function() {
				$(this).animate({marginLeft: -current}, 80);
			});
			return;
		}
		
		if( new_position < 0 ) {
			$slider_content.animate({marginLeft: 20}, 300, function() {
				$(this).animate({marginLeft: 0}, 80);
			});
			return
		}
		
		
		var new_pos_margin = step*new_position;
		$slider_content.animate({marginLeft: - new_pos_margin}, 300);
		current_position = new_position;
	}	
	
	
	
	function move_slider( left_or_right ) {
		
		
		if( left_or_right == 'left' ) {
			move_slider_to_position( current_position + 1 )
		} else { 

				move_slider_to_position( current_position -1 )
			}

	}
	
	
	
	 $('#slider_max').click(function() {
		 clearInterval( auto_slide_interval );
		 //switch_slide( $(this) );
		 //$('#sign_max').css('left', 17);
		 $('#sign_max').rotate({
	          duration:500,
	          angle: 0, 
	          animateTo:-180
	        });
		 
		 move_slider('right');
		 
	 });
	 
	 $('#slider_min').click(function() {
		 clearInterval( auto_slide_interval );
		 //switch_slide( $(this) );
		 //$('#sign_min').css({'top':42, 'left':17});
		 $('#sign_min').rotate({
	          duration:500,
	          angle: 0, 
	          animateTo:-180
	        });
		 
		 move_slider('left');
	 });	
	 
	 function change_caption() {
		 
	 }
	 
	 var $current_li = '';
	 
	 var current_li_index = 0;
	 
	 $('.bigimg').click(function() {
		 var action = $(this).attr('data-action');
		 //alert( action );
		 if( action == 'do_nothing' ) return false;
		 
		 var url = $('#slider_ul').children('li').eq(current_li_index).find("a.slider_desc").attr("href");
		 
		 if( action == 'url' ) {
			 window.location = url;
		 } else {
			 $.prettyPhoto.open(url,'','');
		 }
		 
		 return false;
	 });
	 
	 function switch_slide( $li ) {
		 current_li_index = $li.index();
		 $("#slider_main_img").fadeTo(150, 0);   //  fade the MAIN IMAGE to WHITE. And when is the main image white, we call the callback function 
	     $li.fadeTo(150, 0.5,  function() {
	          $("#slider_post_cat").html($li.find("div.slider_tit").html());
	         // $("#slider_img_tit").attr("href", $(this).find("a.slider_tit").attr("href"));
	            
	          var slider_action = $li.find('.slider_action').html();
	          $('.bigimg').attr('data-action', slider_action);
	          
	          $("#slider_img_desc").html($li.find("a.slider_desc").html());
	          $("#slider_img_desc").attr("href", $(this).find("a.slider_desc").attr("href"));
	          
	          // FLIPPING THE IMAGE
	          $("#slider_main_img").attr("src",$li.find("a.slider_img").attr("href")); 
	          $("#slider_main_img").fadeTo(150, 1); // FADING MAIN IMAGE TO NORMAL
	 	});
	    $('#featured_home li').fadeTo(5, 1);      // FADE ALL SLIDER IMAGES TO NORMAL
	    $li.fadeTo(150, 1);
	 }
	
	 $('#featured_home li').click(function() {
		 switch_slide( $(this) );
		 clearInterval( auto_slide_interval );
	 });
	
	 var auto_slide_current = 0;
	 var auto_slide_interval = null;
	 
	 if( autoslide_interval_value > 0 ) {
		 auto_slide_interval = setInterval( auto_slide, autoslide_interval_value );
	 }
	 function auto_slide() {
		 auto_slide_current++;
		 if( auto_slide_current >= (slider_items) )  {auto_slide_current = 0; move_slider_to_position(0)}
		 
		 var move_to_pos = auto_slide_current -5 + 1;
		 
		 if( move_to_pos > 0 ) move_slider_to_position( move_to_pos );
		 
		 
		 $li = $('#featured_home').find('li').eq(auto_slide_current);
		 switch_slide( $li );
	 }
	 
	 
	})($);
});