
/* --------------------------------------------
	 jQuery clear inputs on focus method / plugin
   --------------------------------------------  */

jQuery.fn.clearonfocus = function() {
	jQuery(this)
		.bind('focus', function() {
			// Set the default value if it isn't set
			if ( !this.defaultValue ) this.defaultValue = this.value;
			// Check to see if the value is different
			if ( this.defaultValue && this.defaultValue != this.value ) return;
			// It isn't, so remove the text from the input
			this.value = '';
		})
		.bind('blur', function() {
			// If the value is blank, return it to the defaultValue
			if ( this.value.match(/^\s*$/) )
				this.value = this.defaultValue;
		});
};


/* --------------------------------------------
	 Ajax form submission callback function
   --------------------------------------------  */

function success(data, textStatus, XMLHttpRequest) {
	
	/* 
	 * need to append data to empty div
	 * to make the returned string filterable with $
	*/
	
	data = $('<div />').append(data);
	responseContent = $("#content",data).html();
	Shadowbox.open({
		content:    '<div id="ajax_response">'+responseContent+'</div>',
		player:     "html",
		title:      "Successful Subscription",
		height:     260,
		width:      420,
		options: {
			onFinish: function() {
				$("#ajax_response p a").click(function() {
					Shadowbox.close();
					return false;
				});
			}		
		}
  });
  $("#trends_form input[type=text]").val('');
}


/* --------------------------------------
	 Do this stuff when the DOM is ready
   --------------------------------------  */
	   
$(document).ready(function() {

	/* --------------------------------------------
	 Inititalise Lightbox
   --------------------------------------------  */

	Shadowbox.init();

	/* -------------------------------------- 
	open rel="external" links in a new window
	 --------------------------------------  */	
	 
	$('a[rel*=external]').click( function() {
    window.open( $(this).attr('href') );
  	return false;
  });
  
	/* Not required by client at present
	$("#t_collections span.category_title.current").text("You are currently browsing "+$("title").text().replace(/^Collections[\s]+\|/g,"").replace(/\|.*$/g,""));   
	*/
	
	if($("#form_contact").length) {
		
		var contactDefaults = {
			name : $("#contact_name").attr('value'),
			email: $("#contact_email").attr('value'),
			website: $("#contact_website").attr('value'),			
			message: $("#contact_message").attr('value')
		}
	}		
  
	$("#form_contact").validate({
		rules: {
			name: {
				required: true,
				minlength: 2
			},
			email: {
				required: true,
				email: true
			},
			phone1: {
				required: true,
				number: true
			},
			message: {
				required: true
			}
		},
		messages: {
			// error messages
			name: {
				required: "Please tell us your name.",
				minlength: "Your name is a bit too short."
			},
			contact_name: {
				required: "Please tell us your name.",
				minlength: "Your name is a bit too short."
			},			
			email: {
				required: "Please tell us your email address.",
				email: "Please enter a valid email address so we can get back to you."
			},
			phone1: {
				required: "Please tell us your contact number",
				number: "Please make sure you have only entered numbers into the contact number field"
			},
			contact_email: {
				required: "Please tell us your email address.",
				email: "Please enter a valid email address so we can get back to you."
			},			
			contact_message: {
				required: "Please type your message or question here."
			},
			message: {
				required: "Please type your message or question here."
			}
		},
		submitHandler: function(form) {
			if($("#contact_name").val()===contactDefaults.name) { 
				$("label.error[for=contact_name]").show(); return false;
			} else {
				$("label.error[for=contact_name]").hide();
			}
			if($("#contact_email").val()===contactDefaults.email) { 
				$("label.error[for=contact_email]").show(); return false;
			} else {
				$("label.error[for=contact_email]").hide();
			}
			if($("#contact_phone1").val()===contactDefaults.phone1) {
				$("label.error[for=contact_phone1]").show(); return false;
			} else {
				$("label.error[for=contact_phone1]").hide();
			}
			if($("#contact_message").val()===contactDefaults.message) { 
				$("label.error[for=contact_message]").show(); return false;
			} else {
				$("label.error[for=contact_message]").hide();
			}
			
			form.submit();
		}				
	});
	
	if($("#comment_form").length) {
		
		var commentDefaults = {
			name : $("#comment_name").attr('value'),
			email: $("#comment_email").attr('value'),
			comment: $("#comment_comment").attr('value')
		}
	}	
	
	$("#comment_form").validate({
		rules: {
			name: {
				required: true,
				minlength: 2
			},
			email: {
				required: true,
				email: true
			},	
			comment: {
				required: true
			}
		},
		messages: {
			// error messages
			name: {
				required: "Please tell us your name.",
				minlength: "Your name is a bit too short."
			},
			email: {
				required: "Please tell us your email address.",
				email: "Please enter a valid email address so we can get back to you."
			},
			message: {
				required: "Please type your comment here."
			}
		},
		submitHandler: function(form) {
			if($("#comment_name").val()===commentDefaults.name) { 
				$("label.error[for=comment_name]").show(); return false;
			} else {
				$("label.error[for=comment_name]").hide();
			}
			if($("#comment_email").val()===commentDefaults.email) { 
				$("label.error[for=comment_email]").show(); return false;
			} else {
				$("label.error[for=comment_email]").hide();
			}
			if($("#comment_comment").val()===commentDefaults.comment) { 
				$("label.error[for=comment_comment]").show(); return false;
			} else {
				$("label.error[for=comment_comment]").hide();
			}			
			form.submit();
		}				
	});

  
	
/* --------------------------------------
		Add a class for incompetent browsers
   --------------------------------------  */	
		
	$("div.pagination ul li:last-child,#nav ul li:last-child").addClass('last-child');

/* --------------------------------------
		Clear input default values on focus
   --------------------------------------  */
	   
	$('.clearonfocus').clearonfocus();	   
	
/* --------------------------------------
		Basic accordions
   --------------------------------------  */	
   
	$("dl.nav dd.accordion-header a").click(function() {
		
		$(this).toggleClass("accordion-open").parent().next().slideToggle('slow');	
		return false;

	});				
	
	/* Collections Category Nav */
	
	$("#product-nav > li:has(ul) > a").click(function() {
		
		$(this).parent().toggleClass("accordion-open").find('ul').slideToggle(500);	
		return false;

	});					
	
	if($("#product-nav").length) {
		$currentParent = $("#product-nav li.current").parentsUntil('li.top-level.has-children').parent('li.top-level.has-children');
		$currentParent.toggleClass('accordion-open').find('ul').slideToggle(500);
	}
	

/* --------------------------------------
		Ajax mailing list signup
   --------------------------------------  */	
   
	if($("#trends_form").length) {
		$("#trends_form").submit(function() {
			var email = $("#trend_email").val();
			var dataString = 'email='+email+'&ACT=3&RET=/&list=default&site_id=1';			
			$.ajax({
			  type: 'POST',
			  url: '/',
			  data: dataString,
			  success: success,
			  dataType:"text"
			});
			return false;
		});		
	}
	
});

