/*******************************************************************************
 jquery.mb.components
 Copyright (c) 2001-2009. Matteo Bicocchi (Pupunzi); Open lab srl, Firenze - Italy
 email: info@pupunzi.com
 site: http://pupunzi.com
 Licences: MIT, GPL
 ******************************************************************************/

(function($){jQuery.preloadBgnds=function(){for(var i=0;i<arguments.length;i++){jQuery("<img>").attr("src",arguments[i])}};jQuery.fn.mbGallery=function(K){return this.each(function(){$(this).hide();var g=!this.id?Math.floor(Math.random()*100):this.id;var j=this;if(j.initialized)return;j.initialized=true;$(j).find("img").each(function(){if($(this).attr("src"))$(this).attr("srcx",$(this).attr("src"))});$(j).find("img").removeAttr("src");var k=$(this).clone();k.attr("id","gall_"+g);g=k.attr("id");var m,actualImg,actualThumb,thumbUnsel,thumbOver,thumbs,full,imgDesc;var n=$.extend({},$.fn.mbGallery.defaults,K);$.preloadBgnds(n.iconFolder+"/thumb.gif",n.iconFolder+"/play.gif",n.iconFolder+"/loaded.gif",n.iconFolder+"/separator.gif",n.iconFolder+"/next.gif",n.iconFolder+"/prev.gif",n.iconFolder+"/close.gif");n.thumbsBorder=n.thumbsBorder+"px solid";var o={thumbSel:{border:n.thumbsBorder,borderColor:n.thumbSelectColor},thumbUnsel:{border:n.thumbsBorder,borderColor:n.thumbStripColor},thumbOver:{border:n.thumbsBorder,borderColor:n.thumbOverColor}};$.extend(n,o);if(n.slideTimer<2000)n.slideTimer=2000;thumbUnsel=n.thumbUnsel;thumbOver=n.thumbOver;thumbs=k.find(n.thumbnailSelector);full=k.find(n.imageSelector);imgDesc=k.find(n.descSelector);imgDesc.find("img").each(function(){$(this).attr("src",$(this).attr("srcx"))});k.empty();thumbs.each(function(){$(this).attr("src",$(this).attr("srcx"))});full.each(function(){$(this).attr("src",$(this).attr("srcx"))});if(n.startFrom=="random")n.startFrom=Math.floor(Math.random()*full.length);function preloadImg(i){$(z).find("img").attr("src",""+n.iconFolder+"/loader.gif");var a=$(full[i]).attr("src");var b=new Image();b.onload=function(){$(z).find("img").attr("src",""+n.iconFolder+"/loaded.gif");changePhoto(i)};b.onerror=function(){alert("can't load "+a)};b.src=a}var p="";function setThumbPos(w){var a=0;switch(n.thumbStripPos){case"left":a=0;break;case"center":a=((w/2)-(n.thumbStripWidth/2));break;case"right":a=(w-n.thumbStripWidth);break;default:a=0;break}return a};p=setThumbPos(n.galleryWidth);if(n.containment=="")$("body").append("<div  id='mbg_"+g+"'><div class='mbMask' id='mbMask_"+g+"'></div><table id='tbl_"+g+"'  cellpadding='0' cellspacing='0' height='"+n.galleryHeight+"'><tr><td id ='gallery_"+g+"' class='mbGallery'></td></tr></table></div>");else{$("#"+n.containment).find(".gallery").remove();j.initialized=false;$("#"+n.containment).append("<div class='gallery' id='mbg_"+g+"'><div class='mbMask' id='mbMask_"+g+"'></div><table id='tbl_"+g+"'  cellpadding='0' cellspacing='0' height='"+n.galleryHeight+"'><tr><td id ='gallery_"+g+"' class='mbGallery'></td></tr></table></div>")}var q=$("#tbl_"+g).find('#gallery_'+g);$(q).css({border:n.galleryFrameBorder+"px solid "+n.galleryFrameColor,background:n.galleryColor});$(q).append(k);$(k).append("<div class='FullImg'></div>");var r=$(k).find(".FullImg");var s=n.labelHeight>0?n.labelHeight:n.galleryFrameBorder;$(q).prepend("<div class='thumbBox'></div>");var t=$(q).find(" .thumbBox");$(t).prepend("<div class='header'>"+"<table cellpadding='0' cellspacing='0'><tr>"+"<td class='thumbWinBtn pointer' > </td>"+"<td class='spacer' > </td>"+"<td class='slideShow pointer' > </td>"+"<td class='spacer' > </td>"+"<td class='prev pointer' > </td>"+"<td class='next pointer' > </td>"+"<td class='spacer' > </td>"+"<td class='loader'> </td>"+"<td class='indexLabel' nowrap> </td>"+"<td class='spacer' > </td>"+"<td class='close' nowrap> </td>"+"</tr></table>"+"</div>");var u=$(t).find(".header");var v=$(u).find(".thumbWinBtn");$(v).append("<img src='"+n.iconFolder+"/thumb.gif' class='thumbIco'>");var y=$(u).find(".slideShow");$(y).append("<img src='"+n.iconFolder+"/play.gif' class='slideIco'>");var z=$(u).find(".loader");$(z).append("<img src='"+n.iconFolder+"/loaded.gif'>");var A=$(u).find(".spacer");$(A).append("<img src='"+n.iconFolder+"/separator.gif'>");var B=$(u).find(".next");$(B).append("<img src='"+n.iconFolder+"/next.gif'>");var C=$(u).find(".prev");$(C).append("<img src='"+n.iconFolder+"/prev.gif'>");var D=$(u).find(".close");$(D).append("<img src='"+n.iconFolder+"/close.gif' alt='close'>");var E=$(t).find(" .indexLabel").html((n.startFrom+1)+".<b>"+full.length+"</b>");$(t).append("<div class='ThumbImg'></div>");var F=$(t).find(" .ThumbImg");$(F).prepend($(thumbs));$(t).append("<div class='descriptionBox'></div>");var G=$(q).find(".descriptionBox");if(n.containment=="")$("#mbg_"+g).css({position:"fixed",bottom:0,left:0,width:"100%",height:"100%",display:"none"});else $("#mbg_"+g).css({position:"relative",width:"100%",height:"100%",display:"none"});$("#mbMask_"+g).css({opacity:n.maskOpacity,background:n.maskBgnd,display:"none"});$("#tbl_"+g).css({margin:"auto",top:n.galleryTop,position:"relative"});$(G).css({position:"absolute",padding:10,zIndex:0,width:n.thumbStripWidth-20});k.css({width:n.galleryWidth,height:n.galleryHeight,overflow:"hidden"});$(thumbs).css({width:n.thumbHeight,height:$.browser.msie?n.thumbHeight:"",padding:"0px",border:"1px solid "+n.labelColor,cursor:"pointer"});$(t).css({textAlign:"left",zindex:1000,marginTop:"-"+s+"px",position:"absolute",width:n.thumbStripWidth+"px",marginLeft:p+"px",zIndex:10000});$(F).css({opacity:n.headerOpacity,backgroundColor:n.thumbStripColor,border:"5px solid "+n.labelColor});$(u).css({opacity:n.headerOpacity,textAlign:"left",color:n.labelTextColor,padding:"0px",border:"0px",height:s});$(u).find("td").css({background:n.labelColorDisactive,padding:"2px",paddingRight:"10px",height:s,color:n.labelTextColor,fontFamily:"Verdana, Arial",fontSize:n.labelTextSize});$(".pointer").css({cursor:"pointer"});jQuery.fn.extend({getW:function(){var a=$(this).width();if(n.galleryMaxWidth>0&&a>n.galleryMaxWidth){$(this).attr("width",n.galleryMaxWidth);a=n.galleryMaxWidth}return a},getH:function(){var a=$(this).height();if(n.galleryMaxHeight>0&&a>n.galleryMaxHeight){$(this).attr("height",n.galleryMaxHeight);a=$(this).attr("height")}return a},getDim:function(){$(this).removeAttr("width");$(this).removeAttr("height");var a=$(this).height();var b=$(this).height();var c=$(this).width();var d=$(this).width();var e=n.containment==""?$(window).height():$("#"+n.containment).innerHeight();var f=n.containment==""?$(window).width():$("#"+n.containment).innerWidth();if(n.galleryMaxHeight>0&&a>n.galleryMaxHeight){b=n.galleryMaxHeight}if(n.galleryMaxWidth>0&&c>n.galleryMaxWidth){d=n.galleryMaxWidth}if(b+n.galleryTop>=e){b=e-n.galleryTop-30;$(this).attr("height",b);d=(b*c)/a}if(d+30>=f){d=f-30;$(this).attr("width",d);b=(d*a)/c}return[b,d]}});function changePhoto(i){$(G).fadeTo(n.fadeTime,0);$(r).fadeTo(n.fadeTime,0,function(){$(r).html(full[i]);$(G).html(imgDesc[i]);setTimeout(function(){$(r).fadeTo(n.fadeTime,1);$(G).fadeTo(n.fadeTime,.8)},n.fadeTime);if(n.autoSize){var w=$(full[i]).getDim()[1];var h=$(full[i]).getDim()[0];k.animate({height:h,width:w},n.fadeTime);if(n.thumbStripWidth==n.galleryWidth){$(t).animate({width:full[i].offsetWidth},n.fadeTime)}else{var l=setThumbPos($(full[i]).width());$(t).animate({marginLeft:l},n.fadeTime)}}});$(actualThumb).css(thumbUnsel);actualImg=full[i];actualThumb=thumbs[i];$(actualThumb).css(n.thumbSel);$(E).html((i+1)+".<b>"+full.length+"</b>")}thumbs.each(function(i){$(this).click(function(){x=i;stopShow();preloadImg(i);x++})});k.show();var H,thumbOpen,headerMO;$(v).click(function(){if(!thumbOpen){$(F).slideDown(500);thumbOpen=true}else{$(F).slideUp(500);thumbOpen=false}stopShow()});$(r).click(function(){stopShow()});$(r).bind("dblclick",function(){startShow()});$(t).mouseover(function(){clearTimeout(H);clearTimeout(headerMO);$(u).find("td").css({opacity:n.headerOpacity,background:n.labelColor});clearTimeout(m)});$(t).mouseout(function(){headerMO=setTimeout(function(){$(u).find("td").css({opacity:n.headerOpacity,background:n.labelColor})},100);H=setTimeout(function(){$(F).slideUp(500);thumbOpen=false},1000)});$(thumbs).mouseover(function(){if(this!=actualThumb){$(this).css(thumbOver)}});$(thumbs).mouseout(function(){if(this!=actualThumb){$(this).css(thumbUnsel)}});$(y).click(function(){startSlide=!startSlide;if(startSlide){startShow()}else stopShow()});var I;$(B).click(function(){stopShow();clearTimeout(I);x+=1;I=setTimeout(function(){if(x>=full.length)x=0;preloadImg(x)},200)});$(C).click(function(){stopShow();clearTimeout(I);x-=1;I=setTimeout(function(){if(x<0)x=full.length-1;preloadImg(x)},200)});$(D).click(function(){stopShow();j.initialized=false;if(n.containment==""){$("#mbMask_"+g).fadeOut("slow",function(){$("#mbg_"+g).slideUp("slow",function(){$("#mbg_"+g).remove()})})}else{$("#mbg_"+g).fadeOut("slow",function(){$("#mbg_"+g).remove()})}});$("#mbMask_"+g).click(function(){stopShow();j.initialized=false;$(this).fadeOut("slow",function(){$("#mbg_"+g).slideUp("slow",function(){$("#mbg_"+g).remove()})})});actualImg=full[n.startFrom];$(thumbs).css(thumbUnsel);actualThumb=thumbs[n.startFrom];$(actualThumb).css(o);m=setTimeout(function(){$(F).hide(500)},2000);var J,x=n.startFrom,startSlide=n.autoSlide;function startShow(){$(y).find("img").attr("src",n.iconFolder+"/stop.gif");if(x==full.length)x=0;preloadImg(x);J=setTimeout(startShow,n.slideTimer);x++};function stopShow(){clearTimeout(J);$(y).find("img").attr("src",n.iconFolder+"/play.gif");startSlide=false}if(startSlide){setTimeout(startShow,n.startTimer)}else{setTimeout(function(){preloadImg(n.startFrom)},n.startTimer)}if(n.containment==""){$("#mbg_"+g).mb_bringToFront().slideDown("slow",function(){$("#mbMask_"+g).fadeIn("slow")})}else{$("#mbg_"+g).mb_bringToFront().fadeIn("slow",function(){})}})};$.fn.mbGallery.defaults={containment:"body",galleryTop:40,galleryWidth:300,galleryHeight:300,galleryMaxWidth:0,galleryMaxHeight:0,galleryColor:"#333",galleryFrameBorder:12,galleryFrameColor:"#fff",maskOpacity:.5,maskBgnd:"transparent",startFrom:"random",headerOpacity:0.8,thumbsBorder:4,thumbHeight:50,thumbStripWidth:250,thumbStripColor:"#333333",thumbStripPos:"right",thumbSelectColor:"black",thumbOverColor:"#cccccc",imageSelector:".imgFull",thumbnailSelector:".imgThumb",descSelector:".imgDesc",descriptionWidth:300,labelColor:"#333",labelColorDisactive:"#333",labelTextColor:"#fff",labelTextSize:"11px",labelHeight:20,iconFolder:"elements/white",fadeTime:500,autoSlide:true,slideTimer:6000,autoSize:true,startTimer:0};jQuery.fn.mb_bringToFront=function(){var b=10;$('*').each(function(){if($(this).css("position")=="absolute"){var a=parseInt($(this).css('zIndex'));b=a>b?parseInt($(this).css('zIndex')):b}});$(this).css('zIndex',b+=1);return $(this)}})(jQuery);
