/*
 * Menu Navigation
 * v1.0
 */

var menuTimer = null;

function getCenterAlign (opener, openee)
{
	var pageLeft = jQuery('.page').offset().left;
	var openerCentre = opener.offset().left-pageLeft+opener.outerWidth()/2;
	var openeeWidth = openee.outerWidth();
	
	return Math.max(0, openerCentre-(openeeWidth/2));
}

var menuObject = function(openers, openees, alignment, viewpx, hidepx) {
    this.openers = openers;
    this.openees = openees;
    this.alignment = alignment || 'left';
    this.viewpx = viewpx || '0px';
    if (alignment == 'centreopener') {
       	this.viewpx = getCenterAlign(jQuery(openers),jQuery(openees));
    }
    this.hidepx = hidepx || '-2000px';
    this.openTimer = null

    var temp = this;
    jQuery(openees + ',' + openers).hover(
    function() {
        temp.openMenu();
    },
    function() {
        temp.closeMenu();
    }
    );
}

menuObject.prototype = {
    openMenu: function() {
        if (this.openTimer) {
            clearTimeout(this.openTimer);
        }
        var temp = this;
        jQuery(this.openees).fadeIn(0);
        switch (this.alignment) {
        case 'centreopener':
        case 'left':
            jQuery(this.openees).css({
                left: temp.viewpx,
                right: 'auto'
            });
            break;
        case 'right':
            jQuery(this.openees).css({
                right: temp.viewpx,
                left: 'auto'
            });
            break;
        }
    },

    closeMenu: function() {
        if (this.openTimer) {
            clearTimeout(this.openTimer);
        }
        var temp = this;
        this.openTimer = setTimeout(function() {
            jQuery(temp.openees).fadeOut(150);
        },
        200)
    }
}

jQuery(document).ready(function() {
      
	var cat = new menuObject('#category-menu-node', '#category-menu', 'centreopener');
	var help = new menuObject('#help-menu-node', '#help-menu', 'centreopener');
    var bag = new menuObject('#shopping-bag-link, #shopping-bag-items', '#shopping-bag-popup', 'right');

	// Fix widths of product labels on product grid view
	jQuery('table.products-grid td a div').each(function() {
		var width = jQuery(this).find('span').outerWidth();
		jQuery(this).width(width + 1);
	});
});


if (!window.WS)
    var WS = new Object();
  
WS.emptyForm = Class.create();
WS.emptyForm.prototype = {
    initialize : function(form, field, emptyText, emptyClass){
        this.form   = $(form);
        this.field  = $(field);
        this.emptyText = emptyText;
        this.emptyClass = emptyClass;

        Event.observe(this.form,  'submit', this.submit.bind(this));
        Event.observe(this.field, 'focus', this.focus.bind(this));
        Event.observe(this.field, 'blur', this.blur.bind(this));
        this.blur();
    },

    submit : function(event){
        if (this.field.value == this.emptyText || this.field.value == ''){
            Event.stop(event);
            return false;
        }
        return true;
    },

    focus : function(event){
        if(this.field.value==this.emptyText){
            this.field.value='';
            this.field.removeClassName(this.emptyClass);
        }
    },

    blur : function(event){
        if(this.field.value==''){
            this.field.value=this.emptyText;
            this.field.addClassName(this.emptyClass);
        } else {
        	this.field.removeClassName(this.emptyClass);
        }
    },

    initAutocomplete : function(url, destinationElement){
        new Ajax.Autocompleter(
            this.field,
            destinationElement,
            url,
            {
                paramName: this.field.name,
                minChars: 2,
                updateElement: this._selectAutocompleteItem.bind(this),
                onShow : function(element, update) { 
                    if(!update.style.position || update.style.position=='absolute') {
                        update.style.position = 'absolute';
                        Position.clone(element, update, {
                            setHeight: false, 
                            offsetTop: element.offsetHeight
                        });
                    }
                    Effect.Appear(update,{duration:0});
                }

            }
        );
    },

    _selectAutocompleteItem : function(element){
        if(element.title){
            this.field.value = element.title;
        }
        this.form.submit();
    }
}