//Slideshow variables
var items, CurrentImage, CurrentTitle, CurrentDescription, Next, Previous, CurrentLink;
var index, maxIndex, previousIndex;
var images, currentLink;
var currentSlide;

$(document).ready(init);
function init()
{
	//Slideshow setup
	items = $('.thumbnails div');
	index = 0;
	previousIndex = 0;
	maxIndex = items.length;
	
	//Set up link
	$('.thumbnails div .full').bind('click', followLink);
	
	//Set up hover effect
	$('.thumbnails div .thumb').hover(zoomIn, function(){});
	$('.thumbnails').append('<div id="imageHoverDiv"><img id="hoverImage" /></div>');
	$('.thumbnails').after('<div style="clear:left"></div>');
	$('#imageHoverDiv').hover(function(){}, zoomOut);
	$('#imageHoverDiv').bind('click', selectImage);
	
	//Set up large photo display
	$('.thumbnails').after('<div id="largeDisplay"><h1></h1><img /><p></p><div id="close">X</div></div>');
	$('#largeDisplay img').bind('click', followLink);
	$('#close').bind('click', function(){$('#largeDisplay').hide();});
	$('#largeDisplay').hide();
	
	//Setup user defined width and height
	$('#largeDisplay').css("width", maxWidth + 50);
	$('#largeDisplay').css("height", maxHeight + 100);
	$('.thumbnails div').css("width", thumbWidth-50);
	$('.thumbnails div').css("height", thumbHeight-50);
	$('#imageHoverDiv').css("width", thumbWidth);
	$('#imageHoverDiv').css("height", thumbHeight);
	$('.thumbnails div .thumb').css("max-width", thumbWidth-50);
	$('.thumbnails div .thumb').css("max-height", thumbHeight-50);
	$('.thumbnails').css("width", (thumbWidth-40)*4 + 50);
	//$('#Next, #Previous').css("top", (maxHeight + 72)/2-24);
	//$('#filmStrip, #filmStripBack').css("height", thumbHeight + 28);
	
	//Detect IE5.5-7 as IE SUCKS
	var version=0
	if (navigator.appVersion.indexOf("MSIE")!=-1){
	var temp=navigator.appVersion.split("MSIE")
	version=parseFloat(temp[1])
	}
	
	if (version>=5.5 && version<7.0) //NON IE browser will return 0
	{
		//alert("You're using a crappy browser")
	$('.thumbnails div .thumb').css("width", '90%');
	$('.thumbnails div .thumb').css("height", '90%');
	}
}

function zoomIn()
{
	$('#hoverImage').get(0).src = $(this).get(0).src;
	$('#imageHoverDiv').css('left', $(this).parent().x()-$(this).parent().parent().x()-25);
	$('#imageHoverDiv').css('top', $(this).parent().y()-$(this).parent().parent().y()-25);
	$('#imageHoverDiv').show();
	currentSlide = $(this);
}
function zoomOut()
{
	$('#imageHoverDiv').hide();
}
function selectImage()
{
	$('#largeDisplay img').get(0).src = currentSlide.parent().children('.full').get(0).src;
	$('#largeDisplay h1').html(currentSlide.parent().children('h1').html());
	$('#largeDisplay p').html(currentSlide.parent().children('p').html());
	currentLink = currentSlide.parent().children('a').get(0).href;
	$('#largeDisplay').show();
}
function followLink()
{
	window.location = currentLink;
}

$.fn.x = function(n) {
     var result = null;
     this.each(function() {
         var o = this;
         if (n === undefined) {
             var x = 0;
             if (o.offsetParent) {
                 while (o.offsetParent) {
                     x += o.offsetLeft;
                     o = o.offsetParent;
                 }
             }
             if (result === null) {
                 result = x;
             } else {
                 result = Math.min(result, x);
             }
         } else {
             o.style.left = n + 'px';
         }
     });
     return result;
};

$.fn.y = function(n) {
     var result = null;
     this.each(function() {
         var o = this;
         if (n === undefined) {
             var y = 0;
             if (o.offsetParent) {
                 while (o.offsetParent) {
                     y += o.offsetTop;
                     o = o.offsetParent;
                 }
             }
             if (result === null) {
                 result = y;
             } else {
                 result = Math.min(result, y);
             }
         } else {
             o.style.top = n + 'px';
         }
     });
     return result;
};


