(function($) {
	$.fn.reverseOrder = function() {
	   return this.each(function() {
	       $(this).prependTo( $(this).parent() );
	   });8
	};
})(jQuery);

$(document).ready(function() 
{ 		
	$('input.innerlabel').innerLabel(); // apply inner labels
	
	$("#contact_forms_wrapper li form").hide(); // hide all forms
	
	$("#contact_forms_wrapper li a").live("click", function() // when choice is clicked slide down form and slide up any other forms
	{
		$(this).addClass("selected").siblings("form").slideDown().parents("li").siblings("li").children("a.selected").removeClass("selected").siblings("form").slideUp();
	});
	
	$("#contact_forms_wrapper li a.selected").live("click", function() // when choice is unselected slide up form
	{
		$(this).removeClass("selected").siblings("form").slideUp();
	});

	
	$(".video_link").click(function(){
		$("#video_demo").slideToggle("slow");
	});
	
	$('a[href*=#]').click(function() {
		if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
		&& location.hostname == this.hostname) {
		  var $target = $(this.hash);
		  $target = $target.length && $target
		  || $('[name=' + this.hash.slice(1) +']');
		  if ($target.length) {
			var targetOffset = $target.offset().top;
			$('html,body')
			.animate({scrollTop: targetOffset}, 1000);
		   return false;
		  }
		}
	  });
	  
		$('#nav li').reverseOrder()
		
		

   $('input, select, textarea').focus(function(){

		$(this).removeClass('warn');

		

	});
	
	$('input.innerlabel').innerLabel();
	
	

/* Rate Our Service */


	$(".star_wrapper span").hide(); // hide radio buttons
	
	$("#complaint_section, #compliment_section").hide(); // hide complaint section of form
	$(".bad_experience, .good_experience").show(); // show bad experience sentence inside hidden div
	
	$(".star_wrapper").prepend('<a class="not_selected" title="Rate Despatch Bay 1 Star :&lsquo;(" alt="1"></a><a class="not_selected" title="Rate Despatch Bay 2 Stars :( " alt="2"></a><a class="not_selected" title="Rate Despatch Bay 3 Stars :|" alt="3"></a><a class="not_selected" alt="4" title="Rate Despatch Bay 4 Stars :)"></a><a class="not_selected" alt="5" title="Rate Despatch Bay 5 Stars :D"></a>'); // add stars
	
	$(".star_wrapper a").click(function()
	{	
		var selected_value = parseInt($(this).attr("alt"), 10); // star rating value	
		var radio_id = "rating_"+selected_value; // radio id
		
		$(this).siblings("a.selected").addClass("not_selected").removeClass("selected"); // reset stars
		
		$(this).addClass("selected").removeClass("not_selected"); // get rid off 'not_selected' class
		
		$(this).parent().find("#"+radio_id).attr("checked", "checked"); // check correct radio value
		
		if (selected_value < 3) // if rating is less than 3
		{
			$("#complaint_section").slideDown("slow"); // show complaint form
			$("#compliment_section").slideUp("slow"); // else hide complaint form
		} else
		{
			$("#compliment_section").slideDown("slow"); // show complaint form
			$("#complaint_section").slideUp("slow"); // else hide complaint form
		}
		
		$(this).siblings("a").each(function(i) // traverse all stars
		{
			// find all siblings which have a lower rating value than the one clicked
			if (parseInt($(this).attr("alt"), 10) < selected_value)
			{
				// select stars which come before the one that was clicked
				$(this).addClass("selected").removeClass("not_selected").removeClass("hover");
			}
		});
	});
	
	$(".star_wrapper a").hover(function()
	{
		var hover_value = parseInt($(this).attr("alt"), 10);
		$(this).siblings("a.not_selected").each(function(i) // traverse all non selected stars
		{
			if (parseInt($(this).attr("alt"), 10) < hover_value)
			{
				$(this).addClass("hover"); // set background position of hover stars
			}
		});		
	}, function()
	{
		var hover_value = parseInt($(this).attr("alt"), 10);
		$(this).siblings("a.not_selected").each(function(i) // traverse all non selected stars
		{
			var sibling_value = parseInt($(this).attr("alt"), 10);
			if (sibling_value < hover_value)
			{
				$(this).removeClass("hover"); // reset elements background position
			}
		});				
	});
}); 
