
var interval			= 4000;
var playtime;
var current 			= 0;
var current_thumb 		= 0;
var nmb_thumb_wrappers	= $('#slideshow_thumbs .slideshow_thumb_wrapper').length;
var nmb_images_wrapper  = 6;
play();
slideshowMouseEvent();
function slideshowMouseEvent(){
$('#slideshow_slideshow').unbind('mouseenter')
.bind('mouseenter',showControls)
.andSelf()
.unbind('mouseleave')
.bind('mouseleave',hideControls);
}
$('#slideshow_grid').bind('click',function(e){
hideControls();
$('#slideshow_slideshow').unbind('mouseenter').unbind('mouseleave');
pause();
$('#slideshow_thumbs').stop().animate({'top':'0px'},500);
e.preventDefault();
});
$('#slideshow_thumb_close').bind('click',function(e){
showControls();
slideshowMouseEvent();
$('#slideshow_thumbs').stop().animate({'top':'-230px'},500);
e.preventDefault();
});
$('#slideshow_pause_play').bind('click',function(e){
var $this = $(this);
if($this.hasClass('slideshow_play'))
play();
else
pause();
e.preventDefault();
});
$('#slideshow_next').bind('click',function(e){
pause();
next();
e.preventDefault();
});
$('#slideshow_prev').bind('click',function(e){
pause();
prev();
e.preventDefault();
});
function showControls(){
$('#slideshow_controls').stop().animate({'right':'15px'},500);
}
function hideControls(){
$('#slideshow_controls').stop().animate({'right':'-110px'},500);
}
function play(){
next();
$('#slideshow_pause_play').addClass('slideshow_pause').removeClass('slideshow_play');
playtime = setInterval(next,interval)
}
function pause(){
$('#slideshow_pause_play').addClass('slideshow_play').removeClass('slideshow_pause');
clearTimeout(playtime);
}
function next(){
++current;
showImage('r');
}
function prev(){
--current;
showImage('l');
}
function showImage(dir){
alternateThumbs();
var $thumb = $('#slideshow_thumbs .slideshow_thumb_wrapper:nth-child('+current_thumb+')')
.find('a:nth-child('+ parseInt(current - nmb_images_wrapper*(current_thumb -1)) +')')
.find('img');
if($thumb.length){
var source = $thumb.attr('alt');
var $currentImage = $('#slideshow_wrapper').find('img');
if($currentImage.length){
$currentImage.fadeOut(function(){
$(this).remove();
$('<img />').load(function(){
var $image = $(this);
imageResize($image);
$image.hide();
$('#slideshow_wrapper').empty().append($image.fadeIn());
}).attr('src',source);
});
}
else{
$('<img />').load(function(){
var $image = $(this);
imageResize($image);
$image.hide();
$('#slideshow_wrapper').empty().append($image.fadeIn());
}).attr('src',source);
}
}
else{ //this is actually not necessary since we have a circular slideshow
if(dir == 'r')
--current;
else if(dir == 'l')
++current;
alternateThumbs();
return;
}
}
function alternateThumbs(){
$('#slideshow_thumbs').find('.slideshow_thumb_wrapper:nth-child('+current_thumb+')')
.hide();
current_thumb = Math.ceil(current/nmb_images_wrapper);
if(current_thumb > nmb_thumb_wrappers){
current_thumb 	= 1;
current 		= 1;
}
else if(current_thumb == 0){
current_thumb 	= nmb_thumb_wrappers;
current 		= current_thumb*nmb_images_wrapper;
}
$('#slideshow_thumbs').find('.slideshow_thumb_wrapper:nth-child('+current_thumb+')')
.show();
}
$('#slideshow_thumb_next').bind('click',function(e){
next_thumb();
e.preventDefault();
});
$('#slideshow_thumb_prev').bind('click',function(e){
prev_thumb();
e.preventDefault();
});
function next_thumb(){
var $next_wrapper = $('#slideshow_thumbs').find('.slideshow_thumb_wrapper:nth-child('+parseInt(current_thumb+1)+')');
if($next_wrapper.length){
$('#slideshow_thumbs').find('.slideshow_thumb_wrapper:nth-child('+current_thumb+')')
.fadeOut(function(){
++current_thumb;
$next_wrapper.fadeIn();
});
}
}
function prev_thumb(){
var $prev_wrapper = $('#slideshow_thumbs').find('.slideshow_thumb_wrapper:nth-child('+parseInt(current_thumb-1)+')');
if($prev_wrapper.length){
$('#slideshow_thumbs').find('.slideshow_thumb_wrapper:nth-child('+current_thumb+')')
.fadeOut(function(){
--current_thumb;
$prev_wrapper.fadeIn();
});
}
}
$('#slideshow_thumbs .slideshow_thumb_wrapper > a').bind('click',function(e){
var $this 		= $(this);
$('#slideshow_thumb_close').trigger('click');
var idx			= $this.index();
var p_idx		= $this.parent().index();
current			= parseInt(p_idx*nmb_images_wrapper + idx + 1);
showImage();
e.preventDefault();
}).bind('mouseenter',function(){
var $this 		= $(this);
$this.stop().animate({'opacity':1});
}).bind('mouseleave',function(){
var $this 		= $(this);
$this.stop().animate({'opacity':0.5});
});
function imageResize($image){
var theImage 	= new Image();
theImage.src 	= $image.attr("src");
var imgwidth 	= theImage.width;
var imgheight 	= theImage.height;
var containerwidth  = 600;
var containerheight = 300;
if(imgwidth	> containerwidth){
var newwidth = containerwidth;
var ratio = imgwidth / containerwidth;
var newheight = imgheight / ratio;
if(newheight > containerheight){
var newnewheight = containerheight;
var newratio = newheight/containerheight;
var newnewwidth =newwidth/newratio;
theImage.width = newnewwidth;
theImage.height= newnewheight;
}
else{
theImage.width = newwidth;
theImage.height= newheight;
}
}
else if(imgheight > containerheight){
var newheight = containerheight;
var ratio = imgheight / containerheight;
var newwidth = imgwidth / ratio;
if(newwidth > containerwidth){
var newnewwidth = containerwidth;
var newratio = newwidth/containerwidth;
var newnewheight =newheight/newratio;
theImage.height = newnewheight;
theImage.width= newnewwidth;
}
else{
theImage.width = newwidth;
theImage.height= newheight;
}
}
$image.css({
'width'	:theImage.width,
'height':theImage.height
});
}

