/**
 * jQuery jqGalScroll Plugin
 * Examples and documentation at: http://benjaminsterling.com/jquery-jqgalscroll-photo-gallery/
 *
 * @author: Benjamin Sterling
 * @version: 2.1
 * @copyright (c) 2007 Benjamin Sterling, KenzoMedia
 * @extendThanks Koesmanto Bong http://www.koesbong.com/
 *		Koes put a fire under my butt to improve this plugin
 *		and when I took too long he took what I had and added
 *		the horizontal scroll and in turn I ripped it from his
 *		hands and made it better :)
 *
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 *   
 * @requires jQuery v1.2.1 or later
 * @optional jQuery Easing v1.2
 *
 * @name jqGalScroll
 * @example $('ul').jqGalScroll();
 * 
 * @Semantic requirements:
 * 				The structure fairly simple; the structure should consist
 * 				of a ul > li > img structure.
 * 
 * 	<ul>
 *		<li><img src="common/img/dsc_0003.thumbnail.JPG"/></li>
 *		<li><img src="common/img/dsc_0012.thumbnail.JPG"/></li>
 *	</ul>
 *
 * @param String ease
 *					refer to http://gsgd.co.uk/sandbox/jquery.easing.php for values
 * 
 * @example $('#gallery').jqGalScroll({speed:1000});
 
 * @param String speed
 * 					fast, slow, 1000, ext..
 * 
 * @example $('#gallery').jqGalScroll({speed:1000});
 * 
 * @param String height
 * 					the default height of your wrapper
 * 
 * @example $('#gallery').jqGalScroll({height:490});
 * 
 * @param String titleOpacity
 * 					the opacity of your title bar (if present)
 * 
 * @example $('#gallery').jqGalScroll({titleOpacity:.70});
 * 
 * @param String direction 
 *					vertical horizontal diagonal
 * 
 * @example $('#gallery').jqGalScroll({direction:'vertical'});
 * 			
 */
(function($) {
	$.fn.jqGalScroll = function(options){
		return this.each(function(i){
								  
	
			var auto = true; //var if autoplying is on
			var aniFinish = false; // var to trigger if animation finished, needed if user clicks on pagingation -> stop autoplay
			
			var el = this
			el.curImage = 0;
			el.jqthis = $(this).css({position:'relative'});
			el.jqchildren = el.jqthis.children();
			el.opts = $.extend({}, jqGalScroll, options);
			
			el.index = i;
			el.totalChildren = el.jqchildren.size();
			var width,height;
			 
			
			switch(el.opts.direction){
				case 'horizontal':
					width = el.totalChildren *el.opts.width;
					height = el.opts.height;
					break;
				case 'vertical':
					width = el.opts.width;
					height = el.totalChildren *el.opts.height;
					break;
				default:
					width = el.totalChildren *el.opts.width;
					height = el.totalChildren *el.opts.height;
					break;
			};
			
			el.container = $('<div id="jqGS'+i+'" class="jqGSContainer"  >').css({position:'relative'});
			el.ImgContainer = $('<div class="jqGSImgContainer" style="height:'+el.opts.height+'px;position:relative;overflow:hidden ">')
								.css({height:el.opts.height,width:el.opts.width,position:'relative',overflow:'hidden'});
			el.jqthis.css({height:height,width:width});
			
			el.jqthis.wrap(el.container);
			el.jqthis.wrap(el.ImgContainer);
			el.pagination = $('<div class="jqGSPagination">');
			
	 		
			 $('#flashcontent').append(el.pagination);
			 
			 if($.browser.msie && $.browser.version=="8.0"){
				  $('#main').append(el.pagination);
				  
				 
			 }
			 else{
				 
				 el.jqthis.parent().parent().append(el.pagination);
				  
			 }



			
			var jqul = $('<ul class="paging">').appendTo(el.pagination);
	
setAutoplay();			



 
function setAutoplay() {

	if(auto==true){
		
						interVal = setInterval(function(){
						if(el.pagination.find('.selected').parent().next().children('.inext a').size()){
					
		
				 			aniFinish = false; //set back var here eaxh time
							el.pagination.find('.selected').parent().next().children('.inext a').click();
					
						}
					
						else{	 
								  aniFinish = false; //set back var here eaxh time
								  el.pagination.find('li:eq(1)').children('.inext a').click(); 
						}
					
					}, 2500);
					
					auto=false;
					el.pagination.find('#autoplay').removeClass('autoplayPause');
		 			el.pagination.find('#autoplay').addClass('autoplayPlay');
						
	}
	
	else {
		
		 window.clearInterval(interVal);
		 auto=true // autoplay switche off
		 
		 el.pagination.find('#autoplay').removeClass('autoplayPlay');
		  el.pagination.find('#autoplay').addClass('autoplayPause');
		 

	}


}
			
		
			var pos = {x:0,y:0};
								
			el.jqchildren
			.each(function(j){    
			
				el.opts.speed = 150;
				var fadeparam =1050;
						   
				var selected = '';
				if(j == 0) selected = 'selected';
				
				var $a = $('<a href="#'+(j)+'" class="'+selected+'">'+(j+1)+'</a>').click(function(){
																								   
					
					var href = this.index;//href.replace(/^.*#/, '');
					
					
					if (aniFinish==true && auto==false ) { setAutoplay(); }; //if user clicks out of interval, means on the button, stop animation
					
					el.pagination.find('.selected').removeClass('selected');
					
					$(this).addClass('selected'); 				
					
					var params = {};
					
					
					 if( el.opts.direction == 'horizontal'){
						params = {right:(el.opts.width*href) }
					};
					
					var param_fadeout = {opacity: 0};
					var param_fadein = {opacity: 1};
				 
					
					  //get next imge an set to the fadelayer background
					  var imgsrc	= $(".jqGalScroll li:eq("+j+")").children("img").attr("src");
					  $('.fadePics').css( 'backgroundImage', 'url('+imgsrc+')'  );  
				  
			 		//fadeout first
				  // $('#fadelayer').css("filter","alpha(opacity=0)");
				 	$('#fadelayer').stop().animate(param_fadeout,10, el.opts.ease );
				
					//start the cross fade in from the fadelayer
					var fadeparam2 =1050;
					 
					    $('#fadelayer').css("filter","alpha(opacity=1)"); 
				 	$('#fadelayer').stop().animate(param_fadein,fadeparam2, el.opts.ease );
				  
					
					//here we start the next picture events, 
					//  el.jqthis.css("filter","alpha(opacity=1)");
					 
					el.jqthis.stop().animate(param_fadeout,fadeparam, el.opts.ease ,  function() {			  
					
				
						var k = j+1; // var to get the next picture for the background
						var pics = $(".jqGalScroll li").length; //trigger length so we get the startpoint again.
					
							if(j!=0) { 
	
											   aniFinish = true; //set alwas, to trigger if user click out of interval, we them stop the animation
											 
							      				if (k == pics) { k = 0; } //start allover with background, when last picture is reached
																					
												
												el.jqthis.animate(params,1, el.opts.ease, function() { // invisible wipe effekt for the nexdt picture, in
							   
							  				 
												el.jqthis.animate(param_fadein,1 , el.opts.ease, function() { //fade in and the the next pic in the background
							   
													var imgsrc	= $(".jqGalScroll li:eq("+k+")").children("img").attr("src");
									
													$('.fadePics').css( 'backgroundImage', 'url('+imgsrc+')'  ); 
				
				
													});  
											}); 
									 
								 
									
									}
									else{ //this one for the first picture
										
										aniFinish = true;
								 
										el.jqthis.animate(params,el.opts.speed, el.opts.ease, function() { // invisible wipe effekt for the nexdt picture, in
							   
											 el.jqthis.css("filter","alpha(opacity=0)");
												el.jqthis.animate(param_fadein,1 , el.opts.ease, function() { //fade in and the the next pic in the background
							   
												var imgsrc	= $(".jqGalScroll li:eq("+k+")").children("img").attr("src");
								
												$('.fadePics').css( 'backgroundImage', 'url('+imgsrc+')'  ); 
												
												 aniFinish = true;
												
				
											});  
								}); 	
											
											
											
									}  				
				});  

					index = href;
		 
					return false;
				});
				
				var n = $a.get(0);

				n.index = j;
	
	            //generate the autoplay button with click event fpr autoplay
				if(j==0) {
				$autoplay = $('<a href="#" class="autoplayPlay" id="autoplay">&nbsp;</a>')
				$('<li class="autoplayPause">').appendTo(jqul).append($autoplay);
				
					 el.pagination.find('#autoplay').click(function(){
			
					 setAutoplay();	
					
						});	
				}
			
				$('<li class="inext">').appendTo(jqul).append($a);

				if( el.opts.direction == 'diagonal'){
					pos.x = j * el.opts.width;
					pos.y = j * el.opts.height;
				}
				else if( el.opts.direction == 'horizontal'){
					pos.x = j * el.opts.width;
				}
				else if( el.opts.direction == 'vertical'){
					pos.y = j * el.opts.height;
				};

				var jqchild = $(this).css({height:el.opts.height,width:el.opts.width,position:'absolute',left:pos.x, top:pos.y});

				var jqimg = jqchild.find('img').hide()
				
				if(jqimg.parent().is('a')){
					var p = jqimg.parent();
					jqimg.get(0).linkHref = p.attr('href');
					p.remove();
					jqimg.appendTo(jqchild);
				};

				jqimg.click(function(){
					var next = n.index + 1;
					if((n.index + 1) == el.totalChildren ){
						el.pagination.find('[href$=#0]').click();
					}
					else{
						el.pagination.find('[href$=#'+next+']').click();
					}
				});

		
				

				var $loader = $('<div class="jqGSLoader">').appendTo(jqchild);
				var $titleHolder = $('<div class="jqGSTitle">').appendTo(jqchild).css({opacity:el.opts.titleOpacity}).hide();
				var image = new Image();
				image.onload = function(){
					image.onload = null;
					$loader.fadeOut();
					 jqimg.css({marginLeft:-image.width*.5,marginTop:-image.height*.5,position:'absolute',left:'50%',top:'50%'}).fadeIn( function() {
							   
											//	var imgsrc	= $(".jqGalScroll li:eq(1)").children("img").attr("src");
											//	$('.fadePics').css( 'backgroundImage', 'url('+imgsrc+')'  ); 					 
				
											});  
																																		
				 
					
					var alt = jqimg.attr('alt');
					if(typeof alt != 'undefined'){
						$titleHolder.text(alt).fadeIn();
					}
				};
				image.src = jqimg.attr('src');
			});

			
		}); // end : this.each(function()
	
		 

	};  // end : $.fn.jqGalScroll
	
	
	
	
	jqGalScroll = {
		ease: null,
		speed:0,
		height: 500,
		width: 500,
		titleOpacity : .60,
		direction : 'horizontal' // vertical horizontal diagonal
	};
	
	
 
	
})(jQuery);
