/*************************************************
AUTHOR: Dave Mingos
CONTACT: http://dmbuilds.com/contact/
*************************************************/

// $(window).load(function() { loadActiveStylesheet(); });
// $(window).unload(function() { unloadActiveStylesheet(); });

$(document).ready(function() {

	$.fn.extend({
		validEmail: function() {
			var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
			return filter.test(this.val());
		},
		validPhone: function() {
			var filter = /^(\(?\d{3}\)?([-\/\s\.])?)?\d{3}-\d{4}$/;
			return filter.test(this.val());
		},
		validZipCode: function() {
			var filter = /^\d{5}([\-]\d{4})?$/;
			return filter.test(this.val());
		},
		autoClear: function() {
			var input = document.getElementById(this.attr('id'));
			if (input && (input.type == 'text' || input.type == 'textarea')) {
			
				this.focus(function() {
					if (this.value == this.defaultValue) {
						this.value = '';
						$(this).removeClass('default-value');
					}
				});
			
				this.blur(function() {
					if (!this.value) {
						this.value = this.defaultValue;
						$(this).addClass('default-value');
					}
				});
			
				if (input.value == input.defaultValue) this.addClass('default-value');
				else this.removeClass('default-value');
			
			}
		},
		check: function() {
			return this.each(function() { this.checked = true; });
		},
		uncheck: function() {
			return this.each(function() { this.checked = false; });
		}
	});

	// attach hover class for navigation li elements in IE6 
	if ($.browser.msie && parseInt($.browser.version) == 6) {
		// add "hover" class on mouseover
		$("ul.navigation").children('li').each(function() {
			$(this).mouseover(function() { $(this).addClass('hover'); });
		});
		// remove "hover" class on mouseout
		$("ul.navigation").children('li').each(function() {
			$(this).mouseout(function() { $(this).removeClass('hover'); });
		});
	}

	/*
	// wire up the text sizer links 
	$('#text-sizer a').each(function() {
		this.onclick = function() {
			setActiveStylesheet(this.title);
			return false;
		}
	});
	*/

});


/* STYLESHEET SWITCHER 
***********************************************************
Based on: http://www.alistapart.com/stories/alternate    */

function setActiveStylesheet(title) {
	var i, a, main;
	for(i=0; (a = document.getElementsByTagName('link')[i]); i++) {
		if(a.getAttribute('rel').indexOf('stylesheet') != -1 && a.getAttribute('title')) {
			a.disabled = true;
			if(a.getAttribute('title') == title) a.disabled = false;
		}
	}
}

function getActiveStylesheet() {
	var i, a;
	for(i=0; (a = document.getElementsByTagName('link')[i]); i++) {
		if(a.getAttribute('rel').indexOf('stylesheet') != -1 && a.getAttribute('title') && !a.disabled) return a.getAttribute('title');
	}
	return null;
}

function getPreferredStylesheet() {
	var i, a;
	for(i=0; (a = document.getElementsByTagName('link')[i]); i++) {
		if(a.getAttribute('rel').indexOf('stylesheet') != -1
			&& a.getAttribute('rel').indexOf('alt') == -1
			&& a.getAttribute('title')
		) return a.getAttribute('title');
	}
	return null;
}

function createCookie(name, value, days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = '; expires='+date.toGMTString();
	}
	else expires = '';
	document.cookie = name+'='+value+expires+'; path=/';
}

function readCookie(name) {
	var nameEQ = name + '=';
	var ca = document.cookie.split(';');
	for(var i=0; i < ca.length; i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1, c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
	}
	return null;
}

function loadActiveStylesheet(e) {
	var cookie = readCookie(window.location.hostname+'.style');
	var title = cookie ? cookie : getPreferredStylesheet();
	setActiveStylesheet(title);
}

function unloadActiveStylesheet(e) {
	var title = getActiveStylesheet();
	createCookie(window.location.hostname+'.style', title, 365);
}

var cookie = readCookie(window.location.hostname+'.style');
var title = cookie ? cookie : getPreferredStylesheet();
setActiveStylesheet(title);
