﻿/* CHANNELTIVITY JAVASCRIPT 

Requires jQuery */

/* MAIN MENU DROPDOWN PANELS
   ------------------------- */

// this variable controls whether the menu should be held open on mouseleave
holdmenu = false;

function close_submenu(menu) {
    var currentsection = $(menu).prev().text();
    window.setTimeout(function () {
        if (holdmenu != currentsection)
            menu.hide();
    }, 350);
}

jQuery(document).ready(function ($) {
    $(".dropdownlist").mouseenter(function () {
        var currentsection = $(this).children(".headerlink").text();
        if (!$(this).is(".home")) {
            $(".sub-menu").hide(); // ensure that any other sub-menus are hidden
            $(this).children(".sub-menu").fadeIn(200);
        }
    });
    $(".headerlink").mouseenter(function () {
        var currentsection = $(this).text();
        if (!($(this).closest(".dropdownlist").is(".home"))) {
            $(".sub-menu").hide(); // ensure that any other sub-menus are hidden
            $(this).children(".sub-menu").fadeIn(200);
        }
    });

    var submenu = $(".sub-menu");
    submenu.live("mouseleave", function () {
        var menu = $(this);
        close_submenu(menu);
    });
    $(".headerlink").mouseleave(function () {
        var menu = $(this).next("sub-menu");
        close_submenu(menu);
    });

    // hook to hold menu open while mouse is hovering over it
    var menuBody = $(".sub-menu > div");
    menuBody.live("mouseenter", function () {
        var currentsection = $(this).parent().prev().text();
        holdmenu = currentsection;
    });

    menuBody.live("mouseleave", function () {
        holdmenu = false;
    });

    // hook to close menu if mouse leaves header
    var menuHeader = $(".sub-menu .menuHeader");
    menuHeader.live("mouseleave", function () {
        var parent = $(this).parent();
        var currentsection = $(parent).prev().text();
        window.setTimeout(function () {
            if (holdmenu != currentsection)
                parent.hide();
        }, 10);
    });

    // hook to close menu if mouse leaves header
    var menuBody = $(".sub-menu .menuBody");
    menuBody.live("mouseleave", function () {
        var parent = $(this).parent();
        var currentsection = $(parent).prev().text();
        window.setTimeout(function () {
            if (holdmenu != currentsection)
                parent.hide();
        }, 350);
    });


    // Make nav LIs clickable and hoverable to activate/hover their child <a>s
    $("#Primary-Navigation div.sub-menu div.menuSection ul.menuItems li").live("click", function () {
        document.location = $(this).find("a")[0].href;
    }).live("mouseenter", function () {
        $(this).find("a").addClass("hover");
    }).live("mouseleave", function () {
        $(this).find("a").removeClass("hover");
    });

    $(".menuHeader").click(function () { if ($(this).find("a").size()) document.location = $(this).find("a")[0].href; })
});


/* FORM SUBMITTER
   -------------- */

jQuery(document).ready(function ($) {
    $("a.submitForm").click(function () {
        var parents = $(this).parents("form");
        $(".optionalblank").val('');
        $(parents[0]).submit();
    });
});

/* FORM VALIDATION BEHAVIOR
   ------------------------ */
jQuery(document).ready(function ($) {
    var optionalInputs = $('.ContactForm input[type="text"], .ContactForm textarea').not('.required');

    optionalInputs.each(function () {
        if ($(this).val() == "") {
            $(this).addClass("optionalblank");
            $(this).val("Optional");
        } else {
            $(this).removeClass("optionalblank");
        }
    });

    optionalInputs.blur(function () {
        if ($(this).val() == "") {
            $(this).addClass("optionalblank");
            $(this).val("Optional");
        } else {
            $(this).removeClass("optionalblank");
        }
    });

    optionalInputs.focus(function () {
        if ($(this).hasClass("optionalblank")) {
            $(this).val('');
            $(this).removeClass("optionalblank");
        }
    });
});


/* FANCYBOX SUPPORT
---------------- */

jQuery(document).ready(function ($) {
    $("a.screenshot, .screenshot a").fancybox({
        'transitionIn' : 'fade',
        'transitionOut' : 'fade',
        'speedIn' : 600,
        'overlayShow': true,
        'overlayColor' : '#fff',
        'padding' : 0,
        'centerOnScroll' : true,
        'overlayOpacity' : 0.8,
        'cyclic' : true,
        'titlePosition' : 'over',
        'titleFormat' : formatTitle
    });
});

function formatTitle(title, currentArray, currentIndex, currentOpts) {
    var pictitle = $(currentArray[currentIndex]).children('img').attr('alt');
    var returnstring = '<div id="fancybox-title-over"><strong>' + pictitle + ':</strong> ' + title + '</div>'; 
    return returnstring;
}
/* CLIENT LOGO ROTATOR
   ------------------- */


jQuery(document).ready(function ($) {
    if (($.browser.msie) && ( ($.browser.version == '7.0') || ($.browser.version == '8.0') )) {
        $('#Hero2 a, #Hero3 a').each(function () {
            MAPPEDLINK = $(this);
            LINKOVERLAY = $(document.createElement('a'));
            LINKOVERLAY
            .addClass('overlay-link')
            .css({
                'top': MAPPEDLINK.get(0).offsetTop,
                'left': MAPPEDLINK.get(0).offsetLeft,
                'height': MAPPEDLINK.css('padding-top'),
                'width': MAPPEDLINK.width()
            }).attr('href', MAPPEDLINK.attr('href'));

            $(document.createElement('div')).css({
                'height': MAPPEDLINK.css('padding-top'),
                'width': MAPPEDLINK.width(),
                'background-color': 'black',
                'opacity': 0,
                'cursor':'pointer'
            }).appendTo(LINKOVERLAY);

            LINKOVERLAY.insertBefore(MAPPEDLINK);
        });
    }
});
