$(document).ready(function() {
    
    site.init.init_general();
    site.init.init_cufon();
    site.init.init_forms();
    site.init.init_navigation();
    
    if($.browser.msie) $('#header .breadcrumbs li:first-child, #address li:first-child').addClass('first-child');
    if(typeof DD_belatedPNG != 'undefined') DD_belatedPNG.fix('#intro .image img, #navigation ul li ul');
    
});

var site = {

    init: {
        
        /**
         * General init.
         */
        init_general: function() {
            site.common.parent_height('#intro .products');
            site.common.hover_class('#header .search .button input');
        },
        
        /**
         * Cufon init.
         */
        init_cufon: function() {
            if(typeof Cufon != 'undefined') {
                Cufon.set('fontFamily', 'Helvetica Neue')
                Cufon.replace(
                    '#content h3,'+
                    '#content .products h3,'+
                    '#content .where-to-buy em,'+
                    '#content .bottom h2'
                );
                $('body').addClass('cufon');
            }
        },
        
        /**
         * Init. forms
         */
        init_forms: function() {
            site.common.ghost_label('#header .search input[type=text]');
        },
        
        /**
         * Init. navigation
         */
        init_navigation: function() {
            if($.browser.msie) {
                $('#navigation ul').superfish({ 
                    delay:      500,
                    hoverClass: 'hover',
                    autoArrows: false,
                    animation: {height:'show'},
                    speed: 'fast'
                });
            } else {
                $('#navigation ul').superfish({ 
                    delay:      500,
                    hoverClass: 'hover',
                    autoArrows: false
                });
            }
        }
        
    },
    
    common: {
        
        /**
         * Set element's height to equal its parent's height
         */
        parent_height: function(e) {
            $(e).height($(e).parent().height());
        },
        
        /**
         * Add class on hover
         */
        hover_class: function(e) {
            $(e).hover(
                function() {
                    $(this).addClass('hover');
                },
                function() {
                    $(this).removeClass('hover');
                }
            )
        },
        
        /**
         * Ghost label
         */
        ghost_label: function(elm) {
            elm = $(elm);
            var id = elm.attr("id");
            var name = elm.attr("name");
            var val = elm.prev().html();
            
            /* handle password fields */
            if(elm.hasClass('password')) {
                var newID = id+'-ghost';
                var newHTML = '<input class="password" type="text" name="'+newID+'" id="'+newID+'" />';
                elm.after(newHTML).hide();
                elm = $('#'+newID);
            }
            
            if(elm.val() == '') {
                if (elm.is('textarea')) {
                    elm.html(val);
                }
                else {
                    elm.val(val);
                }
            }
            
            elm.focus(
                function(){
                    if (elm.val() == val) {
                        if(elm.hasClass('password')) {
                            elm.hide();
                            elm = elm.prev();
                            elm.show();
                            elm.focus();
                        }
                        elm.val('');
                        elm.addClass('focus');
                    }
                }).blur(
                    function(){
                        if (elm.val() == '') {
                            if(elm.hasClass('password')) { elm = elm.next(); }
                            elm.val(val);
                            elm.removeClass('focus');
                        }
                    });
        }
    }
}
