(function($){$.fn.joshbox=function(user_options){var options={'width':'300px','height':'300px'};if(options){$.extend(options,user_options);}
var rel=$(this).attr("rel");function image_width(img_element){var t=new Image();t.src=(img_element.getAttribute?img_element.getAttribute("src"):false)||img_element.src;var width=t.width;t=null;return width;}
function image_height(img_element){var t=new Image();t.src=(img_element.getAttribute?img_element.getAttribute("src"):false)||img_element.src;var height=t.height;t=null;return height;}
function vertically_center(parent,child){$(child).css("position","relative");var parentCenter=$(parent).height()/2;debug.log("parent's center is: "+parentCenter);var childHalf=image_height(child)/2;if(image_height(child)>$(parent).height()){return;}
debug.log("Child half: "+childHalf);var childTop=parentCenter-childHalf;debug.log("Our top ought to be: "+childTop);$(child).css("top",childTop);}
$(this).click(function(ev){ev.preventDefault();var allImages=$("a[rel='"+rel+"']");var ourPosition=1;$(allImages).each(function(i,element){function seekTo(direction){debug.info("Seeking to direction: "+direction);debug.info("Our position: "+ourPosition);var newIndex=ourPosition+direction;debug.info("before new index: "+newIndex);if(newIndex>allImages.length-1){newIndex=0;}else if(newIndex<0){newIndex=allImages.length-1;}
debug.info("after new index: "+newIndex);debug.log(allImages);switchTo(allImages[newIndex]);}
function fitImage(image){var containerWidth=$(centerDiv).width();var containerHeight=$(centerDiv).height();debug.log("Container width: "+containerWidth);debug.log("Container height: "+containerHeight);debug.log("Image Width: "+$(image).width());debug.log("Image Height: "+$(image).height());if(image_width(image)>containerWidth){$(image).width(containerWidth);}
if(image_height(image)>containerHeight){$(image).height(containerHeight);}}
function switchTo(imageLink){debug.log("Switching to: "+imageLink);$(centerDiv).empty();var newImage=document.createElement("img");var imageURL=$(imageLink).attr("href")+"?"+new Date().getTime();function performSwitch(imageLink){$(centerDiv).empty();$(newImage).css("display","none");$(centerDiv).append(newImage);$(newImage).fadeIn('slow');ourPosition=$.inArray(imageLink,allImages);if(ourPosition==-1){ourPosition=0;}
debug.log("Our position: "+ourPosition);fitImage(newImage);vertically_center(centerDiv,newImage);$(newImage).css("cursor","pointer");$(newImage).click(function(){seekTo(1);});}
if($(newImage).width()==0||$(newImage).height()==0){var loadingImage=document.createElement("img");$(loadingImage).attr("src","joshbox/images/loading.gif");$(centerDiv).append(loadingImage);vertically_center(centerDiv,loadingImage);if($.browser.msie){$(newImage).attr("src",imageURL);var intervalId=setInterval(function(){debug.log("Trying to load: "+imageURL);if(!newImage.complete){debug.log("Waiting for the image...");}else{debug.log("That image is loaded! Stopping polling!");clearInterval(intervalId);performSwitch(imageLink);}},1000);}else{$(newImage).attr("src",imageURL);$(newImage).load(function(){performSwitch(imageLink);});}}else{$(centerDiv).children().each(function(i,c){c.fadeOut('slow');});performSwitch(imageLink);}}
ourPosition=$.inArray(element,allImages);var joshboxDiv=document.createElement("div");$(joshboxDiv).addClass("joshbox");$(joshboxDiv).css("position","absolute");$(joshboxDiv).css("width",options['width']);$(joshboxDiv).css("height",options['height']);$(joshboxDiv).css("display","screen");var leftPos=(($(document).width()/2)-$(joshboxDiv).width()/2);var topPos=(($(document).height()/2)-$(joshboxDiv).height()/2);$(joshboxDiv).css("left",leftPos);$(joshboxDiv).css("top",topPos);$(joshboxDiv).css("z-index","11");$(joshboxDiv).css("background","white");var leftDiv=document.createElement("div");$(leftDiv).css("width",$(joshboxDiv).width()*0.5);$(leftDiv).css("position","absolute");$(leftDiv).css("left","0");$(leftDiv).css("height",$(joshboxDiv).height());$(leftDiv).css("float","left");$(rightDiv).css("text-align","left");$(leftDiv).addClass("sidePanel");var leftArrow=document.createElement("img");$(leftArrow).attr("src","joshbox/images/leftPointer.jpg");$(leftDiv).append(leftArrow);vertically_center(leftDiv,leftArrow);$(joshboxDiv).append(leftDiv);$(leftDiv).click(function(){seekTo(-1);});var rightDiv=document.createElement("div");$(rightDiv).css("width",$(joshboxDiv).width()*0.5);$(rightDiv).css("position","absolute");$(rightDiv).css("right","0");$(rightDiv).css("height",$(joshboxDiv).height());$(rightDiv).css("float","left");$(rightDiv).css("text-align","right");$(rightDiv).addClass("sidePanel");var rightArrow=document.createElement("img");$(rightArrow).attr("src","joshbox/images/rightPointer.jpg");vertically_center(rightDiv,rightArrow);$(rightDiv).append(rightArrow);$(rightDiv).click(function(){seekTo(1);});$(joshboxDiv).append(rightDiv);var topDiv=document.createElement("div");$(topDiv).width($(joshboxDiv).width());$(topDiv).height($(joshboxDiv).height()*0.1);$(joshboxDiv).append(topDiv);var centerDiv=document.createElement("div");$(centerDiv).css("width",$(joshboxDiv).width());$(centerDiv).css("height",$(joshboxDiv).height()*0.8);$(centerDiv).css("float","left");$(joshboxDiv).append(centerDiv);var bottomDiv=document.createElement("div");$(bottomDiv).width($(joshboxDiv).width());$(bottomDiv).height($(joshboxDiv).height()*0.1);$(joshboxDiv).append(bottomDiv);var imgSrc=$(element).attr("href");var imgElement=document.createElement("img");$(imgElement).attr("src",imgSrc);switchTo(element);$(centerDiv).css("text-align","center");var closeBox=document.createElement("img");$(closeBox).css("position","absolute");$(closeBox).css("right","10px");$(closeBox).css("top","10px");$(closeBox).css("height","13px");$(closeBox).attr("src","joshbox/images/close.jpg");$(closeBox).hover(function(){$(this).css("cursor","pointer");});$(closeBox).click(function(){$(overlay).remove();$(joshboxDiv).remove();});$(joshboxDiv).append(closeBox);var overlay=document.createElement("img");$(overlay).attr("src","joshbox/images/overlay.png");$(overlay).css("width","100%");$(overlay).css("height","100%");$(overlay).css("position","absolute");$(overlay).css("z-index","10");$(overlay).click(function(){$(overlay).remove();$(joshboxDiv).remove();});$(overlay).css('display','none');$(joshboxDiv).css('display','none');$('html').prepend(overlay);$('html').prepend(joshboxDiv);$(overlay).fadeIn('slow');$(joshboxDiv).fadeIn('fast');$(".sidePanel").hover(function(){$(this).css("cursor","pointer");});return false;});});return this;};})(jQuery);
