
var containerHeight = 400;
var containerWidth = 400;
var imageHeight = 400;
var imageWidth = 400;
var thumbHeight = 400;
var thumbWidth = 400;

$(document).ready(function(){

		$('p.member-field').each(function() {
			$(this).append('<small> ' + $('small', this).text() + '</small>');
			$('small:first', this).remove();
		});
		
		
		// Add selectors to IE6
		if ($.browser.msie && $.browser.version < 7) {

			// Navigation hover
			$('ul#main-nav li').hover(function() {
				$('a', $(this)).addClass('over');
				$('ul', $(this)).css('display', 'block');
			}, function() {
				$('ul', $(this)).css('display', 'none');
				$('a', $(this)).removeClass('over');
			});

			// Left navigation selected first link
			$('#left-nav div ul li.selected>a').addClass('selected-first-link');
			$('#left-nav div ul li.selected>a').css('color', '#000');
			$('#left-nav div ul ul li.selected>a').addClass('selected-inner-first-link');
			$('#left-nav div ul ul li.selected>a').css('color', '#000');
			$('#left-nav div ul ul ul li.selected>a').addClass('selected-inner-inner-first-link');
			$('#left-nav div ul ul ul li.selected>a').css('color', '#000');

			// Drop first vertical divider on top nav
			$('#header #top-nav li:first-child').css('border', 0);

		}

		// Clear the default Text #firstname #lastname #email
		function defaultText(field) {
			if ($('#'+field).val() == 'Enter your '+field) { $('#'+field).css('color', '#aaa'); }
			$('#'+field).focus(function() { $(this).css('background-color','#ffc'); if ($(this).val() == 'Enter your '+field) { $(this).val(''); $(this).css('color', '#000'); } });
			$('#'+field).blur(function() { $(this).css('background-color','#fff'); if ($(this).val() =='') { $(this).val('Enter your '+field); $(this).css('color', '#bbb'); } });
		}

		// Add external interface
		jQuery.extend({
	    initForm: function(label) {
			switch (label) {
				case 'join': var fields = Array('firstname', 'lastname', 'phone', 'email', 'address'); break;
				case 'home': var fields = Array('email'); break;
				case 'contact': var fields = Array('name', 'email', 'subject', 'enquiry'); break;
				default: break;
			}
			for (var i=0; i<fields.length; i++)
				defaultText(fields[i]);
		}
		});

		// Replace default text prior to validation
		$("#contactform").submit(function() {
	    	$("#contactform input[@id]").each(function() {
				if ($(this).val().substring(0,10) == 'Enter your') {
		           	$(this).val('');
				}
			});
	    	$("#contactform textarea").each(function() {
				if ($(this).val().substring(0,10) == 'Enter your') {
        	    	$(this).val('');
				}
			});
		});

		// General - Expand/Collapse Left Navigation Menus
		$("#left-nav a.collapse").click(function(){
			clicked = $(this);
			context = $(this).parent().parent();
			$("ul", context).slideToggle("normal", function () {
				clicked.toggleClass("closed");
			});
			return false;
		});
		
		// Category Page - Re-ordering
		$("#category-sort fieldset select").change(function() {
        	if ($(this).value!=0) {
				$(this).parent().parent().submit();
			}
		});

		// Category Page - Brand Filtering
		$('#filter-brands input').click(function() {
			$('div.' + $(this).val()).toggle();
		});
		var pageBrands = [];
		$('#filter-brands input').each(function(i) {
			pageBrands[pageBrands.length] = $(this).val();
		});
		for(x = 0; x < pageBrands.length; x++) {
			if ($('div.' + pageBrands[x]).css('display') == undefined) {
				$('#label-' + pageBrands[x]).css('color', '#bbb');
				$('input', $('#label-' + pageBrands[x]).parent()).attr('disabled','disabled');
			}
		}
		// Product Page - Category Product Scroller
		$("#product-scroller").accessNews({
			newsHeadline: "Category Products",
			newsSpeed: 200
		});
		
		
		
		// Product Page - Variant Selection
		var productOptions = false;
		var productIdSelect = false;
		$('#product #productId').each(function(){
			productSelectClone = this.cloneNode(true);
			productOptions = productSelectClone.getElementsByTagName("option");
			productIdSelect = this;
		});
		$("#colours li a").click(function(){
			// update the selected color
			$("#colours li.selected").removeClass("selected");
			$(this).parent().addClass("selected");
			
			// remove all options from the select, then append any that match class names
			while (productIdSelect.options.length) { productIdSelect.remove(0); }
			var pattern2 = new RegExp("( |^)(" + $(this).attr('class') + ")( |$)");
			for (var i = 0; i < productOptions.length; i++) {
				if (productOptions[i].className.match(pattern2)) {
					productIdSelect.appendChild(productOptions[i].cloneNode(true));
				}
			}
			productIdSelect.selectedIndex = 0;
			
			// update the selected color htmnl
			var colourDesc = $(this).parent().find("span").html();
			$("#colour-status").html("Your selected colour is "+colourDesc);
			return false;
		});
		$("#colours li.selected a:first-child").click();
				
		// Product Page - Image Scrolling
		containerHeight = $('#image-zoomer').height();
		containerWidth =$('#image-zoomer').width();
		$('#image-zoomer').append('<span class="marker">Click to zoom in</span>');
		$('#image-zoomer img').hide();
		
		$('#image-zoomer img').load(function() { 
			$('#image-zoomer img').css('height', 'auto');
			$('#image-zoomer img').css('width', 'auto');
			imageHeight = $('#image-zoomer img').height();
			imageWidth = $('#image-zoomer img').width();
			if (imageHeight >= imageWidth) {
				thumbHeight = containerHeight;
				thumbWidth = Math.round(containerWidth * (imageWidth/imageHeight));
			} else {
				thumbHeight = Math.round(containerHeight * (imageHeight/imageWidth));
				thumbWidth = containerWidth;
			}
			$('#image-zoomer img').css('height', thumbHeight+'px').fadeIn(250); 
			$('#image-zoomer img').css('top', ((containerHeight/2)-(thumbHeight/2))+'px');
			$('#image-zoomer img').css('left', ((containerWidth/2)-(thumbWidth/2))+'px');
		});
		$('#image-zoomer img').click(function(e) {
			if ($('#image-zoomer img').css('height') == imageHeight+'px') {
				$('#image-zoomer img').animate({height: thumbHeight+'px', top: (containerHeight-thumbHeight)/2, left: (containerWidth-thumbWidth)/2}, 150);
				$('#image-zoomer .marker').html('Click to zoom in');
			} else if ($('#image-zoomer img').css('height') == thumbHeight+'px') {
				offset = $('#image-zoomer').offset();
				mouseX = (e.pageX - offset.left);
				mouseY = (e.pageY - offset.top);
				if (imageHeight >= containerHeight) {
					imageTop = (imageHeight - containerHeight) / 100 * (100 / containerHeight * mouseY) * -1;
				} else {
					imageTop = (containerHeight/2)-(imageHeight/2);
				}
				if (imageWidth >= containerWidth) {
					imageLeft = (imageWidth - containerWidth) / 100 * (100 / containerWidth * mouseX) * -1;
				} else {
					imageLeft = (containerWidth / 2) - (imageWidth / 2);
				}
				$('#image-zoomer img').animate({
					height: imageHeight+'px', 
					top: imageTop + 'px',	
					left: imageLeft + 'px'
				}, 150);
				$('#image-zoomer .marker').html('Click to zoom out');
			}
		});
		$('#image-zoomer img').mousemove(function(e) {
			offset = $('#image-zoomer').offset();
			mouseX = (e.pageX - offset.left);
			mouseY = (e.pageY - offset.top);
			if (imageHeight >= containerHeight) {
				imageTop = (imageHeight - containerHeight) / 100 * (100 / containerHeight * mouseY) * -1;
			} else {
				imageTop = (containerHeight/2)-(imageHeight/2);
			}
			if (imageWidth >= containerWidth) {
				imageLeft = (imageWidth - containerWidth) / 100 * (100 / containerWidth * mouseX) * -1;
			} else {
				imageLeft = (containerWidth / 2) - (imageWidth / 2);
			}
			if ($('#image-zoomer img').css('height') == imageHeight+'px') {
				$('#image-zoomer img').css('top', (imageTop) + 'px');
				$('#image-zoomer img').css('left', (imageLeft) + 'px');
			}
		});
		$('#product-thumbnails a').click(function() {
			$('#image-zoomer img').css('height', thumbHeight+'px').hide(); 
			$('#image-zoomer img').attr('src', $(this).attr('href'));
			return false;
		});
		
		
		// Product Page - Aadditional info tabs
		$("ul#description-menu li a").click(function(){
			var tabIndex = $("ul#description-menu li a").index(this);
			$("ul#description-menu li.active").removeClass("active");
			$(this).parent().addClass("active");
			$("#description-content .wrapper").hide();
			$("#description-content .wrapper").empty();
			var newContent = $("#description-data div:eq("+tabIndex+")").html();
			$("#description-content .wrapper").html(newContent);
			$("#description-content .wrapper").show();
			return false;
		});

		$("table.stores-list tr td a.collapse").click(function(){
            $(this).parent().parent().next().toggle();
			return false;
		});

		$('#calculate-shipping').before('<h2 class="cart">Checkout</h2><p class="intro-text">To continue through the checkout, please enter your delivery details below to select your preferred shipping option(s).</p>');

		$('.cart-shipping-options').append('<p><em><strong>Please note:</strong> Please allow 1-2 days handling prior to Postage for Clearance Items</em></p>');
		
		$('#latest-categories div>ul>li>a').click(function() {
			$('ul', $(this).parent().parent()).animate({height:'hide'});
			$('a', $(this).parent().parent()).removeClass('curr');
			if ($('ul', $(this).parent()).css('display')=='none') {
				$('ul', $(this).parent()).animate({height:'show'});
				$(this).addClass('curr');
			}
			return false;
		});
		
		$('#payment_type_phone').each(function() {
			$(this).parent().html( $(this).parent().html().substr(0, $(this).parent().html().length - 4) );
		});
		$('#payment_type_fax').parent().append(' / Money Order');
		
		
});

// TODO jQuery
function populateAddress(addressType, selectBox, form) {
	selectedId = selectBox.options[selectBox.selectedIndex].value;
	if (selectedId) {
		if (customerAddresses.length) {
			for(x=0; x < customerAddresses.length; x++) {
				if (customerAddresses[x].address_id == selectedId) {
					form.elements[addressType+'_address_firstname'].value = customerAddresses[x].address_firstname;
					form.elements[addressType+'_address_lastname'].value = customerAddresses[x].address_lastname;
					form.elements[addressType+'_address_line_one'].value = customerAddresses[x].address_line_one;
					form.elements[addressType+'_address_line_two'].value = customerAddresses[x].address_line_two;
					form.elements[addressType+'_address_state'].value = customerAddresses[x].address_state;
					if (addressType != 'delivery') {
						form.elements[addressType+'_address_postcode'].value = customerAddresses[x].address_postcode;
						for(y=0; y<form.elements[addressType+'_address_country'].options.length; y++) {
							if (form.elements[addressType+'_address_country'].options[y].value == customerAddresses[x].address_country){
								form.elements[addressType+'_address_country'].selectedIndex = y;			
								break;
							}
						}
					}
					form.elements[addressType+'_address_phone'].value = customerAddresses[x].address_phone;
					break;
				}
			}
		} else if (customerAddresses[0].address_id == selectedId) {
			form.elements[addressType+'_address_firstname'].value = customerAddresses[0].address_firstname;
			form.elements[addressType+'_address_lastname'].value = customerAddresses[0].address_lastname;
			form.elements[addressType+'_address_line_one'].value = customerAddresses[0].address_line_one;
			form.elements[addressType+'_address_line_two'].value = customerAddresses[0].address_line_two;
			form.elements[addressType+'_address_state'].value = customerAddresses[0].address_state;
			if (addressType != 'delivery') {
				form.elements[addressType+'_address_postcode'].value = customerAddresses[0].address_postcode;
				for(y=0; y<form.elements[addressType+'_address_country'].options.length; y++) {
					if (form.elements[addressType+'_address_country'].options[y].value == customerAddresses[0].address_country){
						form.elements[addressType+'_address_country'].selectedIndex = y;			
						break;
					}
				}
			}
			form.elements[addressType+'_address_phone'].value = customerAddresses[0].address_phone;
		}
	}
}

function resetAddressSelect(addressType, form) {
	form.elements[addressType+'_address_id'].selectedIndex = 0;
}
