/*
 * jQuery infinitecarousel plugin
 * @author admin@catchmyfame.com - http://www.catchmyfame.com
 * @version 1.0.1
 * @date July 31, 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/
 */

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

				var numImages = $('img', obj).length; // Number of images
				var imgHeight = 360;
				var imgWidth = 980;
				var autopilot = 1;
				var currentlyAnimating=false;
				var isWindowInBackground=false;
				if ($.browser.msie) {
					// IE EVENTS
					$(document).bind('focusout', function(){
						isWindowInBackground=true;
						//alert('IE vika window blur');
					});
				
					$(document).bind('focusin', function(){
						isWindowInBackground=false;
						//alert('IE vika window focus');
					});
				}
				else {
					$(window).focus(function() {
						isWindowInBackground=false;
						//alert('Other browsers vika window focus');
					});
					
					$(window).blur(function() {
						isWindowInBackground=true;
						//alert('Other browsers vika window blur');
					});
				}
				
				
				var playaFunc=function(){
					//The same as an arrow is clicked after a fixed image display
					var onlyShow=false;
					if (prevSlideHtml!=null) {
						onlyShow=true;
						var $slideul=$("#slide ul");
						
						$slideul.fadeOut("fast",function() {
							$slideul.html(prevSlideHtml);
							prevSlideHtml=null;
							$slideul.fadeTo("fast",1.0);
						});
					}
					// <---
					
					autopilot = 1;
					anim('next',onlyShow);
					clearInt=setInterval(function(){anim('next');},o.displayTime+o.transitionSpeed);
					setTimeout(function(){
						$('#progress'+randID).fadeIn().width(imgWidth).height(5);
					},o.transitionSpeed);
					$('#slide-play_btn').hide();
					$('#slide-pause_btn').show();
				}
				var pauzataFunc=function(){
					autopilot = 0;
					$('#progress'+randID).stop().fadeOut();
					clearTimeout(clearInt);
					$('#slide-pause_btn').fadeOut(0);
					$('#slide-play_btn').fadeIn(0);
					showminmax();
				};
			
				$('p', obj).hide(); // Hide any text paragraphs in the carousel
				$(obj).width(imgWidth).height(imgHeight);
			
				// Build progress bar
				if(o.displayProgressBar)
				{
					$(obj).append('<div id="progress'+randID+'" style="position:absolute;bottom:0;background:#bbb;left:'+$(obj).css('paddingLeft')+'"></div>');
					$('#progress'+randID).width(imgWidth).height(5).css('opacity','.5');
				}
			
				// Move last image and stick it on the front
				$(obj).css({'overflow':'hidden','position':'relative'});
				$('li:last', obj).prependTo($('ul', obj));
				$('ul', obj).css('left',-imgWidth+'px');
			
				// Build textholder div thats as wide as the carousel and 20%-25% of the height
				$(obj).append('<div id="textholder'+randID+'" class="textholder" style="position:absolute;bottom:0px;margin-bottom:'+-imgHeight*o.textholderHeight+'px;left:'+$(obj).css('paddingLeft')+'"></div>');
				var correctTHWidth = parseInt($('#textholder'+randID).css('paddingTop'));
				var correctTHHeight = parseInt($('#textholder'+randID).css('paddingRight'));
				$('#textholder'+randID).width(imgWidth-(correctTHWidth * 2)).height((imgHeight*o.textholderHeight)-(correctTHHeight * 2)).css({'backgroundColor':'#FFF','opacity':'0.5'});
				showtext($('li:eq(1) p', obj).html());
			
				// Prev/next button(img) 
			
				//<a href="#" class="slide-left-btn float-left"></a>
                //<a href="#" class="slide-right-btn float-right"></a> 
			
				html = '<a href="javascript:void(0);" id="btn_lt'+randID+'" class="slide-left-btn float-left"></a>';
				html += '<a href="javascript:void(0);" id="btn_rt'+randID+'" class="slide-right-btn float-right"></a>';
				$(obj).append(html);
			
				/* Pause/play button(img)	
				html = '<a href="javascript:void(0);"><img id="pause_btn'+randID+'" src="../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="../images/play.png" style="position:absolute;top:3px;right:3px;border:none;display:none;" alt="Play" /></a>';
				*/
				
				$('#slide-pause_btn').css('opacity','.5').hover(function(){$(this).animate({opacity:'1'},250)},function(){$(this).animate({opacity:'.5'},250)});
				$('#slide-pause_btn').click(pauzataFunc);
				$('#slide-play_btn').css('opacity','.5').hover(function(){$(this).animate({opacity:'1'},250)},function(){$(this).animate({opacity:'.5'},250)});
				$('#slide-play_btn').click(playaFunc);
				
				$(".pic-btn img").click(function(){				
				//The same as a pause button is clicked
				autopilot = 0;
				$('#progress'+randID).stop().fadeOut();
				clearTimeout(clearInt);
				$('#slide-pause_btn').fadeOut(250);
				$('#slide-play_btn').fadeIn(250);
				showminmax();
				// <---
				
				var imagepath = $(this).attr("src");
				var newpath = imagepath.replace ('small_','');
				
				//...
				var $slideul=$("#slide ul");
				if (prevSlideHtml==null) prevSlideHtml=$slideul.html();
				$slideul.fadeOut("fast",function() {
					$("#slide img").attr({ src: newpath});
					$slideul.fadeTo("slow",1.0);
				});
				
				
				return false;
				});
				
				// Left and right arrow image button actions
				$('#btn_rt'+randID).css('opacity','.75').click(function(){
					if (currentlyAnimating) return;
					
					var onlyShow=false;
					if (prevSlideHtml!=null) {
						onlyShow=true;
						var $slideul=$("#slide ul");
						
						$slideul.fadeOut("fast",function() {
							$slideul.html(prevSlideHtml);
							prevSlideHtml=null;
							$slideul.fadeTo("fast",1.0);
						});
					}
					if (autopilot==1) {
						pauzataFunc();
					}
					$('#progress'+randID).stop().fadeOut();
					anim('next',onlyShow);
					clearTimeout(clearInt);
				}).hover(function(){$(this).animate({opacity:'1'},250)},function(){$(this).animate({opacity:'.75'},250)});
				$('#btn_lt'+randID).css('opacity','.75').click(function(){
					if (currentlyAnimating) return;
					
					var onlyShow=false;
					if (prevSlideHtml!=null) {
						onlyShow=true;
						var $slideul=$("#slide ul");
						
						$slideul.fadeOut("fast",function() {
							$slideul.html(prevSlideHtml);
							prevSlideHtml=null;
							$slideul.fadeTo("fast",1.0);
						});
					}
					
					if (autopilot==1) {
						pauzataFunc();
					}
					
					$('#progress'+randID).stop().fadeOut();
					anim('prev',onlyShow);
					clearTimeout(clearInt);
				}).hover(function(){$(this).animate({opacity:'1'},250)},function(){$(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 != null)
					{
						$('#textholder'+randID).html(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="../images/down.png" title="Minimize" alt="minimize" id="min" /><img style="position:absolute;top:2px;right:18px;display:none;cursor:pointer" src="/js/infiniteCarousel/images/up.png" title="Maximize" alt="maximize" id="max" />';
							html += '<img style="position:absolute;top:2px;right:6px;display:none;cursor:pointer" src="../images/close.png" title="Close" alt="close" id="close" />';
							$('#textholder'+randID).append(html);
							$('#min').fadeIn(250).click(function(){$('#textholder'+randID).animate({marginBottom:(-imgHeight*o.textholderHeight)-(correctTHHeight * 2)+24+'px'},500,function(){$("#min,#max").toggle();});});
							$('#max').click(function(){$('#textholder'+randID).animate({marginBottom:'0px'},500,function(){$("#min,#max").toggle();});});
							$('#close').fadeIn(250).click(function(){$('#textholder'+randID).animate({marginBottom:(-imgHeight*o.textholderHeight)-(correctTHHeight * 2)+'px'},500);});
						}
				}
				function anim(direction,onlyShow)
				{
					if (!isWindowInBackground) {
						currentlyAnimating=true;
						if (onlyShow==null) onlyShow=false;
						// Fade left/right arrows out when transitioning
						$('#btn_rt'+randID).fadeOut(500);
						$('#btn_lt'+randID).fadeOut(500);
						
						// animate textholder out of frame
						$('#textholder'+randID).animate({marginBottom:(-imgHeight*o.textholderHeight)-(correctTHHeight * 2)+'px'},500);					
	
						//?? Fade out play/pause?
						$('#pause_btn'+randID).fadeOut(250);
						$('#play_btn'+randID).fadeOut(250);
				
						if(direction == "next")
						{
							// Copy leftmost (first) li and insert it after the last li
							if (!onlyShow) $('li:first', obj).clone().insertAfter($('li:last', obj));	
							// Update width and left position of ul and animate ul to the left
							$('ul', obj)
								.width(imgWidth*(numImages+1))
								.animate({left:-imgWidth*2},200,function(){
									if (!onlyShow) $('li:first', obj).remove();
									$('ul', obj).css('left',-imgWidth+'px').width(imgWidth*numImages);
									$('#btn_rt'+randID).fadeIn(500);
									$('#btn_lt'+randID).fadeIn(500);
									if(autopilot) $('#pause_btn'+randID).fadeIn(250);
									showtext($('li:eq(1) p', obj).html());
									if(autopilot)
									{
										$('#progress'+randID).width(imgWidth).height(5);
										$('#progress'+randID).animate({'width':0},o.displayTime,function(){
											$('#pause_btn'+randID).fadeOut(50);
											setTimeout(function(){$('#pause_btn'+randID).fadeIn(250)},o.transitionSpeed)
										});
									}
									currentlyAnimating=false;
								});
						}
						if(direction == "prev")
						{
							// Copy rightmost (last) li and insert it after the first li
							if (!onlyShow) $('li:last', obj).clone().insertBefore($('li:first', obj));
							// Update width and left position of ul and animate ul to the right
							$('ul', obj)
								.width(imgWidth*(numImages+1))
								.css('left',-imgWidth*2+'px')
								.animate({left:-imgWidth},200,function(){
									if (!onlyShow) $('li:last', obj).remove();
									$('ul', obj).width(imgWidth*numImages);
									$('#btn_rt'+randID).fadeIn(500);
									$('#btn_lt'+randID).fadeIn(500);
									if(autopilot) $('#pause_btn'+randID).fadeIn(250);
									showtext($('li:eq(1) p', obj).html());
									currentlyAnimating=false;
								});
						}
					}
				}
				var clearInt = setInterval(function(){anim('next');},o.displayTime+o.transitionSpeed);
				$('#progress'+randID).animate({'width':0},o.displayTime+o.transitionSpeed,function(){
					$('#pause_btn'+randID).fadeOut(100);
					setTimeout(function(){$('#pause_btn'+randID).fadeIn(250)},o.transitionSpeed)
				});
  		});
    	}
	});
})(jQuery);



