/*
Powerful Errors Template - Javascript file
Requires: jQuery 1.3+
          jQuery UI Core 
		  jQuery UI Effects Core
*/

/*** OPTIONS ***/

/*** END OPTIONS ***/


// start jQuery
$(document).ready(function(){

	// gets the # achor
	var tag = location.hash;
	
	// appends -link and checks that such a div exists, if so switch to it WITHOUT animation (i.e. make it look like it's static)
	if($(tag + '-link').length > 0)
	{
		switch_tab(tag + '-link', false);
	}

	// this handles the search box message when it's focused and unfocused (on blur)
	$('#searchterms').focus(function() {
		if($(this).attr("value") == search_message) {
			$(this).attr("value", '');
		}
	});
	
	$('#searchterms').blur(function() {
		if($(this).attr("value") == '') {
			$(this).attr("value", search_message);
		}
	});

	// adds switch tab to click event of the tabs
	$('.swtab').click(function(){
		switch_tab('#' + this.id);
	});
	
	// ajax error reporting
	$('#offerform').submit(function(){
		$('#loadingimg').show();												// show loading image
		var datastream = $("#offerform").serialize() + '&ajax=true';		// convert form data and add ajax boolean. This doesn't do anything
																				// by default but it's there for you to utilise
		jQuery.ajax({
			data: datastream,		
			url: this.action,	 // the ajax request will be sent to the ACTION value of the form
			timeout: 2000,
			error: function() {
			  console.log("Failed to submit");
			},
			success: function(r) { 
			  $('.ajaxresult').html(r); 	// put the result in the ajaxresult div
			  $('#loadingimg').hide();		// hide the image again
			}
	  });
	  return false;
	});
		


});

// handles tab switching
function switch_tab(ele, animation)
{
	$('#jquery-content .contentbox').hide();					// hides ALL content
	$('.swtab').parent().removeClass("selected");		// removes ALL selected classes from tabs
	$(ele).parent().addClass("selected");				// adds selected class to clicked tab
	var rel = $(ele).attr("rel");								// gets value of "rel" attribute of clicked link - the content ID is stored here
	if(animation == false)										
	{
		$('#' + rel).show(); // show without effects or animation
	}
	else 
	{
		$('#' + rel).show("slide", { direction: "up"  }); // show with animation
	}
}

