$(document).ready(function() {

    $("#lightBox #agencyTabs").addClass("agencyTabs");

    $('.close').click(function() {
        $('#createBrief').remove();
    });
    $('#loginNow').click(function() {
        $('#loginBox').toggle();
    });
    $('#refine h3').click(function() {
        $('#refine .popup').toggle();
    });
    $('#refine a.cancel').click(function() {
        $('#refine .popup').hide();
        return false;
    });
    $('.refine h3').click(function() {
        $('.refine .popup').toggle();
    });
    $('.refine a.cancel').click(function() {
        $('.refine .popup').hide();
        return false;
    });


    if ($.isFunction($.fn.hoverIntent)) {

        var config = {
            sensitivity: 3, // number = sensitivity threshold (must be 1 or higher)    
            interval: 200, // number = milliseconds for onMouseOver polling interval    
            over: function() {
                $(this).children('ul').toggle();
            },  // function = onMouseOver callback (REQUIRED)    
            timeout: 500, // number = milliseconds delay before onMouseOut    
            out: function() {
                $(this).children('ul').toggle();
            }
        };
        $('#topMenu > li').hoverIntent(config);
    }

    // Tab's support for "Agent — Elements" and "Produce — Source" pages.
    function createNewHandlers_2() {

        $('#tabCaptions li a').click(function() {

            $('.refine .popup').hide();
            var tabIndex = $('#tabCaptions li').length - $(this).parent().nextAll().length - 1;
            //Change tabs.
            var x = $('#tabCaptions li strong span').text();
            $('#tabCaptions li:has(strong)').html("<a onmouseover='javascript:this.style.cursor=\"Pointer\"' onclick='javascript:LoadContent(\"" + x + "\");' title=''>" + x + "</a>");
            $(this).parent().html("<strong><span>" + $(this).text() + "</strong></span>");
            //Change tab's content.
            $('.activeTabContent').removeClass('activeTabContent');
            $('#bc span').removeClass('activeTabContent');
            $('table.elements:eq(' + tabIndex + '), ul.list3:eq(' + tabIndex + '), .tabContent:eq(' + tabIndex + '), #bc span:eq(' + tabIndex + ')').addClass('activeTabContent');
            createNewHandlers_2();
        });
    }
    createNewHandlers_2();

    // Create Fancy box and tab's support for Fancy box.
    if ($.isFunction($.fn.fancybox)) {

        // Tab's support for Fancy box.
        function createNewHandlers() {

            if (($('#fancy_div ul.agencyTabs li:first').find("a")).length) {
                $('#fancy_div #lbContent .innerBottom > div:first').hide();
                $('#fancy_div #lbContent .innerBottom > div:last').show();
            }
            if (($('#fancy_div ul.agencyTabs li:last').find("a")).length) {
                $('#fancy_div #lbContent .innerBottom > div:first').show();
                $('#fancy_div #lbContent .innerBottom > div:last').hide();
            }

            $('#fancy_div ul.agencyTabs li a').click(function cl() {

                if ($(this).parent().hasClass("last")) {
                    $(this).parent().parent().addClass('lastActive');
                }
                else {
                    $('#fancy_div ul[class*="lastActive"]').removeClass('lastActive');
                }
                var linkText = $('#fancy_div ul.agencyTabs li.active strong span').text();
                $(this).parent().parent().find('li:has(strong)').html("<a onmouseover='javascript:this.style.cursor=\"Pointer\"' onclick='javascript:LoadContent(\"" + linkText + "\");' title=''>" + linkText + "</a>");
                $(this).parent().parent().find('.active').removeClass('active');
                $(this).parent().addClass('active');
                $(this).parent().html("<strong><span>" + $(this).text() + "</strong></span>");

                createNewHandlers();

                return false;
            });
        }

        // Create Fancy box.
        $("a.view").fancybox({
            'hideOnContentClick': false,
            'frameWidth': 900,
            'frameHeight': 670,
            callbackOnShow: createNewHandlers
        });
    }
});
