
// jQuery Compatibility Wrapper

;(function($) {
	
	var defaultSettingsFn = function( cfg ) {
		
		return $.extend({},{
			delay: 		8000,
			duration: 	1500
		},cfg);
		
	}
	
	var getActualSlideFn = function( $target ) {
		
		var $actual = $target.find('li.active');
		
		if ( !$actual.length ) $actual = $target.find('li:first');
		
		return $actual;
	
	}
	
	var getNextSlideFn = function( $node ) {
		
		var $next = $node.next();
		
		if ( !$next.length ) {
			
			return $node.parent().find('li:first');
			
		}
		
		return $next;
		
	}
	
	var getPrevSlideFn = function( $node ) {
		
		var $prev = $node.prev();
		
		if ( !$prev.length ) {
			
			return $node.parent().find('li:last');
			
		}
		
		return $prev;
		
	}
	
	var changeSlideFn = function( $actual, $next, duration ) {
		
		$actual.fadeOut(duration);
		$next.fadeIn(duration);
		
		$actual.removeClass('active');
		$next.addClass('active');
		
		imageResizeFn($actual);
		imageResizeFn($next);
		
		return $next;
		
	}
	
	/**
	 * This method works only with IE.
	 * Nice browsers will resize image as a extenden background for the parent container.
	 */
	var imageResizeFn = function( $cnt ) {
		
		if ( !$.browser.msie ) return true;
		
		var $img = $cnt.find('img');
		
		if ( $img[0].complete ) {
		
			$img.css({
				width:		$cnt.outerWidth() + 'px',
				height:		'auto'
			});
			
			return true;
			
		}
		
		// Check for image to be complete.
		var imageCounter = $img.data('image-is-complete');
		if ( imageCounter == null ) imageCounter = 0;
		imageCounter += 1;
		
		if ( imageCounter > 10 ) return false;
		$img.data('image-is-complete', imageCounter);
		
		setTimeout(function(){
			imageResizeFn($cnt);
		},300);
		
	}
	
	
	
	/**
	 * Plugins
	 */
	
	$.fn.slideShowInit = function( cfg ) {
		
		cfg = $.extend({},{
			onReady:		function() {}
		},cfg);
		
		$(this).each(function(){
			
			var $this = $(this);
			
			$this.data('slideShowSettings', defaultSettingsFn(cfg) );
			
			// Setup list bgs to fit the content area.
			$this.find('img').each(function(){
				
				var $this 	= $(this);
				var $li		= $this.parent();
				
				imageResizeFn($li);
				if ( !$.browser.msie ) {				
					$li.css({ backgroundImage: 'url('+ $this.attr('src') +')' });
					$this.hide();
				}
				
			});
			
			// Hide all lists item but not the first.
			$this.find('li').not(':first').hide();
			$this.find('li:first').addClass('active');
			
		});
		
		cfg.onReady.call( this );
		
		return this;
		
	}
	
	$.fn.slideShowAddControls = function() {
		
		$(this).each(function(){
			
			var $this = $(this);
			
			$this.append('<div class="slideshow-crtl slideshow-start"><a href="#slideshow-start">start</a></div>');
			$this.append('<div class="slideshow-crtl slideshow-stop"><a href="#slideshow-stop">stop</a></div>');
			$this.append('<div class="slideshow-crtl slideshow-next"><a href="#slideshow-next">next</a></div>');
			$this.append('<div class="slideshow-crtl slideshow-prev"><a href="#slideshow-prev">prev</a></div>');
			
			$this.find('.slideshow-start').bind('click',function(e){	e.preventDefault();		$this.slideShowStart();		});
			$this.find('.slideshow-stop').bind('click',function(e){		e.preventDefault();		$this.slideShowStop();		});
			$this.find('.slideshow-next').bind('click',function(e){		e.preventDefault();		$this.slideShowNext();		});
			$this.find('.slideshow-prev').bind('click',function(e){		e.preventDefault();		$this.slideShowPrev();		});
			
			
		});
		
		return this;
		
	}
	
	
	$.fn.slideShowSetImageByUrl = function( url ) {
		
		$(this).each(function(){
			
			var $this = $(this);
			
			var $images = $this.find('img');
			
			var $target = null;
			
			for ( var i=0; i<$images.length; i++ ) {
				
				var $img = $($images[i]);
				
				if ( $img.attr('src') == url ) {
					
					$target = $img;
					
				}
				
			}
			
			if ( $target != null ) {
				
				$this.find('li').removeClass('active').hide();
				
				$target.parent().addClass('active').show();
				
				imageResizeFn($target.parent());
				
			}
			
			
			//changeSlideFn( $this.find('li.active'), $target, 1000 );
			
		});
		
		return this;
		
	}
	
	
	$.fn.slideShowStart = function() {
		
		$(this).each(function(){
			
			var $this = $(this);
			
			$this.data('slideShowLoop',true);
			$this.data('slideShowTimer',null);
			
			var slideShowStepFn = function( $obj ) {
				
				if ( !$this.data('slideShowLoop') ) return;
				
				$this.data('slideShowTimer',
					setTimeout(function(){ 
					
						var $next = getNextSlideFn( $obj );
						
						changeSlideFn( $obj, $next, $this.data('slideShowSettings').duration );
						
						slideShowStepFn( $next );
						
					}, $this.data('slideShowSettings').delay )
				);
				
			}
			
			
			slideShowStepFn( getActualSlideFn($this) );
			
			//console.log("slideshow start");
			
		});
		
		return this;
			
	};
	
	$.fn.slideShowStop = function() {
		
		//console.log('slideshow stop');
		
		$(this).each(function(){
			
			var $this = $(this);
			
			$this.data('slideShowLoop',false);
			
			clearTimeout($this.data('slideShowTimer'));
			
		});
		
		return this;
		
	}
	
	$.fn.slideShowToggle = function() {
		
		$(this).each(function(){
			
			var $this = $(this);
			
			if ( $this.data('slideShowLoop') ) {
				$this.slideShowStop();
			} else {
				$this.slideShowStart();
			}
			
		});
		
		return this;
		
	}
	
	$.fn.slideShowNext = function() {
		
		$(this).each(function(){
			
			var $this = $(this);
			
			var $actual = getActualSlideFn($this);
			var $next	= getNextSlideFn($actual);
			
			changeSlideFn( $actual,$next, $this.data('slideShowSettings').duration );
			
		});
		
		return this;
		
	}
	
	$.fn.slideShowPrev = function() {
		
		$(this).each(function(){
			
			var $this = $(this);
			
			var $actual = getActualSlideFn($this);
			var $prev	= getPrevSlideFn($actual);
			
			changeSlideFn( $actual, $prev, $this.data('slideShowSettings').duration );
			
		});
		
		return this;
		
	}
	
	$.fn.slideShowFadeIn = function( cfg ) {
		
		var $this = $(this);
		
		cfg = $.extend({},{
			delay:		1000,
			easing:		'easeInOutExpo',
			onEnd:		function() {}
		},cfg);
		
		$(this).each(function(){
			
			var $this = $(this);
			
			var onEndFn = function() { cfg.onEnd.call( this, cfg ); }
			
			$this.animate({
				opacity:1
			}, cfg.delay, cfg.easing, onEndFn );
			
		});
		
		return this;
		
	}
	
	$.fn.slideShowFadeOut = function( cfg ) {
		
		var $this = $(this);
		
		cfg = $.extend({},{
			delay:		500,
			easing:		'easeInOutExpo',
			onEnd:		function() {}
		},cfg);
		
		$(this).each(function(){
			
			var $this = $(this);
			
			var onEndFn = function() { cfg.onEnd.call( this, cfg ); }
			
			$this.animate({
				opacity:0
			}, cfg.delay, cfg.easing, onEndFn );
			
		});
		
		return this;
		
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	/**
	 * Inizializza il markup ed i comportamenti della gallery.
	 */
	
	$.fn.galleryInit = function( cfg ) {
		
		var $this = $(this);
		
		cfg = $.extend({},{
			onEnd:		function() {}
		},cfg);
		
		$(this).each(function(){
			
			var $this = $(this);
			
			
			
			
			// Thumbnails scrolling initialization.
			$this.find('ul').jcarousel({
				wrap: 'circular'
			});
			
			
			// Background gallery inizialization.
			$this.galleryInitSlideshow(cfg);
			
			
			// EVENT: Close the gallery.
			$this.bind('click',function( e ){
				
				e.preventDefault();
				
				var $gallery = $(this);
				
				if ( !$gallery.hasClass('closed') ) return this;
				
				// Display the index and fade out the gallery!
				//$gallery.galleryIndexAnimate();
				$this.data('gallery-link').slideShowFadeOut({onEnd:function(){
					$gallery.galleryIndexAnimate();
				}});
			
			// Hovering sull'elemento gallery-index.
			});
			
			var ua = navigator.userAgent;
			var isiPad = /iPad/i.test(ua) || /iPhone OS 3_1_2/i.test(ua) || /iPhone OS 3_2_2/i.test(ua);
			if ( !isiPad ) $this.bind('mouseenter',function(e){ $this.galleryIndexAnimate(e); }).bind('mouseleave',function(e){ $this.galleryIndexAnimate(e); });
			
			
			// EVENT: Open the gallery
			$this.find('a').bind('click',function( e ) {
				
				e.preventDefault();
				
				var $gallery = $(this).parents('#gallery-list');
				
				// Set the current slide into the gallery.
				$this.data('gallery-link').slideShowSetImageByUrl( $(this).attr('href') );
				
				// Hide the index and fade in the gallery.
				$gallery.galleryIndexAnimate();
				
			});
			
			
		});
		
		return this;
		
	} // EndOf: "$.fn.galleryInit()" ###
	
	
	
	
	
	
	
	/**
	 * Si occupa della generazione del markup della gallery di background
	 */
	
	$.fn.galleryInitSlideshow = function( cfg ) {
		
		var $this = $(this);
		
		cfg = $.extend({},{
			onEnd:		function() {}
		},cfg);
		
		$(this).each(function(){
			
			var $this = $(this);
			var id 	= $this.attr('id');
			var gid	= 'bg-'+id;
			
			
			var $gallery 		= $('<div id="'+gid+'" class="full-bg"><ul></ul></div>');
			var $galleryList 	= $gallery.find('ul');
			
			// Add images to the gallery
			var $links = $this.find('li a');
			for ( var i=0; i<$links.length; i++ ) {
				$galleryList.append('<li><img src="'+$($links[i]).attr('href')+'" /></li>');
			}
			
			// Init gallery.
			$gallery.slideShowInit({
				delay:3000
			});
			
			
			
			$gallery.slideShowAddControls();
			$gallery.css('opacity',0);
			$this.data('gallery-link', $gallery );
			
			$('#bg').after($gallery);
			
		});
		
		return this;
		
	
	} // EndOf: "$.fn.galleryInitSlideshow()" ###
	
	
	
	
	
	
	
	
	/**
	 * Si occupa delle animazioni di transizione dell'indice della gallery.
	 */
	 
	$.fn.galleryIndexAnimate = function( cfg ) {
		
		var $this = $(this);
		
		cfg = $.extend({},{
			delay: 		500,
			easing:		'easeInOutExpo',
			onEnd:		function() {}
		},cfg);
		
		
		
		
		// Mouse entering opacity transition when gallery index is closed!
		
		if ( cfg.type == 'mouseenter' ) {
			
			if ( $this.hasClass('closed') ) {
			
				$this.data('gallery-index-animate-opacity', $this.css('opacity') );
				
				$this.animate({ opacity: 1 }, cfg.delay, cfg.easing );
			
			}
			
		return this; }
		
		if ( cfg.type == 'mouseleave' ) {
			
			if ( $this.hasClass('closed') ) {
			
				$this.animate({ opacity: $this.data('gallery-index-animate-opacity') }, cfg.delay, cfg.easing );
				
			}
			
		return this; }
		
		
		// Open Animation
		if ( $this.hasClass('closed') ) {
			
			$this.removeClass('closed');
			
			var onEndFn = function() {
				
				//$this.data('gallery-link').slideShowFadeOut();
				
				cfg.onEnd.call( this, cfg );
				
			};
			
			$this.animate(
				{
					opacity: 	1,
					top:		$this.data('gallery-index-animate-start-top')
				},
				cfg.delay,
				cfg.easing
			).animate(
				{
					width:		$this.data('gallery-index-animate-start-width'),
					height:		$this.data('gallery-index-animate-start-height')
				},
				cfg.delay,
				cfg.easing,
				onEndFn
			);
			
			
			
			
		
		// Close Animation	
		} else {
			
			var onEndFn = function() {
				
				$this.addClass('closed');
				
				// This will not work on IE!
				$this.data('gallery-link').slideShowFadeIn();
				
				cfg.onEnd.call( this, cfg );
				
			}
			
			$this.data('gallery-index-animate-start-top',		$this.css('top') );
			$this.data('gallery-index-animate-start-height',	$this.height() );
			$this.data('gallery-index-animate-start-width', 	$this.width() );
			
			$this.animate(
					{
						width: 250,
						height:25
					},
					cfg.delay,
					cfg.easing
				).animate(
					{
						top: $('#page-content').height() - 100,
						opacity:0.3
					},
					cfg.delay,
					cfg.easing,
					onEndFn
				);
		
		}
	
	} // EndOf: "$.fn.galleryIndexAnimate" ###
	
	
	
	
			
})(jQuery);

