/*
 *         developed by Matteo Bicocchi on JQuery
 *         © 2002-2009 Open Lab srl, Matteo Bicocchi
 *			    www.open-lab.com - info@open-lab.com
 *       	version 1.0
 *       	tested on: 	Explorer and FireFox for PC
 *                  		FireFox and Safari for Mac Os X
 *                  		FireFox for Linux
 *         GPL (GPL-LICENSE.txt) licenses.
 */

jQuery.fn.mbMaskGallery = function (options){
	return this.each (function ()
	{
		var galleryId = !this.id ? "maskGallery"+Math.floor (Math.random () * 1000): this.id;
		var thisGallery = this;

		this.options = {
			galleryMask:"mask/monitor.png",
			galleryLoader:"loader/loader.gif",
			galleryColor:"white",
			type:"normal",
			fadeTime: 500,
			loader:true,
			loaderOpacity:.3,
			slideTimer: 2000
		}
		$.extend (this.options, options);

		var loader="<table id='loader' cellpadding='0' cellspacing='0' width='100%' height='100%'><tr><td valign='middle' align='center'><img src='"+this.options.galleryLoader+"' alt='loading'></td></tr></table>";

		// get the images
		var images = $ (this).find ("img");
		$ (thisGallery).empty ();

		var idx=(this.options.type =="random")?Math.floor(Math.random()*$(images).size()):0;

		$(thisGallery).mouseover(
				function(){
					$(this).css({
						cursor: $(images[idx]).attr("url")?"pointer":"default"
					})
				})

		// container
		$(thisGallery).append("<div class='container'></div>");
		var galleryContainer=$ (this).find(".container");

		if (thisGallery.options.loader){
			// loader
			$(galleryContainer).append(loader);
			var galleryloader=$ (thisGallery).find("#loader");
			$(galleryloader).css({
				position: "absolute",
				top: 0,
				left:0
			})
			if(!$.browser.msie) {
				$(galleryloader).css({
				opacity:thisGallery.options.loaderOpacity
			})
			}
		}
		//image
		$(galleryContainer).append("<image class='galleryImage' src=''>");
		var galleryImage=$ (thisGallery).find(".galleryImage");

		//mask
		$(galleryContainer).append("<image class='galleryMask' src='"+this.options.galleryMask+"'>");
		var galleryMask=$ (thisGallery).find(".galleryMask");
		$(galleryMask).click(function(){
			if ($(images[idx]).attr("url")) self.location.href=$(images[idx]).attr("url");
		})

		$(galleryContainer).css({
			position:"absolute",
			overflow:"hidden",
			opacity:0,
			backgroundColor: thisGallery.options.galleryColor
		})

		$(galleryImage).css({
			position: "absolute",
			top: 0,
			left:0,
			opacity: 0
		})

		$(galleryMask).css({
			position: "absolute",
			top: 0,
			left:0
		})

		var changePhoto = function (u){
			$ (galleryImage).fadeTo (thisGallery.options.fadeTime, 0, function (){
				//replacing the image
				$ (galleryImage).attr("src",u);
				//showing the new image
				setTimeout (function ()
				{
					$ (galleryImage).fadeTo (thisGallery.options.fadeTime, 1)
				}, (thisGallery.options.fadeTime/2));
			});
		}

		var preloadImg = function preloadImg(u)
		{
			var o = new Image ();
			o.onload = function (){changePhoto (u)};
			o.onerror = function (){alert ("can't load " + u)};
			o.src = u+"?rnd="+Math.floor (Math.random () * 1000);
		}

		var startGallery = function(){
			setTimeout(function(){
				preloadImg($(images[idx]).attr("src"));
				$(galleryContainer).fadeTo (thisGallery.options.fadeTime, 1);
				var slide= setInterval(function(){
					var rnd = Math.floor(Math.random()*$(images).size());
					idx= (thisGallery.options.type =="random")? rnd: ((idx>=$(images).size()-1)?0:idx+1);
					preloadImg($(images[idx]).attr("src"));
				},thisGallery.options.slideTimer);
				if($.browser.msie6) correctPNG();

			},200)
		}
		var loadMask= function(u){
			var o = new Image ();
			o.onload = function (){
				var w = thisGallery.options.galleryWidth ? thisGallery.options.galleryWidth : $(galleryMask).width();
				var h = thisGallery.options.galleryHeight ? thisGallery.options.galleryHeight : $(galleryMask).height();
				$(thisGallery).css({
					width: w,
					height: h
				})
				$(galleryContainer).css({
					width: w,
					height:h
				})
				startGallery();
			};
			o.onerror = function (){alert ("can't load mask: " + u)};
			o.src = u+"?rnd="+Math.floor (Math.random () * 1000);
		}

		function correctPNG() // correctly handle PNG transparency in Win IE 5.5 or higher.
		{
			for(var i=0; i<document.images.length; i++)
			{
				var img = document.images[i]
				var imgName = img.src.toUpperCase()
				if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
				{
					var imgID = (img.id) ? "id='" + img.id + "' " : ""
					var imgClass = (img.className) ? "class='" + img.className + "' " : ""
					var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
					var imgStyle = "display:inline-block;" + img.style.cssText
					if (img.align == "left") imgStyle = "float:left;" + imgStyle
					if (img.align == "right") imgStyle = "float:right;" + imgStyle
					if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
					var strNewHTML = "<span " + imgID + imgClass + imgTitle
							+ " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
							+ "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
							+ "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>"
					img.outerHTML = strNewHTML
					i = i-1
				}
			}
		}

		loadMask(this.options.galleryMask);
	})

}