//Preload Images
var imgs = new Array(20);
imgs[0] = "/common/css/img/ora_arrow.png";
imgs[1] = "/common/css/img/ora_arrowLongOff.png";
imgs[2] = "/common/css/img/menutabOver.png";
imgs[3] = "/common/css/img/menutabOn.png";
imgs[4] = "/common/css/img/menutabOff.png";
imgs[5] = "/common/css/img/ora_arrowLongOn.png";
imgs[6] = "/common/css/img/ora_arrowOff.png";
imgs[7] = "/common/css/img/ora_arrowOn.png";
imgs[8] = "/common/css/img/bg_content.png";
imgs[9] = "/common/css/img/bg_layover.jpg";
imgs[10] = "/images/btn_showdown_off.png";
imgs[11] = "/images/btn_rodeo_off.png";
imgs[12] = "/images/btn_imitator_off.png";
imgs[13] = "/images/btn_deployment_off.png";
imgs[14] = "/images/btn_command_off.png";
imgs[15] = "/images/btn_robot_off.png";
imgs[16] = "/images/btn_exerciser_off.png";
imgs[17] = "/common/css/img/menutabOver.gif";
imgs[18] = "/common/css/img/menutabOn.gif";
imgs[19] = "/common/css/img/menutabOff.gif";

// Header Search


function search()
{
    window.location = "http://search.microsoft.com/results.aspx?mkt=en-US&setlang=en-US&q=" + $("#searchInput").value;
}

function checkKey(e)
{
    e = e || window.event;
    var key = e.charCode || e.keyCode;

    if (key == 13)
    {
        setTimeout('search()', 500);
        //search();
        //alert('zuh');
    }

    
}


//Switch Main Content
var contIds = new Array('cont1', 'cont2', 'cont3', 'cont4');

/*Modals for Section 1*/
var modal1Ids = new Array('modal1_1', 'modal1_2', 'modal1_3', 'modal1_4', 'modal1_5');

/*Modals for Section 3*/
var modal3Ids = new Array('modal3_1', 'modal3_2', 'modal3_3', 'modal3_4');

/*Sub Modals for Case Study Sections*/
var subModal1Ids = new Array('modal1_1a', 'modal1_1b');
var subModal2Ids = new Array('modal1_2a', 'modal1_2b');

function switchTab(tab, id)
{
    highlightTab(tab);
    switchid(id);
}

function showModal(modalNumber, id, id1)
{
    switchModalId(modalNumber, id);
    switchSubModal1Id(id1);
}

function switchVideoCaseStudy(playerToStop, modalNumber, modalId, subModalId, subModalArray, playerToStart, buttonId)
{

    if (useVideos)
    {
        stopVideo(playerToStop);
    }
    switchModalId(modalNumber, modalId);
    switchSubModal(subModalId, subModalArray);
    if (useVideos)
    {
        playVideo(playerToStart);
    }
    makeBtnActive(buttonId);
}

function switchTextCaseStudy(modalNumber, modalId, buttonId)
{
    if (useVideos)
    {
        stopVideo('modal1player');
        stopVideo('modal2player');
    }
    switchModalId(modalNumber, modalId);
    makeBtnActive(buttonId);
}

function switchCaseStudyView(button, caseStudyLinkArray, casestudyId, modalId, subModalArray, modalPlayer, isVideo)
{
    makeSecActive(button, caseStudyLinkArray, casestudyId);
    switchSubModal(modalId, subModalArray);

    if (useVideos)
    {

        if (isVideo)
        {
            playVideo(modalPlayer);
        }
        else
        {
            stopVideo(modalPlayer);
        }
    }
}

function switchMeetItView(modalNumber, modalId, button, linkArray, btnPrefix, isVideo)
{
    switchModalId(modalNumber, modalId);
    makeSecActive(button, linkArray, btnPrefix);

    if (useVideos)
    {
        if (isVideo)
        {
            playVideo('modal3player');
        }
        else
        {
            stopVideo('modal3player');
        }
    }
}

function closeModal1()
{
    if (useVideos)
    {
        stopVideo('modal1player');
        stopVideo('modal2player');
    }
    resetModal1();
}

function switchid(id)
{
    hideallids(contIds);
    showdiv(id);
}

function switchModalId(modalNumber, id)
{
    if (modalNumber == '1')
    {
        hideallids(modal1Ids);
    } else if (modalNumber == '3')
    {
        hideallids(modal3Ids);
    }
    showdiv(id);
}

function switchSubModal(id, idArray)
{
    hideallids(idArray);
    showdiv(id);
}

function switchSubModal1Id(id)
{
    hideallids(subModal1Ids);
    showdiv(id);
}

function switchSubModal2Id(id)
{
    hideallids(subModal2Ids);
    showdiv(id);
}

function hideallids(ids)
{
    //loop through the array and hide each element by id
    for (var i = 0; i < ids.length; i++)
    {
        hidediv(ids[i]);
    }
}

function hidediv(id)
{
    var localId = "#" + id;
    $(localId).hide();
}

function showdiv(id)
{
    //safe function to show an element with a specified id

    if (document.getElementById)
    { // DOM3 = IE5, NS6
        document.getElementById(id).style.display = 'block';
    }
    else
    {
        if (document.layers)
        { // Netscape 4
            document.id.display = 'block';
        }
        else
        { // IE 4
            document.all.id.style.display = 'block';
        }
    }
}

//Highlighting Main Menu Tabs
function highlightTab(targetBtnEl)
{
    var btnIds = new Array("1", "2", "3", "4");
    var targetBtnId = targetBtnEl.id;

    for (var i = 0; i < btnIds.length; i++)
    {
        var id = btnIds[i];
        var btnId = "tab" + id;
        var btnEl = document.getElementById(btnId);

        if (btnId == targetBtnId)
        {
            // target btn, so turn it on
            btnEl.className = "menuTabOn";

        } else
        {
            // other btns, so turn them off
            if (btnEl)
                btnEl.className = "menuTabOff";
        }
    }
}

var meetITLinkIds = new Array("1", "2", "3", "4");
var caseStudy1LinkIds = new Array("1", "2");
var caseStudy2LinkIds = new Array("3", "4");

function makeSecActive(targetBtnEl, btnIds, btnPrefix)
{

    var targetBtnId = targetBtnEl.id;

    for (var i = 0; i < btnIds.length; i++)
    {
        var id = btnIds[i];
        var btnId = btnPrefix + id;
        var btnEl = document.getElementById(btnId);

        if (btnId == targetBtnId)
        {
            // target btn, so turn it on
            btnEl.className = "active";

        } else
        {
            // other btns, so turn them off
            if (btnEl)
                btnEl.className = "inactive";
        }
    }
}

//Make Case Study Buttons Active
function makeBtnActive(IN_title)
{
    var imgArray = new Array('baboo', 'sd', 'tp', 'hc', 'godaddy');

    for (i = 0; i < imgArray.length; i++)
    {
        if (imgArray[i] == IN_title)
        {
            document.getElementById(IN_title).src = 'images/btn_' + IN_title + '_on.png';
            lastClickedImage = IN_title;
        }
        else
        {
            document.getElementById(imgArray[i]).src = 'images/btn_' + imgArray[i] + '_off.png';
        }
    }
}


//Reset modal1
function resetModal1()
{
    document.getElementById('baboo').src = 'images/btn_baboo_off.png';
    document.getElementById('tp').src = 'images/btn_tp_off.png';
    document.getElementById('sd').src = 'images/btn_sd_off.png';
    document.getElementById('hc').src = 'images/btn_hc_off.png';
    document.getElementById('godaddy').src = 'images/btn_godaddy_on.png';
}


function resetModal3()
{
    switchModalId('3', 'modal3_1');
    $("#modal_3 .active").removeClass('active').addClass('inactive');
    $("#link1").removeClass('inactive').addClass('active');

}

useVideos = true;

//-- Modal Popups --//
$(document).ready(function()
{

    if (window.addEventListener)
    {
        document.getElementById("searchInput").addEventListener("keydown", checkKey, false);
    }
    else
    {
        document.getElementById("searchInput").attachEvent("onkeydown", checkKey);
    }

    $("#searchBtn").click(function() { search(); });

    if (navigator.platform.indexOf("Mac") != -1 || jQuery.browser.safari || (jQuery.browser.version.substr(2, 1) == "8" && jQuery.browser.mozilla))
    {
        useVideos = false;
    }

    $("#scroll_left").click(function() { scrollLeft(); });
    $("#scroll_right").click(function() { scrollRight(); });
    //
    //

    $("#modal_1").dialog({ autoOpen: false, modal: true, width: 824, height: 532, draggable: false, resizable: false });
    $("#modal_1_Trigger").click(function() { $("#modal_1").dialog("open"); if (useVideos) { playVideo('modal1player'); }; makeBtnActive('godaddy'); });
    $("#modal_1_Close").click(function() { $("#modal_1").dialog("close"); });

    $("#modal_3").dialog({ autoOpen: false, modal: true, width: 824, height: 532, draggable: false, resizable: false });
    $("#modal_3_Trigger").click(function() { $("#modal_3").dialog("open"); switchVideo(0, 'showdown'); });
    $("#modal_3_Close").click(function() { $("#modal_3").dialog("close"); if (useVideos) { stopVideo('modal3player'); }; resetModal3(); });

});

function roll(IN_id)
{
    var element = document.getElementById(IN_id);
    if (element.src.substring(element.src.lastIndexOf("_") + 1) != "off.png")
    {
        // was over, so now turn off
        if (lastClickedImage != IN_id)
        {
            // turn off unless the image is the currently selected
            element.src = 'images/btn_' + IN_id + '_off.png';
        }
    } else
    {
        // was off, so now set over
        element.src = 'images/btn_' + IN_id + '_over.png';
    }
}


var videos = new Array('http://mediadl.microsoft.com/MediaDL/WWW/W/windowsserver2008/ServerUnleashed/platformpreference/showdown.wmv', 'http://mediadl.microsoft.com/MediaDL/WWW/W/windowsserver2008/ServerUnleashed/platformpreference/deployment.wmv', 'http://mediadl.microsoft.com/mediadl/www/w/windowsserver/compare/phponwindows/rodeo.wmv', 'http://mediadl.microsoft.com/mediadl/www/w/windowsserver/compare/phponwindows/Imitator.wmv', 'http://download.microsoft.com/download/e/e/7/ee7db9b6-6109-4323-9781-202f28cad5f5/SXWK_0423H_512K_Stream.wmv', 'http://download.microsoft.com/download/e/e/7/ee7db9b6-6109-4323-9781-202f28cad5f5/SXWK_0424H_512K_Stream.wmv', 'http://download.microsoft.com/download/e/e/7/ee7db9b6-6109-4323-9781-202f28cad5f5/SXWK_0425H_512K_Stream.wmv');
var videoImages = new Array('images/scr_showdown.jpg', 'images/scr_deployment.jpg', 'images/scr_rodeo.jpg', 'images/scr_imitator.jpg', 'images/scr_command.jpg', 'images/scr_robot.jpg', 'images/scr_exerciser.jpg');

function switchVideo(IN_videoNumber, IN_title)
{
    var imgArray = new Array('showdown', 'deployment', 'rodeo', 'imitator', 'command', 'robot', 'exerciser');
    var onEl;

    for (i = 0; i < imgArray.length; i++)
    {
        if (imgArray[i] == IN_title)
        {
            onEl = document.getElementById(IN_title);
            lastClickedImage = IN_title;
        }
        else
        {
            document.getElementById(imgArray[i]).src = 'images/btn_' + imgArray[i] + '_off.png';
        }
    }
    if (onEl)
    {
        onEl.src = 'images/btn_' + IN_title + '_on.png';
    }
    if (useVideos)
    {
        var videoPlayerId = 'modal3player';
        stopVideo(videoPlayerId);

        var videoEl = document.getElementById(videoPlayerId);
        var videoSrc = videos[IN_videoNumber];
        videoEl.URL = videoSrc;     // IE
        var embedEl = document.getElementById("modal3player");
        if (embedEl)
        {
            // for FF
            document[videoPlayerId].src = videoSrc;
            embedEl.src = videoSrc;
        }
        playVideo(videoPlayerId);
    }
    else
    {
        $("#modal3player").css("background", "url('" + videoImages[IN_videoNumber] + "')");
        $("#modal3player").html('<a target="_blank" href="' + videos[IN_videoNumber] + '"><img src="images/btn_play.png"/ id="playBtn" title="Video will load in a new window." ></a>');
    }
}

lastClickedImage = '';

var lastPlayVideoTimeoutId;
var playRetries = {};
var stopRetries = {};

/**
* Play the video after a set delay.
*/
function playVideo(videoId)
{
    // if we're currently have a timeout for another video, clear the timeout
    if (lastPlayVideoTimeoutId)
    {
        clearTimeout(lastPlayVideoTimeoutId);
    }

    // clear the number of retries, since this is the first call
    playRetries[videoId] = 0;

    // set the timeout
    lastPlayVideoTimeoutId = setTimeout("playVideoNow('" + videoId + "')", 1000);
}

/**
* Start the video, assuming that we haven't exceeded the number of retries.
*/
function playVideoNow(videoId)
{
    try
    {
        // first try to do it IE/Chrome style
        var videoEl = document.getElementById(videoId);
        videoEl.Play();
    } catch (err)
    {
        // failure probably means it's FF
        var controls = eval("document." + videoId + ".controls;");
        var numRetries = playRetries[videoId];
        if (controls)
        {
            controls.play();
        } else if (numRetries < 6)
        {
            if (lastPlayVideoTimeoutId)
            {
                clearTimeout(lastPlayVideoTimeoutId);
            }
            lastPlayVideoTimeoutId = setTimeout("playVideoNow('" + videoId + "')", numRetries * 500);
            playRetries[videoId] = (numRetries + 1);
        }
    }
}

/**
* Stop the video after a set delay.
*/
function stopVideo(videoId)
{
    if (lastPlayVideoTimeoutId) { clearTimeout(lastPlayVideoTimeoutId); }
    stopRetries[videoId] = 0;
    setTimeout("stopVideoNow('" + videoId + "')", 500);
}

/**
* Stop the video, assuming that we haven't exceeded the number of retries.
*/
function stopVideoNow(videoId)
{
    try
    {
        // first try to do it IE/Chrome style
        document.getElementById(videoId).Stop();
    } catch (err)
    {
        // failure probably means it's FF
        var controls = eval("document." + videoId + ".controls;");
        var numRetries = stopRetries[videoId];
        if (controls)
        {
            controls.stop();
        } else if (numRetries < 4)
        {
            setTimeout("stopVideoNow('" + videoId + "')", 1500);
            stopRetries[videoId] = numRetries + 1;
        }
    }
}

function debugObject(debugObj)
{
    var debugVal = "";
    for (var attr in debugObj)
    {
        debugVal = debugVal + attr + " -> [" + debugObj[attr] + "] ";
    }
    alert(debugVal);
}

//------------------//

var xpos_ini = 0;
var xpos = xpos_ini; //This is the initial coordinate-x for the container    
var container_width = 2650; 
var xpos_limit = (xpos_ini - container_width) + 510; //Limit when moving to the right   

function scrollLeft()
{
    var scrollLeft = document.getElementById('scroll_left');
    var scrollRight = document.getElementById('scroll_right');

    //--Modifying the position for the container only                    
    if (xpos < xpos_ini)
    {
        xpos += 721;
        document.getElementById('container').style.left = xpos + "px";
        if (scrollRight.style.visibility == "hidden")
        {
            scrollRight.style.visibility = "";
        }
    }

    if (xpos < xpos_ini)
    {
        if (scrollRight.style.visibility == "hidden")
        {
            scrollRight.style.visibility = "";
        }
    } else
    {
        if (scrollLeft.style.visibility == "")
        {
            scrollLeft.style.visibility = "hidden";
        }
    }
}

function scrollRight()
{
    var scrollLeft = document.getElementById('scroll_left');
    var scrollRight = document.getElementById('scroll_right');

    //--Modifying the position for the container only                               
    if (xpos > xpos_limit)
    {
        xpos -= 721;
        document.getElementById('container').style.left = xpos + "px";
        if (scrollLeft.style.visibility == "hidden")
        {
            scrollLeft.style.visibility = "";
        }
    }
    if (xpos > xpos_limit)
    {
        if (scrollLeft.style.visibility == "hidden")
        {
            scrollLeft.style.visibility = "";
        }
    } else
    {
        if (scrollRight.style.visibility == "")
        {
            scrollRight.style.visibility = "hidden";
        }
    }
    
    
}

var xpos_ini2 = 0;
var xpos2 = xpos_ini2; //This is the initial coordinate-x for the container    
var container_width2 = 850; 
var xpos_limit2 = (xpos_ini2 - container_width2) + 510; //Limit when moving to the right

function scrollLeft2()
{
    var scrollLeft = document.getElementById('scroll2_left');
    var scrollRight = document.getElementById('scroll2_right');

    //--Modifying the position for the container only
    if (xpos2 < xpos_ini2)
    {
        xpos2 += 721;
        document.getElementById('container2').style.left = xpos2 + "px";
        if (scrollRight.style.visibility == "hidden")
        {
            scrollRight.style.visibility = "";
        }
    }

    if (xpos2 < xpos_ini2)
    {
        if (scrollRight.style.visibility == "hidden")
        {
            scrollRight.style.visibility = "";
        }
    } else
    {
        if (scrollLeft.style.visibility == "")
        {
            scrollLeft.style.visibility = "hidden";
        }
    }
}

function scrollRight2()
{
    var scrollLeft = document.getElementById('scroll2_left');
    var scrollRight = document.getElementById('scroll2_right');

    //--Modifying the position for the container only                               
    if (xpos2 > xpos_limit2)
    {
        xpos2 -= 721;
        document.getElementById('container2').style.left = xpos2 + "px";
        if (scrollLeft.style.visibility == "hidden")
        {
            scrollLeft.style.visibility = "";
        }
    }

    if (xpos2 > xpos_limit2)
    {
        if (scrollLeft.style.visibility == "hidden")
        {
            scrollLeft.style.visibility = "";
        }
    } else
    {
        if (scrollRight.style.visibility == "")
        {
            scrollRight.style.visibility = "hidden";
        }
    }

}
