﻿var index1 = 0;
var index2 = 0;
var index3 = 0;

var divs1 = null;
var divs2 = null;
var divs3 = null;

var showchat = false;
var isdefault = "false";
var ctrlisdefault = null;

var tim;
var chatattivata = false;

function start() {
    var ctrldivChat = null;
    ctrldivChat = document.getElementById('ctl00_ctl00_divChat');
    if (ctrldivChat == null)
    { ctrldivChat = document.getElementById('ctl00_divChat'); }
    if (ctrldivChat == null)
    { ctrldivChat = document.getElementById('divChat'); }
    if (ctrldivChat != null)
    { ctrldivChat.style.display = 'none'; }
    divs1 = getElementsByClassName('divnewsclass1');
    divs2 = getElementsByClassName('divnewsclass2');
    divs3 = getElementsByClassName('divnewsclass3');
    changeDiv();
    var t = setInterval('changeDiv()', 8000);
    tim = setInterval("OnUpdate()", 2000);
    var ka = setInterval('keepAlive()', 10000);
    var showchat = setInterval('getWindowChat()', 5000);
    var isammonline = setInterval('checkIsAmministratoreOnLine()', 2000);
    //alert(document.getElementById('ctl00_ctl00_hfIsDefault').value);
    ctrlisdefault = document.getElementById('ctl00_ctl00_hfIsDefault');
    if (ctrlisdefault == null)
    { ctrlisdefault = document.getElementById('ctl00_hfIsDefault'); }
    if (ctrlisdefault == null)
    { ctrlisdefault = document.getElementById('hfIsDefault'); }
    if (ctrlisdefault != null)
    { isdefault = ctrlisdefault.value; }
    if (!testIsDefault())
    { attivachat(); }
}

function attivachat() {
    chatattivata = true;
    var showchat = setInterval('getWindowChat()', 5000);
}

function checkIsAmministratoreOnLine()
{ XMWS.popsovrws.IsAmministratoreOnline(checkIsAmministratoreOnLine_Complete, checkIsAmministratoreOnLine_Failed, 'fjf'); }

function checkIsAmministratoreOnLine_Complete(result) {
    if (document.getElementById('imgOpScuolaSI') != null) {
        if (result)
        { document.getElementById('imgOpScuolaSI').src = 'Images/live-support-chat-online.gif'; }
        else { document.getElementById('imgOpScuolaSI').src = 'Images/live-support-chat-offline.gif'; }
    }
}

function checkIsAmministratoreOnLine_Failed(result)
{ }

var Bookmark = function () {
    var browser = navigator.userAgent.toLowerCase();
    var keystroke = ((browser.indexOf('mac') != -1) ? 'Command/Cmd' : 'CTRL') + ' + D';
    var canBookmarkPage = (typeof window.external == 'object');

    return {
        bookmarkPage: function (url) {
            if (this.canBookmark()) {
                window.external.AddFavorite(url);
                return true;
            } else {
                return false;
            }
        },

        canBookmark: function () {
            return canBookmarkPage;
        },

        getKeystroke: function () {
            return keystroke;
        }
    };
} ();

function testIsDefault() {
    //alert(isdefault);
    if (isdefault.toLowerCase() == "true")
    {return true;}
    else {return false;}
}

function onOk() {
    var ctrlip = null;
    ctrlip = document.getElementById('ctl00_ctl00_hfIP');
    if (ctrlip == null)
    { ctrlip = document.getElementById('ctl00_hfIP'); }
    if (ctrlip == null)
    { ctrlip = document.getElementById('hfIP'); }
    ip = ctrlip.value;
    XMWS.popsovrws.setWindowChat(ip, "forse", OnOpenWindowChatSuccess, OnOpenWindowChatFailed);
    showchat = true;
    if (!testIsDefault() && showchat) {
        var ctrldivChat = null;
        ctrldivChat = document.getElementById('ctl00_ctl00_divChat');
        if (ctrldivChat == null)
        { ctrldivChat = document.getElementById('ctl00_divChat'); }
        if (ctrldivChat == null)
        { ctrldivChat = document.getElementById('divChat'); }
        ctrldivChat.style.display = 'block';
        document.getElementById('divshowchat').style.display = 'block';
        showchat = true;
    }
    else if (testIsDefault() && showchat) {
        document.getElementById('divshowchat').style.display = 'none';
        tab_change(5);
    }
    //window.open('http://www.scuolasi.it/ChatWindow.aspx', 'NuovaFinestra');
}

function onCancel() {
    XMWS.popsovrws.setWindowChat(document.getElementById('ctl00_ctl00_hfIP').value, "false", OnOpenWindowChatSuccess, OnOpenWindowChatFailed);
}

function changeDiv()
{
    //divnews1
    if (divs1 != null && divs1.length > 0 && index1 > 0 && index1 < divs1.length)
    {
        for (counter = 0; counter < divs1.length; counter++)
        {
            if (counter == index1)
            { document.getElementById(divs1[counter].getAttribute('id')).style.display = 'block'; }
            else { document.getElementById(divs1[counter].getAttribute('id')).style.display = 'none'; }
        }
        index1 += 1;
    }
    else if (divs1 != null)
    {
        index1 = 0;
        for (counter = 0; counter < divs1.length; counter++)
        {
            if (counter == index1)
            { document.getElementById(divs1[counter].getAttribute('id')).style.display = 'block'; }
            else { document.getElementById(divs1[counter].getAttribute('id')).style.display = 'none'; }
        }
        index1 += 1;
    }
    
    //divnews2
    if (divs2 != null && divs2.length > 0 && index2 > 0 && index2 < divs2.length)
    {
        for (counter = 0; counter < divs2.length; counter++)
        {
            if (counter == index2)
            { divs2[counter].style.display = 'block'; }
            else { divs2[counter].style.display = 'none'; }
        }
        index2 += 1;
    }
    else if (divs2 != null)
    {
        index2 = 0;
        for (counter = 0; counter < divs2.length; counter++)
        {
            if (counter == index2)
            { divs2[counter].style.display = 'block'; }
            else { divs2[counter].style.display = 'none'; }
        }
        index2 += 1;
    }
    
    //divnews3
    if (divs3 != null && divs3.length > 0 && index3 > 0 && index3 < divs3.length)
    {
        for (counter = 0; counter < divs3.length; counter++)
        {
            if (counter == index3)
            { divs3[counter].style.display = 'block'; }
            else { divs3[counter].style.display = 'none'; }
        }
        index3 += 1;
    }
    else if (divs3 != null)
    {
        index3 = 0;
        for (counter = 0; counter < divs3.length; counter++)
        {
            if (counter == index3)
            { divs3[counter].style.display = 'block'; }
            else { divs3[counter].style.display = 'none'; }
        }
        index3 += 1;
    }
}

function getElementsByClassName(class_name)
{
    var all_obj,ret_obj=new Array(),j=0,teststr;

    if(document.all)all_obj=document.all;
    else if(document.getElementsByTagName && !document.all)
      all_obj=document.getElementsByTagName("*");

    for(i=0;i<all_obj.length;i++)
    {
      if(all_obj[i].className.indexOf(class_name)!=-1)
      {
        teststr=","+all_obj[i].className.split(" ").join(",")+",";
        if(teststr.indexOf(","+class_name+",")!=-1)
        {
          ret_obj[j]=all_obj[i];
          j++;
        }
      }
    }
    return ret_obj;
}

function Grassetto() {
    var content = new Array();
    content = document.getElementsByName('ctl00$MainContent$htmleditorCorsiDescrizione');
    var sl = (content[0].value).substring(content[0].selectionStart, content[0].selectionEnd);
    content[0].value = content[0].value.replace(sl, "<strong>" + sl + "</strong>");
}

function ColoreTestoViola() {
    var content = new Array();
    content = document.getElementsByName('ctl00$MainContent$htmleditorCorsiDescrizione');
    var sl = (content[0].value).substring(content[0].selectionStart, content[0].selectionEnd);
    content[0].value = content[0].value.replace(sl, "<span style=\"color:#C90D6B;\">" + sl + "</span>");
}

function ColoreTestoStandard() {
    var content = new Array();
    content = document.getElementsByName('ctl00$MainContent$htmleditorCorsiDescrizione');
    var sl = (content[0].value).substring(content[0].selectionStart, content[0].selectionEnd);
    content[0].value = content[0].value.replace(sl, "<span style=\"color:#6f7a91;\">" + sl + "</span>");
}

function ACapo() {
    var content = new Array();
    content = document.getElementsByName('ctl00$MainContent$htmleditorCorsiDescrizione');
    //var sl = (content[0].value).substring(content[0].selectionStart, content[0].selectionEnd);
    content[0].value = (content[0].value).substring(0, content[0].selectionStart) + "<br/>" + (content[0].value).substring(content[0].selectionStart);
}

function Lista() {
    var content = new Array();
    content = document.getElementsByName('ctl00$MainContent$htmleditorCorsiDescrizione');
    content[0].value = (content[0].value).substring(0, content[0].selectionStart) + "<ul><li>Elemento 1</li><li>Elemento 2</li><li>Elemento 3</li></ul>" + (content[0].value).substring(content[0].selectionStart);
}

function Preview() {
    var content = new Array();
    content = document.getElementsByName('ctl00$MainContent$htmleditorCorsiDescrizione');
    var contenutoHTML = content[0].value;
    content[0].visible = false;
    var content2 = document.getElementById('ctl00_MainContent_htmlCorsiDescrizionePreview');
    content2.style.display = 'block';
    content2.innerHTML = contenutoHTML;
    document.getElementById('ctl00_MainContent_imgClose').style.display = 'block';
}

function HidePreview() {
    document.getElementById('ctl00_MainContent_htmlCorsiDescrizionePreview').style.display = 'none';
    document.getElementById('ctl00_MainContent_imgClose').style.display = 'none';
}

//function ShowChat() {
//    document.getElementById('divChat').style.display = 'block';
//}

function HideChat() {
    if (!testIsDefault()) {
        var ctrldivChat = null;
        ctrldivChat = document.getElementById('ctl00_ctl00_divChat');
        if (ctrldivChat == null)
        { ctrldivChat = document.getElementById('ctl00_divChat'); }
        if (ctrldivChat == null)
        { ctrldivChat = document.getElementById('divChat'); }
        ctrldivChat.style.display = 'none';
        var ip = document.getElementById('ctl00_ctl00_hfIP');
        if (ip == null)
        { ip = document.getElementById('ctl00_hfIP'); }
        if (ip == null)
        { ip = document.getElementById('hfIP'); }
        XMWS.popsovrws.setWindowChat(ip.value.toString(), "false", OnOpenWindowChatSuccess, OnOpenWindowChatFailed);
    }
}

function sendMessage(e, username) {
    if (e.keyCode == 13) {
        if (!testIsDefault())
        { XMWS.popsovrws.addConversation(document.getElementById('txtChat').value, OnAddComplete, OnAddFailed, 'fgdfg'); }
        else { XMWS.popsovrws.addConversation(document.getElementById('txtMainChat').value, OnAddComplete, OnAddFailed, 'fgdfg'); }
        return false;
    }
}

function OnAddComplete(result) {
    if (!testIsDefault())
    { document.getElementById('txtChat').value = ""; }
    else { document.getElementById('txtMainChat').value = ""; }
    OnUpdate();
}

function OnAddFailed(result)
{ alert('fallito inserimento'); }

function OnUpdate() {
    var hfuser = document.getElementById('ctl00_ctl00_hfUsername');
    if (hfuser == null)
    { hfuser = document.getElementById('ctl00_hfUsername'); }
    if (hfuser == null)
    { hfuser = document.getElementById('hfUsername'); }
    XMWS.popsovrws.getChatForChat(hfuser.value, OnUpdateComplete, OnUpdateFailed, 'fefwe');
}

function OnUpdateComplete(result) {
    var hfchatcount = document.getElementById("ctl00_ctl00_hfChatCount");
    if (hfchatcount == null)
    { hfchatcount = document.getElementById("ctl00_hfChatCount"); }
    if (hfchatcount == null)
    { hfchatcount = document.getElementById("hfChatCount"); }
    var oldn = hfchatcount.value;
    var newn = result.toString().substring(0, result.toString().indexOf('|'));
//    if (oldn != newn && !showchat)
//    { document.getElementById('imgChatShowHide').src = "Images/chat.gif"; }
    //    else { document.getElementById('imgChatShowHide').src = "Images/chat_clear.gif"; }
    if (oldn != newn && !showchat) {
//        ShowChat();
    }
    if (showchat && isdefault == '')
    { hfchatcount.value = newn; }
    if ((result.toString().indexOf('|') + 1) < result.toString().length) {
        result = result.toString().substring(result.toString().indexOf('|') + 1);
        var pnlchat;
        if (!testIsDefault()) {
            pnlchat = document.getElementById("ctl00_ctl00_pnlChatHistory");
            if (pnlchat == null)
            { pnlchat = document.getElementById("ctl00_pnlChatHistory"); }
            if (pnlchat == null)
            { pnlchat = document.getElementById("pnlChatHistory"); }
        }
        else {
            pnlchat = document.getElementById("ctl00_ctl00_pnlMainChatHistory");
            if (pnlchat == null)
            { pnlchat = document.getElementById("ctl00_pnlMainChatHistory"); }
            if (pnlchat == null)
            { pnlchat = document.getElementById("pnlMainChatHistory"); }
        }
        if (pnlchat != null)
        { pnlchat.innerHTML = result; }
        
    }
}

function OnUpdateFailed(result)
{ }

//function ShowHideChat() {
//    if (!showchat) {
//        alwaysOnTop.init();
//        showchat = true;
//    }
//    else {
//        alwaysOnTop.hidediv('divChat');
//        showchat = false;
//    }
//}

//function ShowChat() {
//    alwaysOnTop.init();
//    showchat = true;
//}

//function HideChat() {
//    alwaysOnTop.hidediv('divChat');
//    showchat = false;
//}

function keepAlive() {
    XMWS.popsovrws.KeepAliveIP(OnKeepAliveComplete, OnKeepAliveFailed);
}

function OnKeepAliveComplete()
{ }

function OnKeepAliveFailed()
{ }


//function OnUpdate() {
//    XMWS.popsovrws.getChatForChat('', OnUpdateComplete, OnUpdateFailed, 'fefwe');
//}

//function OnUpdateComplete(result)
//{ document.getElementById("txtChatHistory").value = result; }

//function OnUpdateFailed(result)
//{ alert(result); }

//function OnAdd(username)
//{ XMWS.popsovrws.addConversation(document.getElementById('txtChatSend').value, username, OnAddComplete, OnAddFailed, 'fgdfg'); }

//function OnAddComplete(result) {
//    document.getElementById('txtChatSend').value = "";
//    OnUpdate();
//}

//function OnAddFailed(result)
//{ alert('fallito inserimento'); }

function OpenWindowChat()
{ XMWS.popsovrws.setWindowChat(document.getElementById('spSelectedIP').innerHTML, "true", OnOpenWindowChatSuccess, OnOpenWindowChatFailed); }

function OnOpenWindowChatSuccess(result) {
    if (!testIsDefault() && showchat) {
        var ctrldivChat = null;
        ctrldivChat = document.getElementById('ctl00_ctl00_divChat');
        if (ctrldivChat == null)
        { ctrldivChat = document.getElementById('ctl00_divChat'); }
        if (ctrldivChat == null)
        { ctrldivChat = document.getElementById('divChat'); }
        ctrldivChat.style.display = 'block';
        showchat = true;
    }
    else if (testIsDefault() && showchat) {
        document.getElementById('divshowchat').style.display = 'none';
        tab_change(5);
    }
}

function OnOpenWindowChatFailed(result)
{ }

function getWindowChat() {
    var ip = "";
    try
    { ip = document.getElementById('ctl00_ctl00_hfIP').value; }
    catch (ex) {
        try
        { ip = document.getElementById('ctl00_hfIP').value; }
        catch (ex) {
            try
            { ip = document.getElementById('hfIP').value; }
            catch (ex)
            { }
        }
    }
    XMWS.popsovrws.getWindowChat(ip, OngetWindowChatSuccess, OngetWindowChatFailed);
}

function OngetWindowChatSuccess(result) {
    if (result == "true") {
        $find('modalpopup2').show();
        document.getElementById('divshowchat').style.display = 'none';
    }
    else if (result == "forse") {
        $find('modalpopup2').hide();
        if (testIsDefault() && showchat) {
            var ctrldivChat = null;
            ctrldivChat = document.getElementById('ctl00_ctl00_divChat');
            if (ctrldivChat == null)
            { ctrldivChat = document.getElementById('ctl00_divChat'); }
            if (ctrldivChat == null)
            { ctrldivChat = document.getElementById('divChat'); }
            ctrldivChat.style.display = 'block';
            document.getElementById('divshowchat').style.display = 'block';
        }
        else if (!testIsDefault() && showchat) {
            document.getElementById('divshowchat').style.display = 'none';
            tab_change(5);
        }
    }
    else if (result == "false") {
        $find('modalpopup2').hide();
        var ctrldivChat = null;
        ctrldivChat = document.getElementById('ctl00_ctl00_divChat');
        if (ctrldivChat == null)
        { ctrldivChat = document.getElementById('ctl00_divChat'); }
        if (ctrldivChat == null)
        { ctrldivChat = document.getElementById('divChat'); }
        ctrldivChat.style.display = 'none';
        document.getElementById('divshowchat').style.display = 'none';
    }

//    if (!testIsDefault()) {
//        if (result == "true" || result == "forse") {
//            $find('modalpopup2').show();
//            document.getElementById('divshowchat').style.display = 'none';
//        }
////        else if (result == "forse") {
////            $find('modalpopup2').hide();
////            var ctrldivChat = null;
////            ctrldivChat = document.getElementById('ctl00_ctl00_divChat');
////            if (ctrldivChat == null)
////            { ctrldivChat = document.getElementById('ctl00_divChat'); }
////            if (ctrldivChat == null)
////            { ctrldivChat = document.getElementById('divChat'); }
////            ctrldivChat.style.display = 'block';
////            document.getElementById('divshowchat').style.display = 'block';
////        }
//        else if (result == "false") {
//            $find('modalpopup2').hide();
//            var ctrldivChat = null;
//            ctrldivChat = document.getElementById('ctl00_ctl00_divChat');
//            if (ctrldivChat == null)
//            { ctrldivChat = document.getElementById('ctl00_divChat'); }
//            if (ctrldivChat == null)
//            { ctrldivChat = document.getElementById('divChat'); }
//            ctrldivChat.style.display = 'none';
//            document.getElementById('divshowchat').style.display = 'none';
//        }
//    }
//    else {
//        document.getElementById('divshowchat').style.display = 'none';
//        if (result == "true")
//        { tab_change(5); }
//    }
}

function OngetWindowChatFailed(result)
{ }

var actualslide = -1;
var maxnumslides = 25;
var slidestart = "Images/aula";
var slideend = "_400.jpg";
var timerslides;
var isplaying = false;

function NextSlide() {
    if ((actualslide + 1) <= maxnumslides)
    { }
    else { actualslide = -1; }
    var imgname = slidestart + (actualslide + 1) + slideend;
    actualslide = actualslide + 1;
    //alert(imgname);
    document.getElementById('ctl00_ctl00_MainContent_MenuMainContent_imgAula').src = imgname;
}

function slideshowPlayPausa() {
    if (isplaying) {
        document.getElementById('btnPlayStop').value = "Play";
        slideshowPausa();
    }
    else {
        document.getElementById('btnPlayStop').value = "Pausa";
        NextSlide();
        document.getElementById('ctl00_ctl00_MainContent_MenuMainContent_imgAula').style.display = 'block';
        slideshowPlay();
    }
}

function slideshowPlay() {
    timerslides = setInterval("NextSlide()", 5000);
    isplaying = true;
}

function slideshowPausa() {
    clearInterval(timerslides);
    isplaying = false;
}

function slideshowBack() {
    if ((actualslide + 1) <= -1)
    { actualslide = maxnumslides + 1; }
    //else { actualslide = actualslide - 1; }
    var imgname = slidestart + (actualslide - 1) + slideend;
    actualslide = actualslide - 1;
    //alert(imgname);
    document.getElementById('ctl00_ctl00_MainContent_MenuMainContent_imgAula').src = imgname;
}

function slideshowForward() {
    if ((actualslide + 1) <= maxnumslides)
    { }
    else { actualslide = -1; }
    var imgname = slidestart + (actualslide + 1) + slideend;
    actualslide = actualslide + 1;
    //alert(imgname);
    document.getElementById('ctl00_ctl00_MainContent_MenuMainContent_imgAula').src = imgname;
}

var urlAddress = "http://www.scuolasi.it";
var pageName = "ScuolaSI";

function addToFavorites() {
    if (window.sidebar) // firefox
        window.sidebar.addPanel(pageName, urlAddress, "");
    else if (window.opera && window.print) { // opera
        var elem = document.createElement('a');
        elem.setAttribute('href', url);
        elem.setAttribute('title', title);
        elem.setAttribute('rel', 'sidebar');
        elem.click();
    }
    else if (document.all)// ie
        window.external.AddFavorite(url, title);
}
