/* 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 $items = $container.children(".gallery_image");
		var $currentItem;
		var $slidingDiv;
		var $fading = false;
		var $is_moving = false;
		var $interval; 
		
		var $contentContainer = $("aside");
		var $contentItems = $contentContainer.children("#aside_all");

		var $howMany = $items.length;
		var $currentIndex = 0;
		var $contentID;
		
		$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);
			$items = $slidingDiv.children(".gallery_image");
			$currentItem = $items[0]; 
			$container.mouseover(handleHover).mouseout(handleHover);
			$interval = setTimeout( hideControls, 8000 );			
		};
		
		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("fast");
			}
		};
		
		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;

				//get the next set of "white arrow" content panes
				$currentContentID = "#white_arrows_" + $currentIndex;
				if($currentIndex == ($howMany - 1)) {
					$currentIndex = 0;
				} else {
					$currentIndex++;
				}
				$($currentContentID).fadeOut("fast", function() {
					$newContentID = "#white_arrows_" + $currentIndex;
					$($newContentID).fadeIn("fast", function(){ heightFix() });
				});
				//fix the height

				//change the rotator bullets
				$("#rotator_bullets li.selected").addClass('old'); //mark current as old
				if($("#rotator_bullets li.selected").is(":last-child")) {
					$("#rotator_bullets li:first").addClass('selected');
				} else {
					$("#rotator_bullets li.selected").next().addClass('selected');
				};

				$("#rotator_bullets .old").removeClass('selected old');
			}

		};
		
		function nextItem(){
			if($($currentItem).next().length == 0){
				return $items[0];
			}else{
				return $($currentItem).next()[0];
			}
		};
		
		function prevItem(){
			if($($currentItem).prev().length == 0){
				return $items[$items.length - 1];
			}else{
				return $($currentItem).prev()[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;
				
				//get the previous set of "white arrow" content panes
				$currentContentID = "#white_arrows_" + $currentIndex;
				if($currentIndex == (0)) {
					$currentIndex = ($howMany - 1);
				} else {
					$currentIndex--;
				}
				$($currentContentID).fadeOut("fast", function() {
					$newContentID = "#white_arrows_" + $currentIndex;
					$($newContentID).fadeIn("fast", function(){ heightFix() });
				});

				//change the rotator bullets
				$("#rotator_bullets li.selected").addClass('old'); //mark current as old
				if($("#rotator_bullets li.selected").is(":first-child")) {
					$("#rotator_bullets li:last").addClass('selected');
				} else {
					$("#rotator_bullets li.selected").prev().addClass('selected');
				};

				$("#rotator_bullets .old").removeClass('selected old');
				
			}			
		};
		

		
		function slidingComplete(){
			$is_moving = false;
		}
	};
	
	$.ImageSlider.defaults = {
		imageSelector: "li.gallery_image",
		width: 893,
		duration: "slow"
	};
	
})(jQuery);
