﻿$(document).ready(function() {

    // primary nav mega dropdown
    $("div.wrapper-parent").hide();
    var config = {
        sensitivity: 3, // number = sensitivity threshold (must be 1 or higher)    
        interval: 200, // number = milliseconds for onMouseOver polling interval    
        over: primaryDown, // function = onMouseOver callback (REQUIRED)    
        timeout: 500, // number = milliseconds delay before onMouseOut    
        out: primaryUp // function = onMouseOut callback (REQUIRED)    
    };
    /*$("#primarynav ul > li:has(ul)").hover(function(){
    $("div.wrapper-parent").slideDown('slow');
    $(this).find('div.wrapper').slideDown('slow');
    $(this).addClass("over");
    $(this).find("ul.sub-nav > li:has(ul) > a").append("<span>&nbsp;&gt;</span>");
    },
    function(){
    $(this).find('div.wrapper').slideUp('fast');
    $(this).removeClass("over");
    $(this).find("ul.sub-nav > li:has(ul) > a span").remove();
    });*/
    $("#primarynav ul > li:has(ul)").hoverIntent(config);

    $("#primarynav-wrapper").mouseleave(function() {
        if ($("div.wrapper").not(":hidden")) {
            $("div.wrapper").hide();
        }
        if ($("div.wrapper-parent").not(":hidden")) {
            $("div.wrapper-parent").slideUp('fast');
        }
        $("#primarynav ul > li:has(ul).over").removeClass("over");
    });

    $("#primarynav ul ul > li").hover(function() {

        $(this).find('div.sub-wrapper').css('display', 'block');
        $(this).addClass("over");

    },
        function() {
            $(this).find('div.sub-wrapper').css('display', 'none');
            $(this).removeClass("over");
        });

    // home page news-events-view
    $('#news-events-view ul li#events-tab').addClass('over');
    $('ul#news-scroller').hide();
    $('a#see-all-news-events').attr("href", "About_industry_events.aspx");
    var newscrawl = $('ul#events-scroller');
    $(newscrawl).cycle({
        fx: 'scrollDown'
    });
    $('#news-events-view > ul li').hover(function() {
        if (!$(this).hasClass('over')) {
            $(this).addClass('over').siblings('li').removeClass('over');
            if ($(this).attr('id') == 'news-tab') {
                newscrawl = $('ul#news-scroller');
                $('ul#events-scroller').fadeOut();
                $('ul#news-scroller').fadeIn();
                $('a#see-all-news-events').attr("href", "About_industry_and_company_news.aspx");
            } else {
                newscrawl = $('ul#events-scroller');
                $('ul#news-scroller').fadeOut();
                $('ul#events-scroller').fadeIn();
                $('a#see-all-news-events').attr("href", "About_industry_events.aspx");
            }
            $(newscrawl).cycle({
                fx: 'scrollDown'
            });
        }
    });
    $('div#news-events-scroller').hover(
        function() {
            $(newscrawl).cycle('pause');
        },
        function() {
            $(newscrawl).cycle('resume');
        });

    //movie launch panel
    var maxHeight = "0";
    $("div.movie-launch-panel").each(function() {
        if ($(this).height() > maxHeight)
            maxHeight = $(this).height();
    });
    $("div.movie-launch-panel").each(function() {
        $(this).css('min-height', maxHeight);
    });
    $("ul.movie-rotator li").css('min-height', maxHeight);
    //$('#movie-view').tabs();
    /*var rotator = $('div#movie-view ul.movie-rotator');
    $(rotator).cycle({
    fx: 'scrollHorz',
    //timeout: 8000,
    timeout: 0,
    speed: 750,
    pager: 'div#movie-view div.nav'
    });
    $("#movie-view div.nav a:nth-child(1)").text("Expert Insights");
    $("#movie-view div.nav a:nth-child(2)").text("Case Studies").addClass("movie-icons");
    $("#movie-view div.nav a:nth-child(3)").text("Client Testimonials").addClass("movie-icons");*/

    // movie display panel
    var movieCurrentParent = "";
    var moviePanel = "";
    $('div.movie-display-panel').each(function() {
        $(this).hide();
    });
    $('div.movie-launch-panel div.launch-cell div.button').click(function() {

        var movieTabIndex = "";
        //var movieParentID = $(this).parent().parent().attr("id");
        var movieParentID = $(this).parents().find('div.movie-launch-panel').attr("id");
        var movieIndex = 0;
        var movieButtonClass = $(this).attr("className");

        if (movieCurrentParent != movieParentID && movieCurrentParent != "")
            $(this).parents().find('div.movie-display-panel').fadeOut();

        movieIndex = movieButtonClass.substring(movieButtonClass.length - 2, movieButtonClass.length);
        //alert(movieIndex);
        movieTabIndex = "#" + movieParentID + "-movie-" + movieIndex;
        //alert(movieTabIndex);
        moviePanel = "#" + movieParentID + "-launch-display";
        movieCurrentParent = movieParentID;
        //var $movieTabs = $(moviePanel).tabs();
        //$movieTabs.fadeIn().tabs('select', movieTabIndex);
        //setCurrentThumb(moviePanel);
        //return false;
        $(moviePanel).fadeIn().find(movieTabIndex).fadeIn().siblings("div.movie-display-screen").hide();
        $("#movie-view").css('margin-top', 0);
        var movieTabHeight = $(movieTabIndex).height();
        var movieViewerTopMargin = "";
        if (movieTabHeight > 214) {
            movieViewerTopMargin = movieTabHeight - 214;
            $("#movie-view").css('margin-top', movieViewerTopMargin);
        }
        //alert(movieViewerTopMargin);
    });

    $('div.movie-display-panel div.caption a.close').click(function() {
        $(this).parents().find('div.movie-display-panel').fadeOut();
    });

    $('div.movie-display-panel div.close').click(function() {
    $(this).parents().find('div.movie-display-panel').fadeOut();
    $("#movie-view").css('margin-top', 0);
    });

    $(".movie-display-panel ul li a").click(function() {
        //alert(moviePanel);
        var currentThumb = "";
        currentThumb = $(this).find("img").attr("src");
        currentThumb = currentThumb.replace('_off.jpg', '_on.jpg');
        $(this).find("img").attr("src", currentThumb);
        $(moviePanel).find("li img").each(function() {
            if ($(this).attr("src") != currentThumb) {
                var defaultThumb = $(this).attr('src');
                defaultThumb = defaultThumb.replace('_on.jpg', '_off.jpg');
                $(this).attr('src', defaultThumb);
            }
        });
    });

    function setCurrentThumb(thePanel) {
        var currentThumb = "";
        currentThumb = $(thePanel).find("li.ui-tabs-selected img").attr('src');
        currentThumb = currentThumb.replace('_off.jpg', '_on.jpg');
        $(thePanel).find("li.ui-tabs-selected img").attr('src', currentThumb);
        $(thePanel).find("li img").each(function() {
            if ($(this).attr("src") != currentThumb) {
                var defaultThumb = $(this).attr('src');
                defaultThumb = defaultThumb.replace('_on.jpg', '_off.jpg');
                $(this).attr('src', defaultThumb);
            }
        });

    }

    $("#masthead div.search input.text").val("Search");

    $("#masthead div.search input.text").click(function() {
        if ($(this).val() == "Search")
            $(this).val("");
    });

    $("#masthead div.search input.text").blur(function() {
        if ($(this).val() == "")
            $(this).val("Search");
    });

});

// primary navigation drop-down menu functions
function primaryDown(){
        $("div.wrapper-parent").slideDown('slow');
        $(this).find('div.wrapper').slideDown('slow');
        $(this).addClass("over");
        $(this).find("ul.sub-nav > li:has(ul) > a").append("<span>&nbsp;&gt;</span>");
}

function primaryUp(){
        $(this).find('div.wrapper').slideUp('fast');
        $(this).removeClass("over");
        $(this).find("ul.sub-nav > li:has(ul) > a span").remove();
}

$.fn.cycle.transitions.scrollHorz = function($cont, $slides, opts) {
    $cont.css('overflow','hidden').width();
    opts.before.push(function(curr, next, opts, fwd) {
        $.fn.cycle.commonReset(curr,next,opts);
        opts.cssBefore.left = fwd ? (next.cycleW-1) : (1-next.cycleW);
        opts.animOut.left = fwd ? -curr.cycleW : curr.cycleW;
    });
    opts.cssFirst = { left: 0 };
    opts.cssBefore= { top: 0 };
    opts.animIn   = { left: 0 };
    opts.animOut  = { top: 0 };
};