﻿function handlers() {
    var div = new Array();
    var highlight
    var oldClick
    var oldHash = ''
    var containerNew
    var currentBtn = null
    var timeout = 2800
    bindit()
    function bindit() {
        setTimeout(function() {
        $('#logo_btn').bind('click', function(evt) {
        evt.preventDefault()
            if (window.location.href.indexOf('#') != -1) {
                var newhref = window.location.href.substring(0, window.location.href.indexOf('#')) + $(evt.currentTarget).attr('href')
                window.location = newhref
            } else {
                window.location = window.location.href + $(evt.currentTarget).attr('href')
            }
           removeit() 
        })
        
            $('#misc-nav li,#btl-nav li').each(function(index) {
                $(this).bind('mouseenter', function(evt) {
                    highlight = div[$(evt.currentTarget).parent('ul').parent('div').find('li').index($(evt.currentTarget))]
                    highlight.css('top', $(evt.currentTarget).position().top);
                    if ($(this).data('hit') != true) highlight.animate({ 'width': '168px' }, { queue: false, duration: 300, easing: 'easeOutExpo' })

                    $(this).bind('mouseout', function(evt) {
                        if ($(this).data('hit') != true) {
                            highlight.animate({ 'width': '0px' }, { queue: false, duration: 300, easing: 'easeInExpo' })
                        }
                    })
                })

                $(this).bind('click', function(evt) {
                evt.preventDefault()
                    if (window.location.href.indexOf('#') != -1) {
                        var newhref = window.location.href.substring(0, window.location.href.indexOf('#')) + $(evt.currentTarget).find('a').attr('href')
                        window.location=newhref
                    } else {
                        window.location = window.location.href + $(evt.currentTarget).find('a').attr('href')
                    }
                    containerNew = Number($(evt.currentTarget).parent('ul').find('li').index($(evt.currentTarget)))
                    $('.li' + containerNew).animate({ 'width': '178px' }, { queue: false, duration: 300, easing: 'easeOutExpo' });
                    if (oldClick != null) {
                        if (oldClick.html() != $(this).html()) {
                            oldClick.data('hit', false)
                            $(evt.currentTarget).parent('ul').parent('div').find('.li' + $(evt.currentTarget).parent('ul').parent('div').find('li').index(oldClick)).animate({ 'width': '0px' }, { queue: false, duration: 300, easing: 'easeOutExpo' })
                        }
                    }
                    $(this).data('hit', true)
                    oldClick = $(this);
                    removeit()
                })
            })
        }, timeout)
    }
    
    function removeit() {
        $('#misc-nav li,#btl-nav li').each(function() {
        $(this).unbind('click')
        $('#logo_btn').unbind('click')
        $(this).find('a').click(function(evt) {
                evt.preventDefault()
            })
            $(div).each(function(){$(this).stop()})
            $(this).unbind('mouseenter')
            $(this).unbind('mouseout')
        })
        timeout = 3500
        bindit() 
    }

    $(window).bind('hashchange', function() {
        reworkSite(window.location.hash);
        var hash = window.location.hash.replace('#', '');
        if (hash.indexOf('&') != -1) {
            hash = hash.substring(0, hash.indexOf('&'));
        }
        if ($('#social-cnt').css('right') == '0px') {
            var socialSet = $('#social-cnt').width() - $('#social-btn').width()
            $('#social-btn').parent().animate({ 'right': '-' + socialSet + 'px' }, 300, function() {
            $('#social-btn').data('on', false)
            })
        }
        barOut(hash)
    })

    $(document).ready(function(evt) {
    oldHash = window.location.hash.replace('#', '');
    if (oldHash.indexOf('&') != -1) {
        oldHash = oldHash.substring(0, oldHash.indexOf('&'));
    }
        $('#misc-nav li,#btl-nav li').each(function(index) {
            div.push($('<div></div>'));
            $(div[index]).addClass('li' + index)
            $(div[index]).addClass('hover');
            $(this).parent('ul').parent('div').append($(div[index]));
        })
        barOut(oldHash)
        $('#lft-nav').find('a').click(function(evt) {
            evt.preventDefault()
        })
        timeout=2500
    })

    function barOut(hashMark) {
        var number
        switch (hashMark) {
            case 'home':
                number = null
                break;
            case '':
                number = null
                break;
            case 'silver':
                number = 0
                break;
            case 'reposado':
                number = 1
                break;
            case 'anejo':
                number = 2
                break;
            case 'vision':
                number = 3
                break;
            case 'process':
                number = 4
                break;
            case 'cocktails':
                number = 5
                break;
            case 'infusions':
                number = 6
                break;

        }
        var list = $('.navbg').find('li')
        if (number != null) {
            $('.li' + number).css({ 'top': list[number].offsetTop })
            $('.li' + number).animate({ 'width': '178px' }, { queue: false, duration: 300, easing: 'easeOutExpo' });
            $(div).each(function(index) {
                if (index != number) {
                    $(div[index]).animate({ 'width': '0px' }, { queue: false, duration: 300, easing: 'easeOutExpo' })
                }
            })
            $(list[number]).data('hit', true)
            oldClick = $(list[number]);
        } else {
        $(div).each(function(index) {
        if (index != number) {
            $(list[index]).data('hit', false)
                $(div[index]).animate({ 'width': '0px' }, { queue: false, duration: 300, easing: 'easeOutExpo' })
            }
            })
        }
    }
    
    
    function reworkSite(hash) {
        var fileName = hash.replace('#', '');
        if (fileName.indexOf('&') != -1) {
            fileName = fileName.substring(0, fileName.indexOf('&'));
        }
        var boolImg;
        var boolTxt;
        var boolDes
        switch (oldHash) {
            case 'home':
                boolImg = true;
                boolTxt = false;
                boolDes = false;
            break;
            case '':
                boolImg = true;
                boolTxt = false;
                boolDes = true;
            break;
            case 'silver':
                boolImg = true;
                boolTxt = false;
                boolDes = true;
            break;
            case 'reposado':
                boolImg = true;
                boolTxt = false;
                boolDes = true;
            break;
            case 'anejo':
                boolImg = true;
                boolTxt = false;
                boolDes = true;
            break;
             case 'vision':
                boolImg = false;
                boolTxt = true;
                boolDes = false;
            break;
            case 'process':
                boolImg = false;
                boolTxt = true;
                boolDes = true;
            break;
            case 'cocktails':
                boolImg = false;
                boolTxt = true;
                boolDes = true;
                break;
            case 'infusions':
                boolImg = false;
                boolTxt = true;
                boolDes = true;
                break;
    }
    if (hash == '') {
            jQuery('body').slideDes('moveTextOut',boolDes);
            jQuery('body').slideTxt('moveTextOut', boolTxt, 'home.htm');
            jQuery('body').slideImg('moveImageOut',boolImg, 'home.htm') 
         }
         else if (oldHash != fileName) {
         var loader=false
        // if (fileName == 'silver' || fileName == 'reposado' || fileName == 'anejo') {
             $('#pageLoader1').show()
             loader=true
        // }
         //jQuery('body').imgLoader('bgOut');
         jQuery('body').slideDes('moveTextOut', boolDes);
         jQuery('body').slideTxt('moveTextOut', boolTxt, fileName + '.htm',loader);
         jQuery('body').slideImg('moveImageOut', boolImg, fileName + '.htm',loader);
     };
     if ($('#overlay').is(':visible')) {
         $('#overlay').hide();
     }
     oldHash = fileName
    }
};
