﻿//var sfTimer;
function drilldownmenu(setting) {
    this.arrowR = '/sites/all/items/easymenu/images/arrowR.gif';
    this.arrowRH = '/sites/all/items/easymenu/images/arrowRH.gif';
    this.arrowL = '/sites/all/items/easymenu/images/arrowL.gif';
    this.arrowLH = '/sites/all/items/easymenu/images/arrowLH.gif';
    this.homecrumbtext = 'Home Dir';
    this.titlelength = 35;
    this.homecrumbtext = setting.menuname;
    this.titlelength = 100;
    this.linkref = setting.linkref;
    this.crumbid = setting.crumbid;
    this.menuid = setting.menuid;
    this.menuname = setting.menuname;
    this.$menudiv = null;
    this.mainul = null;
    this.$ulprimary = null;
    this.navdivs = {}
    this.menuheight = setting.menuheight || 325;
    this.selectedul = null;
    this.speed = 75;
    this.$arrowimgs = null;
    this.currentul = 0;
    this.filesetting = {url: null, targetElement: null}
    this.zIndexvalue = 500;
    this.arrowposx = 0;
    var drillDown = this;
    $(document).ready(function ($) {drillDown.init($, setting)});
}
drilldownmenu.prototype.init = function ($, setting) {
    var drillDown = this;
    var $maindiv = $('#' + setting.menuid).css({ position: 'relative' });
    var $ulprimary = $maindiv.find('ul');
    $ulprimary.css({
        position: 'absolute',
        left: 0,
        top: 0,
        visibility: 'hidden'
    });
    this.$maindiv = $maindiv;
    this.$ulprimary = $ulprimary;
    this.navdivs.$crumb = $('#' + setting.breadcrumbid);
    this.navdivs.$backbuttons = $('a[rel^="drillback-' + setting.menuid + '"]').css({ outline: 'none' }).click(function (e) { drillDown.back(); e.preventDefault();});
    this.buildmenu($, setting);
}
drilldownmenu.prototype.buildmenu = function ($, setting) {
    var $maindiv = $('#' + setting.menuid).children('ul:eq(0)');
    var drillDown = this;
    this.$ulprimary.each(function (i) {
        var $ulsecondary = $(this);
        if (i == 0) {
            drillDown.$maindiv.css({height: (drillDown.menuheight == 'auto') ? $thisul.outerHeight() : parseInt(drillDown.menuheight)}).data('h', {height: parseInt(drillDown.$maindiv.css('height'))});
        } else {
            var $parentul = $ulsecondary.parents('ul:eq(0)');
            var $parentli = $ulsecondary.parents('li:eq(0)');
            var $menulink = $parentli.children('a:eq(0)').css({
                'outline': 'none',
                'float': 'left',
                'width': '160px',
                'clear': 'right'
            }).data('control', {order: i});
            var $anchorelm = $('<a href="#" style="float: right;  width:30px; height:24px; display: inline-block; margin-right: 20px;" class="anchorPicR" ></a>').click(function (e) {
                $maindiv.hide();
                e.preventDefault();
                drillDown.slidemenu($menulink.data('control').order);
                
            });
            $menulink.after($anchorelm);
            $('<li class="backcontrol anchorPicL"><p> Back To ' + drillDown.menuname + ' </p></li>').click(function (e) {
                $maindiv.show();
                e.preventDefault();
                drillDown.back();
            }).prependTo($ulsecondary);
        }
        var ulheight = ($ulsecondary.outerHeight() < drillDown.$maindiv.data('h').height) ? drillDown.$maindiv.data('h').height : 'auto';
        
        $ulsecondary.css({
            visibility: 'visible',
            width: '240px',
            height: ulheight,
            left: (i == 0) ? 'auto' : $parentli.outerWidth(),
            top: 0
        });
        $ulsecondary.data('specs', {
            w: $ulsecondary.outerWidth(),
            h: $ulsecondary.outerHeight(),
            order: i,
            parentorder: (i == 0) ? -1 : $menulink.parents('ul:eq(0)').data('specs').order,
            x: (i == 0) ? 0 : $parentul.data('specs').w
        });
    });
    if (window.opera) {this.$ulprimary.eq(0).css({zIndex: this.zIndexvalue});}
    this.slidemenu(0);

}
drilldownmenu.prototype.rotate = function (theRef) {
    drillDown.slidemenu($(theRef).data('control').order);
    e.preventDefault();
}
drilldownmenu.prototype.slidemenu = function (order) {
    var order = isNaN(order) ? 0 : order;
    this.$ulprimary.css({"display": 'none'});
    var $targetul = this.$ulprimary.eq(order).css({zIndex: this.zIndexvalue++});
    $targetul.parents('ul').andSelf().css({display: 'block'});
    this.currentul = order;
    //var drillSlide = this;
    //if ($targetul.data('specs').h > this.$maindiv.data('h').height) {this.$maindiv.css({"overflow-y": "auto"});}
    this.updatenav($, order);
    this.$ulprimary.eq(0).animate({'left': -$targetul.data('specs').x}, 250);
    this.$maindiv.scrollTop(0);
}
drilldownmenu.prototype.back = function () {
    if (this.currentul > 0) {
        var order = this.$ulprimary.eq(this.currentul).parents('ul:eq(0)').data('specs').order;
        this.slidemenu(order);
    }
}
drilldownmenu.prototype.updatenav = function ($, order) {
    var $currentul = this.$ulprimary.eq(order)
    if (this.navdivs.$crumb.length == 1) {
        var $crumb = this.navdivs.$crumb.empty();
        if (order > 0) {
            var crumbhtml = '';
            while ($currentul && $currentul.data('specs').order >= 0) {
                $currentul = ($currentul.data('specs').order > 0) ? this.$ulprimary.eq($currentul.data('specs').parentorder) : null;
            }
            $crumb.append(crumbhtml).find('img:eq(0)').remove().end().find('a:last').replaceWith(this.$ulprimary.eq(order).data('specs').title);
        } else {
            $crumb.append(this.homecrumbtext)
        }
    }
    if (this.navdivs.$backbuttons.length > 0) {
        if (!/Safari/i.test(navigator.userAgent)) {
            this.navdivs.$backbuttons.css((order > 0) ? {
                opacity: 1,
                cursor: 'pointer'
            } : {
                opacity: 0.5,
                cursor: 'default'
            });
        }
    }
}





$.fn.hoverIntent = function () {
    function openmenu() {
        var that = this;
        if (typeof(that) != undefined) {
            $(that).children().removeClass("on");
            $(that).children(".drillmenu").slideUp(0);
        }
        $(this).children().addClass("on");
        $(this).children(".drillmenu").slideDown(5);
    }
    function closemenu() {
        $(this).children().removeClass("on");
        $(this).children(".drillmenu").slideUp(5);
    }
    var cfg = {
        sensitivity: 7,
        interval: 100,
        timeout: 0,
        over: openmenu,
        timeout: 500,
        out: closemenu
    };
    cfg = $.extend(cfg);
    var cX, cY, pX, pY;
    var track = function (ev) {
        cX = ev.pageX;
        cY = ev.pageY;
    };
    var compare = function (ev, ob) {
        ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t);
        if ((Math.abs(pX - cX) + Math.abs(pY - cY)) < cfg.sensitivity) {
            $(ob).unbind("mousemove", track);
            ob.hoverIntent_s = 1;
            return cfg.over.apply(ob, [ev]);
        } else {
            pX = cX;
            pY = cY;
            ob.hoverIntent_t = setTimeout(function () {compare(ev, ob);}, cfg.interval);
        }
    };
    var delay = function (ev, ob) {
        ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t);
        ob.hoverIntent_s = 0;
        return cfg.out.apply(ob, [ev]);
    };
    var handleHover = function (e) {
        var p = (e.type == "mouseover" ? e.fromElement : e.toElement) || e.relatedTarget;
        while (p && p != this) {try {p = p.parentNode;} catch (e) {p = this;}}
        if (p == this) {return false;}
        var ev = jQuery.extend({}, e);
        var ob = this;
        if (ob.hoverIntent_t) {ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t);}
        if (e.type == "mouseover") {
            pX = ev.pageX;
            pY = ev.pageY;
            $(ob).bind("mousemove", track);
            if (ob.hoverIntent_s != 1) {
                ob.hoverIntent_t = setTimeout(function () {
                    compare(ev, ob);
                }, cfg.interval);
            }
        } else {
            $(ob).unbind("mousemove", track);
            if (ob.hoverIntent_s == 1) {
                ob.hoverIntent_t = setTimeout(function () {
                    delay(ev, ob);
                }, cfg.timeout);
            }
        }
    };
    return this.mouseover(handleHover).mouseout(handleHover);
};
$(document).ready(function ($) {

    $(".drillmenu, .item, .drillmenu ul").css({"visibility": "hidden"});
    $("#nav_btn_0").click(function () {window.location = '/'});
    $("#nav_btn_1").click(function () {window.location = menuDD1.linkref});
    $("#nav_btn_2").click(function () {window.location = menuDD2.linkref});
    $("#nav_btn_3").click(function () {window.location = menuDD3.linkref});
    $("#nav_btn_4").click(function () {window.location = menuDD4.linkref});
    $("#nav_btn_5").click(function () {window.location = menuDD5.linkref});
    $("#nav_btn_6").click(function () {window.location = menuDD6.linkref});
    $("#nav_btn_7").click(function () {window.location = menuDD7.linkref});
    var menuDD1 = new drilldownmenu({
        menuname: 'Business',
        menuid: 'drillmenu1',
        crumbid: 'drillcrumb1',
        linkref: '/oc-business'
    });
    var menuDD2 = new drilldownmenu({
        menuname: 'Dining',
        menuid: 'drillmenu2',
        crumbid: 'drillcrumb2',
        linkref: '/oc-dining'
    });
    var menuDD3 = new drilldownmenu({
        menuname: 'Shopping',
        menuid: 'drillmenu3',
        crumbid: 'drillcrumb3',
        linkref: '/oc-shopping'
    });
    var menuDD4 = new drilldownmenu({
        menuname: 'What To Do',
        menuid: 'drillmenu4',
        crumbid: 'drillcrumb4',
        linkref: '/what-do-oc'
    });
    var menuDD5 = new drilldownmenu({
        menuname: 'Nightlife',
        menuid: 'drillmenu5',
        crumbid: 'drillcrumb5',
        menuheight: 219,
        linkref: '/oc-nightlife'
    });
    var menuDD6 = new drilldownmenu({
        menuname: 'Lifestyle',
        menuid: 'drillmenu6',
        crumbid: 'drillcrumb6',
        menuheight: 246,
        linkref: '/oc-lifestyle'
    });
    var menuDD7 = new drilldownmenu({
        menuname: 'Visiting',
        menuid: 'drillmenu7',
        crumbid: 'drillcrumb7',
        menuheight: 300,
        linkref: '/visiting-oc'
    });
    $("#nav_area_0, #nav_area_1, #nav_area_2, #nav_area_3, #nav_area_4, #nav_area_5, #nav_area_6, #nav_area_7").hoverIntent();
    $(".drillmenu").hide();
    $(".drillmenu, .drillmenu ul, .drillmenu ul li, .drillmenu ul li.item").css({"visibility": 'visible'});
});
