/** 
* Delayer function 
*/
$.fn.delay = function (time, callback) {
    jQuery.fx.step.delay = function () { };
    return this.animate({ delay: 1 }, time, callback);
}

/**
* IF IE6
*/
var isIE6 = (navigator.userAgent.toLowerCase().substr(25, 6) == "msie 6") ? true : false;

/** 
* Element exists function 
*/
$.fn.exists = function () { return jQuery(this).length > 0; }

/** 
* On navigation out
*/
function navigationHoverOut() {
    
    $("#smallBox").stop().css({ display: "none" });
    $("#topnav li .sub").stop().css({ display: "none" });
    $("#topnav li .col").stop().css({ visibility: "hidden" });
    $('#softwareSplash').html($.defaultSoftwareHTML);
    $("#bigBox").stop().css({ display: "none" });
}

/** 
* On navigation over 
*/
function navigationHoverOver(e) {

    var bwidth = 0; // Tab width
    var cSize = 0;  // Number of columns in row
    var maxCol = 0  // Max nbr of columns
    var relativeX = 0;
    var containerOffset = $("#rs-navigation").offset();
    var tabOffset = $(this).offset();
    var relativeCX = (e.pageX - containerOffset.left);
    var relativeTX = (e.pageX - tabOffset.left);
    var totalWidth = 0;
    var contentPos = 0; 									    // Content position (relative)
    var smallBoxPos = bigBoxPos = relativeCX - relativeTX - 25; // Box positions		(absolute)
    var maxHeight = 0;
    var totHeight = 0;
    var rowHeight = 0;
    var smallBoxPadding = (isIE6 == true) ? 25 : 30;

    /**
    * Get the tab width and position
    */
    $('.row', this).each(function () { // Get max number of columns in tab
        cSize = $('.col', this).size();
        if (cSize > maxCol)
            maxCol = cSize;
    });

    if (maxCol != 0) { // Set big box width, get the totalWidth
        switch (maxCol) {
            case 3: bwidth = 655;
                break;
            case 2: bwidth = 450;
                break;
            case 1: bwidth = 230;
                break;
            default: bwidth = 655;
        }

        totalWidth = relativeCX - relativeTX + bwidth;

        if (totalWidth > 974) {
            contentPos = 974 - totalWidth;
            bigBoxPos += contentPos + 20;
            contentPos += 20;
        }

    }
    else {
        jQuery.error("No columns in tab");
    }


    /**
    * Render the tab
    */
    $('.sub', this).css({ display: "block", left: contentPos - 20 }); // Display the content

    
    if ($('.col:not(#processInfo .col)', this).exists()) { // Display the columns one by one
        $('.col:not(#processInfo .col)', this).each(function (i) {
            $(this).delay((80 * i) + 500, function () {
                $(this).css({ visibility: "visible" });
            });
        });
    }

    if ($('#processInfo', this).exists()) {
        $('.process ul li', this).each(function (l) {
            if(l==0)
                $(this).css({ "background-color": "#02609c" });
            else
                $(this).css({ "background-color": "transparent" });
         });
        $('#processInfo', this).children().each(function (i) { 
            if (i == 0) {
                $(this).removeClass("hidden");
                $('.col', this).each(function (t) {
                    $(this).delay((80 * t) + 500, function () {
                        $(this).css({ visibility: "visible" });
                    });
                });
            }
            else
                $(this).addClass("hidden");

        });
    }
    
    $('.row', this).each(function () { // Get the tab height
        $('.col', this).each(function () {
            if ($(this).height() > maxHeight)
                maxHeight = $(this).height();
        });
        totHeight += maxHeight;
    });

    if (totHeight < 180) { // Set the Gradient type
        totHeight = 200;
        $("#bigBox .dialog").addClass("halfSizeBigBox");
    }
    else {
        totHeight = 325;
        $("#bigBox .dialog").removeClass("halfSizeBigBox");
    }

    $('.row', this).each(function () { // If multiple rows, adjust height
        $('.col', this).each(function () {
            if ($(this).height() > rowHeight)
                rowHeight = $(this).height();
        });
        $(this).css({ width: bwidth - 35, height: rowHeight });
        rowHeight = 0;
    });


    /**
    * Render the background boxes
    */
    $("#smallBox").css({ width: $(this).width() + smallBoxPadding, display: "block", left: smallBoxPos + 25, top: 7 }); // Set size
    $("#bigBox .dialog, #smallBox .dialog ").css({ display: "block" });
    $("#bigBox").css({ display: "none", width: bwidth * 0.8, left: bigBoxPos }); // Set size	
    $("#bigBox .wrapper").css({ height: totHeight * 0.8 });

    $("#smallBox").stop().animate( // Animate boxes
		{top: 0 }, "fast", "easeInOutQuint").show();
    $("#bigBox").stop().animate(
		{ width: bwidth }, "fast", "easeInOutQuint").show();
    $("#bigBox .wrapper").stop().animate(
		{ height: totHeight }, "fast", "easeInOutQuint").show();
    

    /**
    * Render the link arrows
    */
    $('.col a:not(.process a, splash a)', this).each(function () { // Adjust arrows to a-tags width.
        var w = $(this).width();
        $(this).siblings('.arrow, .arrowSmall').css({ left: (5 + w) });
    });

    $('.col a:not(.process a, .splash a)', this).mouseover(function () { // On link over, animate arrow.
        var w = $(this).width();
        $(this).siblings('.arrow, .arrowSmall').stop().animate({ left: (15 + w), opacity: 0 }, 200, function () {
            $(this).css({ left: (5 + w), opacity: 1 });
        });
    });


    /**
    * Software tab
    */
    if ($('.software', this).exists()) {
        
        // Resize the splashbox
        if (!$('#softwareSplash').hasClass("softwareSplashExtended softwareSplashDefault")) {
            switch (maxCol) {
                case 3: $('#softwareSplash').addClass("softwareSplashExtended");
                    break;
                case 2: $('#softwareSplash').addClass("softwareSplashDefault");
                    break;
                default: $('#softwareSplash').addClass("softwareSplashDefault");
            }
        }

        $('.software a', this).mouseover(function () { // On softwarelink over, show software info. 
            $('#softwareSplash').html($(this).siblings(".softwareInfo").html());
            $('#softwareSplash img').css({ opacity: 0 }).stop().animate({ opacity: 1 }, "slow", "jswing").show();  
        });
        
        $('.navlink', this).mouseover(function () { // On softwarelink over, show software info.
            $('#softwareSplash').html($.defaultSoftwareHTML);
            $('#softwareSplash img').css({ opacity: 0 }).stop().animate({ opacity: 1 }, "slow", "jswing").show();
        });
    }
    

    /**
    * Process tab
    */
    if ($('.process', this).exists()) {
        $('.process a', this).mouseover(function () {
            $(this).parent('li').css({ "background-color": "#02609c" });
            $(this).parent('li').siblings().css({ "background-color": "transparent" });
            var c = $(this).parent('li').attr("class");

            $(this).parents('.col').siblings('#processInfo').children().each(function () {
                if ($(this).hasClass(c)) {
                    $(this).removeClass("hidden");

                    $('.col', this).each(function () {
                        $(this).css({ visibility: "visible" });
                    });

                    $('a', this).each(function () {
                        var w = $(this).width();
                        $(this).siblings('.arrow, .arrowSmall').css({ left: (5 + w), top: 0 });
                    });
                }
                else {
                    $(this).addClass("hidden");
                }
            });
        });
    }
    
}

$(document).ready(function () {

    // Initialize
    var config = {
        sensitivity: 3, 			// number = sensitivity threshold (must be 1 or higher)    
        interval: 200, 			    // number = milliseconds for onMouseOver polling interval    
        over: navigationHoverOver,  // function = onMouseOver callback (REQUIRED)    
        timeout: 80, 				// number = milliseconds delay before onMouseOut    
        out: navigationHoverOut	    // function = onMouseOut callback (REQUIRED)    
    };

    $.defaultSoftwareHTML = $('#softwareSplash').html(); // Default software info

    $("#topnav .tab").hoverIntent(config);
    $('#topnav a').click(function () { navigationHoverOut(); });
    $("#topnav .col a").not("#topnav .col ul li ul li a").not(".process a").not(".splash a").each(function () { $(this).after('<div class="arrow"></div>'); });
    $('#topnav .col ul li ul li a').not('.process a').each(function () { $(this).after('<div class="arrowSmall"></div>'); });

});
