/* / / / / / / / /  Numbers and statements / / / / / / / / */

//set statements

// the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
if (typeof window.innerWidth != 'undefined')
{
	winWidth = window.innerWidth;
	winHeight = window.innerHeight;
}
	 
// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth !== 0)
{
	winWidth = document.documentElement.clientWidth;
	winHeight = document.documentElement.clientHeight;
}
	 
// older versions of IE
else
{
	winWidth = document.getElementsByTagName('body')[0].clientWidth;
	winHeight = document.getElementsByTagName('body')[0].clientHeight;
}
/*
winWidth = $(window).width();
winHeight = $(window).height();
*/
maxWidth = ( winWidth - 64 );
maxHeight = ( winHeight - ( 86 + 64) );
	
minWidth = 620;
minHeight = 200;
	
orgThumbEdge = 140;	


/* / / / / / / / /  Functions / / / / / / / / */

function imgPos() {
	var activePos = $('#slider img.active').position();
	var imageAreaWidth = $('#image').width();
	if ($.browser.msie && $.browser.version.substr(0,1)<8) {
		var sliderMargin = ( ( (imageAreaWidth - $('#slider img.active').attr('width') ) / 2 ) - activePos.left - (winWidth/2) + 32 );
	} else {
		var sliderMargin = ( ( (imageAreaWidth - $('#slider img.active').attr('width') ) / 2 ) - activePos.left );
	}
	$('#gallery #image #slider').css({marginLeft:sliderMargin});
}

function caption() {
	var activeAlt = $('#slider img.active').attr('alt');
	$('#title').text(activeAlt);
}

function imgPosAni() {
	var activePos = $('#slider img.active').position();
	var imageAreaWidth = $('#image').width();
	if ($.browser.msie && $.browser.version.substr(0,1)<8) {
		var sliderMargin = ( ( (imageAreaWidth - $('#slider img.active').attr('width') ) / 2 ) - activePos.left - (winWidth/2) + 32 );
	} else {
		var sliderMargin = ( ( (imageAreaWidth - $('#slider img.active').attr('width') ) / 2 ) - activePos.left );
	}
	$('#gallery #image #slider').css({marginLeft:sliderMargin});
}

// Gallery image resize function
function resize(){
	if ( $('#gallery').length != 0 ) {
		
		//Make sure elements can't be resized to small
		
		if ( (winWidth - 64 ) < (minWidth+250) ) {
			$('#title').css({'width':'auto'});
		} else {
			$('#title').css({'width': maxWidth});			
		}
		
		if ( ( winWidth - 64 ) < minWidth ) {		
			$('#image').css({'width': (minWidth-10)});
			$('body.gallery').css({'width': (minWidth+64), 'overflow-x':'auto'});
			$('#nav').css({'right':'','left':'340px','margin':'0','padding-right':'32px'});
			$('#navigation').css({'right':'','left':'340px','padding-right':'32px'});
		} else {		
			$('#image').css({'width': maxWidth});
			$('body.gallery').css({'width':'', 'overflow-x':''});
			$('#nav').css({'right':'','left':'','margin':'','padding-right':''});
			$('#navigation').css({'right':'','left':'','padding-right':''});
		}
		
		if ( ( winHeight - ( 86 + 64) ) < minHeight ) {		
			$('#image').css({'height': (minHeight-1)});		
			$('#gallery').css({'height': (minHeight+32)});
			$('body.gallery').css({'height':'auto', 'overflow-y':'auto'});
			$('#navigation').css({'bottom':'','top':(minHeight+86+15),'padding-bottom':'29px'});
		} else {		
			$('#image').css({'height': (maxHeight-1)});
			$('#gallery').css({'height':''});
			$('body.gallery').css({'height':'', 'overflow-y':''});
			$('#navigation').css({'bottom':'','top':'','padding-bottom':''});
		}
		
		$('#slider img').each(function(index) {
			// set holder width & height for centering		
			var width = $(this).attr('width');    // Current image width
			var height = $(this).attr('height');  // Current image height
			// Check if the current width is larger than the max
			if ( width < minWidth ) {
				newSize = minWidth / width;
				$(this).attr("width", minWidth); // Set new width
				$(this).attr("height", Math.floor(height * newSize));  // Scale height based on ratio
				height = height * newSize;    // Reset height to match scaled image
				width = width * newSize;    // Reset width to match scaled image
			} else if ( width > maxWidth ) {
				newSize = maxWidth / width;
				$(this).attr("width", maxWidth);
				$(this).attr("height", Math.floor(height * newSize));
				height = height * newSize;
				width = width * newSize;
			} else {
				newSize = maxWidth / width;
				$(this).attr("width", Math.floor(width * newSize));
				$(this).attr("height", Math.floor(height * newSize));
				height = height * newSize;
				width = width * newSize;
			}			
			// Check if current height is larger than max
			if( height > (maxHeight-1) ){	
				newSize = (maxHeight-1) / height;
				$(this).attr("height", (maxHeight-1));
				$(this).attr("width", Math.floor(width * newSize));
				height = height * newSize;
				width = width * newSize;
				$(this).css({marginTop:0});				
				if (height < minHeight) {
					newSize = minHeight / height;
					$(this).attr("height", minHeight);
					$(this).attr("width", Math.floor(width * newSize));
					height = height * newSize;
					width = width * newSize;
				}
			} else {
				imgTopMargin = Math.floor( (maxHeight-height) / 2 );
				$(this).css({marginTop:imgTopMargin});		
			}
		});	
		imgPos();	
	}
}

function thumbScaling() {	
	if ( $('#new-thumbnails').length != 0 ) {
		
		if ( ( winWidth - 64 ) < minWidth ) {
			var thumbContainerWidth = minWidth;	
		} else {
			var thumbContainerWidth = maxWidth;
		}		
		if ( ( winHeight - ( 86 + 64) ) < minHeight ) {	
			var thumbContainerHeight = minHeight;
		} else {		
			var thumbContainerHeight = maxHeight;	
		}		
		$('#new-thumbnails').css({'width': (thumbContainerWidth+20),'height': thumbContainerHeight});		
		containerArea = ( (thumbContainerWidth+20) * thumbContainerHeight );
		
		thumbs = $("#new-thumbnails .new-thumbnail img");
		thumbArea = (containerArea / thumbs.length);	
		sqrtThumbEdge = Math.floor(Math.sqrt(thumbArea));	
		thumbsTotalArea = ((sqrtThumbEdge+20)*(sqrtThumbEdge+20)) * thumbs.length;
		thumbRatio = ( containerArea / thumbsTotalArea );		
		thumbEdge = Math.floor( sqrtThumbEdge * thumbRatio);		
		thumbs.attr({'width': thumbEdge,'height': thumbEdge});	
		//find which thumb is first on the 2nd line - it's index = number in a row
		thumbs.each(function(index) {
			perRow = "";
			var pos = $(this).position();
			var posTop = pos.top;		
			if ( posTop != 0 ) {
				perRow = index;
				return false;	
			}
		});			
		rowWidth = ( $("#new-thumbnails .new-thumbnail:first img").attr('width') + 20) * perRow;	
		lastThumbPos = $("#new-thumbnails .new-thumbnail:last").position();	
		if ( ( (thumbContainerWidth+20) - rowWidth ) < ( thumbContainerHeight - (lastThumbPos.top+thumbEdge) ) ) {
			newThumbEdge = Math.floor( ( (thumbContainerWidth+20) / perRow )  - 20 );	
			thumbs.attr({'width': newThumbEdge,'height': newThumbEdge});		
		} else {
			newThumbEdge = Math.floor( ( (thumbContainerWidth+20) / (perRow+1) )  - 20 );	
			thumbs.attr({'width': newThumbEdge,'height': newThumbEdge});		
		}
	}
}

function testNavigation(){
	if ( $('#gallery').length != 0 ) {		
		if ( $('#slider img.active:last').next('img').length == 0 ) {
			$('#navigation .next a').text('First image');
		} else {
			$('#navigation .next a').text('Next image');			
		}
		if ( $('#slider img.active:first').prev('img').length == 0 ) {
			$('#navigation .previous a').text('Last image');
			if ( $('#slider img:last').hasClass('loaded') ) {
				$('#navigation .previous a').css({'display':'inline'});
			} else {
				$('#navigation .previous a').css({'display':'none'});				
			}		
		} else {
			$('#navigation .previous a').css({'display':'inline'}).text('Previous image');			
		}
	}
}

$('#navigation .next a').live('click', function() {
	if ( !$('#image').hasClass('loading') ) {
		if ( $('#slider img.active').next('img').length == 0 ) {
			$('#slider img.active').removeClass('active').css({opacity:0.15});
			$('#slider img:first').addClass('active').css({opacity:1});	
			imgPos();
			caption();
		} else {
			if ( $('#slider img.active').next('img').hasClass('loaded') ) {
				$('#slider img.active').removeClass('active').css({opacity:0.15}).next('img').addClass('active').css({opacity:1});
				imgPosAni();
				caption();
			} else if ( $('#slider img.active').next('img').hasClass('loading') ) {
				$('#slider img.active').removeClass('active').css({opacity:0.15}).next('img').addClass('active').addClass('loading-single');
				$('#image').addClass('loading');
				$('#slider').css({opacity:0});
				$('#slider img.loading-single').load(function() {
					$('#slider img.loading-single').removeClass('loading-single').addClass('loaded').css({'display':'block'}).animate({opacity:1}, 200);
					imgPos();
					caption();
					$('#image').removeClass('loading');
					$('#slider').css({opacity:1});
				});
			} else {
				$('#slider img.active').removeClass('active').css({opacity:0.15}).next('img').addClass('active').addClass('loading-single').css({opacity:1});
				$('#image').addClass('loading');
				$('#slider').css({opacity:0});
				var url = $('#slider img.loading-single').attr('original');
				$('#slider img.loading-single').attr({'original':'','src':url});
				$('#slider img.loading-single').load(function() {
					$('#slider img.loading-single').addClass('loaded').css({'display':'block'}).animate({opacity:1}, 200);
					$('#slider img').removeClass('loading-single');
					imgPos();
					caption();
					$('#image').removeClass('loading');
					$('#slider').css({opacity:1});
				});
			}
		}
		testNavigation();
	}
	$('.dropdown ul li ul').css({'display':''});
});

$('#navigation .previous a').live('click', function() {	
	if ( !$('#image').hasClass('loading') ) {	
		if ( $('#slider img.active').prev('img').length == 0 ) {
			$('#slider img.active').removeClass('active').css({opacity:0.15});
			$('#slider img:last').addClass('active').css({opacity:1});	
			imgPos();
			caption();	
		} else {
			if ( $('#slider img.active').prev('img').hasClass('loaded') ) {
				$('#slider img.active').removeClass('active').css({opacity:0.15}).prev('img').addClass('active').css({opacity:1});
				imgPosAni();
				caption();
			} else if ( $('#slider img.active').prev('img').hasClass('loading') ) {
				$('#slider img.active').removeClass('active').css({opacity:0.15}).prev('img').addClass('active').addClass('loading-single');
				$('#image').addClass('loading');
				$('#slider').css({opacity:0});
				$('#slider img.loading-single').load(function() {
					$('#slider img.loading-single').removeClass('loading-single').addClass('loaded').css({'display':'block'}).animate({opacity:1}, 200);
					imgPos();
					caption();
					$('#image').removeClass('loading');
					$('#slider').css({opacity:1});
				});
			} else {
				$('#slider img.active').removeClass('active').css({opacity:0.15}).prev('img').addClass('active').addClass('loading-single').css({opacity:1});
				$('#image').addClass('loading');
				$('#slider').css({opacity:0});
				var url = $('#slider img.loading-single').attr('original');
				$('#slider img.loading-single').attr({'original':'','src':url});
				$('#slider img.loading-single').load(function() {
					$('#slider img.loading-single').addClass('loaded').css({'display':'block'}).animate({opacity:1}, 200);
					$('#slider img').removeClass('loading-single');
					imgPos();
					caption();
					$('#image').removeClass('loading');
					$('#slider').css({opacity:1});
				});
			}
		}
		testNavigation();
	}
	$('.dropdown ul li ul').css({'display':''});
});

$('#slider img.loaded').live('click', function(e) {
	if ( !$(this).hasClass('active') ) {
		$('#slider img.active').removeClass('active').css({opacity:0.15});
		$(this).addClass('active').css({opacity:1});
		imgPosAni();
		caption();
		testNavigation();
	} else {	
		if ( $('#slider img.active').next('img').length != 0 && $('#slider img.active').prev('img').length != 0 ) {			
			var x = e.pageX;
			if ( x > ((maxWidth/2)+32) ) {
				if ( $('#slider img.active').next('img').hasClass('loaded') ) {
					$('#slider img.active').removeClass('active').css({opacity:0.15}).next('img').addClass('active').css({opacity:1});
					imgPosAni();
					caption();
				} else if ( $('#slider img.active').next('img').hasClass('loading') ) {
					$('#slider img.active').removeClass('active').css({opacity:0.15}).next('img').addClass('active').addClass('loading-single');
					$('#image').addClass('loading');
					$('#slider').css({opacity:0});
					$('#slider img.loading-single').load(function() {
						$('#slider img.loading-single').removeClass('loading-single').addClass('loaded').css({'display':'block'}).animate({opacity:1}, 200);
						$('#image').removeClass('loading');
						imgPos();
						caption();
						$('#slider').css({opacity:1});
					});
				} else {
					$('#slider img.active').removeClass('active').css({opacity:0.15}).next('img').addClass('active').addClass('loading-single');
					$('#image').addClass('loading');
					$('#slider').css({opacity:0});
					var url = $('#slider img.loading-single').attr('original');
					$('#slider img.loading-single').attr({'original':'','src':url});
					$('#slider img.loading-single').load(function() {
						$('#slider img.loading-single').addClass('loaded').css({'display':'block'}).animate({opacity:1}, 200);
						$('#slider img').removeClass('loading-single');
						imgPos();
						caption();
						$('#image').removeClass('loading');
						$('#slider').css({opacity:1});
					});
				}			
			} else {			
				if ( $('#slider img.active').prev('img').hasClass('loaded') ) {
					$('#slider img.active').removeClass('active').css({opacity:0.15}).prev('img').addClass('active').css({opacity:1});
					imgPosAni();
					caption();					
				} else if ( $('#slider img.active').prev('img').hasClass('loading') ) {
					$('#slider img.active').removeClass('active').css({opacity:0.15}).prev('img').addClass('active').addClass('loading-single');
					$('#image').addClass('loading');
					$('#slider').css({opacity:0});
					$('#slider img.loading-single').load(function() {
						$('#slider img.loading-single').removeClass('loading-single').addClass('loaded').css({'display':'block'}).animate({opacity:1}, 200);
						imgPos();
						caption();
						$('#image').removeClass('loading');
						$('#slider').css({opacity:1});
					});
				} else {
					$('#slider img.active').removeClass('active').css({opacity:0.15}).prev('img').addClass('active').addClass('loading-single');
					$('#image').addClass('loading');
					$('#slider').css({opacity:0});
					var url = $('#slider img.loading-single').attr('original');
					$('#slider img.loading-single').attr({'original':'','src':url});
					$('#slider img.loading-single').load(function() {
						$('#slider img.loading-single').addClass('loaded').css({'display':'block'}).animate({opacity:1}, 200);
						$('#slider img').removeClass('loading-single');
						imgPos();
						caption();
						$('#image').removeClass('loading');
						$('#slider').css({opacity:1});
					});
				}
			}			
			testNavigation();			
		} else if ($('#slider img.active').next('img').length == 0) {
			var x = e.pageX;			
			if ( x > ((maxWidth/2)+32) ) {
				$('#slider img.active').removeClass('active').css({opacity:0.15});
				$('#slider img:first').addClass('active').css({opacity:1});	
				imgPos();
				caption();
			} else {
				if ( $('#slider img.active').prev('img').hasClass('loaded') ) {
					$('#slider img.active').removeClass('active').css({opacity:0.15}).prev('img').addClass('active').css({opacity:1});
					imgPosAni();
					caption();
				} else if ( $('#slider img.active').prev('img').hasClass('loading') ) {
					$('#slider img.active').removeClass('active').css({opacity:0.15}).prev('img').addClass('active').addClass('loading-single');
					$('#image').addClass('loading');
					$('#slider').css({opacity:0});
					$('#slider img.loading-single').load(function() {
						$('#slider img.loading-single').removeClass('loading-single').addClass('loaded').css({'display':'block'}).animate({opacity:1}, 200);
						imgPos();
						caption();
						$('#image').removeClass('loading');
						$('#slider').css({opacity:1});
					});
				} else {
					$('#slider img.active').removeClass('active').css({opacity:0.15}).prev('img').addClass('active').addClass('loading-single');
					$('#image').addClass('loading');
					$('#slider').css({opacity:0});
					var url = $('#slider img.loading-single').attr('original');
					$('#slider img.loading-single').attr({'original':'','src':url});
					$('#slider img.loading-single').load(function() {
						$('#slider img.loading-single').addClass('loaded').css({'display':'block'}).animate({opacity:1}, 200);
						$('#slider img').removeClass('loading-single');
						imgPos();
						caption();
						$('#image').removeClass('loading');
						$('#slider').css({opacity:1});
					});
				}
			}
			testNavigation();
		} else if ($('#slider img.active').prev('img').length == 0) {
			var x = e.pageX;			
			if ( x > ((maxWidth/2)+32) ) {
				if ( $('#slider img.active').next('img').hasClass('loaded') ) {
					$('#slider img.active').removeClass('active').css({opacity:0.15}).next('img').addClass('active').css({opacity:1});
					imgPosAni();
					caption();
				} else if ( $('#slider img.active').next('img').hasClass('loading') ) {
					$('#slider img.active').removeClass('active').css({opacity:0.15}).next('img').addClass('active').addClass('loading-single');
					$('#image').addClass('loading');
					$('#slider').css({opacity:0});
					$('#slider img.loading-single').load(function() {
						$('#slider img.loading-single').removeClass('loading-single').addClass('loaded').css({'display':'block'}).animate({opacity:1}, 200);
						imgPos();
						caption();
						$('#image').removeClass('loading');
						$('#slider').css({opacity:1});
					});
				} else {
					$('#slider img.active').removeClass('active').css({opacity:0.15}).next('img').addClass('active').addClass('loading-single');
					$('#image').addClass('loading');
					$('#slider').css({opacity:0});
					var url = $('#slider img.loading-single').attr('original');
					$('#slider img.loading-single').attr({'original':'','src':url});
					$('#slider img.loading-single').load(function() {
						$('#slider img.loading-single').addClass('loaded').css({'display':'block'}).animate({opacity:1}, 200);
						$('#slider img').removeClass('loading-single');
						imgPos();
						caption();
						$('#image').removeClass('loading');
						$('#slider').css({opacity:1});
					});
				}
			} else {
				if ( $('#slider img:last').hasClass('loaded') ) {
					$('#slider img.active').removeClass('active').css({opacity:0.15});
					$('#slider img:last').addClass('active').css({opacity:1});
					imgPos();
					caption();
				} else if ( $('#slider img:last').hasClass('loading') ) {
					$('#slider img.active').removeClass('active').css({opacity:0.15});
					$('#slider img:last').addClass('active').addClass('loading-single');
					$('#image').addClass('loading');
					$('#slider').css({opacity:0});
					$('#slider img.loading-single').load(function() {
						$('#slider img.loading-single').removeClass('loading-single').addClass('loaded').css({'display':'block'}).animate({opacity:1}, 200);
						imgPos();
						caption();
						$('#image').removeClass('loading');
						$('#slider').css({opacity:1});
					});
				}
			}	
			testNavigation();	
		}	
		
	}	
	$('.dropdown ul li ul').css({'display':''});
});


$('#slider').live('mouseleave', function(event) {
	if (event.type == 'mouseleave') {					   
		$('#nextArrow').hide();						   
		$('#prevArrow').hide();		
	}
    return false;	
});

$('#slider img.loaded').live('mouseover mouseout', function(event) {
	if (event.type == 'mouseover') {
		$(this).mousemove(function(e){
			var x = e.clientX;
			var activeID = $('#slider img.active').attr('id');
			if ( x > ((maxWidth/2)+32) ) {
				$(this).css({'cursor':''});		
				if ( !$('#nextArrow').is(":visible") ) {		
					$('#nextArrow').show();
				}
				if ( $('#prevArrow').is(":visible") ) {
					$('#prevArrow').hide();
				}			
				$('#nextArrow').css('left', e.clientX - 24).css('top', e.clientY + 3);			
			} else {
				if ( $(this).attr('id') == "1" && !$('#slider img:last').hasClass('loaded') ) {
					$('#prevArrow').hide();	
					$('#nextArrow').hide();
					$(this).css({'cursor':'default'});
				} else {
					$(this).css({'cursor':''});						
					if ( !$('#prevArrow').is(":visible") ) {		
					$('#prevArrow').show();
					}
					if ( $('#nextArrow').is(":visible") ) {
						$('#nextArrow').hide();
					}			
					$('#prevArrow').css('left', e.clientX - 24).css('top', e.clientY + 3);
				}					
			}
		});
	} else {
	//
	}
});

$('#navigation .thumbnail-icon a').live('click', function() {
	$('.dropdown ul li ul').css({'display':''});													  
	if ( $('#new-thumbnails').is(":visible") ) {		
		$('#new-thumbnails').animate({opacity:0}, 200, function(){
			$('#new-thumbnails').hide();
		});			
		$('#image').animate({opacity:1},200);
		$('#navigation').removeClass('close');
		$('#title').css({opacity:1});
		$(this).text('Thumbnails');
	} else {
		$('#navigation').addClass('close');		
		$('#new-thumbnails').show().css({opcaity:0}).animate({opacity:1}, 200);
		$('#image').animate({opacity:0.3},200);
		$('#title').css({opacity:0.3});
		$(this).text('Click to hide thumbnail overview');
	}	
	thumbScaling();
});

$('#new-thumbnails img').live('click', function() {	
	$('#new-thumbnails').hide();
	$('#navigation').removeClass('close');
	$('#navigation .thumbnail-icon a').text('Thumbnails');
	$('#slider img.active').removeClass('active').css({opacity:0.15});	
	$('#image').css({opacity:1});
	var id = $(this).attr('id');	
	$('#slider img[id="'+id+'"]').addClass('active');	
	// Check if image is loaded
	if ( $('#slider img.active').hasClass('loaded') ) {
		$('#slider img.active').css({'display':'block',opacity:1});
		imgPos();
		caption();
	} else {
		$('#image').addClass('loading');
		$('#slider').css({opacity:0});
		var url = $('#slider img[id="'+id+'"]').addClass('loading-thumb').attr('original');
		$('#slider img.loading-thumb').attr({'original':'','src':url});
		$('#slider img.loading-thumb').load(function() {
			$('#slider img.loading-thumb').addClass('loaded').css({'display':'block'}).animate({opacity:1}, 200);
			$('#slider img').removeClass('loading-thumb');
			imgPos();
			caption();
			$('#image').removeClass('loading');
			$('#slider').css({opacity:1});
		});
	}
	testNavigation();		
});

$('#intro').live('click', function() {
	$('#intro').remove();
	$('#wrapper').css({opacity:1});
	$.cookie("intro", "done");
});

function loadNextImg() {	
	
	// check if there are image to load between last loaded and active
	if ( $('#slider img.active').prevAll('img').not('.loaded, .loading').length != 0 ) {
		
		$('#slider img.active').prevAll('img:not(.loaded)').addClass('loading');
		$('#slider img.active').prevAll('img.loading:last').addClass('last');
	
		$('#slider img.loading').each(function(){
			var url = $(this).attr('original');
			$(this).attr({'src':url});			
			if ($(this).attr('src') == '') {
				var url = $(this).attr('original');
				$(this).attr({'src':url});
				//make sure slider stays in place even if images before current is loaded
				imgPos();
			}			
		});
		
		$('#slider img.loading').load(function() {
			$(this).addClass('loaded').removeClass('loading').css({'display':'block'});
			$(this).css({opacity:0.15});
			//make sure slider stays in place even if images before current is loaded
			imgPos();
			if ( $(this).hasClass('last') ) {
				loadNextImg();
			}
		});
		
	// if not keep loading after last loaded
	} else if ( $('#slider img.loaded:last').next('img').length != 0 ) {
		
		$('#slider img.loaded:last').next('img').addClass('loading');
		$('#slider img.loaded:last').next('img').next('img').addClass('loading');
		$('#slider img.loaded:last').next('img').next('img').next('img').addClass('loading');
		$('#slider img.loaded:last').next('img').next('img').next('img').next('img').addClass('loading');
		$('#slider img.loaded:last').next('img').next('img').next('img').next('img').next('img').addClass('loading').addClass('last');
		$('#slider img.loading').each(function(){
			var url = $(this).attr('original');
			$(this).attr({'src':url});			
			if ($(this).attr('src') == '') {
				var url = $(this).attr('original');
				$(this).attr({'src':url});
			}			
		});
		
		$('#slider img.loading').load(function() {
			$(this).addClass('loaded').removeClass('loading').css({'display':'block'});
			$(this).css({opacity:0.15});
			//make sure slider stays in place even if images before current is loaded
			imgPos();
			if ( $(this).hasClass('last') ) {
				loadNextImg();
			}
		});
		
	}
	
}

													
$(document).ready(function() {
	
	if ( $('#gallery').length != 0 ) {
	
		$('#image').addClass('loading');
		
		//set statements
		winWidth = $(window).width();
		winHeight = $(window).height();

		maxWidth = ( winWidth - 64 );
		maxHeight = ( winHeight - ( 86 + 64) );
		
		$('#slider img').each(function(index) {
			$(this).attr('id', index+1).css({'display':'none',opacity:0});
		});
	
		$('#image').css({'width': maxWidth,'height': maxHeight});
		
		$('#navigation .previous a').css({'display':'none'});
		
		$('#new-thumbnails img').each(function(index) {
			$(this).attr('id', index+1);
		});
		
		if ( $.cookie("useKeys") != "off" ) {
			$('body').append('<div id="useKeys">Use your arrow keys to navigate</div>');
			$('#useKeys').css({opacity:0});
		}
		
	}

	$('#bio p').not('#bio p:first').wrapAll('<div class="full" />');
	
	$('#bio p:first').append('<span class="more">Read more</span>');

	$('#bio p:last').append('<span class="less">Read less</span>');
	
	$('#bio .more').click(function(){
		$(this).parents('#bio').addClass('open').removeClass('closed');
		$('#bio .full').slideDown();
	});
		
	$('#bio .less').click(function(){
		$(this).parents('#bio').addClass('closed').removeClass('open');
		$('#bio .full').slideUp();
	});	
	
	$('#bio').addClass('closed');
	
	if ( $.browser.mozilla ) {
		$('ul.links li').css({'float':'none','margin':'0'});
		$('ul.links').css({'margin':'0 10px','width':940,'-moz-column-count':6,'-webkit-column-count':6,'-moz-column-width':'140px','-webkit-column-width':'140px','-moz-column-gap':'20px','-webkit-column-gap':'20px'});
	}
		
	// Regular footer action
	$('#dock .arrow').click(function(){
		if ( $(this).hasClass('down') ) {
			$(this).addClass('up').removeClass('down');
			$(this).parents('#dock').animate({height:305}, 400);
			$(this).parents('#dock').find('.full').animate({height:225}, 400, function(){
				$(this).parents('#dock').find('.left,.right').css({'display':'block',opacity:0}).animate({opacity:1},400);
			});			
			$('html, body').animate({scrollTop: $('#innerWrapper').outerHeight(true)}, 800);
		} else {
			$(this).parents('#dock').find('.left,.right').animate({opacity:0},400, function(){
				$(this).parents('#dock').find('.left,.right').css({'display':'none'});																				
			});
			$(this).addClass('down').removeClass('up');
			$(this).parents('#dock').animate({height:128}, 400);
			$(this).parents('#dock').find('.full').animate({height:48}, 400);
			
		}
										
	});
		
	$('a.active_category').parent('li').addClass('current-cat');
	
	var totalHeight = ($('#header').outerHeight(true)+$('#container').outerHeight(true)+$('#footer').outerHeight());
	if ( totalHeight < winHeight ) {
		var extraMargin = ( winHeight - totalHeight );
		$('#footer').css({marginTop:extraMargin});
	}
	
	$('.cat').bind("mouseenter",function(){
		$(this).addClass('hover');
		$(this).parents('#cats').find('.cat').stop(true, true).not('.hover').css({opacity:0.5});
	}).bind("mouseleave",function(){
		$(this).parents('#cats').find('.cat').removeClass('hover').animate({opacity:1}, 1000);
	});
	
	$('#cats').css({'opacity':0});
		
	// set key use
	$(document).keydown(function(e){
		if (e.keyCode == 37) {
			$('#navigation .previous a').click();
		}
	});
	
	$(document).keydown(function(e){
		if (e.keyCode == 39) {
			$('#navigation .next a').click();
		}
	});
	
	$(document).keydown(function(e){
		if (e.keyCode == 27) {
			if ( $('#new-thumbnails').is(":visible") ) {
				$('#new-thumbnails').hide();
			}
		}
	});
	
	if ( $.cookie("intro") == "done" ) {
		$('#intro').remove();
	}
	
	if ( $('.post').length != 0 ) {
		
		$('.post').each(function() {
				
			var id = $(this).attr("id");
			
			if ( $('#'+id+'.post').find('p:first img').length == 0 ) {
				
				var move = $('#'+id+'.post p:first').clone();
				
				$('#'+id+'.post p:first').remove();
				
				$('#'+id+'.post img:first').parent('p').after(move);
				
			}
		
		});
		
	}
	

});

$(window).load(function() {	
						
	if ( $('#intro').length != 0 ) {
		$('#wrapper').css({opacity:0}).delay(5000).animate({opacity:1},3000);
		$('#intro').css({'background-image':'url(http://dy.lundgrenlindqvist.se/wp-content/themes/yellen/img/intro.gif)'}).delay(5000).animate({opacity:0}, 3000, function() { $(this).remove();$.cookie("intro", "done"); });		
	}
	
	if ( $('#cats').length != 0 ) {
		$('.thumb').css({'height':300});
		$('.thumb a').css({'height':300});
		if ( $('#innerWrapper').outerHeight(true)+40 > winHeight ) {
			var dif = $('#innerWrapper').outerHeight(true)+40 - winHeight;
			if ( dif < 320 ) {			
				$('.thumb').css({'height':300-(dif/2)});
				$('.thumb a').css({'height':300-(dif/2)});
			} else {
				$('.thumb').css({'height':140});
				$('.thumb a').css({'height':140});
			}
		}
		$('#cats').css({'opacity':1});
	}
	
	if ( $('#gallery').length != 0 ) {
		
		//set statements
		winWidth = $(window).width();
		winHeight = $(window).height();
		
		maxWidth = ( winWidth - 64 );
		maxHeight = ( winHeight - ( 86 + 64) );
			
		$('#image').css({'width': maxWidth,'height': maxHeight});
	
		$('#new-thumbnails').css({'width': (maxWidth+20),'height': maxHeight});
		var url = $('#slider img:first').attr('original');
				
		$('#slider img:first').addClass('active').addClass('loading');
		
		var url = $('#slider img.loading').attr('original');
		$('#slider img.loading').attr({'src':url});
		
		$('#slider img.loading').load(function() {
			$(this).addClass('loaded').removeClass('loading').css({'display':'block'}).animate({opacity:1}, 200);
			$('#image').removeClass('loading');
			loadNextImg();			
			caption();
		});
		
		resize();
	
		if ( $('#useKeys').length != 0 ) {
			$('#useKeys').delay(4000).animate({opacity:1},1000).delay(5000).animate({opacity:0}, 1000, function() {
				$(this).remove();
				$.cookie("useKeys", "off");
			});	
		}
		
	
	}
	
});

$(window).resize(function() {
		
	//set statements
	// the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
	if (typeof window.innerWidth != 'undefined')
	{
		winWidth = window.innerWidth;
		winHeight = window.innerHeight;
	}
		 
	// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
	else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth !== 0)
	{
		winWidth = document.documentElement.clientWidth;
		winHeight = document.documentElement.clientHeight;
	}
		 
	// older versions of IE
	else
	{
		winWidth = document.getElementsByTagName('body')[0].clientWidth;
		winHeight = document.getElementsByTagName('body')[0].clientHeight;
	}
	
	maxWidth = ( winWidth - 64 );
	maxHeight = ( winHeight - ( 86 + 64) );
	
	minWidth = 620;
	minHeight = 200;
	
	resize();
	
	thumbScaling();	
	
	if ( $('#cats').length != 0 ) {
		$('.thumb').css({'height':300});
		$('.thumb a').css({'height':300});
		if ( $('#innerWrapper').outerHeight(true)+40 > winHeight ) {
			var dif = $('#innerWrapper').outerHeight(true)+40 - winHeight;
			if ( dif < 320 ) {			
				$('.thumb').css({'height':300-(dif/2)});
				$('.thumb a').css({'height':300-(dif/2)});
			} else {
				$('.thumb').css({'height':140});
				$('.thumb a').css({'height':140});
			}
		}
	}
	
});
