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).width();
	var autoslide_interval_value = parseInt($('.autoslide_settings').find('.autoslide_interval').html());
	$('#slider_ul').css('width', slider_items*190);
	var $slider_content = $('#sl_content');
	
	function move_slider_to_position( 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', 15);
		 $('#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':16});
		 $('#sign_min').rotate({
	          duration:500,
	          angle: 0, 
	          animateTo:-180
	        });
		 
		 move_slider('left');
	 });	
	 
	 
	
	
	 $('#featured_home li').click(function() {
		 clearInterval( auto_slide_interval );
		 switch_slide( $(this) );
	 });
	

	 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 );
	 }
	 var current_li_index = 0;
	 $('#slider_freshcubes').click(function() {
		 //alert(current_li_index);
		 //return;
		 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();
		 
		 $('.fresh_cube').parent().attr('rel','');
		 $('.fresh_cube:animated').parent().attr('rel', 'animating');
		 
		 if( $('.fresh_cube').parent().attr('rel') != 'animating') {
			 
			 $("#slider_post_cat").html($li.find("div.slider_tit").html());
	         
			 var slider_action = $li.find('.slider_action').html();
	          $('#slider_freshcubes').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"));
			 
			 var img_id = $li.index();
			 
		      //actual_img = img_id;
			 //$('#fresh_cube_nav_wrapper li').removeClass('active');
		     //$('#fresh_cube_nav_wrapper li').eq(img_id).addClass('active');
			 var new_img_src = $('.fresh_cube_image_'+img_id).find('img').attr('src');
			 var actual_img_src = $('#slider_freshcubes').css('background-image');
		    
		    //alert(actual_img_src);
		   // alert(img_src);
			 reset_grid();
			 $('.fresh_cube').css('background-image', actual_img_src);// 'url('+img_src+')');
			 $('.fresh_cube').css('display', 'block');
			 $('.fresh_cube').css('opacity', 1);
		     
			 $('#slider_freshcubes').css('background-image', 'url('+new_img_src+')');
			 $('.post_info_holder').html($('.fresh_cube_image_'+img_id).find('.transition').html());  
			 var transition_name =  $('.fresh_cube_image_'+img_id).find('.transition').attr('title');
			 if( transition_name == undefined ) transition_name = 'random';
			 transition(transition_name);
			 var $li2 = $('#slider_ul').children('li').eq( $li.index() );
			 
			 $li2.fadeTo(150, 0.5,  function() {
				 
				 $li2.fadeTo(150,1);
			 });
		   } 
		 
		 /*
		 $("#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"));
	            
	          $("#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);*/
	 }
	 

	  function transition(name)
	  {
		  
		  if(name == undefined || name == '' || name == 'fly_right_top' ) name = 'random';
	    // random, basic, fly
	  //  alert(name);
	    if(name == 'random') {transition_random();}
	    else if( name == 'lcp') {transition_lcp();}
	    else if (name == 'basic') {transition_basic();}
	    else if( name == 'fly') {transition_fly();}
	    else if( name == 'flyback') {transition_flyback();}
	    else if( name == 'lines') {transition_lines();}
	    else if( name == 'column') {transition_column();}    
	  }
	  
	  function reset_grid()
	  {
	    for(var w = 0; w < 8; w++)
	    {
	      for(var h = 0; h < 3; h++)
	      {
	        $('#fresh_cube_'+w+'_'+h).css('width', 120);
	        $('#fresh_cube_'+w+'_'+h).css('height', 120);
	        $('#fresh_cube_'+w+'_'+h).css('top', h*120);
	        $('#fresh_cube_'+w+'_'+h).css('left', w*120);
	      }
	    }
	  }
	  function transition_random()
	  {
	    for(var w = 0; w < 8; w++)
	    {
	      for(var h = 0; h < 3; h++)
	      {
	        var hide_time = Math.random() * 700 + 400;
	        $('#fresh_cube_'+w+'_'+h).animate({opacity:0}, hide_time);
	      }
	    }
	  }
	  function transition_basic()
	  {
	    for(var w = 0; w < 8; w++)
	    {
	      for(var h = 0; h < 3; h++)
	      {
	        $('#fresh_cube_'+w+'_'+h).animate({opacity:0}, 500 + (w*150) );
	      }
	    }
	  }
	  function transition_fly()
	  {
	  var animate_time = 500;
	   for(var h = 0; h < 3; h++)
	    {
	       for(var w = 0; w < 8; w++)
	      {
	        animate_time = (w+h)*100;
	        
	        $('#fresh_cube_'+w+'_'+h).delay(animate_time).animate({opacity: -1,top:(h-1)*120, left:(w-1)*120}, 450);
	     //   $('#fresh_cube_'+w+'_'+h).delay(animate_time).animate({opacity:0}, {queue:false, duration:450/2});
	      }
	    }
	  }  
	  function transition_flyback()
	  {
	    var animate_time = 500;
	     for(var h = 0; h < 3; h++)
	      {
	         for(var w = 0; w < 8; w++)
	        {
	          animate_time = (w+h)*100;
	          
	          $('#fresh_cube_'+w+'_'+h).delay(animate_time).animate({opacity: -1,top:(h+1)*120, left:(w+1)*120}, 450);
	       //   $('#fresh_cube_'+w+'_'+h).delay(animate_time).animate({opacity:0}, {queue:false, duration:450/2});
	        }
	      }
	  }  
	    function transition_lines()
	  {
	    var animate_time = 500;
	     for(var h = 0; h < 3; h++)
	      {
	         for(var w = 0; w < 8; w++)
	        {
	          animate_time = (w+1)*50;
	          
	          if(h==1)
	          {
	            $('#fresh_cube_'+(7-w)+'_'+h).delay(animate_time).animate({opacity: 0}, 450);
	          }
	          else
	          {
	            $('#fresh_cube_'+w+'_'+h).delay(animate_time).animate({opacity: 0}, 450);
	          }
	       //   $('#fresh_cube_'+w+'_'+h).delay(animate_time).animate({opacity:0}, {queue:false, duration:450/2});
	        }
	      }
	  } 
	  function transition_lcp()
	  {
	          // $('#slider_freshcubes').stop().animate({opacity: 0}, 1);
	      var act_bg_img = $('#slider_freshcubes').css('background-image');
	      var counter = 1;
	       $('#slider_freshcubes').css('background-image', '');
	      $('.fresh_cube').stop().animate({opacity: 0}, 190,function(){
	      if(counter == 1)
	      {
	          $('#slider_freshcubes').css('display','none');
	          $('#slider_freshcubes').css('background-image', act_bg_img);
	          $('#slider_freshcubes').animate({opacity: 0}, 1);
	          $('#slider_freshcubes').css('display','block');   
	            $('#slider_freshcubes').animate({opacity: 1}, 190);
	        }
	        counter++;
	      } );
	      
	   // $('.fresh_cube').css('width',0);
	    //$('.fresh_cube').animate({opacity: 0}, 0);
	  } 
	  function transition_column()
	  {
	        $('.fresh_cube').animate({width:0, opacity:0}, 500);
	  }
	 
	})($);
});