﻿//Class UIController

// dictionary related
function LoadDictionary()
{
    top.uiCtrl.currentDictionaryIframe.src = top.uiCtrl.currentDictionaryData;    
    //top.uiCtrl.testwindow.document.writeln('LoadDictionary<br>');
}
    
function UIController()
{    
    //this.testwindow = window.open("");
    //testwindow.document.writeln();
    
    // cancel dictionart.aspx reload if user clicks on items in the syllabus with low intervals. 
    // (clicks on a chapter, after 2 seconds clicks on a topic etc.)
    // in that case there is no reason why to load the chapters syllabus and then the topic syllabus);
    this.LastDictionaryRequestTime = null;
    this.DictionarITimerID = null;
    this.currentDictionaryData = null;
    this.currentDictionaryIframe = null;
    
    this.CENTER = 0;
    this.TOP_RIGHT = 1;
    this.BOTTOM_RIGHT = 2;
    this.IsReady = false;
    
    this.viewers = null;
    this.sendData = sendData;
    this.getViewer = getViewer;
    this.getActiveViewer = getActiveViewer;
    this.setViewer = setViewer;
    this.saveHistoryData = saveHistoryData;
    this.updateMilliSecFromMouseMove = updateMilliSecFromMouseMove;
    this.updatePreviousTab = updatePreviousTab;
    this.showPreviousTab = showPreviousTab;    
    
    // Send data request
    function sendData(request)
    {
        // Redirect to target
        switch(request.type)
        {
        //////////////////////////////////////////////////////////////
             // Request for Locating mouse movements, which means
             // User is still online.
             // Comes from OnMouseMove in js.
             // Runs every minute
             case REQ_MOUSE_MOVE_TYPE:
                 updateMilliSecFromMouseMove(request);
                 return true;
        ///////////////////////////////////////////////////////////////
            case REQ_DICTIONARY_TYPE:
                request.targetViewer = this.getViewer(document.getElementById(DICTIONARY_FRM));
                request.targetLocation = this.BOTTOM_RIGHT;
            //lilah start
            
                if (request.data == null)
                {
                    request.data = request.targetViewer.iframe.src;
                }
                else
                {
                  request.data = DICTIONARY_MV+request.data;  
                }
            //lilah end         
                
                break;
            case REQ_CATALOG_SEARCH_TYPE:
                request.targetViewer = this.getViewer(document.getElementById(CATALOG_SEARCH_FRM));
                request.targetLocation = this.BOTTOM_RIGHT;
                if (request.data == null)
                {
                    request.data = request.targetViewer.iframe.src;
                }
                else
                {
                  request.data = CATALOG_SEARCH_MV+request.data;  
                }
                break;
            case REQ_CLOCK_TYPE:
                request.targetViewer = this.getViewer(document.getElementById(CLOCK_FRM));
                request.targetLocation = this.BOTTOM_RIGHT;
                request.data = CLOCK_MV;
                break;
            case REQ_SYLLABUS_TYPE:
                request.targetViewer = this.getViewer(document.getElementById(SYLLABUS_FRM));
                request.targetLocation = this.TOP_RIGHT;
                request.data = SYLLABUS_MV;
                break;
            case REQ_FORUM_TYPE:
                request.targetViewer = this.getViewer(document.getElementById(FORUM_FRM));
                request.targetLocation = this.CENTER;
                request.data = FORUM_MV;
                break;
            case REQ_FORMULAS_TYPE:
                request.targetViewer = this.getViewer(document.getElementById(FORMULAS_FRM));
                request.targetLocation = this.TOP_RIGHT;
                request.data = FORMULAS_MV;
                break;
            case REQ_ANSWERS_TYPE:
                request.targetViewer = this.getViewer(document.getElementById(ANSWERS_FRM));
                request.targetLocation = this.TOP_RIGHT;
                request.data = ANSWERS_MV;
                break;
            case REQ_FEEDBACK_TYPE:
                request.targetViewer = this.getViewer(document.getElementById(REPORTBUG_FRM));
                request.targetLocation = this.CENTER;
                request.data = REPORTBUG_MV;
                break;
            case REQ_CALCULATOR_TYPE:
                request.targetViewer = this.getViewer(document.getElementById(CALCULATOR_FRM));
                request.targetLocation = this.TOP_RIGHT;
                request.data = CALCULATOR_MV;
                break;
            case REQ_TASKS_TYPE:
                 request.targetViewer = this.getViewer(document.getElementById(TASKS_FRM));
                 request.targetLocation = this.TOP_RIGHT;
                 request.data = TASKS_MV;
             break;   
            case REQ_PARAGRAPH_TYPE:
                request.targetViewer = this.getViewer(document.getElementById(PARAGRAPH_FRM));
                request.targetLocation = this.CENTER;
                break;
            case REQ_COURSE_SYLLABUS_TYPE:
                request.targetViewer = this.getViewer(document.getElementById(SYLLABUS_FRM));
                request.targetLocation = this.TOP_RIGHT;
                request.data = SYLLABUS_MV + "?" + COURSE_ID + "=" + request.id;
                break;
            case REQ_NOTES_TYPE:
                 request.targetViewer = this.getViewer(document.getElementById(NOTES_FRM));
                 request.targetLocation = this.TOP_RIGHT;
                 request.data = NOTES_MV;
                 break;
            case REQ_CHANGE_COURSE_TYPE:
                 request.targetViewer = this.getViewer(document.getElementById(CHANGE_COURSE_FRM));
                 request.targetLocation = this.BOTTOM_RIGHT;
                 request.data = CHANGE_COURSE_MV;
                 break;
            case REQ_CHANGE_NEW_COURSE_TYPE:
                 request.targetViewer = this.getViewer(document.getElementById(CHANGE_COURSE_FRM));
                 request.targetLocation = this.BOTTOM_RIGHT;
                 request.data = MINISITE_MV + "?UID=" + Math.round(Math.random()*1000);
                 break;     
            case REQ_SELECTED_PARAGRAPH_TYPE:
                 request.targetViewer = this.getViewer(document.getElementById(PARAGRAPH_FRM));
                 request.targetLocation = this.CENTER;
                 request.data = PARAGRAPH_MV + "?" + request.id;
                 break;
            case REQ_SHOW_FILE:
                 request.targetViewer = this.getViewer(document.getElementById(PARAGRAPH_FRM));
                 request.targetLocation = this.CENTER;
                 request.data = PARAGRAPH_MV + "?" + request.id;
                 break;
            case REQ_SELECTED_ANSWERS_TYPE:
                 
                 request.targetViewer = this.getViewer(document.getElementById(ANSWERS_FRM));
                 request.targetLocation = this.TOP_RIGHT;
                 request.data = ANSWERS_MV + "?" + request.id;
                 break;
            case REQ_NEWMODULE_TYPE:
                 
                 request.targetViewer = this.getViewer(document.getElementById(NEWMODULE_FRM));
                 request.targetLocation = this.CENTER;
                 request.data = NEWMODULE_MV;
                 break;
            
            case REQ_MESSAGE_TYPE:            
                 request.targetViewer = this.getViewer(document.getElementById(MESSAGE_FRM));
                 request.targetLocation = this.CENTER;
                 /*
                 FIX for countdown, the only way to reinitiate
                 the Countdown is to reload the page.
                 This is ONLY because we are unable to call the StartCountdown function
                 through the viewer.
                 */
                 
                 var now = new Date();
                 request.data = MESSAGE_MV; // + "?StartCount=" + now.getTime();
                 this.updatePreviousTab(request);     
 
                 break;
                 
                 case REQ_PERIODIC_TABLE_TYPE://where the tab will be located
                 
                 
                 //to where the tab will try to persist
                 request.targetViewer = this.getViewer(document.getElementById(PERIODIC_TABLE_FRM));
                 request.targetLocation = this.CENTER; //also this.BOTTOM_RIGHT, BOTTOM_LEFT
                 
                 //located in Constatnts.js , Media Viewer is the aspx page name to load
                 request.data = PERIODIC_TABLE_MV ;
                 break;
                 
            /*
            
            =============================================================================
            case REQ_[modulename]_TYPE://where the tab will be located
                 
                 
                 //to where the tab will try to persist
                 request.targetViewer = this.getViewer(document.all[[modulename]_FRM]);
                 request.targetLocation = this.CENTER; //also this.BOTTOM_RIGHT, BOTTOM_LEFT
                 
                 //located in Constatnts.js , Media Viewer is the aspx page name to load
                 request.data = [newmodule]_MV;
                 break;
              */   
            default:
                return false;
        }        
        this.saveHistoryData(request);
        this.setViewer(request);
    }
    
    // Get viewer by iframe element
    function getViewer(iframe)
    {        
        for(j=0;j<this.viewers.length;j++)
        {
            for(i = 0;i<this.viewers[j].length;i++)
            {
                if (this.viewers[j][i].iframe == iframe)
                {
                    return this.viewers[j][i];
                }
            }        
        }        
        return null;
    }

    // Set Viewer
    function setViewer(request)
    {    
        //CR: hold active viewers in an array[max] of pointers to viewers        
        var viewer = this.getActiveViewer(request.targetLocation);
                
        try
        {
            // not all the viewers have this method
            viewer.requestShutDown();
        }
        catch(ex)
        {
        }
        
        //check if target exists
        if (request.targetViewer != null)
        {            
            // Hide previous viewer
            viewer.iframe.className = "hideIframe";           
            viewer.visible = false;
            if (viewer.button != null)
            {
                viewer.button.state = '0';
                viewer.button.className = "TabOff";
            }
            
            // Show current viewer
            
            //Change the main page
            if (request.type == REQ_CHANGE_NEW_COURSE_TYPE)
            {
                top.window.location = request.data;
                return;
            }
            //lilah start
            
            if (request.type == REQ_DICTIONARY_TYPE)
            {                 
                 now = new Date();                 
                 if(this.LastDictionaryRequestTime != null)
                 {
                    elapsed = now.getTime() - this.LastDictionaryRequestTime; 
                    if(elapsed < 3000)
                        clearTimeout(this.DictionarITimerID);
                 }
                 this.LastDictionaryRequestTime = now.getTime();                 
                 this.currentDictionaryIframe = request.targetViewer.iframe;
                 this.currentDictionaryData = request.data;                
                 this.DictionarITimerID = window.setTimeout('LoadDictionary()',3000);
                 //request.targetViewer.iframe.src = request.data; 
                 //testwindow.document.writeln('<span style="text-size:10px;">' + request.data + '</span><br>');
            }
            //lilah end
            
            //Change source only if needed - no state saved
            if (request.data != null && request.targetViewer.iframe.src != request.data && request.type != REQ_DICTIONARY_TYPE)
            {
              request.targetViewer.iframe.src = request.data;  
            }
            
            //If it's a flash paragraph , resume
//            var frame = top[request.targetViewer.iframe.name];
//	        if (typeof(frame.frmFlashPlayer) == "object")
//	        {
//	            var flashObj = frame.frmFlashPlayer.document.getElementById("flashObj");
//	            if (flashObj != null)
//	            {	                            
//	                flashObj.Play();
//	            }
//	        }
            
            request.targetViewer.iframe.className = "showIframe";
            request.targetViewer.visible = true;
            if (request.targetViewer.button != null)
            {
                request.targetViewer.button.state = '1';
                request.targetViewer.button.className = "TabOn";
            }
        }
    }    
    
    // get Active Viewer from container
    function getActiveViewer(j)
    {
        var isLooking = true;        
        for(i = 0;i<this.viewers[j].length && isLooking ;i++)
        {
            isLooking =   !(this.viewers[j][i].visible == true);
        }        
        i--;        
        return this.viewers[j][i];
    }
    
 
    function saveHistoryData(request)
    {
        if (request.objectId != null)
        {
            switch(request.targetLocation)
            {            
                case this.CENTER:
                    if(document.frames) // IE
                        document.frames[HISTORY_FRM].document.getElementById(TXT_PANEL_0).value = request.objectId;
                    else if(top.frames) // Mozilla
                        top.frames[HISTORY_FRM].document.getElementById(TXT_PANEL_0).value = request.objectId;
                    break;
                case this.TOP_RIGHT:
                    if(document.frames) // IE
                        document.frames[HISTORY_FRM].document.getElementById(TXT_PANEL_1).value = request.objectId;
                    else if(top.frames) // Mozilla
                        top.frames[HISTORY_FRM].document.getElementById(TXT_PANEL_0).value = request.objectId;
                    break;
                case this.BOTTOM_RIGHT:
                    if(document.frames) // IE
                        document.frames[HISTORY_FRM].document.getElementById(TXT_PANEL_2).value = request.objectId;
                    else if(top.frames) // Mozilla
                        top.frames[HISTORY_FRM].document.getElementById(TXT_PANEL_0).value = request.objectId;
                    break;
            }
            //Refresh history
            if(document.frames) // IE
                var theForm =  document.frames[HISTORY_FRM].document.getElementById(FORM_HISTORY);
            else if(top.frames) // Mozilla
                var theForm =  top.frames[HISTORY_FRM].document.getElementById(FORM_HISTORY);

            //emulate a button press to the "updateHistoryClick" in the form
            theForm.__EVENTTARGET.value = BTN_SEND_HISTORY;
            theForm.__EVENTARGUMENT.value = '';
            theForm.submit();
        }
    }
    
    function updateMilliSecFromMouseMove(request)
    {        
        var currentDate = new Date();
        lastMouseMoveTime = currentDate.getTime();
    }
    
    function updatePreviousTab(request)
    {
        var viewer = this.getActiveViewer(request.targetLocation);
        if(document.frames) // IE
            document.frames(TIMER_FRM).document.getElementById(TXT_PANEL_0).value = viewer.iframe.id;
        else if(top.frames) // Mozilla
            top.frames[TIMER_FRM].document.getElementById(TXT_PANEL_0).value = viewer.iframe.id;
    }
    
    function showPreviousTab()
    {
        var previousFrame;
        if(document.frames) // IE
            previousFrame = document.frames(TIMER_FRM).document.getElementById(TXT_PANEL_0).value;
        else if(top.frames) // Mozilla
            previousFrame = top.frames[TIMER_FRM].document.getElementById(TXT_PANEL_0).value;
         
        var targetViewer = this.getViewer(document.getElementById(previousFrame));
        var viewer = this.getActiveViewer(this.CENTER);
        //check if target exists
        
        // Amir: this replaces the 
        // && viewer.requestShutDown()
        // now not all the viewers have this method
        try
        {
            viewer.requestShutDown();
        }
        catch(ex)
        {
        }        
        
        if (targetViewer != null)
        {
            // Hide previous viewer
            viewer.iframe.className = "hideIframe";           
            viewer.visible = false;
           
            targetViewer.iframe.className = "showIframe";
            targetViewer.visible = true;  
        }
    }
  
}