$(document).ready(function() {
	$("div.affil-box a[@href^=http]").each(function() {
		if(this.href.indexOf(location.hostname) == -1) {
			$(this).click(function(){window.open(this.href);return false;});
		}
	});
	$("#menu-footer a").each(function(){
		if ($(this).text() == 'Download Catalogue') {
			$(this).mousedown(function() { 
				pageTracker._trackPageview('/downloads/catalogue-footer');
			});
		}
	});
	$('#graphic-catalogue-download').click(function(){
		pageTracker._trackPageview('/downloads/catalogue-homepage');
	});
	
	$('#product-media .icon-image img').mouseover(function() {
		url = $(this).attr('src');
		thumburl = url.replace('/icon/', '/thumbnail/');
		zoomurl = url.replace('/icon/', '/display/');
		$('#product-detail-box-img img').attr('src', thumburl);
		$('#product-detail-box-img-zoom img').attr('src', zoomurl);
		$('#product-detail-box-img-zoom img').css('top', 0);
		$('#product-detail-box-img-zoom img').css('left', 0);
		return false;
	});
	$('#product-detail-box-img').click(function() {
		$('#product-detail-box-img-zoom img', this).animate({opacity: 'toggle'}, 200);
		return false;
	});
	$('#product-detail-box-img').mousemove(function(e) {
		offset = $('#product-detail-box-img-zoom').offset();
		thumbheight = 180;
		zoomheight = $('#product-detail-box-img-zoom img', this).height();
		thumbwidth = 180;
		zoomwidth = $('#product-detail-box-img-zoom img', this).width();
		yoffset = thumbheight / (zoomheight - thumbheight) * (e.pageY - offset.top);
		xoffset = thumbwidth / (zoomwidth - thumbwidth) * (e.pageX - offset.left);
		if (yoffset > 180) yoffset = 180; if (yoffset < 0) yoffset = 0;
		if (xoffset > 180) xoffset = 180; if (xoffset < 0) xoffset = 0;
		$('#product-detail-box-img-zoom img', this).css('top', 0 - yoffset);
		$('#product-detail-box-img-zoom img', this).css('left', 0 - xoffset);
	});
	
	$('.cart-shipping-method.error').append(' Please call our Toll Free number 1800 615 666 (Australia only) for further assistance.');
	
});

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;
}

function showProductImage(id, filename) {
	openWindow("/shop/media/display/" + id + "/" + filename, "photo", '420', '500', "resizable=1");
}

function addToCart() {
	var popup = $("div#add-product-popup");
	popup.css("position","absolute");
	popup.css("top",($(window).height()/2)-100);
	popup.css("left",($(window).width()/2)-100);
	popup.css("z-index","100");
	popup.toggle();
	setTimeout('$(\"div#add-product-popup\").fadeOut(2000)', 3000);
}

function hidePopup(element) {
	$("div#"+element).toggle();
}

function openWindow(theURL, winName, w, h, features) {
	windowFeatures = "width=" + w + ",height=" + h + "," + features;
	newWindow = window.open(theURL, winName, windowFeatures);
	newWindow.resizeTo(w, h);
	if (window.focus) { newWindow.focus() }
}

function showImage(url) {
	openWindow("/image.php?" + url + "", "image", '800', '600', "resizable=1");
}
function showVideo(url) {
	openWindow("/video.php?" + url + "", "image", '600', '560', "resizable=1");
}
