$(document).ready(function() {
	
	//////////////////////////////////////////////////////////////////////////////
	// Slideshow
	
	
	$("div.slideshow").each(function(){
		$(this).cycle({
			fx: 'fade',
			speed: 'normal',
			timeout: 0,
			next: $("div.slideshow-controls a.next", this.parentNode),
			prev: $("div.slideshow-controls a.prev", this.parentNode)
		});
	});	
	
	
	//////////////////////////////////////////////////////////////////////////////
	// External Links -- opens in new tab and adds external_link class
		
	
	$('a').filter(function() {
		return this.hostname && this.hostname !== location.hostname;
	}).each(function(){
		$(this).attr("target","_blank");
	}).addClass('external_link');
	
	
	//////////////////////////////////////////////////////////////////////////////
	// Clear currency search
	

	$("div.artwork-search > input#search").focus(function() {	
		if (this.value == this.defaultValue) {  
			this.value = "";
		}
	}).blur(function() {
		if (this.value == "") {  
			this.value = (this.defaultValue ? this.defaultValue : "");  
		}
	});
	
	//////////////////////////////////////////////////////////////////////////////
	// Search
	
	$("div.artwork-search > input#search").keypress(function(event) {
		if(event.which == 13) {
			window.location.href = "/artwork/currency/search/"+$(this).val()+"/";
			return false;
		}
	});
	
	$("#search").autocomplete({
		source: "search-terms.php",
		minLength: 2
	});
	
	//////////////////////////////////////////////////////////////////////////////
	// Dates Submit
	
	$("#dates-go").click(function(){
		window.location.href = "/artwork/currency/date/"+$(".month").val()+"-"+$(".year").val()+"/";
		return false;
	});
	
	//////////////////////////////////////////////////////////////////////////////
	// Lightbox
	

	$("a.lightbox").live("click", function() {
		$.get($(this).attr("href"), function(data) {
			$("body").append(data);
			$("div#lightbox-wrapper").fadeIn(500);
		});
		return false;
	});
	
	$("div#overlay, a.#lightbox-close").live('click', function(){
		$("div#lightbox-wrapper").fadeOut(500,function(){$(this).remove();});
		$("div#overlay").remove();
		return false;
	});	
	
	//////////////////////////////////////////////////////////////////////////////
	// Social Media Links -- dynamically sets links (email, facebook, twitter)
	
	/*
	$("a.share-email").each(function(){
		$(this).attr("href","mailto:?subject=Check this out!&body=Look what I found on the 2x4 site: "+escape(document.location.href));
	});
	
	$("a.share-facebook").each(function(){
		$(this).attr("href","http://www.facebook.com/share.php?u="+document.location.href);
		$(this).attr("target","_blank");
	});
	
	$("a.share-twitter").each(function(){
		$(this).attr("href","http://twitter.com/home?status=Currently Reading: "+document.location.href);
		$(this).attr("target","_blank");
	});
	*/
	
	//////////////////////////////////////////////////////////////////////////////
	// Search -- show/hide input text, submit search form on ENTER
	
	/*
	$("#q").focus(function() {	
		if (this.value == this.defaultValue) {  
			this.value = "";
		}
	}).blur(function() {
		if (this.value == "") {  
			this.value = (this.defaultValue ? this.defaultValue : "");  
		}
	}).keypress(function(event) {
		if(event.which == 13) {
			$("#search").submit();
		}
	});
	*/
	
	// On search form submit let's send users to clean URL search
	/*
	$("#search").submit(function() {
		window.location.href = "search/"+$("#q").val()+"/";
		return false;
	});
	*/
	
	//////////////////////////////////////////////////////////////////////////////
	
});
