window.BROYHILL = window.BROYHILL = {};

(function() {
    var Broyhill = {

        // storage for additional things to be run at page init
        //initItems: [],

        // yeah, this is to store carousels, if we so desire.
        // guessing i'll remove this soon.
        carousels: {},

        /**
        * Suckerfish method for nav dropdowns
        */
        sfHover: function() {
            $('#nav li').each(function() {
                $(this).hover(
	    function() {
	        $(this).addClass('sfhover');
	    },
	    function() {
	        $(this).removeClass('sfhover');
	    }
	  );

            });
        }, // sfHover


        /**
        * Removes label from text fields    
        */
        removeLabel: function(field) {
            var node = $(field);
            var defaultText = node.val();

            node.focus(function() {
                if ($(this).val() == defaultText) {
                    $(this).val('');
                }
                $(field).blur(function() {
                    var userInput = $(this).val();
                    if (userInput == '') {
                        $(this).val(defaultText);
                    }
                }); // blur
            }); // focus
        }, // removeLabel


        /**
        * Directs user to the selected page  
        */
        selectNav: function(field) {
            var node = $(field);

            node.change(function() {
                if (!$(this).val() == '') {
                    location.href = $(this).val();
                }
            }); // change
        }, // selectNav


        /**
        * Activates dropdowns on page
        */
        activateDropDowns: function() {
            $('ul.dropdown').each(function() {
                $(this).find('a.ddLink').click(function(e) {
                    e.preventDefault();
                    $(this).next().toggleClass('hide');
                    $(this).blur();
                });
            });
        }, // activateDropDowns

        /**
        * manages standard lightbox forms
        */
        lbForm: {
            launch: function(url, params) {

                // manipulate url for full page v. ajax response
                url = url.replace(/format=nojs/, "format=ajax");

                $.openDOMWindow($.extend({
                    borderSize: '0',
                    windowBGColor: '#f8f8f8',
                    width: 496,
                    windowSource: 'ajax',
                    windowSourceURL: url,
                    functionCallOnOpen: Broyhill.lbForm.handleForm
                }, params));
            },
            handleForm: function() {
                // hijack form inside response

                $("#DOMWindow form").bind("submit", function(e) {

                    // manipulate url for full page v. ajax response
                    var formUrl = $(this).attr("action").replace(/format=nojs/, "format=ajax");


                    if ("post" == $(this).attr("method").toLowerCase()) {
                        $.post(formUrl, $(this).serializeArray(), function(data) {
                            $("#DOMWindow").html(data);
                            Broyhill.lbForm.handleForm();
                        }, "html");
                    } else {
                        // make get request
                        $.get(formUrl, $(this).serializeArray(), function(data) {
                            $("#DOMWindow").html(data);
                            Broyhill.lbForm.handleForm();
                        }, "html");
                    }
                    e.preventDefault();
                });
                $("#DOMWindow a.closeDOMWindow").bind("click", function(e) {
                    $.closeDOMWindow();
                    e.preventDefault();
                });
            }
        },

        /**
        * manages footer behavior
        */

        // hides the toolbar dropdowns when necessary
        dropdownFix: function() {
            var dd = $('.hider');
            $('#nav li:not(#nav li:last)').hover(
		function() { dd.hide(); },
		function() { dd.show(); }
	);
        }, // dropdownFix

        /**
        * Opens links in New Window
        */
        newWin: function() {
            $("a.newWin").bind("click", function(e) {
                window.open(this.href);
                e.preventDefault();
            });
        },

        /**
        * manages email list lightbox
        */
        emailList: function() {
            $("a.emailList").bind("click", function(e) {
                Broyhill.lbForm.launch(this.href, { height: '210' });
                e.preventDefault();
            });
        },

        officialRules: function() {
            $('a.officialRules').bind("click", function(e) {
                BROYHILL.lbForm.launch(this.href, { width: '600', height: '465' });
                e.preventDefault();
            });
        },

        choicesEprevue: function() {
            $('a.choicesEprevue').bind("click", function(e) {
                BROYHILL.lbForm.launch(this.href, { width: '850', height: '770' });
                e.preventDefault();
            });
        },

        choicesConfigurator: function() {
            $('a.choicesConfigurator').bind("click", function(e) {
                BROYHILL.lbForm.launch(this.href, { width: '848', height: '548' });
                e.preventDefault();
            });
        },

        winnersList: function() {
            $('a.winnersList').bind("click", function(e) {
                BROYHILL.lbForm.launch(this.href, { width: '500', height: '200' });
                e.preventDefault();
            });
        },
        /**
        * Initializes all global page interactions
        */
        _init: function() {
            this.sfHover();
            this.removeLabel('.searchTerm');
            this.selectNav('#selectCollection');
            this.activateDropDowns();
            this.dropdownFix();
            this.newWin();
            this.emailList();
            this.officialRules();
            this.choicesEprevue();
            this.choicesConfigurator();
            this.winnersList();
        } // _init

    }; // Broyhill Namspace

    $(document).ready(function() {
        Broyhill._init();
    });

    window.BROYHILL = Broyhill;

})();
