$(document).ready(function ()
{
    // !Setup Roundabout
    $('.announcements').append('<ul id="leftSideli" class="splitCol"></ul><ul id="rightSideli" class="splitCol"></ul>');

    var imgExt = '.png';
    if ($.browser.msie == true) imgExt = '.jpg';
    $('#positioning_needs li.developers').html("<img src='/sfxhome/images/icon_developer" + imgExt + "' /><span>Developers</span>");
    $('#positioning_needs li.mobile').html("<img src='/sfxhome/images/icon_mobile" + imgExt + "' /><span>Mobile</span>");
    $('#positioning_needs li.sharepoint').html("<img src='/sfxhome/images/icon_sharepoint" + imgExt + "'/><span>SharePoint</span>");

    // !ROUNDABOUT HOVERS
    $('#positioning_needs li.developers img').mouseover(function ()
    {
        if ($(this).closest('li').hasClass('roundabout-in-focus') == false) $(this).attr('src', '/sfxhome/images/icon_developer-over' + imgExt);
    });

    $('#positioning_needs li.developers img').mouseout(function ()
    {
        if ($(this).closest('li').hasClass('roundabout-in-focus') == false) $(this).attr('src', '/sfxhome/images/icon_developer' + imgExt);
    });

    $('#positioning_needs li.mobile img').mouseover(function ()
    {

        if ($(this).closest('li').hasClass('roundabout-in-focus') == false) $(this).attr('src', '/sfxhome/images/icon_mobile-over' + imgExt);
    });
    $('#positioning_needs li.mobile img').mouseout(function ()
    {
        if ($(this).closest('li').hasClass('roundabout-in-focus') == false) $(this).attr('src', '/sfxhome/images/icon_mobile' + imgExt);
    });
    $('#positioning_needs li.sharepoint img').mouseover(function ()
    {

        if ($(this).closest('li').hasClass('roundabout-in-focus') == false) $(this).attr('src', '/sfxhome/images/icon_sharepoint-over' + imgExt);
    });
    $('#positioning_needs li.sharepoint img').mouseout(function ()
    {
        if ($(this).closest('li').hasClass('roundabout-in-focus') == false) $(this).attr('src', '/sfxhome/images/icon_sharepoint' + imgExt);
    });


    $('#positioning_needs').roundabout(
    {
        minOpacity: 0.8,
        minScale: 0.4
    });
    $('.secondaryCol h3 ').wrapInner('<span/>');
    $('#positioning_needs li').blur(function ()
    {
        var mySelection = $(this).attr('class');
        $('.roundabout-moveable-item span').attr('style', 'display:none');
        mySelection = mySelection.replace(" roundabout-moveable-item", "");
    });

    // !Show Subpositioning based on roundabout selection
    $('#positioning_needs li').focus(function ()
    {

        var mySelection = $(this).attr('class');
        mySelection = mySelection.replace(" roundabout-moveable-item", "");
        mySelection = mySelection.replace(" roundabout-in-focus", "");
        $('#positioning_heading li.active').fadeOut('slow');
        $('#positioning_heading li.active').removeClass('active');
        $('#positioning_subheading > li.active').hide();
        $('#positioning_subheading li.active').removeClass('active');
        $('#positioning_heading li.' + mySelection).fadeIn('slow');
        $('#positioning_heading li.' + mySelection).addClass('active');
        $('#positioning_subheading li.' + mySelection).show();
        $('#positioning_subheading li.' + mySelection).addClass('active');

        $('.roundabout-moveable-item span').attr('style', 'display:block');
        $('.roundabout-in-focus span').attr('style', 'display:none');
    });

    $('#positioning_needs li.developers').focus(function ()
    {
        LoadDevSubPositioning();
    });
    $('#positioning_needs li.mobile').focus(function ()
    {
        LoadMobileSubPositioning();

    });
    $('#positioning_needs li.sharepoint').focus(function ()
    {
        LoadSharePointSubPositioning();
    });

    $('#positioning_needs li.developers').focus();

    $('.subpositioning_nav li a').each(function ()
    {
        var topSpace = '2px';
        if ($(this).parent().hasClass('dataparts') || $(this).parent().hasClass('vtc'))
        {
            topSpace = '6px';
        }
        var myIndex = parseInt($(this).parent().index());
        var myBGPos = 'background-position: ' + myIndex * -32 + 'px ' + topSpace;
        $(this).attr('style', myBGPos);
    });

    // !Subpositioning Nav Click
    $('#developers_nav li a').click(function ()
    {
        currDevIndex = parseInt($(this).parent().index());
        ShowDevSubPos();
        return false;
    });

    $('#mobile_nav li a').click(function ()
    {
        currMobileIndex = parseInt($(this).parent().index());
        ShowMobileSubPos();
        return false;
    });

    $('#sharepoint_nav li a').click(function ()
    {
        currSharePointIndex = parseInt($(this).parent().index());
        ShowSharePointSubPos();
        return false;
    });

    // !Timers for roundabout animation
    $('#positioning_needs, #positioning_subheading').mouseover(function ()
    {
        StopPositioningTimer();
    });

    $('.subpositioning_nav li a, #positioning_needs li').click(function ()
    {
        optionsClicked = true;
        StopPositioningTimer();
    });

    $('#positioning_needs, #positioning_subheading').mouseout(function ()
    {
        if (optionsClicked == false)
        {
            StartPositioningTimer();
        }
    });

    var timerRoundabout;
    StartPositioningTimer();

    // !News and Announcement Setup
    $('ul.announcements li.hl').each(function ()
    {
        var newPosTop = parseInt($(this).outerHeight()) + parseInt($('#newsrotator').css('top'));
        $('#newsrotator').css('top', newPosTop + 'px');
    });
	var newsIndexIsEven = true;
    $('ul.announcements li').each(function ()
    { 
        if ($(this).hasClass('hl').toString() == 'false')
        {

            $(this).addClass('splitCol');
            if (newsIndexIsEven == true)
            {
                $(this).appendTo('#leftSideli');
                newsIndexIsEven = false;
            }
            else
            {
                $(this).appendTo('#rightSideli');
                newsIndexIsEven = true;
            }
        }
    });
    $('.splitCol').each(function ()
    {
        $(this).children('p').hide();
        $(this).children('h3').after('<a class="truncateToggle">&nbsp;</a>');
    });

    $('.splitCol a.truncateToggle').click(function ()
    {
        $(this).parent().children('p').slideToggle('fast');
        if ($(this).hasClass('truncateDown'))
        {
            $(this).removeClass('truncateDown');
        }
        else
        {
            $(this).addClass('truncateDown');
        }
        return false;
    });

})

function StartPositioningTimer()
{
    timerRoundabout = setInterval(ChangeNeed, 8000);
}

function StopPositioningTimer()
{
    clearInterval(timerRoundabout);
}

function ChangeNeed()
{
    if (optionsClicked == false) $('#positioning_needs').roundabout_animateToNextChild();
}

// !SubPotitioning Functions
var currDevIndex = -1;
var currMobileIndex = -1;
var currSharePointIndex = -1;
var optionsClicked = false;
var newsIndexIsEven = false;

function LoadDevSubPositioning()
{
    if (optionsClicked == false)
    {
        var arL = $('#developers_nav li').length;
        if (currDevIndex < arL - 1) currDevIndex++;

        else currDevIndex = 0;
    }
    ShowDevSubPos();
}

function LoadMobileSubPositioning()
{
    if (optionsClicked == false)
    {
        var arL = $('#mobile_nav li').length;
        if (currMobileIndex < arL - 1) currMobileIndex++;

        else currMobileIndex = 0;
    }
    else
    {
        if (currMobileIndex == -1) currMobileIndex = 0;
    }
    ShowMobileSubPos();
}

function LoadSharePointSubPositioning()
{
    if (optionsClicked == false)
    {
        var arL = $('#sharepoint_nav li').length;
        if (currSharePointIndex < arL - 1) currSharePointIndex++;

        else currSharePointIndex = 0;
    }
    else
    {
        if (currSharePointIndex == -1) currSharePointIndex = 0;
    }
    ShowSharePointSubPos();
}

function ShowDevSubPos()
{
    $('#developers_nav li.active').removeClass('active');
    $('#developers_options li.active').removeClass('active');

    $('#developers_nav li:nth-child(' + (currDevIndex + 1) + ')').addClass('active');
    $('#developers_options li:nth-child(' + (currDevIndex + 1) + ')').addClass('active');
}

function ShowMobileSubPos()
{
    $('#mobile_nav li.active').removeClass('active');
    $('#mobile_options li.active').removeClass('active');

    $('#mobile_nav li:nth-child(' + (currMobileIndex + 1) + ')').addClass('active');
    $('#mobile_options li:nth-child(' + (currMobileIndex + 1) + ')').addClass('active');
}

function ShowSharePointSubPos()
{
    $('#sharepoint_nav li.active').removeClass('active');
    $('#sharepoint_options li.active').removeClass('active');
    $('#sharepoint_nav li:nth-child(' + (currSharePointIndex + 1) + ')').addClass('active');
    $('#sharepoint_options li:nth-child(' + (currSharePointIndex + 1) + ')').addClass('active');
}

