$(document).ready(function() {
	// Create dynamic text in search input
	$('.hideValue').each(function(index){
		var defaultValue = $(this).val();
		$(this).focus(function() {
			if ($(this).val() == defaultValue) {
				$(this).val('');
			}
			$(this).blur(function() {
				if ($(this).val() == '') {
					$(this).val(defaultValue);
				}
			});
		});
	});
	
	// Check for colorbox images
	$("a[rel='colorbox']").colorbox({transition:'elastic', maxWidth:'95%', maxHeight:'95%'});
	
	// Home slider variables etc.
	if($('#slider').length)
	{
		$('#slider').nivoSlider({
			effect: 'fade', // sliceDown | sliceDownLeft | sliceUp | sliceUpLeft | sliceUpDown | sliceUpDown | fold | fade | random
			slices: 4, // Amount of slices for slice effects
			animSpeed: 350, // Animation speed in milliseconds
			pauseTime: 5000, // Pause amount when hovered over, in milliseconds
			directionNav: true, // Next & Prev
			directionNavHide: true, // Only show on hover
			controlNav: true, // 1,2,3...
			keyboardNav: true, // Use left & right arrows
			captionOpacity: 1, // Universal caption opacity
			pauseOnHover: true // Stop animation while hovering
		});
	}
	
	// Portfolio selector - displays all when javascript is disabled
	if($('#portfolio').length)
	{
		function portSelect(name) {
			if (!($.browser.msie && $.browser.version === '6.0')) {
				$("div[id^='portfolio-']").removeClass('current').fadeTo('fast', 0).hide();
				$('#portfolio-'+name).addClass('current').fadeTo('fast', 1).show();
			} else {
				$("div[id^='portfolio-']").removeClass('current').hide();
				$('#portfolio-'+name).addClass('current').show();
			}
			
			// Set height to stop scrolling to top
			// $('#portfolio').height('auto');
			// var height = $('#portfolio').height();
			// $('#portfolio').height(height);
		}
		
		$("#portfolioNav a").click(function () {
			if (!$(this).is('.selected')) {
				$('#portfolioNav a').removeClass('selected');
				$(this).addClass('selected');
				portSelect($(this).attr('rel'));
				return false;
			}
		})
		
		var checkTime = 0;
		$(document).keydown(function(e) {
			var currentTime = new Date();
			var code = (e.keyCode ? e.keyCode : e.which);
			var direction = null;
			
			if((currentTime.getTime() - checkTime) > 450) {
			
				// handle cursor keys
				if (code == 37) { // left key
					direction = 'prev';
				}
				else if (code == 39) { // right key
					direction = 'next';
				}

				if (direction != null) {
					$('#portfolioNav a.selected').parent()[direction]().find('a').click();
				}
				
				checkTime = currentTime.getTime();
			}
		});
		
		$("div[id^='portfolio-']").hide();
		$("#portfolioNav a:first").click();
		$("#portfolio h2.portfolioHead").hide();
		
		// Fade portfolio images only if browser is not IE6
		if (!($.browser.msie && $.browser.version === '6.0')) {
			$('#portfolio .item .itemHeader img').hover(function() {
				$(this).fadeTo('normal', 0.65);
			}, function() {
				$(this).fadeTo('fast', 1);
			});
		}
	}
});
