$(function() {
	var active_color = '#241c1a'; // Colour of user provided text
	var inactive_color = '#949494'; // Colour of default text
	//show / hide input values on select 
	$("input.default").css("color", inactive_color);
	var default_values = new Array();
	$("input.default").focus(function() {
		if (!default_values[this.id]) {
			default_values[this.id] = this.value;
		}
		if (this.value == default_values[this.id]) {
			this.value = '';
			this.style.color = active_color;
		}
		$(this).blur(function() {
			if (this.value == '') {
				this.style.color = inactive_color;
				this.value = default_values[this.id];
			}
		});
	});

	//initiate the slider on the homepage
	$('#slider').nivoSlider({
		effect:'sliceDown', //Specify sets like: 'fold,fade,sliceDown'
		slices:15,
		animSpeed:500,
		pauseTime:5000,
		startSlide:0, //Set starting Slide (0 index)
		directionNav:true, //Next & Prev
		directionNavHide:true, //Only show on hover
		controlNav:true, //1,2,3...
		controlNavThumbs:false, //Use thumbnails for Control Nav
		controlNavThumbsFromRel:false, //Use image rel for thumbs
		keyboardNav:true, //Use left & right arrows
		pauseOnHover:true, //Stop animation while hovering
		manualAdvance:false, //Force manual transitions
		captionOpacity:0.9
	}); 

	//initiate cycle through project images
	$('#projectSlider').cycle({ 
		fx:'fade', 
    	speed:  'fast', 
    	timeout: 5000, 
    	pager:  '#nav'
    });
    
  $('#projectSlider2').cycle({ 
		fx:'fade', 
    	speed:  'fast', 
    	timeout: 5000, 
    	pager:  '#nav2'
    });

  $('#projectSlider3').cycle({ 
		fx:'fade', 
    	speed:  'fast', 
    	timeout: 5000, 
    	pager:  '#nav3'
    });
    
  $('#projectSlider4').cycle({ 
		fx:'fade', 
    	speed:  'fast', 
    	timeout: 5000, 
    	pager:  '#nav4'
    });
    
  $('#projectSlider5').cycle({ 
		fx:'fade', 
    	speed:  'fast', 
    	timeout: 5000, 
    	pager:  '#nav5'
    });
    
  $('#projectSlider6').cycle({ 
		fx:'fade', 
    	speed:  'fast', 
    	timeout: 5000, 
    	pager:  '#nav6'
    });
    
	//full caption sliding
	$('.boxgrid.captionfull').hover(function(){
		$(".cover", this).stop().animate({top:'100px'},{queue:false,duration:160});
	}, function() {
		$(".cover", this).stop().animate({top:'160px'},{queue:false,duration:160});
	});

   	// validate contact form on keyup and submit
	$("#contact").validate({
		//set the rules for the fild names
		rules: {
			name: {
				required: true,
				minlength: 2
			},
			email: {
				required: true,
				email: true
			},
			message: {
				required: true
			}
		},
		//set messages if you want to add these
		messages: {
			name: "",
			email: "",
			message: ""
		},
		submitHandler: function() {
			$('#contact').hide(); //hide off the contact form
			$('#page #topRight #messageStatus').show(); //show the loading bar
			$.post('mail.php',{name:$('#name').val(), email:$('#email').val(), message:$('#message').val()},
			function(data) { 
				$('#page #topRight #messageStatus').css({display:'none'}); //hide the loading bar when complete
				if( data == 'success') { //show a success message
					$('#callback').show().append("Thank you "+$('#name').val()+" we will be in contact with you soon");
				} else {//show a fail message
					$('#callback').show().append("Sorry but your message could not be sent. please try a different means of contact");
				}
			});		
		}
	});
	
	//project sorting function
	$('#controls li a.toggle').click(function() {
		//highlight the link that was clicked
		$('#controls li a.toggle:not('+this+')').addClass('off').removeClass('on'); //turn off all other selections
		$(this).addClass('on'); //turn the new selection on
		var selection = $(this).attr('title'); // store the selection that was clicked
		$('ul.projects li').show(); //start by showing everything
		var stored = $('ul.projects li.'+selection); //assign the project selection
		$('ul.projects li:not(.'+selection+')').hide(); // hide everything but the selection you made
		stored.removeClass('last'); //remove the .last class 
		var count = 1; //start an iteration counter
		$.each(stored, function(key, value) { //loop through results set apply
			if(count == 3) { //if on the third element put add our last class
				$(this).addClass('last');
				count = 0; // set the count back to 1 after this loop so we can count again
			}
			count++;
		});
		return false;	
	});

});
