var megaDropdown = {
    els: {},
    namespace: "megadropdown",
    open: false,
    
    hide: function(){
        this.els.container.style.display = "none";
        this.open = false;
        this.els.navLink.className = this.els.navLink.className.replace(' open', '');
        
        if(this.browser.msie6){
            for(var i = 0; i < this.els.selects.length; i++){
                this.els.selects[i].style.visibility = "visible";
            }
        }
    },
    
    show: function(){
        this.els.container.style.display = "block";
        this.open = true;
        this.els.navLink.className = this.els.navLink.className + " open";
        
        if(this.browser.msie6){
            for (var i = 0; i < this.els.selects.length; i++) {
                this.els.selects[i].style.visibility = "hidden";
            }
        }
    },
    
    toggle: function(){
        if(this.open === true){
            this.hide();
        } else {
            this.show();
        }
    },
    
    init: function(){
        var self = this;
        
        var UA = navigator.userAgent.toLowerCase();
        this.browser = {
            chrome: UA.match(/chrome/) ? true : false,
            firefox: UA.match(/firefox/) ? true : false,
            firefox2: UA.match(/firefox\/2/) ? true : false,
            firefox30: UA.match(/firefox\/3\.0/) ? true : false,
            msie: UA.match(/msie/) ? true : false,
            msie6: (UA.match(/msie 6/) && !UA.match(/msie 7|8/)) ? true : false,
            msie7: UA.match(/msie 7/) ? true : false,
            msie8: UA.match(/msie 8/) ? true : false,
            chromeFrame: (UA.match(/msie/) && UA.match(/chrome/)) ? true : false,
            opera: UA.match(/opera/) ? true : false,
            safari: (UA.match(/safari/) && !UA.match(/chrome/)) ? true : false
        };
        
        this.els.selects = document.getElementsByTagName('select');
        this.els.container = document.getElementById(this.namespace + '-container');
        this.els.navLink = document.getElementById('link-hotel-megadropdown');
        this.els.close = document.getElementById(this.namespace + '-link-close');
        
        var childElements = ['ul','li','a','dl','dd','dt','div','span'];
        for(var i = 0; i < childElements.length; i++){
            var children = this.els.container.getElementsByTagName(childElements[i]);
            for(var c = 0; c < children.length; c++){
                children[c].onmouseover = function(){
                    self.show();
                };
                children[c].onmouseenter = function(){
                    self.show();
                }
                children[c].onmouseout = function(){
                    self.hide();
                }
                children[c].onmouseleave = function(){
                    self.hide();
                }
            }
        }
        
        this.els.close.onclick = function(){
            self.hide();
            return false;
        };
        
        this.els.navLink.onmouseover = function(){
            self.show();
        };
        this.els.navLink.onmouseenter = function(){
            self.show();
        }
        this.els.navLink.onmouseout = function(){
            self.hide();
        }
        this.els.navLink.onmouseleave = function(){
            self.hide();
        }
        this.els.container.onmouseover = function(){
            self.show();
        };
        this.els.container.onmouseenter = function(){
            self.show();
        }
        this.els.container.onmouseout = function(){
            self.hide();
        }
        this.els.container.onmouseleave = function(){
            self.hide();
        }
		
//		//Appending Hotel Theodore to the Affiliated List Temporarily
//		var theodoreLink = document.createElement('a');
//		var theodoreLinkText = document.createTextNode('Hotel Theodore');		
//		var theodoreLiElem = document.createElement('li');
//		var affiliatedList = document.getElementById('region-').childNodes[0].childNodes[0];
//		
//		//create attributes
//		theodoreLink.setAttribute('href', 'http://www.hoteltheodore.com/');
//		theodoreLink.setAttribute('target', '_blank');
//		//append link text
//		theodoreLink.appendChild(theodoreLinkText);
//		//append link to li
//		theodoreLiElem.appendChild(theodoreLink);
//		//append li to ul
//		affiliatedList.appendChild(theodoreLiElem);
    }
};

(function(){
    // DOMReady script functionality courtesy of jQuery 1.4.2 core. Ripped from jQuery source 09/24/2010.
    
    var userAgent = navigator.userAgent.toLowerCase();
    
    // Figure out what browser is being used
    var browser = {
    	version: (userAgent.match( /.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/ ) || [])[1],
    	safari: /webkit/.test(userAgent),
    	opera: /opera/.test(userAgent),
    	msie: (/msie/.test(userAgent)) && (!/opera/.test( userAgent )),
    	mozilla: (/mozilla/.test(userAgent)) && (!/(compatible|webkit)/.test(userAgent))
    };    
    
    var readyBound = false;	
    var isReady = false;
    var readyList = [];
    
    // Cleanup functions for the document ready method
    if ( document.addEventListener ) {
    	DOMContentLoaded = function() {
    		document.removeEventListener( "DOMContentLoaded", DOMContentLoaded, false );
    		ready();
    	};
    
    } else if ( document.attachEvent ) {
    	DOMContentLoaded = function() {
    		// Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
    		if ( document.readyState === "complete" ) {
    			document.detachEvent( "onreadystatechange", DOMContentLoaded );
    			ready();
    		}
    	};
    }
    
    function ready(){
    	// Make sure that the DOM is not already loaded
    	if ( !isReady ) {
    		// Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
    		if ( !document.body ) {
    			return setTimeout( ready, 13 );
    		}
    
    		// Remember that the DOM is ready
    		isReady = true;
    
    		// If there are functions bound, to execute
    		if ( readyList ) {
    			// Execute all of them
    			var fn, i = 0;
    			while ( (fn = readyList[ i++ ]) ) {
    				fn.call( document, [] );
    			}
    
    			// Reset the list of functions
    			readyList = null;
    		}
    
    		ready();
    	}
    }
    
    function bindReady(){
    	if ( readyBound ) {
    		return;
    	}
    
    	readyBound = true;
    
    	// Catch cases where $(document).ready() is called after the
    	// browser event has already occurred.
    	if ( document.readyState === "complete" ) {
    		return ready();
    	}
    
    	// Mozilla, Opera and webkit nightlies currently support this event
    	if ( document.addEventListener ) {
    		// Use the handy event callback
    		document.addEventListener( "DOMContentLoaded", DOMContentLoaded, false );
    		
    		// A fallback to window.onload, that will always work
    		window.addEventListener( "load", ready, false );
    
    	// If IE event model is used
    	} else if ( document.attachEvent ) {
    		// ensure firing before onload,
    		// maybe late but safe also for iframes
    		document.attachEvent("onreadystatechange", DOMContentLoaded);
    		
    		// A fallback to window.onload, that will always work
    		window.attachEvent( "onload", ready );
    
    		// If IE and not a frame
    		// continually check to see if the document is ready
    		var toplevel = false;
    
    		try {
    			toplevel = window.frameElement == null;
    		} catch(e) {}
    
    		if ( document.documentElement.doScroll && toplevel ) {
    			doScrollCheck();
    		}
    	}
    }
    
    // The DOM ready check for Internet Explorer
    function doScrollCheck() {
    	if ( isReady ) {
    		return;
    	}
    
    	try {
    		// If IE is used, use the trick by Diego Perini
    		// http://javascript.nwbox.com/IEContentLoaded/
    		document.documentElement.doScroll("left");
    	} catch( error ) {
    		setTimeout( doScrollCheck, 1 );
    		return;
    	}
    
    	// and execute any waiting functions
    	ready();
    }

    megaDropdown.ready = function(fn){
    	// Attach the listeners
    	bindReady();
    
    	// If the DOM is already ready
    	if ( isReady ) {
    		// Execute the function immediately
    		fn.call( document, [] );
    
    	// Otherwise, remember the function for later
    	} else if ( readyList ) {
    		// Add the function to the wait list
    		readyList.push( fn );
    	}
    
    	return this;
    };
    
    megaDropdown.ready(function(){
        megaDropdown.init();
    });
})();
