var slideSpeed = 300;
var sliderWidth = 199;
var slides = 0;
var currentSlide = 1; 
var fadeSpeed = 450; 
var wotsCaption = '';
var wotsFile = '';
var wotsContent = '';
var wotsLink = '';
var fileWidth2;
var fileHeight2;

$(document).ready(function(){
	$('body').append('<div id="wotzFader"></div><!-- wotz fader --><div id="wotzContainer"><div id="wotzHolder"><div id="wotzButtons"><div id="wotzClose">Close X</div><!-- wotz close --><div id="wotzNext">Next <span class="wotzDir">&raquo;</span></div><!-- wotz next --><div id="wotzBack"><span class="wotzDir">&laquo;</span> Back</div><!-- wotz back --></div><!-- wotz buttons --><div id="wotzContent"></div><!-- wotz content --><div id="wotzFile"></div><!-- wotz file --><div id="wotzCaption"></div><!-- wotz caption --></div><!-- wotz holder --></div><!-- wotz container -->');
	if($.browser.msie && $.browser.version == 6) {
		$('#wotzFader, #wotzContainer').css({
			position: 'absolute'
		});
		$('body').css('overflow-y','auto');
		$('html').css('overflow-y','hidden');
		$('#nav li').each(function(){ $(this).css('position','static'); });
		$('.hider, .hider ul, .hider li').each(function(){ $(this).css('position','static'); });
	}

	$('#wotzFader, #wotzContainer').hide();
	
	$('#whatsImages li').each(function(i) {
		slides ++;		
	});
	
	// hide the previous
	$('#whatsPrev').css('visibility','hidden');
	
	
	// setting the width of the slides
	var theSliderWidth = slides*sliderWidth;

	$('#whatsItems ul').width(theSliderWidth);
	
	$('#whatsNext').click(function(){
		updateSlider('next');
		return false; 	
	});	
	
	$('#whatsPrev').click(function(){
		updateSlider('prev');
		return false; 	
	});	
	
	$('#whatsView').click(function(){
		showMedia();
		return false;	
	});


	$('#wotzNext').click(function(){
		if(currentSlide < slides) {
			updateSlider('next');
			showMedia();															 
		}
		return false;
	});
	
	//the back button
	$('#wotzBack').click(function(){
		if(currentSlide > 0) {
			updateSlider('prev');
			showMedia();															 
		}
		return false;
	});
	
	//hide the large view when you click outside or on the close box
	$('#wotzContainer, #wotzClose').click(function(){ 
		$('#wotzFader').fadeOut(fadeSpeed); 
		$('#wotzContainer').fadeOut(fadeSpeed);
		if($.browser.msie && $.browser.version == 6) {
			$('select').each(function(){
				$(this).show();
			});
		}

		return false;
	});

	//overriding the above statement
	$('#wotzHolder').click(function(){
		return false;																		
	});


});

function slideContent() {
	currentSlide > 1 ? $('#whatsPrev').css('visibility','visible') : $('#whatsPrev').css('visibility','hidden');
	var margin = ((currentSlide-1)*sliderWidth);
	margin == '0' ? margin = 0 : margin = '-'+margin;
	$('#whatsItems ul').animate({'left': margin },slideSpeed);
}

function updateSlider(dir) {
	switch(dir) {
		case 'prev':
			if(currentSlide > 1) {
				currentSlide --;
				currentSlide == slides ? prev = slides-1 : prev = currentSlide + 1;
			} else {
				currentSlide = slides;
				prev = slides-1;
			}
		break;
		case 'next':
			if(currentSlide < slides) {
				currentSlide ++;
				currentSlide == 1 ? prev = slides : prev = currentSlide - 1;
			} else {
				currentSlide = 1;
				prev = slides;
			}		
		break;
	} 
	wotsLink = $('.wotsCap'+currentSlide+' .hide').text();
	$('#whatsMore').attr('href',wotsLink);
	slideContent();
	
}

function showMedia() {
	$('#wotzFader').fadeIn(fadeSpeed);
	$('#wotzContainer').fadeIn(fadeSpeed);
	
	//hide the next and back buttons, depending on what file number you are on
	if(currentSlide <= 1) {
		$('#wotzBack').css('visibility','hidden'); 
	} else {
		$('#wotzBack').css('visibility','visible'); 
	}
	if(currentSlide >= slides) {
		$('#wotzNext').css('visibility','hidden');
	} else {
		$('#wotzNext').css('visibility','visible'); 
	}

	// getting the content
	wotsCaption = $('.wotsCap'+currentSlide).html();
	wotsFile = $('.wots'+currentSlide+' img.hide').attr('src');
	
	loadContent();
}


function loadContent(){
	// first fade them down
	$('#wotzContent').fadeOut(fadeSpeed);
	$('#wotzCaption').fadeOut(fadeSpeed);
	$('#wotzFile').fadeOut(fadeSpeed,function(){
		// second we empty them
		$('#wotzFile, #wotzCaption, #wotsContent').empty();
		// thrid we apply new content to them
		//console.log('fileWidth2: '+fileWidth2+"\nfileHeight2: "+fileHeight2);
		resizeImage();
		//console.log('fileWidth2: '+fileWidth2+"\nfileHeight2: "+fileHeight2);
		resizeBox();
	});
		
}


function resizeImage(){
	
	//getting window dimensions for image re-sizing.  Im removing 200px so there is a nice gap around the container box when image is resized.
	windowWidth2 = ($(window).width())-200;
	windowHeight2 = ($(window).height())-200;
	
	//getting the image dimensions
	var img = new Image();
	img.src = wotsFile;
	if($.browser.msie) {
		imgWidth2 = $('.wots'+currentSlide+' img.hide').attr('wi');
		imgHeight2 = $('.wots'+currentSlide+' img.hide').attr('he');
	} else {
		imgWidth2 = $('.wots'+currentSlide+' img.hide').attr('width');
		imgHeight2 = $('.wots'+currentSlide+' img.hide').attr('height');
	}
	//console.log(imgWidth2+' x '+imgHeight2+"\n"+windowWidth2+' x '+windowHeight2);
	
	if (imgWidth2 > windowWidth2) {
		imgHeight2 = imgHeight2 * (windowWidth2 / imgWidth2); 
		imgWidth2 = windowWidth2; 
		if (imgHeight2 > windowHeight2)	{ 
			imgWidth2 = imgWidth2 * (windowHeight2 / imgHeight2); 
			imgHeight2 = windowHeight2; 
		}
	} else if (imgHeight2 > windowHeight2) { 
		imgWidth2 = imgWidth2 * (windowHeight2 / imgHeight2); 
		imgHeight2 = windowHeight2; 
		if (imgWidth2 > windowWidth2){ 
			imgHeight2 = imgHeight2 * (windowWidth2 / imgWidth2); 
			imgWidth2 = windowWidth2;
		}
	}
	imgWidth2 = Math.round(imgWidth2);
	imgHeight2= Math.round(imgHeight2);
	if(imgWidth2 < 200) { fileWidth2 = 200; } else { fileWidth2 = imgWidth2; }
	if(imgHeight2 < 130) { fileHeight2 = 130; } else { fileHeight2 = imgHeight2; }
}


function resizeBox() {
	
	if(wotsContent == '') {
		var holderWidth2 = parseInt(fileWidth2); 
		$('#wotzContent').hide();
	} else {
		var holderWidth2 =	40+parseInt(fileWidth2)+210;
		$('#wotzContent').show();
	}
	var holderHeight2 = parseInt(fileHeight2)+60;
	var	offsetTop2 = -(holderHeight2 / 2)-10;
	var	offsetLeft2 = -(holderWidth2 / 2)-20;
	
	//making any content links work
	$('#wotzContent a').each(function(){
		$(this).click(function(){
			window.open($(this).attr('href'));
		});
	});
	
	
	$('#wotzFile').css({width: fileWidth2, height: fileHeight2 });
	//animate the box
	$('#wotzHolder').animate({
		width:holderWidth2+'px',
		height:holderHeight2+'px',
		marginTop:offsetTop2+'px',
		marginLeft: offsetLeft2+'px'
		}, slideSpeed, function() {
		$('#wotzFile').append('<img src="'+wotsFile+'" width="'+fileWidth2+'" height="'+fileHeight2+'"/>').fadeIn(fadeSpeed);
	});
		
	$('#wotzCaption').animate({
		width:fileWidth2+'px'
	}, slideSpeed, function(){
		$('#wotzCaption').append(wotsCaption).fadeIn(fadeSpeed);
	});

}
