// vars for the nav system
menuWidth = 'shadow'; /* fat=fixed, shadow=aligned */
ie6fix = 0;


//Hover intent - to fix menu
(function ($) { $.fn.hoverIntent = function (f, g) { var cfg = { sensitivity: 7, interval: 100, timeout: 0 }; cfg = $.extend(cfg, g ? { over: f, out: g} : f); var cX, cY, pX, pY; var track = function (ev) { cX = ev.pageX; cY = ev.pageY }; var compare = function (ev, ob) { ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t); if ((Math.abs(pX - cX) + Math.abs(pY - cY)) < cfg.sensitivity) { $(ob).unbind("mousemove", track); ob.hoverIntent_s = 1; return cfg.over.apply(ob, [ev]) } else { pX = cX; pY = cY; ob.hoverIntent_t = setTimeout(function () { compare(ev, ob) }, cfg.interval) } }; var delay = function (ev, ob) { ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t); ob.hoverIntent_s = 0; return cfg.out.apply(ob, [ev]) }; var handleHover = function (e) { var ev = jQuery.extend({}, e); var ob = this; if (ob.hoverIntent_t) { ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t) } if (e.type == "mouseenter") { pX = ev.pageX; pY = ev.pageY; $(ob).bind("mousemove", track); if (ob.hoverIntent_s != 1) { ob.hoverIntent_t = setTimeout(function () { compare(ev, ob) }, cfg.interval) } } else { $(ob).unbind("mousemove", track); if (ob.hoverIntent_s == 1) { ob.hoverIntent_t = setTimeout(function () { delay(ev, ob) }, cfg.timeout) } } }; return this.bind('mouseenter', handleHover).bind('mouseleave', handleHover) } })(jQuery);

// vars for the solutuion map viewer
var up = true;
var in_header = false;
var target_height;
var header_height = 37;

$(window).resize(function () {
      handleResize();
});

function handleResize() {

    if ($('#header').length == 0) return;
    ypos = $('#header').position().top + $('#header').height();
    $.each($('div.mega'), function (index, value) {

        pos = $(this).parent().position().left + $(this).parent().width();

        if (menuWidth == 'fat') {
            $('ul.nav').addClass('fat');
            $('ul.nav').removeClass('shadow');
            $(this).css("left", $('.feature').position().left + 430);
        }
        if (menuWidth == 'shadow') {
            $('ul.nav').removeClass('fat');
            $('ul.nav').addClass('shadow');
            $(this).css("left", (pos - $(this).width() + 8) + "px");
            $(this).css("left", pos - 305 + 8 + "px");
        }

    });

    $('div.mega').css('margin-top', '' + ypos + 'px');
    $('.modalFrame').css('height', "" + $('#s4-workspace').attr('scrollHeight') + "px")
        .css('width', "" + $('#s4-workspace').attr('scrollWidth') + "px");
    // $('.breadcrumb_functions .sitesmenu .sites').css('left', $('#s4-workspace').position().left + 1000 - $('.breadcrumb_functions .sitesmenu .sites').width() - 20);
    var divFeature = $('div.feature');
    if (divFeature.position() && divFeature.position().left) {  /* skip this if the feature panel is not being used */
        $('div.feature .image').css('left', divFeature.position().left)
            .css('top', divFeature.position().top);
    }
   // setTimeout('handleResize()', 100);
    
    
}
handleResize();

function AddRoundedCornersInIE() {
    $('ul.nav li.depth0').prepend('<div class="ieFixStart"></div>');
    $('ul.nav li.depth0').append('<div class="ieFixEnd"></div>');
    $('ul.nav li a.depth0').css('padding-left', '5px');
    $('ul.nav li a.depth0').css('padding-right', '5px');
}

// The toggle paragraph JQuery Extension

(function ($) {

    $.fn.toggle = function (settings) {
        var config = { 'foo': 'bar' };
        if (settings) $.extend(config, settings);

        this.each(function () {
            tag = $(this);
            if ($(this)[0].nodeName == "TABLE") {   // tables animate oddly, so wrap
                tag = $(this).wrap('<div></div>').parent('div');
            }
            tag.hide();
            tag.before('<div class="toggleControl">Click For More Detail</div>');
        });

        $('.toggleControl').live('click', function () {
            if ($(this).hasClass('down')) {
                $(this).contract();
            } else {
                $(this).expand();
            }
            return false;
        });

        return this;
    };



    $.fn.contract = function () {
        this.each(function () {
            $(this).next('div').slideUp();
            $(this).removeClass('down');
            $(this).html('Click For More Detail')
        });
    }

    $.fn.expand = function () {
        this.each(function () {
            $(this).next('div').slideDown();
            $(this).addClass('down');
            $(this).html('Click To Hide Detail')
        });
    }

})(jQuery);

// The modal popup JQuery extension

(function ($) {

    $.fn.modal = function (settings) {

        var config = { 'foo': 'bar' };
        if (settings) $.extend(config, settings);

        this.each(function () {
            if ($(this).find("div.modalScroll").length == 0) {
                $(this).hide();
                $(this).wrapInner('<div class="modalScroll">');
                $(this).wrapInner('<div class="modalBorder">');
                $(this).wrapInner('<div class="modalPositioner">');
                $(this).find('div.modalBorder').prepend('<a class="modalTop" href="#" title="Close Window">&nbsp;</a>');
            }
        });

        $('.modalTop').click(function () {
            $('#modalFrameDark').hide();
            $('.modal').hide();
        });

        return this;
    };

    $.fn.display = function () {
        this.each(function () {
            $('#modalFrameDark').show();
            $(this).show();
            // $(this).wrapInner('<div class="modalFrame">');

        });
    }

})(jQuery);





















// Solution map viewer functions

function slideDown() {
    target_height = $('.sliderDrawerBody img').height() + header_height;
    height = $('.sliderDrawer').height();
    height = height + 20;
    if (height > target_height) height = target_height;
    $('.sliderDrawer').css('height', '' + height + 'px');
    if (height < target_height && !up) {
        setTimeout('slideDown()', 10);
    }
}

function slideUp() {
    offset = 0;
    if (in_header) offset = 5;
    height = $('.sliderDrawer').height();
    height = height - 20;
    if (height < (header_height + offset)) height = (header_height + offset)
    $('.sliderDrawer').css('height', '' + height + 'px');
    if (height > (header_height + offset) && up) {
        setTimeout('slideUp()', 10);
    }
}

// screenshot viewer functions

function screenshotCloseHelpMessage() {
    $('.screenshotHelpMessage').fadeOut('slow');
}


EclipseJsDocumentReadyRun = 0;

$(document).ready(function () {

    var offset1 = $("div.News_blog div.News_right .title").height();
    var offset2 = $("div.News_blog div.News_right .background").height();
    $("div.News_blog .upperPanel").css({ 'height': offset1 + offset2 + "px" });


    if ($(".News_left .rssinlistwebpart").is(':visible'))
        $(".News_left .linkedininlistwebpart").css({ 'padding-left': 0 + "px" });
    else
        $(".News_left .linkedininlistwebpart").css({ 'padding-left': 8 + "px" });



    if ($(".News_left .rss").is(':visible'))
        $(".News_left .linkedin").css({ 'padding-left': 8 + "px" });
    else
        $(".News_left .linkedin").css({ 'padding-left': 18 + "px" });

    // guard against running twice : can happen if referenced from the Dot Net Nuke site
    EclipseJsDocumentReadyRun++;
    if (EclipseJsDocumentReadyRun > 1)
        return;


    if (disableFancyJavaScripts) //remove the padding on the webpart zones.
        $(".ms-SPZone").attr("cellspacing", "0")


    extrapadIE = 0;
    if ($('#header').length == 1) {// pls skip this bit if we are not displaying the header (DNN site).  Thx!

        // fixes for IE in the nav system

        if ($.browser.msie && ($.browser.version < 9)) {
            AddRoundedCornersInIE();
            extrapadIE = 35;
        }

        /*if ($.browser.msie && ($.browser.version < 7)) {*/
        $('ul.nav li').hoverIntent(function () {
            $(this).find('.mega').show();
            /*  $(this).addClass('ie6Fix');*/
        }, function () {

            $(this).find('.mega').hide();
            /*  $(this).removeClass('ie6Fix');*/
        });


        /* }*/

        // support for the mega menu (probably safe to delete as we aren't using it)

        $('div.mega.mainMegaMenu .section:not(:last-child)').css('border-right', 'solid 1px #999999');
        $('div.mega.mainMegaMenu .section:not(:first-child)').css('padding-left', '10px');


    }



    //Collapsing zones
    if (!disableFancyJavaScripts) {
        $(".leftTop,.leftBottom,.rightBottom,.rightTop").each(function () {

            if ($(this).children().not(".ms-SrvMenuUI").size() <= 0) {
                // alert("removeing " + $(this).attr("class"));
                $(this).remove();

            }
        });

        if ($(".mainContent").children().size() <= 0) {
            $(".mainContainer").css("min-height", "20px");
        }

        if ($(".topRight").children().not(".ms-SrvMenuUI").size() <= 0) {
            $(".topLeft").width(980);
        }
    }

    if ($(".leftTop").size() == 0) {
        $(".page_body .main").width(710);
    }

    if ($(".rightTop").size() == 0) {
        $(".page_body .main").width(750);
    }

    if ($(".leftBottom").size() == 0 && $(".rightBottom").size() > 0) {
        $(".new_floating .main").width(710);
    }
    else if ($(".rightBottom").size() == 0 && $(".leftBottom").size() > 0) {
        $(".new_floating .main").width(750);
    }
    else if ($(".rightBottom").size() == 0 && $(".leftBottom").size() == 0) {
        $(".new_floating .main").css("margin-left", "10px");
        $(".new_floating .main").css("margin-right", "10px");
        $(".new_floating .main").width(980);
    }
    else
        $(".new_floating .main").width(480);


    //$('div.News_blog .News_main').css('position', 'absolute');



    /*

    TF fixed some defects / increased performance then commented out 10/05/11
   


    // Set up the solution map viewer for verticals
    $('div.sliderDrawer').click(function () {
    if (up) {
    $(this).addClass('down').css('cursor', 'auto');
    .find('.drawerTitle').html('Click To Hide');
    up = false;
    slideDown();
    } else {
    $(this).removeClass('down').css('cursor', 'hand');
    .find('.drawerTitle').html('Click To View');
    up = true;
    slideUp();
    }
    });
    
    
    $('div.sliderDrawer').hover(
    function () {
    in_header = true;
    if (up) {
    $(this).css('height', (header_height + 5) + 'px');
    $(this).css('padding-top', '0px');
    }
    },
    function () {
    in_header = false;
    if (up) {
    $(this).css('height', header_height + 'px');
    $(this).css('padding-top', '5px');
    }
    }
    );

    //set up the screenshot viewer 
    $('.screenshotShow').click(function () {
    //$('.screenshotHelpMessage').css('top', ($('#s4-workspace').height() / 2) - 50 + 'px');
    handleResize(); // just incase
    $('#s4-workspace').scrollTop(0);
    $('#modalFrameWhite').fadeIn('fast');
    $('.screenshotHelpMessage').fadeIn('fast', function () {
    setTimeout('screenshotCloseHelpMessage()', 800);
    });
    $("." + $(this).attr('id')).fadeIn('fast');
    });

    $('#modalFrameWhite, .screenshot, .screenshotHelpMessage').click(function () {
    $('#modalFrameWhite').fadeOut('fast');
    $(".screenshot").fadeOut('fast');
    });
    */


    //set up any accordion slider headings
    /**
    index = 0;
    $('.eclipseStyle-Accordion').each(function () {
    if (!disableFancyJavaScripts) {
    $(this).attr('index', index);
    $(this).nextUntil('.eclipseStyle-Accordion').wrapAll('<div class="accordionBody index' + index + '"></div>');
    $('.accordionBody.index' + index).css('height', $('.accordionBody.index' + index).height() + 40 + extrapadIE); 
    index++;
    }
    });
    
    $('.eclipseStyle-Accordion').live('click', function () {
    if (!disableFancyJavaScripts) {
    index = $(this).attr('index');
    $('.accordionBody').slideUp();
    if ($(this).hasClass('selected')) {
    $('.eclipseStyle-Accordion').removeClass('selected');
    } else {
    $('.eclipseStyle-Accordion').removeClass('selected');
    $(this).addClass('selected');
    $('.accordionBody.index' + index).slideDown();
    }
    }
    });
   
    */
    $('div.toggle, .eclipseTable-2').toggle();

    if (window.location.href.indexOf("hideUI") > 0) {
        $('#header').hide();
        $('.feature').hide();
        $('.footer').hide();
        $('body #s4-ribbonrow').css('display', 'none !important');
        $('#wrapper').css('background', 'none !important');
    }

    if (window.location.href.indexOf("hideNavigation") > 0) {
        $('ul.nav').hide();
        $('.breadcrumb_container *').hide();
        $('.footer *').hide();
    }

    $('.modal').modal();
    $('.modal1show').click(function () {
        $('.modal:eq(0)').display();
    });
    $('.modal2show').click(function () {
        $('.modal:eq(1)').display();
    });
    $('.modal3show').click(function () {
        $('.modal:eq(2)').display();
    });
    $('.modal4show').click(function () {
        $('.modal:eq(3)').display();
    });
    $('.modal5show').click(function () {
        $('.modal:eq(4)').display();
    });

    handleResize();

});

function initMenus() {
    var sub_navi_index = 0;
    $('div.right_nav_container h1').each(function (index) {
        if (!$(this).hasClass('eclipseStyle-PageTitle')) {
            $(this).addClass('eclipseStyle-PageTitle');
            $(this).add($(this).nextUntil('div.right_nav_container h1')).wrapAll('<div class= "eclipse_navi_content_wraper"></div>');
        }

        if ($(this).hasClass('third_navi_title')) //3rd level navigation
        {
            //var parent = $('ul#menu li a#section' + sub_navi_index);            
            var parent = $('ul#menu li:eq(' + sub_navi_index + ')');
            parent.append('<li class="third_level_link" parent=' + sub_navi_index + '><a class="third_navi_link" href="#" id="section' + index + '"> ' + $(this).text() + '</a></li>');
        }
        else {
            $('.linklist #menu').append('<li><a href="" id="section' + index + '"> ' + $(this).text() + '</a></li>');
            sub_navi_index = index; //index of the 2nd level navigation

        }

    });

    for (i = 0; i <= sub_navi_index; i++) {

        $('.linklist #menu [parent=' + i + ']').wrapAll('<ul class= "sub_wrapper"> </ul>');

    }

}

//some workarounds for the production server
$(document).ready(function () {
    //js fix for the contact us web part button image
    $('div.button').children().addClass('contactusBtn');
    turnOffYoutubeRelativeContent();
});

function turnOffYoutubeRelativeContent() {

    //js fix fof the custom youtube web part embeded url
    var links = $('div.customisedVideoWebPart');
    links.each(function () {

        var link = $(this).children(":first");
        var youtubeUrl = link.attr('src');
        link.attr('src', youtubeUrl + "&rel=0");
    });
   
}


$(document).ready(function () {        
    $('div.eclipse_navi_content_wraper').hide();
    //$('.linklist #menu a').remove();

    initMenus();

    //register event
    $('div.linklist li a').click(function (event) {
        event.stopPropagation();
        event.preventDefault();

        var id = $(this).attr("id");
        num = id.substring(id.length - 1);
        $('div.eclipse_navi_content_wraper').hide();
        $('div.eclipse_navi_content_wraper:eq(' + num + ')').show();


        if (!$(this).hasClass('third_navi_link')) {
            //click on a 2nd level link                      
            $('#menu ul:visible').slideUp(100);
            $('div.linklist #menu li').removeClass('active');
            $('a.third_navi_link').removeClass('third_level_active')
            $('li.third_level_link').removeClass('active')
            $(this).parent().addClass('active');

            //var checkElement = $(this).find(':first-child');
            var checkElement = $(this).next();
            if (checkElement.is('ul') && (!checkElement.is(':visible'))) {
                checkElement.slideDown(100);
            }

        }
        else {
            $('a.third_navi_link').removeClass('third_level_active')
            $(this).addClass('third_level_active');

            $('li.third_level_link').removeClass('active')
            $(this).parent().addClass('active');
        }
    });
    //default look
    $('#menu li a#section0').trigger('click');


    $('div.sitesmenu').click(function () {
        $(this).find('.sites').slideToggle();
    });

});

$(document).ready(function () {

    var currentRow = $.getUrlVar('row');
    if (currentRow) {
        $('div.NewsLinkPanel li:eq(' + currentRow + ')').attr('class', 'News_link_active');
    }
    else {

        var listName = $('div.NewsLinkPanel li a:eq(0)');
        if (listName && listName.html()) {
            var NewUrl = window.location.href + "?listName=" + jQuery.trim(listName.html()) + "&row=0";
            window.location.href = NewUrl;
            //console.log(listName)
        }
    }

});


//function GetData_Click() {
//    $('div.NewsLinkPanel li a').click(function (event) {
//        $('div.NewsLinkPanel li').attr('class', 'News_link');
//        $(this).parent().attr('class', 'News_link_active');
//    });

//}

function updatePageUrl(rowNumber) {
    var urlElements = [];
    var currentUrl = window.location.href;
    urlElements = currentUrl.split("?");
    var listName = $('div.NewsLinkPanel li a:eq(' + rowNumber + ')').html();
    var NewUrl = urlElements[0].replace("#", "") + "?listName=" + jQuery.trim(listName) + "&row=" + rowNumber;
    window.location.href = NewUrl;
}

$.extend({
    getUrlVars: function () {
        var vars = [], hash;
        var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
        for (var i = 0; i < hashes.length; i++) {
            hash = hashes[i].split('=');
            vars.push(hash[0]);
            vars[hash[0]] = hash[1];
        }
        return vars;
    },
    getUrlVar: function (name) {
        return $.getUrlVars()[name];
    }
});


$('#s4-workspace').scroll(function () {
    handleScroll();
});

function handleScroll() {

    drawerOffset = 0;
    ribbonHeight = 0;

    if (!up) drawerOffset = $('.sliderDrawerBody').height();
    if ($('#s4-ribbonrow').is(':visible')) ribbonHeight = $('#s4-ribbonrow').height();

    //if ($.browser.msie) {
    //	hiddenAmount = $('#s4-workspace').get(0).scrollTop;
    //} else {
    hiddenAmount = $('#s4-workspace').scrollTop();
    //}
    //$('#debug').html(hiddenAmount);

    if (hiddenAmount > menuDivMainStartPos + drawerOffset - 20) {
        $('#menuDivMain').css('position', 'fixed');
        $('#menuDivMain').css('top', 20 + ribbonHeight + 'px');
    } else {
        $('#menuDivMain').css('position', 'static');
        $('#menuDivMain').css('top', menuDivMainStartPos + 'px');
    }
}

function eventsClose(result, value) {
    if (value == '1') {
        this.statusId = SP.UI
            .Status
            .addStatus("Success",
               "Your Registration was successful.",
                true);
        SP.UI.Status.setStatusPriColor(this.statusId, "Green");
    }

    if (value == '0') {
        this.statusId = SP.UI
            .Status
            .addStatus("Error",
                "An Error Occurred. Please try again.",
                true);
        SP.UI.Status.setStatusPriColor(this.statusId, "Orange");
    }

    setTimeout(RemoveStatus, 6000);
}

/*
Below is for common JS helpers for SHarePoint specifically, if its not re-usable, please place above
*/


function openModalDialog(url, functionClosed) {
    var options = SP.UI.$create_DialogOptions();
    options.width = 400;
    options.height = 330;
    options.url = url;
    if (functionClosed)
        options.dialogReturnValueCallback = Function.createDelegate(null, functionClosed);
    else options.dialogReturnValueCallback = Function.createDelegate(null, defaultDialogClosedCallback);
    SP.UI.ModalDialog.showModalDialog(options);
}

function defaultDialogClosedCallback(result, value) {
    if (value == '1') {
        this.statusId = SP.UI
            .Status
            .addStatus("Success",
               "The action was successful.",
                true);
        SP.UI.Status.setStatusPriColor(this.statusId, "Green");
    }

    if (value == '0') {
        this.statusId = SP.UI
            .Status
            .addStatus("Error",
                "An Error Occurred. Please try again.",
                true);
        SP.UI.Status.setStatusPriColor(this.statusId, "Orange");
    }

    setTimeout(RemoveStatus, 6000);
}

function RemoveStatus() {
    SP.UI.Status.removeStatus(this.statusId);
}

