function isiPad() {
    return navigator.userAgent.match(/iPad/i) != null;
}

function isiPhone() {
    return navigator.userAgent.match(/iPhone/i) != null;
}

Navigation = Class.create({
    initialize: function (element)
    {

        this.element = $(element);
        if (this.element)
        {
            this.items = this.element.childElements();
            if (isiPad())
            {
                this.items.invoke('observe', 'mousedown', this.onIpadMouseDown.bindAsEventListener(this));
            }
            else
            {
                this.items.invoke('observe', 'mouseover', this.onItemOver.bindAsEventListener(this));
                this.items.invoke('observe', 'mouseout', this.onItemOut.bindAsEventListener(this));
            }
            this.timer = null;
            this.state = "CLOSED";

            if (isiPad())
            {
                this.removeLinks();
            } else
            {
                this.element.getElementsBySelector('.NavInnerClose').invoke('hide');
            }
        }
    },
    removeLinks: function ()
    {
        for (var i = 0; i < this.items.length; i++)
        {
            var item = this.items[i];
            var childElements = $(item).childElements();
            var linkItem = childElements[0];
            var href = linkItem.readAttribute("href");
            var urlArray = href.split("/");
            var url = urlArray[urlArray.length - 1].toLowerCase();
            if (url != "extras")
            {
                $(linkItem).observe("click", this.checkStatus.bindAsEventListener(this));
                //$(linkItem).observe("click", function (event) {
                //                    
                //                    event.stop();
                //                });
            }
        }

    },
    checkStatus: function (event)
    {
        var item = event.element().up('li[class]') || event.findElement('li');

        if (this.state == "CLOSED")
        {
            event.stop();
        }
        if (this.element.className != item.className && isiPad())
        {
            event.stop();
        }
    },
    onIpadMouseDown: function (event)
    {
        var item = event.element().up('li[class]') || event.findElement('li');

        if (this.timer)
        {
            clearTimeout(this.timer);
        }
        if (this.state == "CLOSED")
        {
            this.timer = setTimeout(this.showMenu.bind(this, item), 250);
        }
        else if (this.state == "OPEN" || event.element().identify() == 'NavCloseButton')
        {
            if (this.element.className != item.className)
            {
                this.hideMenu(this);
                this.timer = setTimeout(this.showMenu.bind(this, item), 250);
            } else
            {
                if (event.element().identify() == 'NavCloseButton')
                {
                    this.timer = setTimeout(this.hideMenu.bind(this), 250);
                    jQuery("#orig_encore_nav").hide();
                    jQuery("#orig_starz_nav").hide();
                }
            }

        }
    },
    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 (jQuery.trim(item.className) == "orig_starz_selected" || jQuery.trim(item.className) == "")
        {           
            this.element.className = "originals"
        }
        else
        {
            if (this.element.className != item.className)
            {
                this.element.className = item.className;
            }
        }
        this.state = "OPEN";
    },
    hideMenu: function ()
    {
        //alert("actually hiding menu");
        this.element.className = '';
        this.state = "CLOSED";
        jQuery(".OrigCloseButton").css("background-image", "url(/SiteImagesLib/NavCloseButton.gif)");
     }
});
Navigation.init = function () {
    Navigation.instance = new Navigation('Navigation');
}
Event.observe(document, 'dom:loaded', Navigation.init);
