/*
 * Copyright (c) 2009 Rafael Viotti <rviotti@gmail.com>
 *
 *   Author.: Rafael Viotti <rviotti@gmail.com>
 *   Version: 0.2
 *   License: GPL v3 (http://www.gnu.org/licenses/gpl-3.0.txt)
 *
 * This file is part of Sideshow.
 */
(function(e){var d=e(window),w=e('<div id="sideshow-overlay"/>'),l=e('<div id="sideshow-frame"/>'),u=e('<div id="sideshow-upper"/>'),y=e('<div id="sideshow-lower"/>'),n=e('<img id="sideshow-image"/>'),q=e('<div id="sideshow-next"/>'),o=e('<div id="sideshow-prev"/>'),m=e('<div id="sideshow-close"/>'),x=e('<div id="sideshow-caption"/>'),g=e('<div id="sideshow-counter"/>'),k=[new Image(),new Image(),new Image()],r=-1,c={},j=[],i=!window.XMLHttpRequest,h={counterText:"Image {x} of {y}.",imageFade:true,imageFadeTime:"normal",initialWidth:150,initialHeight:150,keyCodesClose:[27,88,67],keyCodesNext:[39,78],keyCodesPrev:[37,80],overlayOpacity:0.8,overlayFadeTime:"fast",resizeEasing:"swing",resizeTime:"normal",visualArea:0.9};e(function(){e("body").append(w.add(l));u.appendTo(l).append(n.add(o).add(q));y.appendTo(l).append(m.add(x).add(g));k[1].onload=f;o.click(p);q.click(s);m.click(b);n.load(a);if(e.browser.msie){if(i){w.add(l).css("position","absolute");o.add(q).unbind("click");o.replaceWith('<a href="#" id="sideshow-prev"/>');q.replaceWith('<a href="#" id="sideshow-next"/>');o=e("#sideshow-prev");q=e("#sideshow-next");q.click(s);o.click(p);q.css("right","-1px")}q.addClass("transparent");o.addClass("transparent");if(!i){x.css("display","inline")}}});e.sideshow=function(B,A,D){var C;if(typeof B=="string"){j=[[B,A]];C=0}else{j=B;C=A||0}e.extend(c,h,D);z();t(C)};e.sideshow.defaults=function(A){e.extend(h,A)};e.fn.sideshow=function(C,F,D){var E,B,A;E=F||function(G){return[G.href,G.title]},B=D||function(){return true},A=this;return A.click(function(){grepfunc=function(I,H){return B.call(this,I,H)};filtered=e.grep(A,grepfunc);startidx=0;for(var G=0;G<filtered.length;G++){if(filtered[G]==this){startidx=G}filtered[G]=E(filtered[G],G)}e.sideshow(filtered,startidx,C);return false})};function t(B){function A(){x.html(j[B][1]);if(j.length==1||!c.counterText){g.empty()}else{text=c.counterText;text=text&&text.replace("{x}",B+1);text=text&&text.replace("{y}",j.length);text&&g.html(text)}if(e.browser.msie){l.removeClass("white")}l.addClass("loading");k[1].src=j[B][0]}if(B>=0&&B<j.length){r=B;o.add(q).hide();y.slideUp(c.resizeTime,function(){l.width(l.width()).height(l.height());if(i){u.add(o).add(q).css({width:"auto",height:"auto"})}if(c.fadeImage){n.fadeOut(c.imageFadeTime,A)}else{n.hide();A()}})}}function f(A){function C(M,N){return M.outerWidth(typeof N=="undefined"&&true||N)-M.width()}function H(M,N){return M.outerHeight(typeof N=="undefined"&&true||N)-M.height()}var D,F,B,L,E,J,G,K,I;D=c.resizeTime,F=c.resizeEasing,B=c.visualArea,L={h:C(u)+C(n),v:H(u)+H(n)},E={h:C(l,false),v:H(l,false)},G={w:k[1].width,h:k[1].height},K={w:parseInt((d.width()-E.h-L.h)*B),h:parseInt((d.height()-E.v-L.v)*B)};if((G.w/K.w>G.h/K.h)&&G.w>K.w){G.h=parseInt((K.w/G.w)*G.h);G.w=K.w}else{if((G.w/K.w<G.h/K.h)&&G.h>K.h){G.w=parseInt((K.h/G.h)*G.w);G.h=K.h}}I={h:y.width(G.w-C(y)+L.h).outerHeight(true)};while(I.h<K.h&&G.h+I.h>K.h){G.w=G.w-parseInt((G.h+I.h-K.h)*(G.w/G.h));G.h=K.h-I.h;I.h=y.width(G.w-C(y)+L.h).outerHeight(true)}J={w:G.w+L.h,h:G.h+L.v};J.x=parseInt((d.width()-J.w-E.h)/2);J.y=parseInt((d.height()-J.h-E.v-I.h)/2);l.removeClass("loading");if(e.browser.msie){l.addClass("white")}l.animate({width:J.w,left:J.x},D,F,function(){l.animate({height:J.h,top:J.y},D,F,function(){n.width(G.w).height(G.h);n.attr("src",k[1].src)})})}function a(B){function A(){l.css({width:"auto",height:"auto"});y.slideDown(c.resizeTime);if(r>0){o.show();k[0].src=j[r-1][0]}if(r<j.length-1){q.show();k[2].src=j[r+1][0]}}if(i){u.width(n.width()).height(n.height());o.width(parseInt(n.width()/2)).height(n.height());q.width(parseInt(n.width()/2)).height(n.height())}if(c.imageFade){n.fadeIn(c.imageFadeTime,A)}else{n.show();A()}}function s(){t(r+1)}function p(){t(r-1)}function z(){function C(){return parseInt((d.width()-l.outerWidth(true))/2)}function A(){return parseInt((d.height()-l.outerHeight(true))/2)}var B,E,D;B={width:c.initialWidth,height:c.initialHeight},E=c.overlayOpacity,D=c.overlayFadeTime;e(document).keydown(v);n.hide();y.hide().children().empty();if(i){d.scroll(function(){w.css({left:d.scrollLeft(),top:d.scrollTop()})});d.resize(function(){w.css({width:d.width(),height:d.height()})});w.css({left:d.scrollLeft(),top:d.scrollTop()})}w.css("opacity",E).fadeIn(D);l.css(B).css({left:C(),top:A()}).show()}function b(A){e(document).unbind("keydown",v);n.stop();y.stop();if(i){d.unbind("scroll resize");u.add(o).add(q).css({width:"auto",height:"auto"})}w.stop().fadeOut(c.overlayFadeTime);l.stop().hide()}function v(B){var A=B.keyCode;if(e.inArray(A,c.keyCodesClose)>=0){b()}else{if(e.inArray(A,c.keyCodesNext)>=0){s()}else{if(e.inArray(A,c.keyCodesPrev)>=0){p()}else{return true}}}return false}})(jQuery);