/**
 * jQuery jqGalFade Plugin
 * 
 *
 * @author: Sergio Carracedo Martinez
 * @version: 1.
 * @copyright (c) 2009 Sergio Carracedo, Opsou
 * @extend Based in jQuery Gal Fade http://benjaminsterling.com/jquery-jqgalFade-photo-gallery/
 *
 * 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 jqGalFade
 * @example $('ul').jqGalFade();
 * 
 * @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').jqGalFade({speed:1000});
 
 * @param String speed
 * 					fast, slow, 1000, ext..
 * 
 * @example $('#gallery').jqGalFade({speed:1000});
 * 
 * @param String height
 * 					the default height of your wrapper
 * 
 * @example $('#gallery').jqGalFade({height:490});
 * 
 * @param String titleOpacity
 * 					the opacity of your title bar (if present)
 * 
 * @example $('#gallery').jqGalFade({titleOpacity:.70});
 * 
 */
(function($) {	
	$.fn.jqGalFade = function(options){
		var el = null
		this.each(function(i){			
			el=this
			el.curImage = 0;
			el.jqthis = $(this).css({position:'relative'});
			el.jqchildren = el.jqthis.children();
			el.opts = $.extend({}, jqGalFade, options);
			el.index = i;
			el.totalChildren = el.jqchildren.size();
			var width,height;
			
			
			width = el.totalChildren *el.opts.width;
			height = el.opts.height;
			
			
			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">');
			el.jqthis.parent().parent().append(el.pagination);
			var jqul = $('<ul>').appendTo(el.pagination);
			var pos = {x:0,y:0};
			
			el.jqchildren.each(function(j){
				var selected = '';
				if(j == 0) {
					selected = 'selected';
				}
				$("li",el.jqthis).fadeOut(0);
				$("li:first",el.jqthis).fadeIn(0);
				
				
				
				var $a = $('<a href="#'+(j)+'" class="'+selected+'">'+(j+1)+'</a>').click(function(){				
					var href = this.index;//href.replace(/^.*#/, '');
	
					el.pagination.find('.selected').removeClass('selected');				
					$(this).addClass('selected');
					
					
					
					var i=0;
					$("li",el.jqthis).each(function() {
						if (i==href) {
							$(this).fadeIn();
						} else {
							$(this).fadeOut();
						}
						i++;
					});
					
					index = href;
					return false;
				});
			

				var n = $a.get(0);

				n.index = j;

				$('<li>').appendTo(jqul).append($a);

				
				pos.x = 0;
				pos.y = 0;
				
				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);
				};

				
				//console.log($("img",jqchild));
				/*$("img",jqchild).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();
					var alt = jqimg.attr('alt');
					if(typeof alt != 'undefined'){
						$titleHolder.text(alt).fadeIn();
					}
				};
				image.src = jqimg.attr('src');
			});
		}); // end : this.each(function()				
		return el;					

	};  // end : $.fn.jqGalFade
	jqGalFade = {
		ease: null,
		speed:0,
		height: 500,
		width: 500,
		titleOpacity : .60,
		direction : 'horizontal' // vertical horizontal diagonal
	};
})(jQuery);
