 jQuery(document).ready(function() {
        //alert('dom ready');
        jQuery('.buybutton').each(function(idx) {
            jQuery(this).hover(
                function() {
                    var txt = jQuery(this).parent().find('.buyqty');
                    var preval = txt.val();
                    //alert(preval);
                    if (preval == "0") {
                        txt.addClass('autobuyone');
                        txt.val("1");
                    }
                },
                function() {
                    var txt = jQuery(this).parent().find('.buyqty');
                    if (txt.hasClass('autobuyone')) {
                        txt.removeClass('autobuyone');
                        txt.val("0");
                    }
                }
            );
        });

	var bulktip_top = new YAHOO.widget.Tooltip("bulktip_top", { 
		context: "bulkaddbutton_top", 
		text: "",
		showDelay: 500
	});
	var bulktip_bottom = new YAHOO.widget.Tooltip("bulktip_bottom", { 
		context: "bulkaddbutton_bottom", 
		text: "",
		showDelay: 500
	});
		
		
	jQuery('#bulkaddbutton_top').hover(
		function() {
			var totalqty = 0;
			jQuery('.buyqty').each(function(idx) {
				var v = parseInt(jQuery(this).val(),10);
				if (!v.isNaN) {
					totalqty += v;
				}
			});
			if (totalqty > 0) {
				bulktip_top.cfg.setProperty("text","Add the " + totalqty + " units below to your basket")
			} else {
				bulktip_top.hide();
				bulktip_top.cfg.setProperty("text","There are no item quantities specified below.")
			}
		},
		function() {
			bulktip_top.cfg.setProperty("text","");
		}
	);
	jQuery('#bulkaddbutton_bottom').hover(
		function() {
			var totalqty = 0;
			jQuery('.buyqty').each(function(idx) {
				var v = parseInt(jQuery(this).val(),10);
				if (!v.isNaN) {
					totalqty += v;
				}
			});
			if (totalqty > 0) {
				bulktip_bottom.cfg.setProperty("text","Add the " + totalqty + " units above to your basket")
			} else {
				bulktip_bottom.hide();
				bulktip_bottom.cfg.setProperty("text","There are no item quantities specified above.")
			}
		},
		function() {
			bulktip_bottom.cfg.setProperty("text","");
		}
	);
	
	var fn_bulkaddbutton_click = function(e) {
		var totalqty = 0;
		jQuery('.buyqty').each(function(idx) {
			var v = parseInt(jQuery(this).val(),10);
			if (!v.isNaN) {
				totalqty += v;
			}
		});
		if (totalqty == 0) {
			e.preventDefault();
		}
	}
	jQuery('#bulkaddbutton_top').click(fn_bulkaddbutton_click);
	jQuery('#bulkaddbutton_bottom').click(fn_bulkaddbutton_click);

		
    });
	
