/* ImageSlider // jQuery 1.1.2+
 *
 * A jQuery plugin for creating simple image galleries with a slide transition
 * v0.0.2
 *
 * ADAM MILLER <adam@barbariangroup.com>
 * The Barbarian Group, LLC. http://thebarbariangroup.com/
 *
 */
 
(function($) { 
	
	$.fn.image_slider = function(options){
		options = $.extend({}, $.ImageSlider.defaults, options);
		return this.each(function() {
			new $.ImageSlider(this, options);
		});
	};
	
	$.ImageSlider = function(container, options){
		var $container = $(container);
		var $items = $container.children(options.imageSelector);
		var $currentItem;
		var $slidingDiv;
		var $fading = false;
		var $is_moving = false;
		var $interval; 
		
		$container.bind("nextImage", function(){nextImage();});
		$container.bind("prevImage", function(){prevImage();});
		
		if($items.length > 1){
			init();
		}
		

		
		function init(){
			var x = 0;
			$items.each(function(){
				$(this).attr("style", "position:absolute; top:0; left:"+x+"px;");
				x = x + options.width;
			});
			
			$slidingDiv = $("<div style=\"position:absolute; top:0; left:0;\" ></div>");
			$items.clone().appendTo($slidingDiv);
			$items.remove();
			$slidingDiv.appendTo($container);
			$items = $slidingDiv.children(options.imageSelector);
			$currentItem = $items[0]; 
			$container.mouseover(handleHover).mouseout(handleHover);
			$interval = setTimeout( hideControls, 3000 );
		};
		
		function handleHover(e){
			// next three lines copied from jQuery.hover, ignore children onMouseOver/onMouseOut
			var p = (e.type == "mouseover" ? e.fromElement : e.toElement) || e.relatedTarget;
			while ( p && p != this ) { try { p = p.parentNode; } catch(e) { p = this; } }
			if ( p == this ) { return false; }

			// copy objects to be passed into t (required for event object to be passed in IE)
			var ev = $.extend({},e);
			var ob = this;
			
			// cancel hoverIntent timer if it exists
			if ($interval) { $interval = clearTimeout($interval); }

			// else e.type == "onmouseover"
			if (e.type == "mouseover") {
				showControls();
			// else e.type == "onmouseout"
			} else {
				// if hoverIntent state is true, then call the mouseOut function after the specified delay
				//ob.hoverIntent_t = setTimeout( function(){delay(ev,ob);, options.timeout );
					hideControls();
			}
		};
		
		function hideControls(){
			if($('#image_gallery_controls li').queue("fx").length == 0 ){
				$('#image_gallery_controls li').fadeOut("slow");
			}
		};
		
		function showControls(){
			if($('#image_gallery_controls li').queue("fx").length == 0 ){
				$('#image_gallery_controls li').fadeIn("fast");
			}
		};
		
		function nextImage(){
			if(!$is_moving){
				$is_moving = true;
				var pos = parseFloat($slidingDiv[0].style.left) * -1 + options.width;
				var nextImage = nextItem();
				if(nextImage.offsetLeft < pos){
					$(nextImage).attr("style", "position:absolute; left:"+pos+"px;");
				}
				$slidingDiv.animate({"left":"-="+options.width+"px"}, {duration: options.duration, complete:slidingComplete});
				$currentItem = nextImage;
			}
		};
		
		function nextItem(){
			if($($currentItem).next(options.imageSelector).length == 0){
				return $items[0];
			}else{
				return $($currentItem).next(options.imageSelector)[0];
			}
		};
		
		function prevItem(){
			if($($currentItem).prev(options.imageSelector).length == 0){
				return $items[$items.length - 1];
			}else{
				return $($currentItem).prev(options.imageSelector)[0];
			}
		};
		
		function prevImage(){
			if(!$is_moving){
				$is_moving = true;
				var pos = parseFloat($slidingDiv[0].style.left) * -1 - options.width;
				var nextImage = prevItem();
				if(nextImage.offsetLeft > pos){
					$(nextImage).attr("style", "position:absolute; left:"+pos+"px;");
				}
				$slidingDiv.animate({"left":"+="+options.width+"px"}, {duration:options.duration, complete:slidingComplete});
				$currentItem = nextImage;
			}
		};
		
		function slidingComplete(){
			$is_moving = false;
		}
	};
	
	$.ImageSlider.defaults = {
		imageSelector: "li.gallery_image",
		width: 1256,
		duration: "slow"
	};
	
})(jQuery);