jQuery.noConflict();

jQuery(document).ready(function()
{	
	var autorotate = 1;
	var autorotate_duration = 4000;
	/*image slider (levitation)*/
	if (jQuery('.featured_item').length > 0 )jQuery('#featured').levitate({  
			duration:600, 					// transition duration 
			transition:"easeInOutCubic", 	// transition easing
			opacity:0.6,					// opacity of second row
			opacity_level2:0,				// opacity of third row
			interval: autorotate_duration	// interval between auto rotate, set to false if you want to disable outorotation
		});
});

jQuery(window).load(function(){
	/*image slider (alternate)*/
	var autorotate = 1;
	var autorotate_duration = 4000;	
	if (jQuery('.featured_alternate').length > 0 && autorotate == 1)
	{	
		jQuery('.featured_alternate').not('.featured_alternate_active').css({"display":"none"});
		// set the automatic image rotation, number is time between transitions in miliseconds
		interval = setInterval(function() { k_fader(".featured_alternate",'1'); }, autorotate_duration); 	
	}
});

function k_fader($items_to_fade, $next_or_prev)
{	
	var $items = jQuery($items_to_fade);
	var $currentitem = $items.filter(":visible");
	var $new_item;
	var $selector;
	
	if($items.length > 1)
	{
		for(i = 0; i < $items.length; i++)
		{
			if($items[i] == $currentitem[0])
			{	
				$selector = $next_or_prev >= 0 ? i != $items.length-1 ? i+1 : 0 : i == 0 ? $items.length-1 : i-1;
				
				$new_item = jQuery($items[$selector]);
				break;
			}
		}
		
		if( $new_item.css("display") == "none" )
			{	
				$currentitem.css({zIndex:1});
				$new_item.css({zIndex:2}).fadeIn(1200, function()
				{
					$currentitem.css({display:"none"});
				});
				
			}
	}
}




(function($)
{ 
	$.fn.levitate = function(default_options) 
	{
		var defaults = 
		{  
			duration:600, 					// transition duration 
			transition:"easeInOutCubic", 	
			opacity:0.6,
			opacity_level2:0,
			interval: 5000
			
		};  
		
		var def = $.extend(defaults, default_options); 
		
		return this.each(function()
		{	
			// variables and elements we need
			var $container = $(this), $items = $container.find('.featured_item'), $values = [], $zindex = [], $offset = 0, $animating = false, $clicked=false;
			
			var interval = setInterval(function(){}, 50000);//fake interval needed to circumvent some js errors in not-so-good-browsers ;D
			
			jQuery(window).load(function(){
				if(def.interval && !$clicked )
					{
					interval = setInterval(function() { rotate(-1); }, def.interval); 
					}
				});
		
			//sets the opacity for elements in JS since its not yet a valid css declaration (if user wants opacity):
			if (def.opacity != 1 && def.opacity_level2 != 1)
			{	$items.not('.featured_item_active').css('opacity',def.opacity_level2);
				$container.find('.featured_item_active').css('opacity', 1);
				$container.find('.featured_item_last, .featured_item_upcoming').css('opacity', def.opacity);
				
			}
			//gets the values for each element by extracting the css, this way we can use unlimited items
			$items.each(function(i)
			{	
				var $item = $(this);
				 
				$values[i]= {
								width: $item.width(),
								top: parseInt($item.css('top')),
								left: parseInt($item.css('left')),
								opacity: $item.css('opacity')
							};
							
				$zindex[i] =	$item.css('zIndex');
							
			}); // end each loop
			
			
			$items.click(function(e)
			{	
				if (! $animating)
				{	
					$direction = e.pageX > $(window).width() / 2 ? -1 : 1;
					rotate($direction);
				}
				clearInterval(interval);
				$clicked = true;
			}); // end click
			
			
			function rotate($direction)
			{	
				if ($items.length <= 2) return;
				$animating = true;
				
				if($items.length == $offset || $items.length == ($offset*-1))
				{
					$offset = 1 * $direction;
				}
				else
				{
					$offset = $offset + $direction;
				}
								
				
				//$offset = $items.length == $offset ? 1 : $offset + 1;
				$items.each(function(i)
				{	
					var $item = $(this), $next;
					
					$next = i + $offset;
					
						if($next >= $items.length)
						{
							$next = i - $items.length + $offset;
						}
						else if($next < 0)
						{
							$next = i + $items.length + $offset;
						}
					
					//modifier of -12 for images because of border + padding				
					$item.animate($values[$next], def.duration, def.transition);
					$item.find("img").animate({width:$values[$next].width-12}, def.duration, def.transition, function()
					{
						$animating = false;
					});
					
					setTimeout(function()
					{
        				$item.css({zIndex: $zindex[$next]});
    				}, def.duration / 2);
    				
				});
			} //end rotate
		});	
	};
})(jQuery); 


jQuery.extend( jQuery.easing,
{
	easeInOutCubic: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t + b;
		return c/2*((t-=2)*t*t + 2) + b;
	}
});
