
//	Please feel free to borrow, steal, abduct, and/or torture the code 
//	contained in this file. Though you need not give credit to
//	electricsnow.co.uk, a comment in your source code would help other 
//	developers.

//	Stay hungry, Stay foolish.

// Ajax load vars
var loadstatus="<center><div id=\"loadstatus\"><img src='_img/ajax-loader-lg.gif' alt='Loading' width='29' height='29' /></div></center>";

// ------- jQuery Functions ------- \\

function ajaxjHistoryIE(){
	if($.browser.msie) {
	$(".ajaxjHistoryIE").click(function(){
		var hash = this.href;
		hash = hash.replace(/^.*#/, ''); // moves to a new page. 
		$.historyLoad(hash); // pageload is called at once. 
	});
	} else {
		// Do nothing (MicroSoft IE sucks floppy donkey cock).
	};
};


// Initialize history plugin. (// http://www.mikage.to/jquery/jquery_history.html)
function historyJazz(){
	$.historyInit(pageload);
	$("[@rel='history']").click(function(){ // set onlick event for buttons
		var hash = this.href;
		hash = hash.replace(/^.*#/, ''); // moves to a new page. 
		$.historyLoad(hash); // pageload is called at once. 
	});
};

// Toggle Show / Hide - Rob Morgan electricSnow.co.uk
function showFootDir(){
	$(".showFootDir").toggle(function(){
	  $('#directorynav').slideDown(400);
	  $(this).html("<span class='colorMe'>Close Site Map</span>").fadeIn(400);
	},function(){
	  $('#directorynav').slideUp(400);
	  $(this).html("Site Map").fadeIn(400);
	});
	
};

function requestQuote() {
	// Close FormResonse Window (Forms)
	$("#requestQuote").click(function(){
		$('#salesFormBox').slideDown(800);
		$('#salesFormBox1').slideUp(800);
		return false;
	});
};

// Check fields on form - Rob Morgan electricSnow.co.uk 
function validate() { 
	// Check Fields
	var nameValue = $('input[@name=user]').fieldValue(); 
	var emailValue = $('input[@name=email]').fieldValue();
	var spamValue = $('input[@name=antiSpam]').fieldValue(); 
	// General 'if empty'
	if (!nameValue[0] || !emailValue[0]) { 
		$('.required').css({ color: "red"}).fadeIn(400);
		$('div#requiredMsg').fadeIn(400);
		$('.antiSpam').css({ border: "2px solid red" }).fadeIn(400);
		return false; 
	}
	// Anti-Spam 'if empty'
	if (!spamValue[0]) { 
		$('.antiSpam').css({ border: "2px solid red" }).fadeIn(400);
		alert('Please answer the Anti-Spam Question');
		return false; 
	} 
	// debug alert('Please answer the Anti-Spam Question');
}

function formResponse() {
    // bind form using ajaxForm 
    $('.actionForm').ajaxForm({ 
        // target identifies the element(s) to update with the server response 
        beforeSubmit: validate,
        target: '#formResponse',
        // success identifies the function to invoke when the server response 
        // has been received; here we apply a fade-in effect to the new content 
        success: function() { 
            $('#formResponse').fadeIn('slow'); // Show Respose Box
            $('.formResponseClose').fadeIn('slow'); // Show Respose Box Close Button
            $('.actionForm').css({opacity: .30}); // Dim form
            return false;
        } 
    }); 
};

function downloadFormShow() {
	// Close FormResonse Window
	$('.dlOption').change( function(){
		$('.hideMe').slideDown(400);
		$("#salesLogos").fadeOut(100);
		return false;
	});
};


function formResponseClose() {
	// Close FormResonse Window
	$(".formResponseClose").click(function(){
		$(this).hide();
		$('#formResponse').fadeOut(400);
		$('.actionForm').css({opacity: 1});
		return false;
	});
};

// This initialises the scollpanes on the page.
// http://kelvinluck.com/assets/jquery/jScrollPane/jScrollPane.html
$(function()
{
	$('div#scrollPane').jScrollPane({showArrows:true, scrollbarWidth: 15, arrowSize: 16});
});
// http://www.mikage.to/jquery/jquery_history.html
function pageload(hash) {
	// hash doesn't contain the first # character.
	if(hash) {
		// restore ajax loaded state
		//historyJazz();
		$('div#ajaxContent').html(loadstatus).load(hash + ".php", function() {
		$('div#scrollPane').jScrollPane({showArrows:true, scrollbarWidth: 15, arrowSize: 16});
		formResponse();
		formResponseClose();
		downloadFormShow();
		emailCloak();
		requestQuote();
		ajaxjHistoryIE();
	}).fadeIn(100);
	} else {
		// start page
		$("#ajaxContent").html(loadstatus).load("welcome.php");
	}
}		
// http://kelvinluck.com/assets/jquery/jScrollPane/jScrollPane.html
$(function()
{
	// Scroll to - Scroll Pane
	// set up the links
	$('a.scroll2element').bind(
		'click',
		function()
		{
			$this = $(this);
			var destinationSelector = $(this).attr('rel');
			$('#scrollPane', $this.parent().parent().parent().parent().parent().parent().parent()).each(
				function()
				{
					this.scrollTo(destinationSelector);
				}
			);
		}
	);
	
});

// Cloak Email Address
function emailCloak() {
	if (document.getElementById) {
		var alltags = document.all? document.all : document.getElementsByTagName("*");
		for (i=0; i < alltags.length; i++) {
		  if (alltags[i].className == "emailCloak") {
			var oldText = alltags[i].firstChild;
			var emailAddress = alltags[i].firstChild.nodeValue;
			var user = emailAddress.substring(0, emailAddress.indexOf("("));
			var website = emailAddress.substring(emailAddress.indexOf(")")+1, emailAddress.length);
			var newText = user+"@"+website;
			var a = document.createElement("a");
			a.href = "mailto:"+newText;
			var address = document.createTextNode(newText);
			a.appendChild(address);
			alltags[i].replaceChild(a,oldText);
		  }
		}
	}
}


// ------- jQuery Doc Ready ------- \\

$(document).ready(function(){
	// Initialise History
	historyJazz();
	// Draggable Menu
	$('#mainNav').draggable();
	// Accordion Menu
	$('.navigation').accordion({ 
	    active: true, 
	    header: '.head', 
	    navigation: true, 
	    event: 'click', 
	    autoheight: false
	});
	// Initialise Site Map
	showFootDir();
	// Initialise Cloak Email Address
	emailCloak();
});



