function getHeight(element) {
	var heightValue = jQuery(element).css("height");
	var pattern = new RegExp("^[0-9]+");
	var result = heightValue.match(pattern);
	return new Number(result[0]);
}


function submitOnEnter(field, event) {
    var keycode;
    if (window.event) {
        keycode = window.event.keyCode;
    } else if (event) {
        keycode = event.which;
    } else {
        return true;
    }

    if (keycode == 13) {
        field.form.submit();
        return false;
    } else {
        return true;
    }
}

function loadProductGallery(url, offsetVar, directionVar) {
	jQuery.get(url, {offset:offsetVar, direction:directionVar}, function(data) {
		// preload images
		jQuery(data).find("img").each(function() {
				var image = new Image();
				image.src = this.src;
			});
		jQuery("#productGalleryContainer").fadeOut("normal", function() {
			jQuery("#productGalleryContainer").html(data);
			jQuery("img.rollover").hover(
    				 function() { this.src = this.src.replace("_off","_on"); },
    				 function() { this.src = this.src.replace("_on","_off"); }
    			);
    			jQuery("#tp_button_left").click(function() {
	    			var offset = jQuery(this).attr("class");
	    			loadProductGallery(offset, "left");
    			});
    			jQuery("#tp_button_right").click(function() {
    				var offset = jQuery(this).attr("class");
    				loadProductGallery(offset, "right");
    			});
		});
		jQuery("#productGalleryContainer").fadeIn("normal");
	});
}

function adjustRightPane() {
    var hasRightPane = jQuery("#rightPaneDiv").length;
    if (hasRightPane == 1) {
    	var cpHeight = getHeight(jQuery("#contentPaneDiv"));
    	var rpHeight = getHeight(jQuery("#rightPaneDiv"));
    	if (cpHeight > rpHeight) {
    		var calculatedHeight = cpHeight - rpHeight;
    		jQuery("#rightPaneDiv").css("height", cpHeight);
    		jQuery("#scSpacer").css("height", calculatedHeight);
    	}
    }
}

function initRollovers() {
//	jQuery("img.rollover").each(function() {
//		alert("src= " + this.src);
    	jQuery("img.rollover").hover(
				 function() { this.src = this.src.replace("_off","_on"); },
				 function() { this.src = this.src.replace("_on","_off"); }
		);
//	});
}

function initGalleryButtons(url) {
		jQuery("#tp_button_left").click(function() {
			var offset = jQuery(this).attr("class");
			loadProductGallery(url, offset, "left");
		});
		jQuery("#tp_button_right").click(function() {
			var offset = jQuery(this).attr("class");
			loadProductGallery(url, offset, "right");
		});
}

function decreaseQuantity(itemId) {
	var qtyInput = jQuery("#sc_qty_val_" + itemId);
	var qty = new Number(qtyInput.val());
	qty = qty - 1;
	if (qty < 0) {
		qty = 1;
	}
	qtyInput.val(qty);
	qtyInput.change();
}

function increaseQuantity(itemId) {
	var qtyInput = jQuery("#sc_qty_val_" + itemId);
	var qty = new Number(qtyInput.val());
	qty = qty + 1;
	qtyInput.val(qty);
	qtyInput.change();
}

