// cookie functions http://www.quirksmode.org/js/cookies.html

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 var 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 eraseCookie(name) { createCookie(name,"",-1); }

// Remove useless whitespace from all unordered lists in a page
function cleanLists() {
	$("ul").each(function() {
		var node	= this.firstChild;
		while(node) {
			var nextNode	= node.nextSibling;
			if(node.nodeType == 3 && !/\S/.test(node.nodeValue))
			this.removeChild(node);
			node	= nextNode;
		}
		return this;
	});
}

// Return the value of all textfields to their default in the case where a they are blank onBlur
// call the function with the class of the inputs you wish to affect ie. swapDefault("my_class")
function swapDefault(clss) {
	$("input." + clss).each(
		function() {
			$(this).focus(
				function() {
					if(this.defaultValue && this.value == this.defaultValue) this.value = "";
				}
			).blur(function() {
				if(this.defaultValue && !this.value.length) this.value = this.defaultValue;
			}
		);
	});
}

// Open a new window for links with the attribute rel="external"
function targetLinks() {
	$("a[@rel=external]").click(function() {
		window.open(this.href);
		return false;
	});
}

function focusField() {
	$("input[@type=text]").each(function() {
		$(this).focus(function() {
			$(this).addClass("focus");
			// make the Enter/Return key submit the form in IE7
			$(this).keydown(function(e) {
				if(e.keyCode == 13) {
					alert("foo");
					$(this).parents('form').submit();
				};
			});
		}).blur(function() {
			$(this).removeClass("focus");
		});
	});
}

function smartTabbing() {	
	$("a").focus(function() { 
		$(this).addClass("hover") 
	}).blur(function() {
		$(this).removeClass("hover")
	});
}

$.fn.extend({
	reset: function() {
		return this.each(function() {
			$(this).is('form') && this.reset();
		})
	}
});

function formButtons() {
	$("form:not(#form-search) input[@type=submit]").each(function() {
		$(this).before("<a href='' class='submit'>" + $(this).val() + "</a>");
	});
	$("form input[@type=reset]").each(function() {
		$(this).before("<a href='' class='reset'>" + $(this).val() + "</a>");
	});
	$("form a.submit").click(function() { $(this).parents("form").submit(); return false });
	$("form a.reset").click(function()  { $(this).parents("form").reset(); return false });
}

$(document).ready(function() {

	$("a").click(function() {
		if (this.getAttribute("href") && this.getAttribute("rel") == "external") this.target = "_blank";
	});

	$("ul#roster li").each(function() {
		var artist	= $(this).children("a").html().toString();
		if(artist.match("span")) artist		= artist.substring(0, (artist.indexOf("span") - 2));
		artist 		= artist.replace(/[^a-zA-Z 0-9 !]+-/g,'');
		artist		= artist.replace(/ /g,"_");
		artist		= artist.toLowerCase();
		$(this).append("<h3><a class='book' href='/forms/?form=offer&source=site&artist=" + artist + "'>Book This Artist</a></h3>");
	});

});
