/*************************************************
TITLE: Page Scripts
DESCRIPTION:
   A set of classes and functions used on various
   pages throughout the site.
AUTHOR: David Mingos
CONTACT: http://dmdesigns.com/contact/
*************************************************/


/* BEHAVIOURS \\\
**************************************************

// The format of the rule definitions is like so:

var myRules = {
	'b.someclass' : function(element){
		element.onclick = function(){
			alert(this.innerHTML);
		}
	},
	'#someid u' : function(element){
		element.onmouseover = function(){
			this.innerHTML = "BLAH!";
		}
	}
};
Behaviour.register(myRules);


// To add an onclick event to every list item <li> 
// in a page - you would write something like this:

var myRules = {
	'li' : function(element){
		element.onclick = function(){
			// Your onclick event goes here - eg;
			// load a page - do an AJAX etc.;
		}
	}
};
Behaviour.register(myRules);

**************************************************
*/

// a comma delimited list of element-id strings
// e.g. autoClearingInputs.push('s', 'email');
autoClearingInputs.push('s');

var searchValidation = {
	'#site-search' : function(element) {
		element.onsubmit = function() {
			return validateSearch(); 
			//validateSearch() is defined in 
			//FUNCTIONS section below
		}
	}
};

var signupValidation = {
	'#signup' : function(el) {
		el.onsubmit = function() {
			return validateSignup();
		}
	}
};

var textSizeChangers = {
	'#text-sizer a' : function(element) {
		element.onclick = function() {
			setActiveStyleSheet(this.title);
			return false;
		}
	}
};

/* FUNCTIONS \\\
*************************************************/

function validateSearch() {

   var searchbox = document.getElementById('s');
   var submitbutton = document.getElementById('go');

   if (searchbox.value.length > 0 && searchbox.value != searchbox.defaultValue) {
      submitbutton.disabled = true;
      //return true;
      document.location = '/search/' + escape(searchbox.value).replace('%20','+');
      return false;
   } else {
      alert('Please enter a word or phrase to search for.');
      searchbox.focus();
      return false;
   }

}

function validateSignup() {

   var emailBox = document.getElementById('email');
   var submitButton = document.getElementById('subscribe');

   if (emailBox.value.length > 0 && emailBox.value != emailBox.defaultValue && validateEmail(emailBox.value)) {
      submitButton.disabled = true;
      return true;
   } else {
      alert('Please enter a valid email address.');
      emailBox.focus();
      return false;
   }

}

/************************************************/

//Behaviour.register(searchValidation);
//Behaviour.register(signupValidation);
Behaviour.register(textSizeChangers);

addEvent(window, 'load', loadActiveStylesheet);
addEvent(window, 'load', setAutoClearHandlers);
addEvent(window, 'load', initAutoClearingInputs);

addEvent(window, 'unload', unloadActiveStylesheet);
