
Navigation = Class.create({
    initialize: function(element) {
        try {
            this.element = $(element);
            this.items = this.element.childElements();
            this.items.invoke('observe', 'mouseover', this.onItemOver.bindAsEventListener(this));
            this.items.invoke('observe', 'mouseout', this.onItemOut.bindAsEventListener(this));
            this.timer = null;
        } catch (e) { }
    },
    onItemOver: function(event) {
        // obtain the root <li>
        var item = event.element().up('li[class]') || event.findElement('li');

        if (this.timer) {
            clearTimeout(this.timer);
        }
        this.timer = setTimeout(this.showMenu.bind(this, item), 250);

    },
    onItemOut: function(event) {
        if (this.timer) {
            clearTimeout(this.timer);
        }
        this.timer = setTimeout(this.hideMenu.bind(this), 250);
    },
    showMenu: function(item) {
        if (this.element.className != item.className) {
            this.element.className = item.className;
        }
    },
    hideMenu: function() {
        this.element.className = '';
    }
});
Navigation.init = function() {
    updateNavigationElements();
    Navigation.instance = new Navigation('Navigation');
}
//Event.observe(document, 'dom:loaded', Navigation.init);
Event.observe(window, 'load', Navigation.init);

Search = {
    SEARCH_URL: 'http://www.starz.com/search/starz/',
    SEARCH_INPUT: 'searchTerm',
    keypress: function(event) {
        if (event.keyCode == Event.KEY_RETURN) {
            Search.submit(event);
        }
    },
    submit: function(event) {
        event.stop();
        var term = $(Search.SEARCH_INPUT).getValue();
        window.location.href = Search.SEARCH_URL + term;
    }
}


Event.observe(window, 'load', function() {
    try {
        $('btnSearch').observe('click', Search.submit);
        $('searchTerm').observe('keypress', Search.keypress);
    } catch (e) { }
});


// <script src="http://test.starz.com/MiscPages/GetHeaderNavigation.aspx?output=javascript&var=dynamicNavigation" type="text/javascript" charset="utf-8" ></script>

var attempt = 0;

function updateNavigationElements() 
{
    try{
		$('Navigation').update(dynamicNavigation["headerData"]);
	}
	catch(err){
		attempt++;
		if(attempt<5) updateNavigationElements();
	}
}

