// autoTab between form fields 
//
// usage: set class="autotab" on elements, and when they're 'full' it will tab to the next one.

if (typeof Form == "undefined") { var Form = new Object(); }
Form.AutoTab = Class.create();
Form.AutoTab.prototype = {
    
    initialize: function() {
        // $$(".autotab") failed in IE6 when called immediately, so use getElementsByClassName
        var inputs = Element.select(document, ".autotab");
 
        for (var i=0; i < inputs.length; i++) {
            try {
                var el = inputs[i];
                Event.observe(el, "keyup", this.switchField.bindAsEventListener(this), false);
            } catch(e) { }
        }       
    },
    
    switchField: function(e) {
        var el = Event.element(e);
        try {
            if ((el.value.length != el.maxLength) ||  // dont move if the field isnt full
                (e.keyCode == 9) ||                   // or they are pressing 'tab',
                (e.keyCode == 9 && event.shiftKey) || // 'shift-tab'
                (e.keyCode == 16)) { return; }        // or just 'shift'
         
            var inputs = $$(".autotab"); 
            for (var i=0; i < inputs.length; i++) {
                var input = inputs[i];
                if (el == input) {
                    if (inputs[i+1]) {
                         inputs[i+1].focus();
                    }
                }
            }       

        } catch(e) { }
    }
    
}


// utility function to open new windows 
//
// usage: new Window.Open({url:'http://www.yahoo.com/'});

if (typeof Window == "undefined") { var Window = new Object(); }
Window.Open = Class.create();

Window.Open.prototype = {

    initialize: function(params) {

        if (!window.focus) { return true; }

        this.options = Object.extend({
            url: "#",
            width: 300,
            height: 300,
            scrollbars: "1",
            resizable: "1",
            menubar: "0",
            status: "0",
            toolbar: "0",            
            name: ""
        },params || {});

        if (this.options.type) {
            if (this.options.type == "rating") {
                this.options.height = "295";
                this.options.width = "485";
                this.options.scrollbars = "1";
                this.options.name = "rating"; // reuse the same window if it exists
            } else if (this.options.type == "terms") {
                this.options.height = "500";
                this.options.width = "350";
                this.options.scrollbars = "1";
                this.options.name = "terms";
            } else if (this.options.type == "privacy") {
                this.options.height = "800";
                this.options.width = "600";
                this.options.scrollbars = "1";
                this.options.name = "privacy";
            } else if (this.options.type == "catalog") {
                this.options.height = "800";
                this.options.width = "900";
                this.options.scrollbars = "1";
                this.options.menubar = "1";
                this.options.status = "1";
                this.options.toolbar = "1";
                this.options.name = "catalog";
            }
        }

        if (typeof(this.options.url) != "string") {
           this.options.url = this.options.url.href;
        }

        window.open(this.options.url, this.options.name, "width=" + this.options.width + ",height=" + this.options.height + ",scrollbars=" + this.options.scrollbars + ",resizable=" + this.options.resizable + ",menubar=" + this.options.menubar + ",status=" + this.options.status + ",toolbar=" + this.options.toolbar);
    }
}

// generic hover behavior - set a corresponding .hover for each :hover in the css 
// for whatever element you need, then initialize on load for the page.
// you can add additional ones in the initialization of HoverBehavior.
//
// ex. new HoverBehavior('ul.selectMenu li', '#nav li', 'div #something ul' );
//
// http://www.shifteleven.com/articles/2006/12/14/mimicking-the-hover-pseudo-class-in-ie
//
// searching all the rules to setup the class automatically has noticable lag on IE
// so we just make sure to put the rules in the css ourselves

if (typeof UI == "undefined") { var UI = new Object(); }
UI.HoverBehavior = Class.create();
UI.HoverBehavior.prototype = {
    initialize: function() {

        $A(arguments).each( function(arg) {
            $$(arg).each( function(tag) {
                Event.observe(tag, 'mouseover', function() { Element.addClassName(tag, 'hover'); });
                Event.observe(tag, 'mouseout', function() { Element.removeClassName(tag, 'hover'); });
            });
        });

    }
};



// This similar function allows swapping of the image source on mouseover/mouseout
// 
// hook up to elements based on the initialization param, and swap the image from 
// foo_btn.gif to foo-hover_btn.gif
// 
// so hover images need to be named in a consistent fashion.
//
// NOTE: images must be _btn or _bg images.

if (typeof UI == "undefined") { var UI = new Object(); }
UI.ImageSourceHoverBehavior = Class.create();
UI.ImageSourceHoverBehavior.prototype = {
    initialize: function() {

        $A(arguments).each( function(arg) {

            $$(arg).each( function(tag) {
                Event.observe(tag, 'mouseover', function() { 
                    // NOTE: we need to do something better than just try and match on the first underscore in the image path
                    tag.src = tag.src.sub('(.*?)(_bg|_btn|_btn_bg|_bg_btn)(.*?)',function(match) { return match[1] + '-hover' + match[2] + match[3]; }); 
                });
                Event.observe(tag, 'mouseout', function() { 
                    tag.src = tag.src.sub('-hover','', 1); 
                });
            });
        });

    }
};

