/*
 * jQuery infinitecarousel plugin
 * @author admin@catchmyfame.com - http://www.catchmyfame.com
 * @version 1.0
 * @date April 29, 2009
 * @category jQuery plugin
 * @copyright (c) 2009 admin@catchmyfame.com (www.catchmyfame.com)
 * @license CC Attribution-No Derivative Works 3.0 - http://creativecommons.org/licenses/by-nd/3.0/
 * 
 * Modified by Overhaul Industres for fade transitions instead of sliding
 * 
 */

(function(jQuery){
	jQuery.fn.extend({ 
		infiniteCarousel: function(options)
		{
			var defaults = 
			{
				transitionSpeed : 1500,
				displayTime : 6000,
				textholderHeight : .2,
				displayProgressBar : 1
			};
		var options = jQuery.extend(defaults, options);
	
    		return this.each(function() {
    			var randID = Math.round(Math.random()*100000000);
				var o=options;
				var obj = jQuery(this);

				var numImages = jQuery('img', obj).length; // Number of images
				var imgHeight = jQuery('img:first', obj).height();
				var imgWidth = jQuery('img:first', obj).width();
				var autopilot = 1;
			
				jQuery('p', obj).hide(); // Hide any text paragraphs in the carousel
				jQuery(obj).width(imgWidth).height(imgHeight);
			
				// Build progress bar
				if(o.displayProgressBar)
				{
					jQuery(obj).append('<div id="progress'+randID+'" style="position:absolute;bottom:0;background:#bbb;left:'+jQuery(obj).css('paddingLeft')+'"></div>');
					jQuery('#progress'+randID).width(imgWidth).height(5).css('opacity','.5');
				}
			
				// Move last image and stick it on the front
				jQuery(obj).css({'overflow':'hidden','position':'relative'});
				jQuery('li:last', obj).prependTo(jQuery('ul', obj));
				jQuery('ul', obj).css('left',-imgWidth+'px');
			
				// Build textholder div thats as wide as the carousel and 20%-25% of the height
				jQuery(obj).append('<div id="textholder'+randID+'" class="textholder" style="position:absolute;bottom:0px;margin-bottom:'+-imgHeight*o.textholderHeight+'px;left:'+jQuery(obj).css('paddingLeft')+'"></div>');
				var correctTHWidth = parseInt(jQuery('#textholder'+randID).css('paddingTop'));
				var correctTHHeight = parseInt(jQuery('#textholder'+randID).css('paddingRight'));
				jQuery('#textholder'+randID).width(imgWidth-(correctTHWidth * 2)).height((imgHeight*o.textholderHeight)-(correctTHHeight * 2)).css({'backgroundColor':'#FFF','opacity':'0.6'});
				showtext(jQuery('li:eq(1) p', obj).text());
			
				// Prev/next button(img) 
				html = '<div id="btn_rt'+randID+'" style="position:absolute;right:0;top:'+((imgHeight/2)-15)+'px"><a href="javascript:void(0);"><img style="border:none;margin-right:2px" src="wp-content/themes/overhaul/images//rt.png" /></a></div>';
				html += '<div id="btn_lt'+randID+'" style="position:absolute;left:0;top:'+((imgHeight/2)-15)+'px"><a href="javascript:void(0);"><img style="border:none;margin-left:2px" src="wp-content/themes/overhaul/images//lt.png" /></a></div>';
				jQuery(obj).append(html);
			
				// Pause/play button(img)	
				html = '<a href="javascript:void(0);"><img id="pause_btn'+randID+'" src="wp-content/themes/overhaul/images//pause.png" style="position:absolute;top:3px;right:3px;border:none" alt="Pause" /></a>';
				html += '<a href="javascript:void(0);"><img id="play_btn'+randID+'" src="wp-content/themes/overhaul/images//play.png" style="position:absolute;top:3px;right:3px;border:none;display:none;" alt="Play" /></a>';
				jQuery(obj).append(html);
				jQuery('#pause_btn'+randID).css('opacity','.5').hover(function(){jQuery(this).animate({opacity:'1'},250)},function(){jQuery(this).animate({opacity:'.5'},250)});
				jQuery('#pause_btn'+randID).click(function(){
					autopilot = 0;
					jQuery('#progress'+randID).stop().fadeOut();
					clearTimeout(clearInt);
					jQuery('#pause_btn'+randID).fadeOut(250);
					jQuery('#play_btn'+randID).fadeIn(250);
					showminmax();
				});
				jQuery('#play_btn'+randID).css('opacity','.5').hover(function(){jQuery(this).animate({opacity:'1'},250)},function(){jQuery(this).animate({opacity:'.5'},250)});
				jQuery('#play_btn'+randID).click(function(){
					autopilot = 1;
					anim('next');
					jQuery('#play_btn'+randID).hide();
					clearInt=setInterval(function(){anim('next');},o.displayTime+o.transitionSpeed);
					setTimeout(function(){jQuery('#pause_btn'+randID).show();jQuery('#progress'+randID).fadeIn().width(imgWidth).height(5);},o.transitionSpeed);
				});
				
				// Left and right arrow image button actions
				jQuery('#btn_rt'+randID).css('opacity','.75').click(function(){
					autopilot = 0;
					jQuery('#progress'+randID).stop().fadeOut();
					anim('next');
					setTimeout(function(){jQuery('#play_btn'+randID).fadeIn(250);},o.transitionSpeed);
					clearTimeout(clearInt);
				}).hover(function(){jQuery(this).animate({opacity:'1'},250)},function(){jQuery(this).animate({opacity:'.75'},250)});
				jQuery('#btn_lt'+randID).css('opacity','.75').click(function(){
					autopilot = 0;
					jQuery('#progress'+randID).stop().fadeOut();
					anim('prev');
					setTimeout(function(){jQuery('#play_btn'+randID).fadeIn(250);},o.transitionSpeed);
					clearTimeout(clearInt);
				}).hover(function(){jQuery(this).animate({opacity:'1'},250)},function(){jQuery(this).animate({opacity:'.75'},250)});
			
				function showtext(t)
				{
					// the text will always be the text of the second list item (if it exists)
					if(t != '')
					{
						jQuery('#textholder'+randID).text(t).animate({marginBottom:'0px'},500); // Raise textholder
						showminmax();
					}
				}
				function showminmax()
				{
						if(!autopilot)
						{
							html = '<img style="position:absolute;top:2px;right:18px;display:none;cursor:pointer" src="wp-content/themes/overhaul/images//down.png" title="Minimize" alt="minimize" id="min" /><img style="position:absolute;top:2px;right:18px;display:none;cursor:pointer" src="wp-content/themes/overhaul/images//up.png" title="Maximize" alt="maximize" id="max" />';
							html += '<img style="position:absolute;top:2px;right:6px;display:none;cursor:pointer" src="wp-content/themes/overhaul/images//close.png" title="Close" alt="close" id="close" />';
							jQuery('#textholder'+randID).append(html);
							jQuery('#min').fadeIn(250).click(function(){jQuery('#textholder'+randID).animate({marginBottom:(-imgHeight*o.textholderHeight)-(correctTHHeight * 2)+24+'px'},500,function(){jQuery("#min,#max").toggle();});});
							jQuery('#max').click(function(){jQuery('#textholder'+randID).animate({marginBottom:'0px'},500,function(){jQuery("#min,#max").toggle();});});
							jQuery('#close').fadeIn(250).click(function(){jQuery('#textholder'+randID).animate({marginBottom:(-imgHeight*o.textholderHeight)-(correctTHHeight * 2)+'px'},500);});
						}
				}
				function anim(direction)
				{
					// Fade left/right arrows out when transitioning
					jQuery('#btn_rt'+randID).fadeOut(500);
					jQuery('#btn_lt'+randID).fadeOut(500);
					
					// animate textholder out of frame
					jQuery('#textholder'+randID).animate({marginBottom:-imgHeight*.25+'px'},500);
					
					//?? Fade out play/pause?
					jQuery('#pause_btn'+randID).fadeOut(250);
					jQuery('#play_btn'+randID).fadeOut(250);
			
					if(direction == "next")
					{
						// Copy leftmost (first) li and insert it after the last li
						jQuery('li:first', obj).clone().insertAfter(jQuery('li:last', obj));	
						// Update width and left position of ul and animate ul to the left
						jQuery('ul', obj)
							.width(imgWidth*(numImages+1))
							.animate({left:-imgWidth*1},o.transitionSpeed,function(){
								jQuery('li:first', obj).fadeOut(500).remove();
								jQuery('ul', obj).css('left',-imgWidth+'px').width(imgWidth*numImages).fadeIn(500);
								jQuery('#btn_rt'+randID).fadeIn(500);
								jQuery('#btn_lt'+randID).fadeIn(500);
								if(autopilot) jQuery('#pause_btn'+randID).fadeIn(500);
								showtext(jQuery('li:eq(1) p', obj).text());
								if(autopilot)
								{
									jQuery('#progress'+randID).width(imgWidth).height(5);
									jQuery('#progress'+randID).animate({'width':0},o.displayTime,function(){
										jQuery('#pause_btn'+randID).fadeOut(500);
										setTimeout(function(){jQuery('#pause_btn'+randID).fadeIn(500)},o.transitionSpeed)
									});
								}
							});
					}
					if(direction == "prev")
					{
						jQuery('ul', obj)
							.fadeOut(500)
							.width(imgWidth*(numImages+1))
							//.css('left',-imgWidth*1+'px')
							.animate({left:-imgWidth},o.transitionSpeed,function(){
								jQuery('li:last', obj).remove();
								jQuery('ul', obj).width(imgWidth*numImages).fadeIn(500);
								// Copy rightmost (last) li and insert it after the first li
								jQuery('li:last', obj).clone().insertBefore(jQuery('li:first', obj));
								jQuery('#btn_rt'+randID).fadeIn(500);
								jQuery('#btn_lt'+randID).fadeIn(500);
								if(autopilot) jQuery('#pause_btn'+randID).fadeIn(500);
								showtext(jQuery('li:eq(1) p', obj).text());
							});
					}
				}
				var clearInt = setInterval(function(){anim('next');},o.displayTime+o.transitionSpeed);
				jQuery('#progress'+randID).animate({'width':0},o.displayTime+o.transitionSpeed,function(){
					jQuery('#pause_btn'+randID).fadeOut(100);
					setTimeout(function(){jQuery('#pause_btn'+randID).fadeIn(250)},o.transitionSpeed)
				});
  		});
    	}
	});
})(jQuery);


