/* $Id$ */

// stores which is the currently displayed menu
var current_menu = -1;
// stores the menu timeout which is set/cleared when over/out
var menu_timeout = false;
// constants for product variations array
var VARIATION_PRICE = 0;
var VARIATION_PRICE_ORIGINAL = 1;
var VARIATION_CAN_ORDER = 2;
var VARIATION_AVAILABILITY = 3;
var VARIATION_COLOUR = 4;
var VARIATION_COLOUR_DISPLAY = 5;
// constant for index of outlet in nav array
var NAVHTML_OUTLET = 5;

function navover(i) {

	clearTimeout(menu_timeout);
	
	// if the menu that's currently showing is the one passed in, don't need to do anything so return
	if(current_menu == i) {
		return;
	}
	// if a menu is currently visible then hide it
	if(current_menu > -1) {
		$("#menu"+current_menu).fadeOut('fast');
	}

	if(i == NAVHTML_OUTLET) {
		var nav_i = "#routlet";
	}
	else {
		var nav_i = "#nav"+i;
	}
	// calculate offset and reset it each time we show the menu
	// that way if the window is resized the menus still appear correctly
	var offset = $(nav_i).offset();
	// center the popup menu under the navigation item
	var x = offset.left + Math.round(($(nav_i).width() - $("#menu"+i).width()) / 2);
	// and drop it just below the menu text
	var y = offset.top + $(nav_i).height() + 3;
	// position it
	$("#menu"+i).css('left', x);
	$("#menu"+i).css('top', y);
	// and now show the new menu
	$("#menu"+i).fadeIn('fast');
	// add remember which one is showing
	current_menu = i;
	
}

function navout() {

	menu_timeout = setTimeout('hidenav()', 200);
	
}

function hidenav() {
	
	$("#menu"+current_menu).fadeOut('fast');
	current_menu = -1;
	
}

function hidenavnow() {
	
	$("#menu"+current_menu).hide();
	current_menu = -1;
	
}

function nav_init() {

	// load the nav from the cached javascript file/array into divs
	// then center each under the appropriate nav item

	// if there's no nav in the page (e.g. it's a popup etc) then return now
	if(!document.getElementById('nav')) {
		return;
	}

	for(var i = 0; i < navhtml.length; i++) {
		
		// add the menu div to the html
		$("body").append("<div id='menu" + i + "' class='menu' style='display: none;'></div>");
		// add the content to it
		$("#menu"+i).html(navhtml[i]);
		// add hover handler for the menu
		$("#menu"+i).hover(
			function() {
				// using i from the loop doesn't work, so extract it from the ele's id
				navover(this.id.substr(4));
			},
			function() {
				navout();
			}
		);
		// hover for navitem in the nav
		$("#nav"+i).hover(
			function() {
				// using i from the loop doesn't work, so extract it from the ele's id
				navover(this.id.substr(3));
			},
			function() {
				navout();
			}
		);
		
	}
	
	// hover for outlet nav
	$("#routlet").hover(
		function() {
			navover(NAVHTML_OUTLET);
		},
		function() {
			navout();
		}
	);

}

var productbox_clicked = false;

function product_show(url) {
	
	// if the productcontainer and product box haven't yet been created then create them
	if(!document.getElementById('productcontainer')) {
		if(typeof document.body.style.maxHeight === "undefined") {  // if true then this is IE6.0
			$("body").append("<div id='productbox'></div><div id='productcontainer'></div>");
		}
		else {
			$("body").append("<div id='productcontainer'><div id='productbox'></div></div>");
			$("#productbox").click(function() { productbox_clicked = true; });
		}
		// if they click the container then we want to hide the product popup
		$("#productcontainer").click(product_hide);
	}

	document.onkeydown = function(e) {
		if (e == null) { // ie
			keycode = event.keyCode;
		}
		else { // others
			keycode = e.which;
		}
		if(keycode == 27) {
			product_hide();
		}
	};
	
	// if it's IE 6.0 then do the necessary fixes
	if (typeof document.body.style.maxHeight === "undefined") { // if true then this is IE6.0
		$("html", "body").css({ width : "100%", height: "100%" });
		$("body").css("overflow", "hidden");
		$("#productcontainer").css("top", document.body.scrollTop + "px");
		$("#productbox").css("top", document.body.scrollTop + "px");
	}
	else {
		// need to recalc the position each time the box is displayed in case of window resize
		// no need to do it for IE6 because the CSS does it
		var win_height = parseInt($(window).height());
		// we center based on 450 height, because that's about the average size
		var my_height = 450;
		var margin = parseInt((win_height - my_height) / 2 - 20);
		if(margin < 0 || isNaN(margin)) margin = 0;
		$('#productbox').css('margin-top', margin + 'px');
	}

	// clear the html from the productbox
	$("#productbox").html('');
	
	// show the loading gif
	set_productbox_background(true);
	
	// and execute the ajax, hiding the loading gif when done
	$.ajax({
		url: url+'?ajax', // adding this query string isn't necessary for the site code, but is required so a request for the normal page doesn't get cached and used for the ajax request
		cache: true,
		error: function() {
			set_productbox_background(false);
			$("#productbox").html("<p style='padding:20px;'>There was an error loading the product information.<br />Please try again.<br /><br /><a href='javascript:product_hide()'>Close</a></p>");
		},
		success: function(html) {
			set_productbox_background(false);
			$("#productbox").html(html);
			jkzoom_init();
		}
	});
	
	// show the container and box now; the ajax call is async so this will happen while waiting
	$("#productcontainer").show();
	$("#productbox").show();
	
	return false;
	
}

function set_productbox_background(loading) {
	
	if(loading) {
		$("#productbox").css({
			'background-image': 'url(/images/gui/loading.gif)',
			'background-position': 'center center',
			'background-repeat': 'no-repeat'
		});
	}
	else {
		$("#productbox").css({
			'background-image': 'url(/images/gui/dots.gif)',
			'background-repeat': 'repeat'
		});
	}
	
}

function product_hide() {

	if (typeof document.body.style.maxHeight === "undefined") { // if true then this is IE6.0
		$("html", "body").css({ width : "auto", height: "auto" });
		$("body").css("overflow", "");
	}
	else if(productbox_clicked) {
		// if #productbox is clicked it also triggers #productcontainer.onclick
		// productbox_clicked is set to true when box is clicked so we know not
		// to close the popup; set it back to false here so clicking container
		// will still close it
		productbox_clicked = false;
		return;
	}

	document.onkeydown = "";
	
	$("#productbox").hide();
	$("#productcontainer").hide();
	
}

function swatch_click(colour) {

	var currently_selected, i, size, first;
	var currently_selected_matched = false;
	
	for(mycolour in variations) {
		if(mycolour == colour) {
			$('#s_'+mycolour).parent().attr('class', 'swatch selected');
		}
		else {
			$('#s_'+mycolour).parent().attr('class', 'swatch');
		}
	}
	
	for(i = 0; i < sizes.length; i++) {
		if($('#s_'+sizes[i]).attr('class') == 'selected') {
			currently_selected = sizes[i];
		}
		$('#s_'+size_ids[sizes[i]]).attr('class', 'unselectable');
	}
	for(size in variations[colour]) {
		if(variations[colour][size][VARIATION_CAN_ORDER] == 1) {
			$('#s_'+size_ids[size]).attr('class', '');
		}
		if(size == currently_selected) {
			currently_selected_matched = true;
		}
		if(!first) {
			first = size;
		}
	}
	if(!currently_selected_matched) {
		currently_selected = first;
	}
	
	current_colour = colour;
	current_size = currently_selected;

	if(variations[current_colour][current_size][VARIATION_CAN_ORDER] == 1) {
		$('#s_'+size_ids[current_size]).attr('class', 'selected');
	}
	else {
		$('#s_'+size_ids[current_size]).attr('class', 'selected unselectable');
	}
	
	$('#pimage').attr({ 
		src: image_http + '/' + images[current_colour][0], 
		width: images[current_colour][1], 
		height: images[current_colour][2] 
	});
	
	if(zooms[current_colour]) {
	
		$('#zoombig img').attr({ 
			src: image_http + '/' + zooms[current_colour][0], 
			width: zooms[current_colour][1], 
			height: zooms[current_colour][2] 
		});

	}
	else {
		
		$('#zoombig img').attr({ 
			src: '', 
			width: 0, 
			height: 0 
		});

		
	}
		
	update_price_etc();
	jkzoom_init();
	
}

function update_price_etc() {

	if(variations[current_colour][current_size][VARIATION_PRICE] == variations[current_colour][current_size][VARIATION_PRICE_ORIGINAL]) {
		$('#poriginal').text('$' + variations[current_colour][current_size][VARIATION_PRICE].toFixed(2));
		$('#pprice').text('');
		$('#pprice').attr('class', '');
	}
	else {
		$('#poriginal').text('$' + variations[current_colour][current_size][VARIATION_PRICE_ORIGINAL].toFixed(2));
		$('#pprice').text('$' + variations[current_colour][current_size][VARIATION_PRICE].toFixed(2));
	}
	
	$('#pcolour').text(variations[current_colour][current_size][VARIATION_COLOUR_DISPLAY]);
	$('#psize').text(current_size);
	$('#pavailability').text(variations[current_colour][current_size][VARIATION_AVAILABILITY]);
	
	document.productform.colour.value = variations[current_colour][current_size][VARIATION_COLOUR];
	document.productform.size.value = current_size;
	
}

function size_click(size_id) {
	
	var size = false;
	
	for(var index in size_ids) {
		if(size_ids[index] == size_id) {
			size = index;
			//size_id = size;
			break;
		}
	}
	
	if(size && variations[current_colour][size]) {
	
		if(variations[current_colour][current_size][VARIATION_CAN_ORDER] == 1) {
			$('#s_'+size_ids[current_size]).attr('class', '');
		}
		else {
			$('#s_'+size_ids[current_size]).attr('class', 'unselectable');
		}
		current_size = size;
		if(variations[current_colour][current_size][VARIATION_CAN_ORDER] == 1) {
			$('#s_'+size_id).attr('class', 'selected');
		}
		else {
			$('#s_'+size_id).attr('class', 'selected unselectable');
		}
		update_price_etc();

	}
		
}

function do_product_links(style) {

	$("#product .swatch a").click(
		function() {
			swatch_click($(this).attr("id").substr(2));
			return false;
		}
	);
	
	$("#product #sizes a").click(
		function() {
			size_click($(this).attr("id").substr(2));
			return false;
		}
	);
	
	$("#addtobag").click(
		function() {
			add_to_bag(false, false, false, false);
			return false;
		}
	);

	$("#addtolist").click(
		function() {
			add_to_list();
			return false;
		}
	);

	if(window.products && window.products.length > 1) {
		var myindex = -1;
		for(var i = 0; i < window.products.length; i++) {
			if(products[i] == style) {
				myindex = i;
				break;
			}
		}
		if(myindex > 0) {
			$("#pprev").html("<a href='javascript:void(0)' onclick='product_show(\"/product/" + products[(i-1)] + window.url_suffix + "\")'><img src='/images/gui/previous.png' width='123' height='16' alt='previous item' /></a><br />");
		}
		if(myindex > -1 && myindex < window.products.length - 1) {
			$("#pnext").html("<a href='javascript:void(0)' onclick='product_show(\"/product/" + products[(i+1)] + window.url_suffix + "\")'><img src='/images/gui/next.png' width='123' height='16' alt='next item' /></a><br />");
		}
	}
	
	$("#gotobag").html("<input type='button' value='Go to Shopping Bag' onclick='window.location=http+\"bag\"' />");	
	
}

function add_to_bag(style, size, colour, quantity) {

	var referrer = '';
	var bag = 0; // whether or not to update the bag in the page
	
	if(style == false) {
		
		style = document.productform.style.value;
		size = document.productform.size.value;
		colour = document.productform.colour.value;
		quantity = document.productform.quantity.value;
		referrer = document.productform.direct_referrer.value;
		bag = document.getElementById('bag') ? 1 : 0;

		if(variations[current_colour][current_size][VARIATION_CAN_ORDER] == 0) {
			window.alert("This colour/size combination is no longer available.\nPlease try another colour/size combination.");
			return;
		}
		if(variations[current_colour][current_size][VARIATION_AVAILABILITY] != 'Now') {
			if(!window.confirm("This item is due for despatch on " + variations[current_colour][current_size][VARIATION_AVAILABILITY] + "\nClick OK to order or Cancel to not order.")) {
				return;
			}
		}
	
	}
	
	$.ajax({
		url: '/bag/add',
		data: {
		 	style: style,
			size: size,
			colour: colour,
			quantity: quantity,
			bag: bag,
			direct_referrer: referrer
		},
		cache: false,
		type: 'post',
		dataType: 'json',
		error: function() {
			window.alert('There was an error adding this item to your shopping bag.\nPlease try again.');
		},
		success: function(json) {
			$("#ribbonsubtotal").text("$" + json.subtotal);
			if(json.items == 1) {
				$("#ribbonitems").text("1 Item");
			}
			else {
				$("#ribbonitems").text(json.qty + " Items");
			}
			if(document.getElementById('bag')) {
				$("#bag").html(json.bag);
			}
			window.alert("This item has been added to your shopping bag");
		}
	});

}

function add_to_list() {

	$.ajax({
		url: '/wishlist/add',
		data: {
		 	style: document.productform.style.value,
			size: document.productform.size.value,
			colour: document.productform.colour.value
		},
		cache: false,
		type: 'post',
		dataType: 'json',
		error: function() {
			window.alert('There was an error adding this item to your wish list.\nPlease try again.');
		},
		success: function(json) {
			if(json.success == '1') {
				window.alert("This item has been added to your wishlist");
			}
			else {
				window.alert(json.message);
			}
		}
	});
	
}

function add_from_list(inputname) {

	var index = inputname.substr(4);
	var style = document.wishlist.elements['style_'+index].value;
	var size = document.wishlist.elements['size_'+index].value;
	var colour = document.wishlist.elements['colour_'+index].value;
	var quantity = document.wishlist.elements['qty_'+index].value;
	
	add_to_bag(style, size, colour, quantity);
	
}

function set_ribbon_height(timeout) {

	// get the height of #right
	
	var h_right = $("#right").height();
	
	// and of the window, applying an offset for comparison/setting below
	// also add the scroll top to it so as the user scrolls it can grow
	
	var h_window = $(window).height() - 270 + $(window).scrollTop();
	
	// top of ribbon ul is 40px from top and we need to subtract the height of it
	// this gives the maximum amount of padding we can use
	
	var max = h_right - 40 - $("#ribbon ul").height();
	
	// initialise padding
	
	var padding = 0;
	
	// and do comparisons to work out what it should be
	
	if(max > h_window) {
		padding = h_window;
	}
	else {
		padding = max;
	}
	
	if(padding < 0) {
		padding = 0;
	}

	// and finally apply the padding
	
	$("#ribbon ul").css('padding-top', padding);

	// this timeout is for konqueror which fails to set the scrollTop correctly
	// within this function while scrolling; the timeout calls this function again
	// after the scrollTop will have had time to set to the correct amount
	// it has no perceivable affect in the other browsers
	
	if(timeout) {
		setTimeout("set_ribbon_height(false)", 500);
	}
	
}

// quickshop functions

var qs_currently_selected = -1;
//var qs_value;
var qs_values = new Array('', '', '', '', '');

function qs_init() {
	
	if(document.getElementById('qs_0')) {
	
		$(".qs_style").focus(
			function() {
				var id = $(this).parent().parent().attr('id').substr(3);
				qs_currently_selected = id;
			}
		);
		
		$(".qs_style").blur(
			function() {
				qs_blur(this);
			}
		);
	
		$(".qs_style").keypress(
			function(e) {
				if(e.which == 13) {
					var id = '#qs_'+(qs_currently_selected)+' .qs_style';
					$(id).blur();
					qs_currently_selected++;
					id = '#qs_'+(qs_currently_selected)+' .qs_style';
					$(id).focus();
					$(id).select();
				}
			}
		)

	}
		
}

function qs_blur(qs) {
	
	var index = $(qs).parent().parent().attr('id').substr(3);
	var id = '#qs_'+index;
	var style = $(id+' .qs_style').attr('value');

	// if they've clicked into the box, made no change and clicked out, don't reload the item
	if(style == qs_values[index]) return;
	
	qs_currently_selected = index;
	if(!style) return;
	
	qs_values[index] = style;
	
	$(id+' .qs_right').html('Loading...');
	$(id+' .qs_image').html('&nbsp;');
	
	set_ribbon_height(false);
	
	$.ajax({
		url: 'quickshop/product/style/' + escape(style) + '/index/' + index,
		cache: false,
		dataType: 'json',
		error: function() {
			set_productbox_background(false);
			$(id+' .qs_right').html('<span class="warning">There was an error loading this product.<br />Please try again shortly.</span>');
			set_ribbon_height(false);
		},
		success: function(json) {
			
			$(id+' .qs_style').attr('value', json.style);
			$(id+' .qs_image').html(json.image);
			$(id+' .qs_right').html(json.right);
			
			qs_values[index] = json.style;
			
			var last = $('.qs_style:last').parent().parent().attr('id').substr(3);
			if((last-1) == index || last == index) {
				for(var i = 1; i < 4; i++) {
					$("#qs_"+last).after('<tr id="qs_' + (last+i) + '" valign="top"><td class="center"><input name="style_' + (last+i) + '" autocomplete="off" class="qs_style" type="text" value="" size="10" /></td><td class="qs_image">&nbsp;</td><td class="qs_right">&nbsp;</td></tr>');
				}
				qs_init();
			}

			set_ribbon_height(false);
			
		}
	});

}

// called from the "Remove" link in the "Selection" column
function qs_clear(id) {

	id = '#qs_' + id;
	
	$(id+' .qs_style').attr('value', '');
	$(id+' .qs_image').html('&nbsp;');
	$(id+' .qs_right').html('&nbsp;');
	
	set_ribbon_height(false);
	
}

// set up the ribbon

function ribbon_init() {
	
	if(document.getElementById('right')) {
	
		set_ribbon_height(false);
		
		$(window).scroll(
			function() {
				set_ribbon_height(true);
			}
		);
		$(window).resize(
			function() {
				set_ribbon_height(false);
			}
		);
		
	}
		
}

// init the popup window with products
function ppop_init() {

	if(!document.getElementById('popproduct')) {
		return;
	}

	var myheight = parseInt($('#popcontentclose').height()) + parseInt($("#pimage").height());
	
	if(window.outerHeight && document.documentElement && document.documentElement.clientHeight) {
	
		var chrome_h = window.outerHeight - document.documentElement.clientHeight;
		window.resizeTo(720, myheight + chrome_h);
	
	}
	else if (typeof document.body.style.maxHeight === "undefined") { // if true then this is IE6.0
		
		window.resizeTo(720, 570);
		var chrome_h = 570 - document.body.clientHeight;
		window.resizeTo(720, myheight + chrome_h);
		
	}
	else if(document.documentElement && document.documentElement.clientHeight) {
	
		window.resizeTo(720, 570);
		var chrome_h = 570 - document.documentElement.clientHeight;
		window.resizeTo(720, myheight + chrome_h);
		
	}
	
}

// product zoom stuff

var zoomsmall_left = 0;
var zoomsmall_top = 0;
var zoomsmall_right = 0;
var zoomsmall_bottom = 0;

var zoomindicator = document.getElementById('zoomindicator');

var zoomindicator_width = 67;
var zoomindicator_height = 67;
var zoomindicator_x_offset = 33; // half of zoomindicator_width  } gets the mouse in the middle
var zoomindicator_y_offset = 33; // half of zoomindicator_height } of the zoom indicator

var zoom_scaleX = 0;
var zoom_scaleY = 0;

var zoombig_visible = false;

function jkzoom_calculate_zoomsmall() {

	// the scroll values are needed when on a product list page; if the page is scrolled
	// down or to the right then we need to adjust the mouseover area by subtracting the
	// scroll pixels off the left/right position
	
	// however! it works fine in FF without the scroll values <sigh>
	
	var scroll_x = 0;
	var scroll_y = 0;
	
	if(document.getElementById('productbox')) {
		scroll_x = parseInt($(document).scrollLeft());
		scroll_y = parseInt($(document).scrollTop());
	}
	
	zoomsmall_left = $('#zoomsmall').offset().left;
	zoomsmall_top = $('#zoomsmall').offset().top;
	zoomsmall_right = zoomsmall_left + $('#zoomsmall').width();
	zoomsmall_bottom = zoomsmall_top + $('#zoomsmall').height();
	
}

function jkzoom_init() {

	// if we're not showing a zoomable image, OR there is no image
	// OR it's safari earlier than 3.0 (zooming crashes it)
	// then return
	
	if(!document.getElementById('pimage') || !document.getElementById('zoomsmall') || $('#zoombig_img').attr('src') == '' || (webkit_version && webkit_version < 500)) {
		jkzoom_helptext(false);
		document.onmousemove = '';
		window.onresize = '';
		return;
	}

	if(document.getElementById('productbox')) {
		zoomindicator_width = 67;
		zoomindicator_x_offset = 33;
	}
	else {
		zoomindicator_width = 50;
		zoomindicator_x_offset = 25;
	}
	
	$('#zoombig').width(
		parseInt($("#product .right").width())
		+ parseInt($("#product .right").css('padding-left'))
		+ parseInt($("#product .right").css('padding-right'))
	);
	
	$('#zoombig').height($("#product .right").height());
	
	$('#zoomsmall').width(parseInt($('#pimage').width()));

	zoom_scaleX = parseInt($('#zoombig img').css('width')) / $('#pimage').width();
	zoom_scaleY = parseInt($('#zoombig img').css('height')) / $('#pimage').height();
	$("#pimage").attr('alt', '');

	jkzoom_helptext(false);
	jkzoom_calculate_zoomsmall();
	document.onmousemove = jkzoom_mousemove;
	window.onresize = jkzoom_calculate_zoomsmall;
	
}

function jkzoom_helptext(mouse_is_over) {

	$('#zoomhelp').show();
	
	if($('#zoombig_img').attr('src') == '') {
		$('#zoomhelp').hide();
	}
	else if(mouse_is_over) {
		$('#zoomhelp').html('Move your mouse off the image to show the product details and to order.');
	}
	else {
		$('#zoomhelp').html('Move your mouse over the image to zoom in or use the links below for other images.');
	}
	
}

function jkzoom_mousemove(e) {

	if(typeof event != 'undefined') {
		var x = event.clientX + parseInt($(document).scrollLeft());
		var y = event.clientY + parseInt($(document).scrollTop());
	}
	else {
		var x = e.pageX;
		var y = e.pageY;
	}

	if(x >= zoomsmall_left && x <= zoomsmall_right && y >= zoomsmall_top && y <= zoomsmall_bottom) {

		zoomindicator_left = x - zoomindicator_x_offset;
		if(zoomindicator_left < zoomsmall_left) {
			zoomindicator_left = zoomsmall_left;
		}
		else if(zoomindicator_left + zoomindicator_width > zoomsmall_right) {
			zoomindicator_left = zoomsmall_right - zoomindicator_width;
		}
		
		zoomindicator_top = y - zoomindicator_y_offset;
		if(zoomindicator_top < zoomsmall_top) {
			zoomindicator_top = zoomsmall_top;
		}
		else if(zoomindicator_top + zoomindicator_height > zoomsmall_bottom) {
			zoomindicator_top = zoomsmall_bottom - zoomindicator_height;
		}

		if (typeof document.body.style.maxHeight === "undefined") { 
			// don't show the indicator for IE6 because it's really jerky
			// make the cursor a crosshair instead
			$('#zoomindicator').css('cursor', 'crosshair');
			$('#zoomsmall').css('cursor', 'crosshair');
		}
		else {
		
			$("#zoomindicator").show();
			
			// if we're in the popup then need to subtract scroll from left/top
			// don't adjust the zoomindicator_left/top values as they are used
			// further down and expect to be not adjusted
			
			if(document.getElementById('productbox')) {
				$("#zoomindicator").css({
					left: zoomindicator_left - parseInt($(document).scrollLeft()),
					top: zoomindicator_top - parseInt($(document).scrollTop())
				});
			}
			else {
				$("#zoomindicator").css({
					left: zoomindicator_left,
					top: zoomindicator_top
				});
			}

		}
		
		if(!zoombig_visible) {
			// do these only when going from !visible to visible so less processing
			// required and less jerkiness in IE
			$("#zoombig").show();
			jkzoom_helptext(true);
			// this is required for opera to work...
			// it cannot calculate the scale while zoombig is not visible
			zoom_scaleX = parseInt($('#zoombig img').css('width')) / $('#pimage').width();
			zoom_scaleY = parseInt($('#zoombig img').css('height')) / $('#pimage').height();
		}
		zoombig_visible = true;

		new_left = -Math.round((zoomindicator_left - zoomsmall_left) * zoom_scaleX) + 'px';
		new_top = -Math.round((zoomindicator_top - zoomsmall_top) * zoom_scaleY) + 'px';
		
		$("#zoombig_img").css({
			left: new_left,
			top: new_top
		});
		
	}
	else {
		$("#zoombig").hide();
		zoombig_visible = false;
		$("#zoomindicator").hide();
		jkzoom_helptext(false);
	}
	
}


// safari before 2.0.4 (419.3) will load jquery and attempt to work,
// but crashes whenever ajax stuff is done, and doesn't render the
// top nav correctly. all other browsers that can load jquery seem
// to work ok, so this test just checks to make sure it's not a bad
// version of safari

function browser_supported() {
	
	return webkit_version == 0 || webkit_version >= 419.3;
	
}

function get_webkit_version() {
   
	var version = 0;
	
	var regexp = /Safari\/([\d.]+)/;
	var result = regexp.exec(navigator.userAgent);
	
	if(result) {
		version = parseFloat(result[1]);
	}
	
	return version;
   
}

var webkit_version = get_webkit_version();


// get everything in the page ready

$(document).ready(function(){

	if(!browser_supported()) {
		return;
	}
	
	nav_init();
	qs_init();
	ribbon_init();
	ppop_init();
	
	if(webkit_version) {
		// in webkit based browsers we need to wait a very short period
		// before the jkzoom_init() function works as intended; without
		// this nothing will show until the swatch is clicked on a regular
		// product page. it's fine on the popups though.
		setTimeout("jkzoom_init()", 50);
	}
	else {
		jkzoom_init();
	}

	$("#checkoutdelivery tr:last").hide();
	$("#checkoutdelivery input[type=radio]").click(
		function() {
			// only submit the form if the radio option has changed
			if($(this).attr('checked') != $(this).attr('defaultChecked')) {
				document.checkoutform.deliverybtn.click();
			}
		}
	);
	
	// safari < 3 crashes when we open the popup so don't show for them
	if(!webkit_version || webkit_version >= 500) {
		
		$(".listproduct a").click(
			function() {
				if($(this).attr('href').substr(0, 7) == '/admin/') {
					return true;
				}
				else {
					product_show($(this).attr('href'));
					return false;
				}
			}
		);
	
		$(".viewitem").click(
			function() {
				product_show($(this).attr('href'));
				return false;
			}
		);

	}
		
	$("#promocodetext").keypress(function(e) {
		if(e.which == 13) {
			$("#promocodebtn").click();
		}
	});

	$(".wishlistadd").click(function() {
		add_from_list($(this).attr('name'));
		return false;
	});
	
});
