/*
 * 
 * jbgallery 1.0
 * @requires jQuery v1.3.2
 * 
 * Copyright (c) 2009 Massimiliano Balestrieri
 * Examples and docs at: http://maxb.net/blog/
 * Licensed GPL licenses:
 * http://www.gnu.org/licenses/gpl.html
 *
 * Inspired by http://www.ringvemedia.com/introduction
 */

(function(a){jBGallery={};jBGallery.Init=function(b,c){if(typeof b=="string"){if(jBGallery.Api.enabled(this)||b==="enable"||b==="destroy"){return jBGallery.Api[b](this,c)}}else{return this.each(function(d){new jBGallery.Core(this,b,d)})}};jBGallery.Api={enabled:function(b){return a(b).data("enabled")},current:function(b){return a(b).data("current")},go:function(b,c){return a(b).trigger("go",[c])},left:function(b){return a(b).trigger("left")},right:function(b){return a(b).trigger("right")},play:function(c,b){return a(c).trigger("play",[b])},stop:function(b){return a(b).trigger("stop")},disable:function(b){return a(b).trigger("disable")},enable:function(b){return a(b).trigger("enable")},destroy:function(b){return a(b).trigger("destroy")}};jBGallery.Core=function(c,m,i){var k={};if(a.metadata){k=a(c).metadata()}var l=a.extend({style:"zoom",slideshow:false,interval:4000,fade_time:400,fade:true,shortcuts:[37,39],after:function(n){},before:function(){},load:function(n){},close:function(){},popup:false,labels:{},log:function(){}},k,m);if(!l.fade){l.fade_time=0}var e=c;var j=i;a("html, body").addClass("jbg");a("#jbgallery-css").removeAttr("disabled");a(e).find("ul").hide();var d=a.map(a(e).find("ul > li > a"),function(n,o){if(l.menu=="numbers"){a(n).text((o+1))}return{href:n.href,title:n.title}});var h=d[0].href;var g=d[0].title;a(e).prepend('<div id="jbg"><table cellpadding="0" cellspacing="0"><tr><td><img class="'+l.style+'" id="jbgallery_target" alt="'+g+'" src="'+h+'" /></td></tr></table></div>');var b=a("#jbgallery_target",e);if(l.style=="centered"){a("#jbg").css({top:"auto",height:"100%"});a("#jbg").height("100%")}jBGallery.OnLoadImage(e,l,b);if(d.length>1){new jBGallery.Engine(e,l,b,j,d)}function f(n){if(window.console){console.log(n)}else{alert(n)}}};jBGallery.slideshow=false;jBGallery.interval=false;jBGallery.timeout=false;jBGallery.OnLoadImage=function(g,c,e){var f=navigator.userAgent.toLowerCase().search(/webkit/)!=-1;var b=/MSIE 6/i.test(navigator.userAgent);e.bind("load",function(i){c.log("jBGallery.OnLoadImage : load");d(i);c.load(i);c.after(i);if(jBGallery.slideshow){clearTimeout(jBGallery.timeout);jBGallery.timeout=setTimeout(function(){a(g).trigger("right",[jBGallery.interval])},jBGallery.interval)}});if(f||b||c.style=="centered"){jBGallery.Fix(g,c,e,b,c.style=="centered")}function d(i){c.log("ev: _onload");a("#jbg-loading").hide();h(i)}function h(i){c.log("ev: _appear");e.fadeIn(c.fade_time)}};jBGallery.Fix=function(g,l,b,c,i){var e=null;var k=b.get(0);a(window).bind("resize",function(m){if(e){clearTimeout(e)}e=setTimeout(function(){l.log("resize: _fix_dimensions");h(m)},100)});b.bind("load",function(m){h(m)});function h(m){if(i){f(m)}else{if(c){b.height("auto").width("auto");setTimeout(d,10)}else{d(m)}}}function f(m){l.log("jBGallery.Fix: load/fix_bigimage");a(b).width("auto").height("auto");var n=j();if(n.h>n.bh||n.w>n.bw){if(n.p<=1){a(b).width(n.bw)}else{if(n.p>1){a(b).height(n.bh)}}}}function d(m){l.log("jBGallery.Fix: load/fix_dimensions");if(a("#jbgallery-css").size()==0){alert("load css");return}var n=j();if(c){if(n.h<n.bh||n.w<n.bw){if(n.p==1){if(n.pm>1){a(b).width(n.bw)}else{a(b).height(n.bh)}}else{if(n.p<1){a(b).width("100%")}else{if(n.p>1){a(b).height("100%")}}}}}else{if(n.h<n.bh){a(b).height(n.bh)}}}function j(){var q=a("body").width();var m=a("body").height();var n=q/m;var o=k.width/k.height;return{bw:q,bh:m,pm:n,p:o,h:k.height,w:k.width}}};jBGallery.Engine=function(j,p,k,d,e){var c=1;var t=true;jBGallery.interval=p.interval;jBGallery.Data(j,"current",c);jBGallery.Data(j,"enabled",true);a(j).bind("play",m).bind("stop",h).bind("right",l).bind("left",s).bind("go",g).bind("enable",u).bind("disable",o).bind("destroy",i);r();b();q();if(p.slideshow){m()}function r(){if(p.menu){a(j).find("ul").hide()}}function w(x){v();c=jBGallery.Data(j,"current",x);k.fadeOut(p.fade_time,function(){a(this).hide().attr({src:e[(c-1)].href,alt:e[(c-1)].title})})}var n=0;var f=1;function m(y,x){p.log("jBGallery.Engine: _play");if(jBGallery.slideshow){return}jBGallery.slideshow=true;if(x){jBGallery.interval=x}if(y){a(j).trigger("right",[jBGallery.interval])}}function h(x){p.log("jBGallery.Engine: _stop");jBGallery.slideshow=false;clearTimeout(jBGallery.timeout)}function l(z,y){p.log("jBGallery.Engine: _right");if(c<e.length){var x=c+1}else{var x=1}a(j).trigger("go",[x,y])}function s(y){p.log("jBGallery.Engine: _left");if(c>1){var x=c-1}else{var x=e.length}a(j).trigger("go",[x])}function g(A,z,x){if(jBGallery.slideshow){f=n?A.timeStamp-n:x;n=A.timeStamp;if(x&&((x-100)>f)){p.log("Skip go elapsed only : "+f+" Interval: "+x);clearTimeout(jBGallery.timeout);jBGallery.timeout=setTimeout(y,x)}else{p.log("Elapsed : "+f+" Interval: "+x);y()}}else{y()}function y(){p.log("jBGallery.Engine: _go "+z);p.before();w(z)}}function u(x){p.log("jBGallery.Engine: _enable");t=jBgSlide.Data(j,"enabled",true)}function o(x){p.log("jBGallery.Engine:_disable");t=jBgSlide.Data(j,"enabled",false);if(_slideshow){h()}}function i(x){jBGallery.slideshow=false;clearTimeout(jBGallery.timeout);p.log("jBGallery.Engine:_destroy");a("#jbgallery-css").attr("disabled","disabled");a("#jbg-loading").remove();a("html, body").removeClass("jbg");k.unbind();k.remove();var y=a("body").find("ul#jbg-menu-"+d);y.find("a").unbind();y.remove();a.removeData(j);a(j).unbind();a("#jbg",j).remove();a(document).unbind("keydown.jbgallery")}function b(){a(window).unload(function(){a(j).trigger("destroy")})}function q(){}function v(){a("#jbg-loading").show()}};jBGallery.Data=function(c,b,d){if(typeof d!=="undefined"){a(c).data(b,d);return d}else{return a(c).data(b)}};jBGallery.Css=function(b){a(document.getElementsByTagName("head")[0]).append('<link rel="stylesheet" type="text/css" id="jbgallery-css" media="screen" href="'+b+'" />')};a.fn.jbgallery=jBGallery.Init;a.fn.jbgcss=jBGallery.Css})(jQuery);