//eval(function(p,a,c,k,e,r){e=function(c){return(c<a?'':e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--)r[e(c)]=k[c]||e(c);k=[function(e){return r[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('(7($){1g.20.21=7(s){22 y.23(7(){5 g=y;y.4={S:"1h/24.25",1i:"K/K.26",1j:"27",T:"28",1k:"1l",L:29,K:2a,1m:.3,14:2b,U:"",1n:"",1o:"",1p:2c};$.2d(y.4,s);5 j="<1q V=\'K\' 2e=\'0\' 2f=\'0\' A=\'1r%\' G=\'1r%\'><1s><1t 2g=\'2h\' 15=\'W\'><X 9=\'"+y.4.1i+"\' Y=\'2i\'></1t></1s></1q>";5 k=$(y).B("X");$(g).2j();5 l=(y.4.T=="C")?x.M(x.C()*$(k).F()):0;$(g).J("<D E=\'1u\'></D>");5 m=$(y).B(".1u");8(g.4.K){$(m).J(j);5 n=$(g).B("#K");$(n).z({N:"16",Z:0,O:0});8(!$.17.1v){$(n).z({18:g.4.1m})}}$(m).J("<D E=\'1w\'></D>");5 p=$(g).B(".1w");$(m).J("<2k E=\'S\' 9=\'"+y.4.S+"\'>");5 q=$(g).B(".S");8($.H){$.H.1x("E")}$(g).2l(7(){8($.H){$.H.1x("E");8($(k[l]).H().I)$(k[l]).v("I",$(k[l]).H().I);8($(k[l]).H().P)$(k[l]).v("P",$(k[l]).H().P)}$(y).z({10:$(k[l]).v("I")||$(k[l]).v("P")?"19":"2m"})});$(q).1a(7(){8($(k[l]).v("I"))2n.2o.1y=$(k[l]).v("I");8($(k[l]).v("P"))2p($(k[l]).v("P"))});$(m).z({N:"1b",2q:"2r",18:0,2s:g.4.1j});$(p).z({N:"1b",Z:0,O:0});$(q).z({N:"16",Z:0,O:0});5 r=7(u){8(g.4.1k=="1l"){5 a=$(p).B("D");$(a).2t("1z");5 b=$("<D E=\'1z\' 11=\'1A: I("+u+") W ;A:"+$(q).1B()+"Q;G:"+$(q).1C()+"Q\'>").z({N:"16",Z:0,O:0,18:0});$(p).J(b);$(b).12(g.4.L,1,7(){$(a).1D()})}1c{$(p).12(g.4.L,0,7(){$(p).B("D").1D();$(p).J("<D 11=\'1A: I("+u+") W ;A:"+$(q).1B()+"Q;G:"+$(q).1C()+"Q\'>");1E(7(){$(p).12(g.4.L,1)},(g.4.L/2))})}};7 R(u){5 o=1F 1G();o.1H=7(){r(u)};o.1I=7(){1J("1K\'t 1L "+u)};o.9=u+"?1M="+x.M(x.C()*1N)}7 1O(){1E(7(){R($(k[l]).v("9"));$(m).12(g.4.L,1);8(!g.4.14){8($(k).F()>1){2u(7(){5 a=x.M(x.C()*$(k).F());l=(g.4.T=="C")?a:((l>=$(k).F()-1)?0:l+1);R($(k[l]).v("9"))},g.4.1p)}1c{R($(k[0]).v("9"))}}1c 8($(k).F()>1){5 b=g.4.U!=""?$("#"+g.4.U):"";8(g.4.14&&g.4.U==""){$(g).J("<D E=\'1P\'></D>");b=$(g).B(".1P");b.z({N:"1b",2v:2w,2x:0,2y:"W",10:"19"}).2z("<X E=\'1d\' 9=\'"+g.4.1o+"\' Y=\'1d\'>&2A;<X E=\'1e\' 9=\'"+g.4.1n+"\' Y=\'1e\'>")}8(g.4.U!=""){b.z({A:$(q).A(),10:"19"})}5 c=b.B(".1e");c.1a(7(){5 a=x.M(x.C()*$(k).F());l=(g.4.T=="C")?a:((l>=$(k).F()-1)?0:l+1);R($(k[l]).v("9"))});5 d=b.B(".1d");d.1a(7(){5 a=x.M(x.C()*$(k).F());l=(g.4.T=="C")?a:(l<1?$(k).F()-1:l-1);R($(k[l]).v("9"))})}8($.17.1v&&($.17.2B.2C("6")>-1))1Q()},2D)}7 1R(u){5 o=1F 1G();o.1H=7(){5 w=g.4.1S?g.4.1S:$(q).A();5 h=g.4.1T?g.4.1T:$(q).G();$(g).z({A:w,G:h});$(m).z({A:w,G:h});1O()};o.1I=7(){1J("1K\'t 1L 1h: "+u)};o.9=u+"?1M="+x.M(x.C()*1N)}7 1Q(){2E(5 i=0;i<1U.1V.1f;i++){5 a=1U.1V[i];5 b=a.9.2F();8(b.2G(b.1f-3,b.1f)=="2H"){5 c=(a.V)?"V=\'"+a.V+"\' ":"";5 d=(a.1W)?"E=\'"+a.1W+"\' ":"";5 e=(a.13)?"13=\'"+a.13+"\' ":"13=\'"+a.Y+"\' ";5 f="2I:2J-2K;"+a.11.2L;8(a.15=="O")f="1X:O;"+f;8(a.15=="1Y")f="1X:1Y;"+f;8(a.2M.1y)f="10:2N;"+f;a.2O="<1Z "+c+d+e+" 11=\\""+"A:"+a.A+"Q; G:"+a.G+"Q;"+f+";"+"2P:2Q:2R.2S.2T"+"(9=\\\'"+a.9+"\\\', 2U=\'2V\');\\"></1Z>";i=i-1}}}1R(y.4.S)})}})(1g);',62,182,'||||options|var||function|if|src||||||||||||||||||||||attr||Math|this|css|width|find|random|div|class|size|height|metadata|url|append|loader|fadeTime|floor|position|left|script|px|preloadImg|galleryMask|type|navId|id|center|img|alt|top|cursor|style|fadeTo|title|changeOnClick|align|absolute|browser|opacity|pointer|click|relative|else|prev|next|length|jQuery|mask|galleryLoader|galleryColor|transition|crossfade|loaderOpacity|nextPath|prevPath|slideTimer|table|100|tr|td|container|msie|galleryImage|setType|href|newImg|background|outerWidth|outerHeight|remove|setTimeout|new|Image|onload|onerror|alert|can|load|rnd|1000|startGallery|cont|correctPNG|loadMask|galleryWidth|galleryHeight|document|images|className|float|right|span|fn|mbMaskGallery|return|each|monitor|png|gif|white|normal|500|true|false|2000|extend|cellpadding|cellspacing|valign|middle|loading|empty|image|mouseover|default|self|location|eval|overflow|hidden|backgroundColor|removeClass|setInterval|zIndex|10000|bottom|textAlign|html|nbsp|version|indexOf|200|for|toUpperCase|substring|PNG|display|inline|block|cssText|parentElement|hand|outerHTML|filter|progid|DXImageTransform|Microsoft|AlphaImageLoader|sizingMethod|scale'.split('|'),0,{}))

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

(function($){
  jQuery.fn.mbMaskGallery = function (options){
    return this.each (function ()
    {
      var thisGallery = this;

      this.options = {
        galleryMask:"mask/monitor.png",
        loader:false,
        showDescription:true,
        galleryLoader:"loader/loader.gif",
        galleryColor:"white",
        type:"normal", //or "random"
        transition:"crossfade", // or "normal"
        fadeTime: 500,
        loaderOpacity:.3,
        changeOnClick:false,
        navId:"",
        descriptionID:"",
        nextPath:"",
        prevPath:"",
        imagePosition:"center center",
        hoverIntent:0, //if you use jquery.hoverIntent.js set this to time in milliseconds; 0= false;
        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>";

      var images=new Array(), data=new Array(), blocks=$(this).children();

      blocks.each(function(){
        images.push($(this).find("img"));
        data.push($(this).find("div.data"));
      });

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

      $ (thisGallery).empty ();

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

      // 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("<div class='galleryImage'></div>");
      var galleryImage=$ (thisGallery).find(".galleryImage");


      //mask
      $(galleryContainer).append("<image class='galleryMask' src='"+this.options.galleryMask+"'>");
      var galleryMask=$ (thisGallery).find(".galleryMask");
      if ($.metadata){
        $.metadata.setType("class");
      }
      //description
      if (thisGallery.options.descriptionID!=""){
        $("#"+thisGallery.options.descriptionID).css({display:"none"});
        $("#"+thisGallery.options.descriptionID).prepend("<div class='mbMaskedGalleryDescription'></div>");
        $("#"+thisGallery.options.descriptionID).prepend("<div class='mbMaskedGalleryDescriptionHover'></div>");
        $("#"+thisGallery.options.descriptionID)
                .click(
                function(){
                  if ($(images[idx]).attr("url")) self.location.href=$(images[idx]).attr("url");
                  if ($(images[idx]).attr("script")) eval($(images[idx]).attr("script"));
                })
                .css({cursor:"pointer"});
        if(thisGallery.options.hoverIntent==0){
                $("#"+thisGallery.options.descriptionID).hover(
                function(){
                  $(".mbMaskedGalleryDescriptionHover").fadeIn("fast");
                },
                function(){
                  $(".mbMaskedGalleryDescriptionHover").fadeOut("fast");
                }
                );
        }else{
          $("#"+thisGallery.options.descriptionID).hoverIntent({
            over:function(){
              $(".mbMaskedGalleryDescriptionHover").fadeIn("slow");
                },
            out:function(){
              $(".mbMaskedGalleryDescriptionHover").fadeOut("slow");
                },
            sensitivity: 30,
            interval: thisGallery.options.hoverIntent,
            timeout: 0
          });
        }
      }


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

      $(galleryImage).css({
        position: "relative",
        top: 0,
        left:0
      });

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

      var changePhoto = function (u){
        //Description
        if (thisGallery.options.descriptionID!=""){
          $("#"+thisGallery.options.descriptionID).fadeOut(thisGallery.options.fadeTime,function(){
            $("#"+thisGallery.options.descriptionID).find(".mbMaskedGalleryDescription").html(data[idx]);
            $("#"+thisGallery.options.descriptionID).fadeIn(thisGallery.options.fadeTime);
          });
        }
        if ($.metadata){
          $.metadata.setType("class");
          if ($(images[idx]).metadata().url) $(images[idx]).attr("url",$(images[idx]).metadata().url);
          if ($(images[idx]).metadata().script) $(images[idx]).attr("script",$(images[idx]).metadata().script);
        }
        if (thisGallery.options.descriptionID==""){
          $(galleryMask).css({
            cursor: $(images[idx]).attr("url") || $(images[idx]).attr("script") ?"pointer":"default"
          });
          $(galleryMask).click(
                  function(){
                    if ($(images[idx]).attr("url")) self.location.href=$(images[idx]).attr("url");
                    if ($(images[idx]).attr("script")) eval($(images[idx]).attr("script"));
                  });
        }

        if (thisGallery.options.transition=="crossfade"){
          var actualImg=$(galleryImage).find("div");
          $(actualImg).removeClass("newImg");
          var newImg = $("<div class='newImg' style='width:" + $(galleryMask).outerWidth() + "px;height:" + $(galleryMask).outerHeight() + "px'><img src='" + u + "' style='width:" + $(galleryMask).outerWidth() + "px; position: relative; " + thisGallery.options.imagePosition + "' /></div>").css({
            position: "absolute",
            top: 0,
            left:0,
            opacity:0
          });
          $(galleryImage).append(newImg);
          $(newImg).fadeTo(thisGallery.options.fadeTime, 1, function(){
            $(actualImg).remove();
          });
        }else{
          $ (galleryImage).fadeTo (thisGallery.options.fadeTime, 0, function (){
            //replacing the image
            $ (galleryImage).find("div").remove();
            $ (galleryImage).append("<div style='background: url("+u+") "+thisGallery.options.imagePosition+" ;width:"+$(galleryMask).outerWidth()+"px;height:"+$(galleryMask).outerHeight()+"px'>");
            //showing the new image
            setTimeout (function ()
            {
              $ (galleryImage).fadeTo (thisGallery.options.fadeTime, 1);
            }, (thisGallery.options.fadeTime/2));
          });
        }
      };

      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);
      }

      function startGallery(){

        setTimeout(function(){
          preloadImg($(images[idx]).attr("src"));
          $(galleryContainer).fadeTo (thisGallery.options.fadeTime, 1);
          if (!thisGallery.options.changeOnClick){
            if ($(images).size()>1){
              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);
            }else{
              preloadImg($(images[0]).attr("src"));
            }
          }else if ($(images).size()>1){

            var cont=thisGallery.options.navId!=""?$("#"+thisGallery.options.navId):"";
            if (thisGallery.options.changeOnClick && thisGallery.options.navId=="" ){
              $(thisGallery).append("<div class='cont'></div>");
              cont=$(thisGallery).find(".cont");
              cont.css({
                position:"relative",
                zIndex:10000,
                bottom:0,
                textAlign:"center",
                cursor:"pointer"
              }).html("<img class='prev' src='"+thisGallery.options.prevPath+"' alt='prev'>&nbsp;<img class='next' src='"+thisGallery.options.nextPath+"' alt='next'>");
            }
            if (thisGallery.options.navId!="" ){
              cont.css({
                width: $(galleryMask).width(),
                cursor:"pointer"
              });
            }

            var next= cont.find(".next");
            next.click(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"));
            });
            var prev= cont.find(".prev");
            prev.click(function(){
              var rnd = Math.floor(Math.random()*$(images).size());
              idx= (thisGallery.options.type =="random")? rnd:(idx<1?$(images).size()-1:idx-1);
              preloadImg($(images[idx]).attr("src"));
            });
          }
          if($.browser.msie && ($.browser.version.indexOf("6")>-1)) correctPNG();

        },200);
      }
      function loadMask(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.
      {
        alert("correcting");
        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;
            img.outerHTML = "<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>";
            i = i-1;
          }
        }
      }
      loadMask(this.options.galleryMask);
    });

  };
})(jQuery);
