/*******************************************************************************
 jquery.mb.components
 Copyright (c) 2001-2010. Matteo Bicocchi (Pupunzi); Open lab srl, Firenze - Italy
 email: mbicocchi@open-lab.com
 site: http://pupunzi.com

 Licences: MIT, GPL
 http://www.opensource.org/licenses/mit-license.php
 http://www.gnu.org/licenses/gpl.html
 ******************************************************************************/

/*
 * Name:jquery.mb.bgndGallery
 * Version: 1.0
 *
 */

(function($){

	$.mbBgndGallery ={
		name:"mb.bgndGallery",
		author:"Matteo Bicocchi",
		version:"1.0",
		defaults:{
			containment:"body",
			images:[],
			timer:4000,
			effTimer:3000,
			folderPath:false,

			onLoad:function(){},
			onBeforeClose:function(){},
			onClose:function(){},
			onChangePhoto:function(){}
		},
		buildGallery:function(options){

			var opt = {};

			$.extend(opt, $.mbBgndGallery.defaults,options);
			opt.galleryID= new Date().getTime();
			var el= $(opt.containment).get(0);
			el.opt= opt;

			var gallery= $("<div/>").attr("id","bgndGallery_"+opt.galleryID);
			gallery.css({position:"absolute",top:0,let:0,width:"100%",height:"100%",overflow:"hidden"});

			var containment = el.opt.containment;

			if(containment !="body"){
				var wrapper=$("<div/>").css("position","relative");
				$(containment).wrapInner(wrapper);

				if($(containment).css("position")=="static")
					$(containment).css("position","relative");
			}

			$(containment).prepend(gallery);

			if(el.opt.folderPath)
				el.opt.images=jQuery.loadFromSystem(el.opt.folderPath);

			var images= el.opt.images;

			var totImg= images.length;

			var loadCounter=0;
			$.mbBgndGallery.preload(images[0],el);
			$(document).bind("imageLoaded_"+el.opt.galleryID,function(){
				loadCounter++;
				if(loadCounter==totImg){
					$(document).unbind("imageLoaded_"+el.opt.galleryID);
					return;
				}
				$.mbBgndGallery.preload(images[loadCounter],el);
			});

			$.mbBgndGallery.changePhoto(images[0],el);

			var imageCounter=0;
			$(document).bind("imageReady_"+el.opt.galleryID,function(){
				imageCounter++;
				setTimeout(function(){
					$.mbBgndGallery.changePhoto(images[imageCounter],el);
					if (imageCounter==images.length-1)
						imageCounter=0;
				},el.opt.timer)
			});

			$(window).bind("resize",function(){
				var image=$("#bgndGallery_"+opt.galleryID+" img");
				$.mbBgndGallery.checkSize(image,el);
			});

		},
		preload:function(url,el){
			var img= $("<img/>").load(function(){
				$(document).trigger("imageLoaded_"+el.opt.galleryID);
			}).attr("src",url);
		},
		checkSize:function(image,el){

			//alert([image.attr("w"),image.attr("h")]);


			var w= image.attr("w");
			var h= image.attr("h");

			var containment = el.opt.containment == "body"? window : el.opt.containment;

			var nw,nh;

			if(image.height()<$(containment).height()){

				nw= $.browser.msie && $.browser.version!="9.0" ? image.width()*w/h :"";
				nw= "";

				image.css("height","100%");
				image.css("width",nw);

			} else if(image.width()<$(containment).width()){

				nh= $.browser.msie && $.browser.version!="9.0" ? image.height()*w/h :"";
				nh= "";

				image.css("height",nh);
				image.css("width","100%");
			}

			image.css("margin-left",(($(containment).width()-image.width())/2));
			image.css("margin-top",(($(containment).height()-image.height())/2));

		},
		changePhoto:function(url,el){

			var opt= el.opt;
			var image=$("<img/>").hide().load(function(){
				var image=$(this);

				var tmp=$("<div/>").css({position:"absolute",top:-5000});
				tmp.append(image);
				$("body").append(tmp);

				image.attr("w", image.width());
				image.attr("h", image.height());
				tmp.remove();


				$("#bgndGallery_"+opt.galleryID+" img").fadeOut(opt.effTimer,function(){$(this).remove()});
				image.css({position:"absolute", width:"100%"});
				$("#bgndGallery_"+opt.galleryID).prepend(image);

				$.mbBgndGallery.checkSize(image, el);
				image.fadeIn(opt.effTimer,function(){
					$(document).trigger("imageReady_"+el.opt.galleryID);
				});
			}).attr("src",url);
		},
		loader:{
			show:function(){},
			hide:function(){}
		}
	};

	jQuery.loadFromSystem=function(folderPath){
		if(!folderPath)
			return;
		var bgnds=[];
		$.ajax({
			url:folderPath,
			async:false,
			success:function(response){
				var tmp=$(response);
				var images= tmp.find("[href*='.jpg']");
				images.each(function(){
					bgnds.push(folderPath+$(this).attr("href"));
				});
				tmp.remove();
			}
		});
		return bgnds;
	}

})(jQuery);
