
(function($){var masterCache={};var images;var swf;var $box=$('<div id="easy-gallery-box"><div class="easy-gallery-nav"><a class="easy-gallery-close" href="#" title="close (press ESC)">close x</a><span class="easy-gallery-nav-links">&nbsp;</span></div><div id="easy-gallery-img" /></div>');var $backdrop=$('<div><div id="easy-gallery-backdrop"></div></div>').css({left:0,opacity:0,position:'absolute',top:0,'z-index':-9999}).find('div').end().append($box);function Images(img,count){var src=img.lastIndexOf('_');var index=0;var base=src>-1?img.substr(0,src):img;var ext=img.substr(img.lastIndexOf('.'));this.count=count;this.images=[];for(var i=0;i<count;i++){this.images[i]=new Image();this.images[i].src=base+'_'+(i+1)+ext;}
this.current=function(){return this.images[index];};this.next=function(){index++;return this;};this.previous=function(){index--;return this;};this.end=function(){index=count-1;return this;};this.rewind=function(){index=0;return this;};this.index=function(){return index;};this.valid=function(){return this.images[index];};}
function Media(swf,files){var base=swf+'?src=';var index=0;this.count=files.length;this.files=files;this.current=function(){return base+this.files[index];};this.next=function(){index++;return this;};this.previous=function(){index--;return this;};this.end=function(){index=this.count-1;return this;};this.rewind=function(){index=0;return this;};this.index=function(){return index;};this.valid=function(){return this.files[index];};}
function easyGalleryNav(offset){$('*').bind('click.closeEasyGallery keyup.closeEasyGallery',function(ev){if($(ev.target).hasClass('easy-gallery-close')||ev.keyCode==27||(ev.type!='keyup'&&$(ev.target)!==$box[0]&&!$(ev.target).parents('#easy-gallery-box')[0])){ev.stopPropagation();ev.preventDefault();$backdrop.fadeTo(400,0,function(){$(this).css('z-index',-9999);});$box.animate($.extend(offset,{width:0,height:0}),400,function(){$(this).removeAttr('style').find('#easy-gallery-img').empty();});$('*').unbind('.closeEasyGallery');}else{var img;if($(ev.target).hasClass('easy-gallery-next')||$(ev.target).hasClass('easy-gallery-prev')||ev.keyCode==37||ev.keyCode==39){if($(ev.target).hasClass('easy-gallery-next')||ev.keyCode==39){img=images.next().valid()?images.current():images.rewind().current();}else{img=images.previous().valid()?images.current():images.end().current();}
if(images instanceof Images){resizeBox(img,offset);}else{insertSwf(img,offset);}}
ev.preventDefault();}});}
function makeGallery(img,offset){$('<img src="'+img.src+'" style="height:'+img.height+'px;width:'+img.width+'px;" />').appendTo($('#easy-gallery-img'));$backdrop.css({height:$('body').height(),width:$('body').width(),'z-index':9999}).prependTo('body').fadeTo(400,1);$('div.easy-gallery-nav').hide();$box.fadeTo(300,1,function(){resizeBox(img,offset);});}
function insertSwf(swffile,offset,size){var newBox=false;if(size){newBox=true;size=size.split('x');}else{size=[swf.width,swf.height];}
swf={'bgcolor':'#000000','height':size[1],'swf':swffile,'width':size[0]};$('#easy-gallery-img').flash(swf);if(newBox){$backdrop.css({height:$().height(),width:$('body').width(),'z-index':9999}).prependTo('body').fadeTo(400,1);$('div.easy-gallery-nav').hide();$box.fadeTo(300,1,function(){$('*').unbind('.closeEasyGallery');$box.animate({left:parseInt(($backdrop.width()-swf.width)/2),top:parseInt(($backdrop.height()-swf.height)/4),width:swf.width},400,function(){$('div.easy-gallery-nav').show('normal',function(){$box.height($box.height());});easyGalleryNav(offset);});});}}
function resizeBox(img,offset){$('*').unbind('.closeEasyGallery');$box.animate({left:parseInt(($backdrop.width()-img.width)/2),top:parseInt(($backdrop.height()-img.height)/4),width:img.width},400,function(){$('div.easy-gallery-nav').show('normal');easyGalleryNav(offset);}).find('img').attr('src',img.src).animate({height:img.height,width:img.width},400);}
function loadImages(img,count){var src=img.lastIndexOf('_');var ext=img.substr(img.lastIndexOf('.'));if(src>-1){src=img.substr(0,src);}else{src=img;}
for(var i=0;i<count;i++){images[i]=new Image();images[i].src=src+'_'+(i+1)+ext;}}
function buildNav(count){$('span.easy-gallery-nav-links').html('&nbsp;');if(count==1)
return;$('<a class="easy-gallery-prev" href="#" title="Previous (left arrow)">Prev</a> <a class="easy-gallery-next" href="#" title="Next (right arrow)">Next</a>').appendTo('span.easy-gallery-nav-links');}
$.fn.easyGallery=function(options){var opts=$.extend({},$.fn.easyGallery.defaults,options);if(opts.swfscript.indexOf('://')<0)
opts.swfscript=document.location.protocol+'//'+document.location.host+opts.swfscript;$backdrop.prependTo('body');if($.support.leadingWhitespace&&!$backdrop.children(':first').css('background-color').match(/rgba/))
$backdrop.children(':first').css('opacity',.7).children(':first').appendTo($backdrop);$(opts.launch).live('click.launchGallery',function(){var offset=$(this).offset();offset.left-=(parseInt($box.css('border-left-width'))+parseInt($box.css('padding-left'))+parseInt($box.css('margin-left')));offset.top-=(parseInt($box.css('border-top-width'))+parseInt($box.css('padding-top'))+parseInt($box.css('margin-top')));$box.css(offset);if(this.href.indexOf('.swf')>-1){var size=$(this).attr('class').match(/(?:\{size\:)(.+?)(?:\})/);if(!size)
size=['','640x50'];var srcs=$(this).attr('class').match(/(?:\{srcs\: *\[)(.*?)(?:\]\})/);var base=this.href.substr(0,this.href.indexOf('?'));if(srcs){srcs=srcs[1].split(',');}else{var src=this.href.match(/(?:src=)(.*)/);srcs=[src[1]];}
buildNav(srcs.length);images=new Media(base,srcs);if(!$.fn.flash){$.getScript(opts.swfscript,function(){insertSwf(images.current(),offset,size[1]);});}else{insertSwf(images.current(),offset,size[1]);}}else{var count=$(this).attr('class').match(/(?:\{count\:)(\d+)(?:\})/);count=count?count[1]:1;buildNav(count);images=new Images(this.href,count);if(!images.current().width){$(images.current()).bind('load',function(){makeGallery(images.current(),offset);});}else{makeGallery(images.current(),offset);}}
return false;});return this.each(function(){var $this=$(this);var o=$.metadata?$.extend({},$this.metadata(),opts):opts;if(!o.replacement)
o.replacement=o.toReplace;$(o.thumbnails,$this).bind('click.easyGallery',function(){var ajax=this.href.indexOf('#');if(ajax==-1){ajaxHandler(this.href,o.dataType,o.toReplace,o.replacement);}else{var selector=this.href.substr(ajax+1);$(selector).show().siblings().hide();}
return false;});});};function ajaxHandler(url,dataType,toReplace,replacement){var key=url.substr(url.indexOf('/')>-1?url.lastIndexOf('/')+1:0);if(!masterCache[key]){$.ajax({dataType:dataType,success:function(data){masterCache[key]=$(data).find(replacement).html();$(toReplace).html(masterCache[key]);},url:url});}else{$(toReplace).html(masterCache[key]);}}
$.fn.easyGallery.defaults={backdropColor:'#000000',backdropOpacity:.7,dataType:'html',launch:'a.launch-gallery',nav:'',swfscript:'/scripts/jquery.swfobject.js',toReplace:'#portrightcol',thumbnails:'a.easy-gallery'};})(jQuery);