$(function () {
	//Blur links
	$('a').bind("click", function(e) {
		$(this).blur();
	});
	
	if ( $('#tagline').length > 0 ) {
		var so = new SWFObject("http://www.ayrpak.co.nz/flash/tagline.swf", "taglineswf", "100%", "100%", "7", "#000000");
		so.write("tagline");
	}
	
	
	
	//Category list
	scrollOptions = {
		showArrows:true,
		scrollbarWidth: 18,
		scrollbarOnLeft:true,
		arrowSize: 0,
		scrollbarMargin: 5,
		animateTo: true,
		animateInterval:50,
		animateStep:5
	};
	
	treeviewOptions = {
		collapsed: true,
		speed: "slow",
		unique: true,
		persist: "location"
	};
	
	$("#pane").treeview(treeviewOptions);
	$('#pane').jScrollPane(scrollOptions);
	$('#pane li.expandable span, #pane li.collapsable span:first').append(' &raquo;');

	// ScrollPane hover
	$('#pane a').hover(
      function () {
        $(this).toggleClass('scrollHover');
      }, 
      function () {
        $(this).toggleClass('scrollHover');
      }
    );
	
	
	
	
	
 	// Bind add-to-cart inputs (dynamic)
 	$('.cart-button').livequery("click", function(e) {
		add_item($(this));
		return false;
	});
	
	
	
	
	
	
	
}); // end $function



function add_item(obj) {
		$this = obj;
		$this.blur();
		var pattern = /[(\*\(\)\[\]\+\.\,\?\:\;\'\"\`\~\\#\$\%\^\&\<\>\_\-)+]/;
		$('.added').remove();		
		var $curr = $this;
		var $theparent = $curr.parent('div').parent('div');
		var code = $theparent.parent('div').attr('id');
		//alert(code);
		
		var $item = $curr.prev('.cart-input');
		var entry_id = $item.attr('id');
		//alert(entry_id);
		
		var qty = $item.val();
		//alert(qty);
		
		if ( qty=='' || qty=='undefined' || qty=='null' )  {
			$('#' + code + ' .item-message').html('<p class="added">Please enter a quantity to order</p>');
			return false;
		} else if ( parseInt(qty) != parseFloat(qty) ) {
			$('#' + code + ' .item-message').html('<p class="added">Please enter a integer for the quantity</p>');
			return false;
		} else if (qty.match(pattern) || qty.match(/\D+/i) ) { 
			$('#' + code + ' .item-message').html('<p class="added">Invalid character. Please use numbers only.</p>');
			return false;
		} else {
			$.post('http://'+window.location.hostname+'/cart/&P=add_items', { entry_id: entry_id, qty: qty}, function(data){
				if (window.location.pathname.match(/cart/i) ) 
					window.location.href = 'http://'+window.location.hostname+'/cart';
				else
					if (qty == '0')
						$('#' + code + ' .item-message').html('<p class="added">Item '+code.toUpperCase()+' has been removed from your cart</p>');
					else	
						$('#' + code + ' .item-message').html('<p class="added">'+qty+' of item '+code.toUpperCase()+' has been added to your cart</p>');
			});
			return true;
		}
		
}






function validate_details(aform) {
    $('#messages').hide();
	var errors = new Array();
	$(':input').each(function() { 
	var n = $(this).attr('name');
		if (n == 'name') {
			var myname = $(this).val();
			if ((myname==null)||(myname=="")){
				errors.push('Please enter a name');
				$(this).addClass('fieldError');
			} else {
				$(this).removeClass('fieldError'); 
			}
		} else if (n == 'company') {
			mycompany = $(this).val();
			if ((mycompany==null)||(mycompany=="")){
				errors.push('Please enter the name of your company or organisation');
				$(this).addClass('fieldError');
			} else {
				$(this).removeClass('fieldError');  
			}
		} else if (n == 'email_address') {	
			myemail = $(this).val();
			var epattern  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
			if ((myemail==null)||(myemail=="")){
				errors.push('Please enter an email address');
				$(this).addClass('fieldError');
			} else if (!myemail.match(epattern)) {
				errors.push('Invalid email address');
				$(this).addClass('fieldError');
			} else {
				$(this).removeClass('fieldError');  
			}
		} else if (n == 'phone') {
			myphone = $(this).val();
			if ((myphone==null)||(myphone=="")){
				errors.push('Please enter a contact phone number');
				$(this).addClass('fieldError');
			} else {
				$(this).removeClass('fieldError');  
			}
		} else if (n == 'shipping_address') {
			myship = $(this).val();
			if ((myship==null)||(myship=="")){
				errors.push('Please enter your shipping address number');
				$(this).addClass('fieldError');
			} else {
				$(this).removeClass('fieldError');  
			}
		} else if (n == 'postal_address') {
			mypost = $(this).val();
			if ((mypost==null)||(mypost=="")){
				errors.push('Please enter your postal address number');
				$(this).addClass('fieldError');
			} else {
				$(this).removeClass('fieldError');  
			}
		}	
	});
    
	if (errors.length > 0) {
		var errormsg = "";
		for (n in errors) {
			k = parseInt(n)+1;
			errormsg += k + '. ' + errors[n] + '<br>';
		}
		$('#messages').show().html(errormsg);
		return false;
	}
	return true;
}


function validate_signup(aform) {
	$('#messages').hide();
	var errors = new Array();
	$(':input').each(function() { 
		var n = $(this).attr('name');
		if (n == 'name') {
			var myname = $(this).val();
			if ((myname==null)||(myname=="")){
				errors.push('Please enter a name');
				$(this).addClass('fieldError');
			} else {
				$(this).removeClass('fieldError'); 
			}
		} else if (n == 'company') {
			mycompany = $(this).val();
			if ((mycompany==null)||(mycompany=="")){
				errors.push('Please enter the name of your company or organisation');
				$(this).addClass('fieldError');
			} else {
				$(this).removeClass('fieldError');  
			}
		} else if (n == 'email_address') {	
			myemail = $(this).val();
			var epattern  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
			if ((myemail==null)||(myemail=="")){
				errors.push('Please enter an email address');
				$(this).addClass('fieldError');
			} else if (!myemail.match(epattern)) {
				errors.push('Invalid email address');
				$(this).addClass('fieldError');
			} else {
				$(this).removeClass('fieldError');  
			}
		} else if (n == 'agree') {	
			if (!this.checked) {
				errors.push('You must agree to the terms and conditions to subscribe.');
				$(this).addClass('fieldError');
			} else {
				$(this).removeClass('fieldError');  
			}
		}				
	});
	if (errors.length > 0) {
		var errormsg = "";
		for (n in errors) {
			k = parseInt(n)+1;
			errormsg += k + '. ' + errors[n] + '<br>';
		}
		$('#messages').show().html(errormsg);
		return false;
	} 
	return true;
}


function validate_unsubscribe(aform) {
	$('#messages').hide();
	var errors = new Array();
	$(':input').each(function() { 
		var n = $(this).attr('name');
		if (n == 'email_address') {	
			myemail = $(this).val();
			var epattern  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
			if ((myemail==null)||(myemail=="")){
				errors.push('Please enter an email address');
				$(this).addClass('fieldError');
			} else if (!myemail.match(epattern)) {
				errors.push('Invalid email address');
				$(this).addClass('fieldError');
			} else {
				$(this).removeClass('fieldError');  
			}
		} 
	});
	
	if (errors.length > 0) {
		var errormsg = "";
		for (n in errors) {
			errormsg += errors[n] + '<br>';
		}
		$('#messages').show().html(errormsg);
		return false;
	} 
	return true;
}

function image_pop(url) {
	window.open(url,'','width=420,height=620,resizable=1,scrollbars=1');
}

Number.prototype.formatMoney = function(c, d, t){
	var n = this, c = isNaN(c = Math.abs(c)) ? 2 : c, d = d == undefined ? "," : d, t = t == undefined ? "." : t, s = n < 0 ? "-" : "", i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;
	return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
};