/*!############################################################################
##############################################################################
##
##	swooshGallery © Liam Potter - 2009
##	
##  Author: Liam Potter
##	Email:	liam@designedbyevo.com
##
##	Version:		0.3 beta
##	Created:		27/05/09
##	Updated:		01/06/09
##
##
##	Changes:
##	0.1 - First Version
##	0.2 - Fixed pause to pause as soon as clicked
##	0.3 - Automatically insert fist large image, no need for markup
##
##
##	The MIT License
##	
##	Copyright (c) 2009 Liam Potter
##	
##	Permission is hereby granted, free of charge, to any person obtaining a copy
##	of this software and associated documentation files (the "Software"), to deal
##	in the Software without restriction, including without limitation the rights
##	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
##	copies of the Software, and to permit persons to whom the Software is
##	furnished to do so, subject to the following conditions:
##	
##	The above copyright notice and this permission notice shall be included in
##	all copies or substantial portions of the Software.
##	
##	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
##	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
##	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
##	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
##	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
##	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
##	THE SOFTWARE.
##	
##############################################################################
############################################################################*/
(function($) {

	$.fn.swoosh = function(options) {
		var defaults = {
			delay: 5000,
			autoPlay: true,
			arrow: '/assets/images/swoosh/arrow.gif',
			thumbs: '/assets/images/content/thumbs/',
			thumbsHover: '/assets/images/content/thumbs/hover/',
			largeImg: '/assets/images/content/'
		};
		var settings = $.extend(defaults, options);
		
		var autoPlay = settings.autoPlay;
		
		//emulate PHP strstr function
		function strstr( haystack, needle, bool ) {
			var pos = 0;
		 
			haystack += '';
			pos = haystack.indexOf( needle );
			if( pos == -1 ){
				return false;
			} else{
				if( bool ){
					return haystack.substr( 0, pos );
				} else{
					return haystack.slice( pos );
				}
			}
		}
		
		this.each(function() {
						   
			/**********************************************************
			* Setup, wrap images in container, mark out sets of seven *
			**********************************************************/
			var thumbs = $(this).find("div.slider")		    
			var thumbCount = thumbs.children("img").size();
			var thumbWidth = thumbs.children("img").width();
			if ($.browser.msie && $.browser.version.substr(0,1)<7) {
				var containerWidth = ((thumbWidth+8) * thumbCount);	
			} else {
				var containerWidth = ((thumbWidth+6) * thumbCount);
			}
			var widthOfSeven = ((thumbWidth+6) * 7)
			var pages = thumbWidth / thumbCount;
			var pages = Math.round(pages);
			
			thumbs.children("img").wrapAll('<div class="container" style="position:absolute;width:'+containerWidth+'px"></div>');
			
			var imgIndex = $(this).index(this)
			var set = 1
			
			thumbs.find("img").each(function(){
				imgIndex++	
				$(this).addClass('set'+set);	
				var num = imgIndex / 7;
				var check = strstr(num, '.');
				if (check == false) {
					set++
					$(this).addClass('multiple');
				}
			});
			
			thumbs.find("img:last").addClass("lastImage");
			
			//counter
			countIndex = 0
			$(this).find("div#count").html('<span id="updateThisCount">1</span>/'+thumbCount);
			
			var largeImage = $("div#thumbnails").find("img:first").attr("src");
			var newLargeImage = largeImage;
			var newLargeImage = newLargeImage.replace (settings.thumbs, settings.largeImg);
			
			$(this).prepend('<div id="viewport"><img src="'+newLargeImage+'" width="315" height="266" alt="" style="z-index:2; position:absolute;"</div>');
			
			/**********************************************************
			* play/pause & controls                                   *
			**********************************************************/
			
			
			play();
			
			
			function play(){
				if ( autoPlay == true ){
					$("div#controls a#play").addClass("active");
					thumbs.find("img").eq(countIndex).css({opacity:.5}).animate({opacity:.5},settings.delay,function(){
						$(this).animate({opacity:1},500).next().animate({opacity:.5},500, function(){
							countIndex++
							$(this).parents("div#controls").find("span#updateThisCount").html(countIndex+1);
							
							var largeImage = $(this).attr("src");
							newLargeImage = largeImage;
							var newLargeImage = newLargeImage.replace (settings.thumbs, settings.largeImg);
							
							$(this).parents("div#controls").prev("div#viewport")
								.append('<img src="'+newLargeImage+'" width="315" height="266" alt="" style="display:none;position:absolute;z-index:1;">')
								.find("img:eq(0)")
								.animate({opacity:0},500,function(){ 
									 $(this).remove()
								});
								
							$(this).parents("div#controls").prev("div#viewport")
							.children("img").css({zIndex: 2,display: 'block',opacity: 0})
							.animate({opacity:1},500);
								
							if ( $(this).hasClass("multiple") ) {	
								if ( !$(this).hasClass("lastImage") ) {
									scrollRight();
									
								} else {
									scrollStart();
									countIndex = 0;	
									scrollCount = 1;
								}
							}
							if ( $(this).hasClass("lastImage") ) {
									scrollStart();
									countIndex = 0;
									scrollCount = 1;
								} 
							
							play();
							
							
						});
						
					});
				} //if
			}//function
			
			function pause(){
				if ( autoPlay == false ){
					
						thumbs.find("img").eq(countIndex).stop();				
					
				}
			}
			
			var scrollCount = 1;
			function scrollRight() {
				if ( scrollCount < pages ) {
					thumbs.find("div.container").animate({left:'-='+(widthOfSeven)+'px'},800);
					scrollCount++
				}
			}
			
			function scrollLeft() {
				if ( scrollCount > 1 ) {
					thumbs.find("div.container").animate({left:'+='+(widthOfSeven)+'px'},800);
					scrollCount--
				}
			}
			
			function scrollStart() {
				thumbs.find("div.container").animate({left:'0px'},800);
				scrollCount = 1
				$(this).find("div#count").html('<span id="updateThisCount">1</span>/'+thumbCount);
			}
			
			$("div#controls a").click(function(){
				return false;
			});
			
			
			$("div#controls a#pause").click(function(){
				autoPlay = false;
				pause();
				$(this).addClass("active");
				$("div#controls a#play").removeClass("active")
				return false;
			});
			
			$("div#controls a#play").click(function(){
				autoPlay = true;
				play();
				$(this).addClass("active");
				$("div#controls a#pause").removeClass("active")
				return false;
			});
			
			$("div#controls div#thumbnails a.left").click(function(){scrollLeft()});
			$("div#controls div#thumbnails a.right").click(function(){scrollRight()});
			
			/**********************************************************
			* hover thumbnail                                         *
			**********************************************************/
			function findPosX(obj) {
				var curleft = 0;
				if (obj.offsetParent) {
					while (1) {
						curleft+=obj.offsetLeft;
					if (!obj.offsetParent) {
					break;
					}
						obj=obj.offsetParent;
					}
					} else if (obj.x) {
						curleft+=obj.x;
				}
				return curleft;
			}
			
			/**********************************************************
			* click thumbnail                                         *
			**********************************************************/
			
			$("div#controls div#thumbnails div.slider div.container").find("img").click(function(){
					var newIndex = $(this).attr("id");
					var newIndex = parseFloat(newIndex);
					countIndex = newIndex

					$(this).parents("div.container").find("img").each(function(){
						$(this).stop().css({opacity:1});
					});
					
					$(this).css({opacity:.5});
					
					var largeImage = $(this).attr("src");
					newLargeImage = largeImage;
					var newLargeImage = newLargeImage.replace (settings.thumbs, settings.largeImg);
							
					$(this).parents("div#controls").prev("div#viewport").append('<img src="'+newLargeImage+'" width="315" height="266" alt="" style="display:none;position:absolute;z-index:7;">')
					.find("img:eq(0)")
					.animate({opacity:0},500,function(){ 
						 $(this).remove()
					});
								
					$(this).parents("div#controls").prev("div#viewport").children("img")
					.css({zIndex: 2,display: 'block',opacity: 0}).animate({opacity:1},500);
					
					$(this).parents("div#controls").find("span#updateThisCount").html(countIndex+1);
					
					play();
				});
			countIndex = 0;
			
		});
		
		
		return this;
	}//close jquery function
	
})(jQuery);