var count = 0;
var botPosition = 0;
var topPosition = 0;
var pages = ['pages/visitors.aspx', 'pages/hostedbuyers.html', 'pages/media.aspx', 'pages/exhibitors.aspx', 'pages/travelaccom.aspx'];
var topContFull = 280;
var topContReduced = 50;
var botContFull = 280;
var botContWidth = 820;
var topContWidth = 860;
var curTabTop = 1;
var curTabBot = 1;
var topPositions = [0, 0, 0, 0, 0, 0, 0];

$(document).ready(function() {

    var clickMore = function() {

        if ($('#top-content-cont').css('height') == (topContReduced + 'px') || $('#top-content-cont').css('height') == ((topContReduced - 2) + 'px')) {
            $('#bottom-content').stop();
            $('#bottom-content > #holder').stop();
            $('#top-content a.more').hide();
            $('#nav a h5').css('color', '');

            $('#nav a .bottom').css('background', 'url(img/nav-background.jpg) repeat-x top #d2d2d2');

            $('#bottom-content #holder').animate({ opacity: 0 }, 500, null, function() {
                $('#bottom-content').animate({ paddingTop: 0, paddingBottom: 0 }, 50, 'swing');
                $('#bottom-content').animate({ height: 0 }, 800, 'swing', function() {
                    $(this).hide();
                    //$('#bottom-content').animate({ marginBottom: '15px' }, 0, 'swing');
                    $('#top-content-cont').animate({ height: (topContFull + 'px') }, 850, 'swing', function() {

                    });
                    $('#top-content a.more').hide();
                    $('#nav a p').animate({ height: '50px' }, 500);
                    $('#nav #nav-head').show().animate({ height: '78px' }, 500, null, 'swing', function() { //height of images above middle buttons

                    });
                    $('#top-content-cont').animate({ height: (topContFull + 'px') }, 500, 'swing', function() { $($('#sp-' + curTabTop).children().get(topPositions[curTabTop - 1])).find('.hideOnScroll').show(); });
                });
            });
        }
    };

    $('#sub-nav > li').each(function(i) {
        $(this).click(function() {
            var prevnum = $('#sub-nav > li.active:first').attr('id').split('-').pop();
            var num = $(this).attr('id').split('-').pop();

            //$($(this).find('ul.tabs:first').children().get(topPositions[num-1])).find('.hideOnScroll').show();

            curTabTop = num;
            var margin = (num - 1) * topContFull;
            if ($.browser.msie) {
                margin = (num - 1) * (topContFull + 5);
            }
            var name = $(this).find('a').html();
            var slf = this;
            $('#sp-' + prevnum).find('.hideOnScroll').hide();

            $('#top-content').stop().animate({ 'marginTop': -margin + 'px' }, 500, 'swing', function() {
                if ($('#top-content-cont').css('height') == topContReduced + 'px' || $('#top-content-cont').css('height') == (topContReduced - 2) + 'px') {
                    clickMore();
                }
                else {
                    $($('#sp-' + curTabTop).find('ul.tabs:first').children().get(topPositions[num - 1])).find('.hideOnScroll').show();
                }
                s.pageName = name;
                void (s.t());
            });

            $('#sub-nav > li').removeClass('active');
            $(this).addClass('active');
        });
    });
    //Top Horizontal Scrolling tabs within vertical main tabs 
    $('#top-content > li.main-tab > .next').click(function() {
        var tabs = $(this).parent().find('ul.tabs:first');
        var num = $(this).parent().attr('id').split('-').pop();
        $(tabs.children().get(topPositions[num - 1])).find('.hideOnScroll').hide();

        if ((topPositions[num - 1] + 1) < tabs.children().length) {
            topPositions[num - 1]++;
        }
        else {
            topPositions[num - 1] = 0;
        }
        var margin = topPositions[num - 1] * topContWidth;
        tabs.stop().animate({ 'marginLeft': -margin + 'px' }, 1000, 'swing', function() {
            $(tabs.children().get(topPositions[num - 1])).find('.hideOnScroll').show();
        });
        return false;
    });
    $('#top-content > li.main-tab > .prev').click(function() {
        var tabs = $(this).parent().find('ul.tabs:first');
        var num = $(this).parent().attr('id').split('-').pop();
        $(tabs.children().get(topPositions[num - 1])).find('.hideOnScroll').hide();

        if ((topPositions[num - 1] - 1) >= 0) {
            topPositions[num - 1]--;
        }
        else {
            topPositions[num - 1] = tabs.children().length - 1;
        }
        var margin = topPositions[num - 1] * topContWidth;
        tabs.stop().animate({ 'marginLeft': -margin + 'px' }, 1000, 'swing', function() {
            $(tabs.children().get(topPositions[num - 1])).find('.hideOnScroll').show();
        });

        return false;
    });


    //Top content box tabs
    $('#top-content a.more').click(clickMore);
    //Hover arrow
    $('#nav li').hover(function() { $(this).find('a > div.curr').stop().css('opacity', 0).show().animate({ opacity: 1 }, 500); }, function() { $(this).find('a > div.curr').stop().hide(); });

    //Caching disabled until site fully developed
    $.ajaxSetup({
        cache: false
    });

    //Bottom content box tabs
    var setupBottomSlider = function() {
        $('#holder .next').click(function() {
            if ((botPosition + 1) < $('#holder .tabs li.tab').length) {
                botPosition++;
            }
            else {
                botPosition = 0;
            }
            var margin = botPosition * botContWidth;
            $('#holder .tabs').stop().animate({ 'marginLeft': -margin + 'px' }, 1000);
            return false;
        });
        $('#holder .prev').click(function() {
            if ((botPosition - 1) >= 0) {
                botPosition--;
            }
            else {
                botPosition = $('#holder .tabs li.tab').length - 1;
            }
            var margin = botPosition * botContWidth;
            $('#holder .tabs').stop().animate({ 'marginLeft': -margin + 'px' }, 1000);
            return false;
        });
    };

    //Main Navigation and loading of content into bottom box.
    $('#nav > ul > li > a').click(function() {
        botPosition = 0;
        var link = $(this).parent();
        curTabBot = $(link).attr('id').split('-').pop();
        $('#sp-' + curTabTop).find('.hideOnScroll').hide();
        $('#top-content .main-tab').each(function() {
        });
        var name = $(this).find('h5').html();
        $('#bottom-content').stop();
        $('#bottom-content > #holder').stop();
        $('#nav a h5').css('color', '#333333');

        $('#nav a .bottom').css('backgroundImage', '');

        $(this).find('.bottom').css('background', '#d8d8d8');

        $(this).find('h5').css('color', '');


        var loadContent = function(el, showLoad) {
            var loadUrl = '';

            for (var i = 0; i < pages.length; i++) {
                if ($(el).attr('id') == ("t-" + (i + 1))) {
                    loadUrl = pages[i];
                }
            }

            if (loadUrl) {
                $('#bottom-content > #holder').animate({ opacity: 0 }, 500, null, function() {
                    $(this).html('');
                    if (showLoad == true) {
                        $('#loader').stop().css('opacity', 0).show().animate({ opacity: 1 }, 50, null);
                    }
                    $(this).load(loadUrl, null, function() {
                        setupBottomSlider();
                        $('#holder h3').css('color', $(el).find('h5').css('color'));
                        if (showLoad == true) {
                            $('#loader').stop().animate({ opacity: 1 }, 50, null, function() {
                                $(this).hide();
                            });
                        }
                        $('#holder').animate({ opacity: 1 }, 500, null, function() {
                            if ($.browser.msie) {
                                this.style.removeAttribute('filter');
                            }

                            s.pageName = name;
                            void (s.t());
                        });
                    });
                });
            }
        };

        if ($('#bottom-content').is(':hidden')) {
            $('#nav #nav-head').animate({ height: '0px' }, 500, null, function() {
                $(this).hide();
            });
            $('#nav a p').animate({ height: '0px' }, 800, function() {
                $(this).hide();
            });
            if ($.browser.msie) {
                topContReduced -= 2;
            }
            $('#top-content-cont').animate({ height: (topContReduced + 'px') }, 800, function() {   //shrink top content box
                if ($.browser.msie) {
                    topContReduced += 2;
                }
                $('#top-content a.more').show();
                
                $('#bottom-content').animate({ paddingTop: '15px', paddingBottom: '15px', marginTop: '0px'}, 50, 'swing');

                $('#bottom-content').css('height', '0px').show().animate({ height: (botContFull + 'px') }, 1000, 'swing', loadContent(link, false));

                if ($.browser.msie) {
                    $('#bottom-content').animate({ marginTop: '0px' }, 0, 'swing');
                    $('#clear-div').css('clear', 'none');   //remove 'clear:both' in IE as it creates unwanted white space 15px in height
                }
                count = 0;
            });
        }
        else {
            loadContent(link, true);
        }
    });

    var checkNav = function(navName) {
        if (window.location.href.indexOf('#') != -1) {
            var ele = window.location.href.substr(window.location.href.indexOf('#'));

            $(navName).children().each(function(i) {
                if ($(this).find('a:first').attr('href').substr($(this).find('a:first').attr('href').indexOf('#')) == ele) {
                    $(this).find('a:first').trigger('click');
                }
            });
        }
    };

    checkNav('#sub-nav');
    checkNav('#main-nav');
});

var goToTab = function(li, tabNum){    
    topPositions[curTabTop-1] = tabNum-2;
    $(li).find('.next').trigger('click');        
};
