
(function($){var win=$(window),options,images,activeImage=-1,activeURL,prevImage,nextImage,compatibleOverlay,middle,centerWidth,centerHeight,ie6=!window.XMLHttpRequest,hiddenElements=[],documentElement=document.documentElement,preload={},preloadPrev=new Image(),preloadNext=new Image(),descriptions={},overlay,center,center_border,image,sizer,prevLink,nextLink,bottomContainer,topContainer,bottom,caption,number;var transparent_border_width=100;var transparent_border_height=60;var top_navi_spacing=3;$(function(){$("body").append($([overlay=$('<div id="lbOverlay" />')[0],center=$('<div id="lbCenter" />')[0],center_border=$('<div id="lbCenterBorder" />')[0],bottomContainer=$('<div id="lbBottomContainer" />')[0],topContainer=$('<div id="lbTopContainer" />')[0]]).css("display","none"));image=$('<div id="lbImage" />').appendTo(center).append(sizer=$('<div style="position: relative;" />').append([prevLink=$('<a id="lbPrevLink" href="#" />').click(previous)[0],nextLink=$('<a id="lbNextLink" href="#" />').click(next)[0]]).append(imgPreview=$('<img id="lbImgPreview" src="" alt="" />'))[0])[0];bottom=$('<div id="lbBottom" />').appendTo(bottomContainer).append([caption=$('<div id="lbCaption" />')[0],number=$('<div id="lbNumber" />')[0],$('<div style="clear: both;" />')[0]])[0];$(topContainer).append([top_navi_prev=$('<a id="lbNaviPrevLink" href="#" />').click(previous)[0],top_navi_next=$('<a id="lbNaviNextLink" href="#" />').click(next)[0],top_navi_first=$('<a id="lbNaviFirstLink" href="#" />').click(first)[0],top_navi_last=$('<a id="lbNaviLastLink" href="#" />').click(last)[0],top_navi_number=$('<div id="lbNaviNumber" />')[0],$('<a id="lbCloseLink" href="#" />').add(overlay).click(close)[0]]);});$('#lbNaviNumber a').live('click',function(){var image_idx=$(this).attr('rel')*1;return changeImage(image_idx);});$.slimbox=function(_images,startImage,_options){options=$.extend({loop:false,overlayOpacity:0.8,overlayFadeDuration:400,resizeDuration:400,resizeEasing:"swing",initialWidth:250,initialHeight:250,imageFadeDuration:400,captionAnimationDuration:400,counterText:"Image {x} of {y}",closeKeys:[27,88,67],previousKeys:[37,80],nextKeys:[39,78]},_options);if(typeof _images=="string"){_images=[[_images,startImage]];startImage=0;}
middle=win.scrollTop()+(win.height()/2);centerWidth=options.initialWidth;centerHeight=options.initialHeight;$(center).css({top:Math.max(0,middle-(centerHeight/2)),width:centerWidth,height:centerHeight,marginLeft:-centerWidth/2}).show();$(center_border).css({top:Math.max(0,middle-(centerHeight/2)),width:centerWidth,height:centerHeight,marginLeft:-centerWidth/2,opacity:0.8}).show();compatibleOverlay=ie6||(overlay.currentStyle&&(overlay.currentStyle.position!="fixed"));if(compatibleOverlay)overlay.style.position="absolute";$(overlay).css("opacity",options.overlayOpacity).fadeIn(options.overlayFadeDuration);if(_images.length<=1){$(top_navi_prev).hide();$(top_navi_next).hide();$(top_navi_first).hide();$(top_navi_last).hide();$(top_navi_number).hide();}
else{$(top_navi_prev).show();$(top_navi_next).show();$(top_navi_first).show();$(top_navi_last).show();$(top_navi_number).show();}
position();setup(1);images=_images;options.loop=options.loop&&(images.length>1);return changeImage(startImage);};$.fn.slimbox=function(_options,linkMapper,linksFilter){linkMapper=linkMapper||function(el){var longDescUrl=$(el).children('img').first().attr('longdesc');return[el.href,el.title,longDescUrl];};linksFilter=linksFilter||function(){return true;};var links=this;return links.unbind("click").click(function(){var link=this,startIndex=0,filteredLinks,i=0,length;filteredLinks=$.grep(links,function(el,i){return linksFilter.call(link,el,i);});for(length=filteredLinks.length;i<length;++i){if(filteredLinks[i]==link)startIndex=i;filteredLinks[i]=linkMapper(filteredLinks[i],i);}
return $.slimbox(filteredLinks,startIndex,_options);});};function position(){var l=win.scrollLeft(),w=win.width();$([center,center_border,bottomContainer,topContainer]).css("left",l+(w/2));if(compatibleOverlay)$(overlay).css({left:l,top:win.scrollTop(),width:w,height:win.height()});}
function setup(open){if(open){$("object").add(ie6?"select":"embed").each(function(index,el){hiddenElements[index]=[el,el.style.visibility];el.style.visibility="hidden";});}else{$.each(hiddenElements,function(index,el){el[0].style.visibility=el[1];});hiddenElements=[];}
var fn=open?"bind":"unbind";win[fn]("scroll resize",position);$(document)[fn]("keydown",keyDown);}
function keyDown(event){var code=event.keyCode,fn=$.inArray;return(fn(code,options.closeKeys)>=0)?close():(fn(code,options.nextKeys)>=0)?next():(fn(code,options.previousKeys)>=0)?previous():false;}
function first(){return changeImage(0);}
function last(){return changeImage(images.length-1);}
function previous(){return changeImage(prevImage);}
function next(){return changeImage(nextImage);}
function changeImage(imageIndex){if(imageIndex>=0){activeImage=imageIndex;activeURL=images[activeImage][0];prevImage=(activeImage||(options.loop?images.length:0))-1;nextImage=((activeImage+1)%images.length)||(options.loop?0:-1);stop();center.className="lbLoading";preload=new Image();preload.onload=animateBox;preload.src=activeURL;}
return false;}
function animateBox(){center.className="";var bordersHeight=(transparent_border_height*2)+$(bottomContainer).outerHeight()+$(topContainer).outerHeight();var bordersWidth=transparent_border_width*2;if(preload.height+bordersHeight>$(window).height()){preload.height=$(window).height()-bordersHeight;}
if(preload.width+bordersWidth>$(window).width()){preload.width=$(window).width()-bordersWidth;}
$(image).css({visibility:"hidden",display:""});$(imgPreview).attr('src',activeURL).css({maxWidth:preload.width,maxHeight:preload.height});$(sizer).css({maxWidth:preload.width});$([sizer,prevLink,nextLink]).css({maxHeight:preload.height});$(caption).html(images[activeImage][1]||"");loadDescription(activeImage,images[activeImage][2],true);$(number).html((((images.length>1)&&options.counterText)||"").replace(/{x}/,activeImage+1).replace(/{y}/,images.length));var navi_num_html='';for(i=0;i<images.length;i++){if(i){navi_num_html+='<span class="delim">|</span>';}
if(i==activeImage){navi_num_html+='<span id="tn_num_active">'+(i+1)+'</span>';}
else{navi_num_html+='<a href="#" rel="'+i+'">'+(i+1)+'</a>';}}
$(top_navi_number).html(navi_num_html);if(prevImage>=0){preloadPrev.src=images[prevImage][0];loadDescription(prevImage,images[prevImage][2],false);}
if(nextImage>=0){preloadNext.src=images[nextImage][0];loadDescription(nextImage,images[nextImage][2],false);}
centerWidth=image.offsetWidth;centerHeight=image.offsetHeight;var top=Math.max(0,middle-(centerHeight/2));if(center.offsetHeight!=centerHeight){$(center).animate({height:centerHeight,top:top},options.resizeDuration,options.resizeEasing);$(center_border).animate({height:centerHeight+(transparent_border_height*2)+$(bottomContainer).outerHeight()+$(topContainer).outerHeight(),top:top-transparent_border_height-$(topContainer).outerHeight()},options.resizeDuration,options.resizeEasing);}
if(center.offsetWidth!=centerWidth){$(center).animate({width:centerWidth,marginLeft:-centerWidth/2},options.resizeDuration,options.resizeEasing);$(center_border).animate({width:centerWidth+(transparent_border_width*2),marginLeft:-centerWidth/2-transparent_border_width},options.resizeDuration,options.resizeEasing);}
$(center).queue(function(){$(bottomContainer).css({width:centerWidth,top:top+centerHeight,marginLeft:-centerWidth/2,visibility:"hidden",display:""});$(topContainer).css({width:centerWidth+(transparent_border_width*2),top:top-38,marginLeft:-centerWidth/2-transparent_border_width,visibility:"hidden",display:""});$(top_navi_prev).css('left',transparent_border_width+'px');$(top_navi_next).css('left',centerWidth+transparent_border_width-$('#lbNaviNextLink').outerWidth()+'px');$(top_navi_first).css('left',$(top_navi_prev).position().left-$('#lbNaviFirstLink').outerWidth()-top_navi_spacing+'px');$(top_navi_last).css('left',$(top_navi_next).position().left+$(top_navi_next).outerWidth()+top_navi_spacing+'px');$(top_navi_number).css('width',centerWidth-$('#lbNaviNextLink').outerWidth()-$('#lbNaviPrevLink').outerWidth()-(top_navi_spacing*2)+'px');$(image).css({display:"none",visibility:"",opacity:""}).fadeIn(options.imageFadeDuration,animateCaption);$(top_navi_number).scrollLeft(0);var selectedObj=$(top_navi_number).find('span#tn_num_active');var lastObj=$(top_navi_number).children().last();if(selectedObj.length&&lastObj.length){var selectedObjPos=selectedObj.position();var selectedObjLeftPos=selectedObjPos.left;var selectedObjWidth=selectedObj.outerWidth();var selectedObjCenterPos=selectedObjLeftPos+(selectedObjWidth/2);var lastObjPos=lastObj.position();var lastObjLeftPos=lastObjPos.left;var lastObjWidth=lastObj.outerWidth();var lastObjRightPos=lastObjLeftPos+lastObjWidth;var contWidth=$(top_navi_number).innerWidth();var scroll=selectedObjCenterPos-(contWidth/2);$(top_navi_number).scrollLeft(scroll);}});}
function animateCaption(){if(prevImage>=0){$(prevLink).show();$(top_navi_prev).removeClass('disable');$(top_navi_first).removeClass('disable');}
else{$(top_navi_prev).addClass('disable');$(top_navi_first).addClass('disable');}
if(nextImage>=0){$(nextLink).show();$(top_navi_next).removeClass('disable');$(top_navi_last).removeClass('disable');}
else{$(top_navi_next).addClass('disable');$(top_navi_last).addClass('disable');}
$(bottom).css("marginTop",-bottom.offsetHeight).animate({marginTop:0},options.captionAnimationDuration);bottomContainer.style.visibility="";topContainer.style.visibility="";}
function stop(){preload.onload=null;preload.src=preloadPrev.src=preloadNext.src=activeURL;$([center,center_border,image,bottom]).stop(true);$([prevLink,nextLink,image,bottomContainer,topContainer]).hide();}
function close(){if(activeImage>=0){stop();activeImage=prevImage=nextImage=-1;$(center).hide();$(center_border).hide();$(overlay).stop().fadeOut(options.overlayFadeDuration,setup);}
return false;}
function loadDescription(imgIdx,longdescUrl,showDescription){displayDesc=function(desc){$(caption).append('<div id="lbCaptionDesc">'+desc+'</div>');};if(typeof(descriptions[imgIdx])=='undefined'&&typeof(longdescUrl)=='string'){$.getJSON(longdescUrl,{},function(data){descriptions[imgIdx]=typeof(data.desc)=='string'?data.desc:'';if(showDescription){displayDesc(descriptions[imgIdx]);}});}
else if(showDescription&&typeof(descriptions[imgIdx])=='string'&&descriptions[imgIdx].length){displayDesc(descriptions[imgIdx]);}
return true;}})(jQuery);
