﻿//---
Type.registerNamespace("Efprog");

Efprog.ImageListManagerSettings = function(){
    Efprog.ImageListManagerSettings.initializeBase(this);
    this._addImageHandlerUrl = "/Handlers/AddImage.ashx";           //Url хендлера добавления
    this._deleteImageHandlerUrl = "/Handlers/DeleteImage.ashx";     //Url хендлера добавления
    this._getImageHandlerUrl = "/Handlers/GetImage.ashx";           //Url хендлера добавления    
    this._contextID = "1";                                           //Ид контекста
    this._contextTypeID = "1";                                           //Ид типа контекста    
    this._fileIframesContainerId = "FileIframesContainer";           //Ид контейнера фреймов 
    this._fileInputId = "File";                                      //Ид поля ввода файла 
    this._submitId = "Submit";                                       //Ид кнопки отправки  
    this._imageListContainerId = "ImageList";                        //Ид контейнера для изображений 
    this._indicatorImageUrl = "/Images/indicator.gif";               //Url изображения-индкатора процесса 
    this._uploadResultContainerId = "Result";                        //Ид контейнера результатов загрузки 
    this._imageViewerId = "ImageViewer"; 
    this._imageViewerImageId = "ImageViewerImage";
    
}
Efprog.ImageListManagerSettings.prototype = {
    get_addImageHandlerUrl: function(){
        return this._addImageHandlerUrl;
    },
    set_addImageHandlerUrl: function(value){
        this._addImageHandlerUrl = value;
    },
    get_deleteImageHandlerUrl: function(){
        return this._deleteImageHandlerUrl;
    },
    set_deleteImageHandlerUrl: function(value){
        this._deleteImageHandlerUrl = value;
    }, 
    get_getImageHandlerUrl: function(){
        return this._getImageHandlerUrl;
    },
    set_getImageHandlerUrl: function(value){
        this._getImageHandlerUrl = value;
    },         
    get_contextID: function(){
        return this._contextID;
    }, 
    set_contextID: function(value){
        this._contextID = value;
    },  
    get_fileIframesContainerId: function(){
        return this._fileIframesContainerId;
    },
    get_fileInputId: function(){
        return this._fileInputId;
    },
    get_submitId: function(){
        return this._submitId;
    },  
    get_imageListContainerId: function(){
        return this._imageListContainerId;
    },
    set_imageListContainerId: function(value){
        this._imageListContainerId = value;
    },
    get_indicatorImageUrl: function(){
        return this._indicatorImageUrl;
    },
    set_indicatorImageUrl: function(value){
        this._indicatorImageUrl = value;
    }, 
    get_uploadResultContainerId: function(){
        return this._uploadResultContainerId;
    },
    get_imageViewerId: function(){
        return this._imageViewerId;
    },
    get_imageViewerImageId: function(){
        return this._imageViewerImageId;
    },
    get_contextTypeID: function(){
        return this._contextTypeID;
    }, 
    set_contextTypeID: function(value){
        this._contextTypeID = value;
    }                                   
}
Efprog.ImageListManagerSettings._instance = null;
Efprog.ImageListManagerSettings.get_instance = function(){
        if(this._instance == null){
            this._instance = new Efprog.ImageListManagerSettings();
        }
        return this._instance;
}

Efprog.ImageListManagerSettings.registerClass("Efprog.ImageListManagerSettings");

Efprog.ThumbnailType = function(){}
Efprog.ThumbnailType.prototype = {
    Upload: 1,
    Presentation: 2,
    Deletion: 3
}
Efprog.ThumbnailType.registerEnum("Efprog.ThumbnailType");

Efprog.Thumbnail = function(){
    Efprog.Thumbnail.initializeBase(this);
    this._chargeDiv = null;
    this._type = null;
}

Efprog.Thumbnail.prototype = {
    ConstructUpload: function(text){
        this._type = Efprog.ThumbnailType.Upload;
            
        var doc = this._getDoc();
        if(!doc) throw "Document is not found!";

        var listDiv = this._getListDiv();
        if(!listDiv) throw "Could not find list div!";
        
        var uploadDiv = doc.createElement('div');
        uploadDiv.className='imageListThumbnailWait';
        
        var uploadDivImage = doc.createElement('div');
        uploadDivImage.className='imageListThumbnailWaitImage';
        var uploadImg = doc.createElement('img');
        uploadImg.src = Efprog.ImageListManagerSettings.get_instance().get_indicatorImageUrl();
        uploadImg.alt = 'progress indicator';                     

        var uploadDivText = doc.createElement('div');
        uploadDivText.className='imageListThumbnailWaitText';      
        uploadDivText.innerHTML = String.format("<span>{0}</span>", text);     
        uploadDivImage.appendChild(uploadImg);
        uploadDiv.appendChild(uploadDivImage);
        uploadDiv.appendChild(uploadDivText);

        listDiv.appendChild(uploadDiv); 
        //listDiv.insertBefore(indicatorDiv, destination); 
        uploadDiv.scrollIntoView(true); 
        
        this._chargeDiv = uploadDiv;     
    },
    
    ConstructPresentation: function(dic){
       this._type = Efprog.ThumbnailType.Presentation; 
       
        var doc = this._getDoc();
        if(!doc) throw "Document is not found!";

        var listDiv = this._getListDiv();
        if(!listDiv) throw "Could not find list div!";
        
        var presentationDiv = doc.createElement('div');
        var now = new Date();
        var presentationDivId = String.format("imageListThumbnail_{0}{1}{2}{3}{4}{5}{6}", 
            now.getUTCFullYear().toString(), 
            (now.getUTCMonth() + 1).toString(), 
            now.getUTCDate().toString(), 
            now.getUTCHours().toString(), 
            now.getUTCMinutes().toString(), 
            now.getUTCSeconds().toString(), 
            now.getUTCMilliseconds().toString());
        presentationDiv.id = presentationDivId;   
        presentationDiv.className='imageListThumbnail'; 
              
        var presentationDivImage = doc.createElement('div');
        var presentationImage = doc.createElement('img');
        presentationImage.className = 'Thumbnail';
        presentationImage.alt = dic['fileName'];
        presentationImage.src = String.format("{0}?itemId={1}&itemTypeId={2}&imageFileName={3}&sizeFactor=Thumbnail", Efprog.ImageListManagerSettings.get_instance().get_getImageHandlerUrl(), Efprog.ImageListManagerSettings.get_instance().get_contextID(), Efprog.ImageListManagerSettings.get_instance().get_contextTypeID(), dic['fileName']);
        presentationImage.onmouseover = DisplayViewForImage;
        presentationDivImage.appendChild(presentationImage);
        presentationDiv.appendChild(presentationDivImage);
        
        var presentationDivDelete = doc.createElement('div');
        var presentationCenter = doc.createElement('center');
        var presentationADelete = doc.createElement('a');
        presentationADelete.href = String.format("javascript:Efprog.ControllerImageDelete.get_instance().Process('{0}', '{1}')", presentationDivId, dic['fileName']);
        presentationADelete.innerHTML = "Удалить";
        presentationCenter.appendChild(presentationADelete);
        presentationDivDelete.appendChild(presentationCenter);
        presentationDiv.appendChild(presentationDivDelete);
        
        if(this._chargeDiv)
            listDiv.replaceChild(presentationDiv, this._chargeDiv);             
        else
            listDiv.appendChild(presentationDiv);
             
        presentationDiv.scrollIntoView(true); 
        
        this._chargeDiv = presentationDiv;           
    },
    
    ConstructDeletion: function(text, chargeDivId){
        this._type = Efprog.ThumbnailType.Deletion;
            
        var doc = this._getDoc();
        if(!doc) throw "Document is not found!";

        var listDiv = this._getListDiv();
        if(!listDiv) throw "Could not find list div!";
        
        var userChargeDiv = doc.getElementById(chargeDivId);
        if(!userChargeDiv) throw "User charge div is not found!";
        
        var deletionDiv = doc.createElement('div');
        deletionDiv.className='imageListThumbnailWait';
        
        var deletionDivImage = doc.createElement('div');
        deletionDivImage.className='imageListThumbnailWaitImage';
        var deletionImg = doc.createElement('img');
        deletionImg.src = Efprog.ImageListManagerSettings.get_instance().get_indicatorImageUrl();
        deletionImg.alt = 'progress indicator';                     

        var deletionDivText = doc.createElement('div');
        deletionDivText.className='imageListThumbnailWaitText';      
        deletionDivText.innerHTML = String.format("<span>{0}</span>", text);     
        deletionDivImage.appendChild(deletionImg);
        deletionDiv.appendChild(deletionDivImage);
        deletionDiv.appendChild(deletionDivText);

        listDiv.replaceChild(deletionDiv, userChargeDiv); 
        userChargeDiv.scrollIntoView(true); 
        
        this._chargeDiv = deletionDiv;     
    },
    
    Clear: function(){
        if(this._chargeDiv){
            var doc = this._getDoc();
            if(!doc) throw "Document is not found!";

            var listDiv = this._getListDiv();
            if(!listDiv) throw "Could not find list div!";
            
            listDiv.removeChild(this._chargeDiv);
            
            this._chargeDiv = null;
        }
        if(this._type) this._type = null;
    },
    
    _getDoc: function(){
        return window.parent.document;
    },
    _getListDiv: function(){
        var doc = this._getDoc();
        if(!doc) throw "Document is not found!";
        var listDiv = doc.getElementById(Efprog.ImageListManagerSettings.get_instance().get_imageListContainerId());
        return listDiv;  
    }
}

Efprog.Thumbnail.registerClass("Efprog.Thumbnail");


Efprog.UploadCompletedEventArgs = function(fileName){
  var e = Function._validateParams(arguments, [{name: "fileName", type: String}]);
  if (e) throw e;
  Efprog.UploadCompletedEventArgs.initializeBase(this);
  this._fileName = fileName;
}

Efprog.UploadCompletedEventArgs.prototype = {
    get_fileName: function(){
        return this._fileName;
    }
}

Efprog.UploadCompletedEventArgs.registerClass('Efprog.UploadCompletedEventArgs', Sys.EventArgs);


Efprog.UploadStartedEventArgs = function(fileName){
  var e = Function._validateParams(arguments, [{name: "fileName", type: String}]);
  if (e) throw e;
  Efprog.UploadStartedEventArgs.initializeBase(this);
  this._fileName = fileName;
}

Efprog.UploadStartedEventArgs.prototype = {
    get_fileName: function(){
        return this._fileName;
    }
}

Efprog.UploadStartedEventArgs.registerClass('Efprog.UploadStartedEventArgs', Sys.EventArgs);

Efprog.CommandImageAdd = function(destinationContainerDiv){
    Efprog.CommandImageAdd.initializeBase(this);
    this._imageFileInputChangeHandler = Function.createDelegate(this, this._onImageFileInputChange);
    this._completionHandler = Function.createDelegate(this, this.OnComplete); 
    this._thumbnail = null;   
    this._iframe = null;
    
    this._eventList = new Sys.EventHandlerList();
}

Efprog.CommandImageAdd.prototype = {
    CreateFileIframe: function(){
        var par = window.parent.document;
        var fileFrames = par.getElementById(Efprog.ImageListManagerSettings.get_instance().get_fileIframesContainerId());
        if(!fileFrames) throw "Could not find file frames!";

        var new_iframe = par.createElement('iframe');
        new_iframe.style.display = 'none';
        new_iframe.style.margin = '0';
        new_iframe.style.padding = '0';
        fileFrames.appendChild(new_iframe);
        new_iframe.frameBorder = "0";
        new_iframe.className = "imageListFile";
        new_iframe.contentWindow.document.open();
        new_iframe.contentWindow.document.write(this._getFileFrameMarkup());
        new_iframe.contentWindow.document.close();
        new_iframe.style.display = 'block';   
              
        this._iframe = new_iframe;       
        this._attachToFileInputChangeEvent();    
    },    
    _attachToFileInputChangeEvent: function(){
        if(!this._iframe) throw "Frame is not assigned!";
                
        var fileInput = this._iframe.contentWindow.document.getElementById(Efprog.ImageListManagerSettings.get_instance().get_fileInputId());
        if(!fileInput) throw "Could not find fileInput";        
        if(Sys.Browser.agent == Sys.Browser.InternetExplorer)
            fileInput.attachEvent("onpropertychange", this._imageFileInputChangeHandler);
        else
            fileInput.addEventListener("change", this._imageFileInputChangeHandler, false);  
    },
    _onImageFileInputChange: function(){
        if(!this._iframe) throw "Frame is not assigned!";
         
        this._iframe.style.visibility = 'hidden';
        this._iframe.style.height = '0px';
        this._iframe.style.width = '0px';
        
        this._thumbnail = new Efprog.Thumbnail();
        this._thumbnail.ConstructUpload('Идёт загрузка...');      
        
        if(Sys.Browser.agent == Sys.Browser.InternetExplorer)
            this._iframe.attachEvent("onload", this._completionHandler);
        else
            this._iframe.addEventListener("load", this._completionHandler, false);          
        
        var submitBtn = this._iframe.contentWindow.document.getElementById(Efprog.ImageListManagerSettings.get_instance().get_submitId())
        if(!submitBtn) throw "Could not find submit button!";                                        
        submitBtn.click();  
            //...уведомление подписчиков;
        var args = new Efprog.UploadStartedEventArgs('');
        this.OnUploadStartedEvent(args);  
    },
    
    _getFileFrameMarkup: function(){
        var sb = new Sys.StringBuilder();
        sb.append("<html><head></head><body style='margin:0;padding:0;border:none 0;'><form style='padding:0;margin:0;border:none 0;' name=\'FileForm\' action=\'");
        sb.append(Efprog.ImageListManagerSettings.get_instance().get_addImageHandlerUrl());
        sb.append("?itemId=");
        sb.append(Efprog.ImageListManagerSettings.get_instance().get_contextID());       
        sb.append("&itemTypeId=");
        sb.append(Efprog.ImageListManagerSettings.get_instance().get_contextTypeID());                
        sb.append("\' method=\'post\' enctype=\'multipart/form-data\'><input id=\'");
        sb.append(Efprog.ImageListManagerSettings.get_instance().get_fileInputId());
        sb.append("' type=\'file\' style=\'width:100%;\' name=\'");
        sb.append(Efprog.ImageListManagerSettings.get_instance().get_fileInputId());
        sb.append("\' /><input type=\'submit\' style=\'display:none;\' id=\'");
        sb.append(Efprog.ImageListManagerSettings.get_instance().get_submitId());
        sb.append("\' name=\'");
        sb.append(Efprog.ImageListManagerSettings.get_instance().get_submitId());
        sb.append("\'/></form></body></html>");
        return sb.toString();
    },
    OnComplete: function(){
        if(!this._iframe) throw "Frame is not assigned!";
        if(!this._thumbnail) return;
        
        var result = Sys.Serialization.JavaScriptSerializer.deserialize(this._iframe.contentWindow.document.getElementById(Efprog.ImageListManagerSettings.get_instance().get_uploadResultContainerId()).innerHTML);
        this._thumbnail.ConstructPresentation(result);
            //...уведомление подписчиков;
        var args = new Efprog.UploadCompletedEventArgs(result['fileName']);
        this.OnUploadCompletedEvent(args);
    },
    add_eventUploadCompleted: function(handler){
        var e = Function._validateParams(arguments, [{name: "handler", type: Function}]);
        if (e) throw e;
        this._eventList.addHandler("UploadCompletedEvent", handler);        
    },
    remove_eventUploadCompleted: function(handler){
        var e = Function._validateParams(arguments, [{name: "handler", type: Function}]);
        if (e) throw e;
        this._eventList.removeHandler("UploadCompletedEvent", handler);
    },
    
    OnUploadCompletedEvent: function(eventArgs){
        var e = Function._validateParams(arguments, [{name: "eventArgs", type: Efprog.UploadCompletedEventArgs}]);
        if (e) throw e;
        var handler = this._eventList.getHandler("UploadCompletedEvent");
        if (handler) handler(this, eventArgs);
    },
    
    add_eventUploadStarted: function(handler){
        var e = Function._validateParams(arguments, [{name: "handler", type: Function}]);
        if (e) throw e;
        this._eventList.addHandler("UploadStartedEvent", handler);        
    },
    remove_eventUploadStarted: function(handler){
        var e = Function._validateParams(arguments, [{name: "handler", type: Function}]);
        if (e) throw e;
        this._eventList.removeHandler("UploadStartedEvent", handler);
    },
    
    OnUploadStartedEvent: function(eventArgs){
        var e = Function._validateParams(arguments, [{name: "eventArgs", type: Efprog.UploadStartedEventArgs}]);
        if (e) throw e;
        var handler = this._eventList.getHandler("UploadStartedEvent");
        if (handler) handler(this, eventArgs);
    }              
}

Efprog.ControllerImageAdd = function(){
    Efprog.ControllerImageAdd.initializeBase(this);
    this._commands = null;
    this._onCommandUploadStartedHandler = Function.createDelegate(this, this._onCommandUploadStarted);
    this._isInitialized = false;
}

Efprog.ControllerImageAdd.prototype = {
    Initialize: function(){
        if(this._isInitialized) return;
        this._createFileIframe();
        this._isInitialized = true;
    },
    _createFileIframe: function(){
        var command = this._getCommand();
        command.CreateFileIframe();
    },   
    _getCommand: function(){
        if(this._commands == null) this._commands = new Array();
        var command = new Efprog.CommandImageAdd();
        command.add_eventUploadStarted(this._onCommandUploadStartedHandler);
        this._commands[this._commands.length] = command;
        return  command;  
    },
    _onCommandUploadStarted: function(sender, args){
        this._createFileIframe();
    }
}
Efprog.ControllerImageAdd._instance = null;
Efprog.ControllerImageAdd.get_instance = function(){
        if(this._instance == null){
            this._instance = new Efprog.ControllerImageAdd();
        }
        return this._instance;
}

Efprog.ControllerImageAdd.registerClass("Efprog.ControllerImageAdd");

Efprog.CommandImageDelete = function(){
    Efprog.CommandImageDelete.initializeBase(this);
    this._request = null;
    this._isInProcess = false;
    this._completionHandler = Function.createDelegate(this, this._onComplete);
    this._sourceThumbnail = null;       
}

Efprog.CommandImageDelete.prototype = {
    get_isInProcess: function(){
        return this._isInProcess;
    },
    Process: function(sourceImageContainerId, fileName){
        var e = Function._validateParams(arguments, [
            {name: "fileName", type: String, mayBeNull: false}, 
            {name: "sourceImageContainerId", type: String, mayBeNull: false}
            ]);
        if (e) throw e;
        
        if(this._isInProcess) return true;
        this._isInProcess = true;
        
        if(!this._request) this._request = new Sys.Net.WebRequest();
        this._request.httpVerb = "GET";
        this._request.set_url(String.format("{0}?itemId={1}&itemTypeId={2}&imageFileName={3}", Efprog.ImageListManagerSettings.get_instance().get_deleteImageHandlerUrl(), Efprog.ImageListManagerSettings.get_instance().get_contextID(), Efprog.ImageListManagerSettings.get_instance().get_contextTypeID(), fileName));       
        this._request.set_userContext(""); 
        this._request.add_completed(this._completionHandler); 
                
        this._sourceThumbnail = new Efprog.Thumbnail();
        this._sourceThumbnail.ConstructDeletion("Идёт удаление...", sourceImageContainerId);
        
        this._request.invoke();           
        return true;   
    },
    _onComplete: function(executor, eventArgs){
        if(!this._request) throw "Request is not found!";
        if(!this._sourceThumbnail) throw "Source thumbnail is not found!";
        
        this._sourceThumbnail.Clear();
        
        this._request.remove_completed(this._completionHandler);
        this._isInProcess = false; 
        this._isInitialized = false;     
    }
}

Efprog.ControllerImageDelete = function(){
    Efprog.ControllerImageDelete.initializeBase(this);
    this._commands = null;
}

Efprog.ControllerImageDelete.prototype = {
    Initialize: function(){
        this._isInitialized = true;
    },  
    Process: function(sourceImageContainerId, fileName){
        var e = Function._validateParams(arguments, [
            {name: "fileName", type: String, mayBeNull: false}, 
            {name: "sourceImageContainerId", type: String, mayBeNull: false}
            ]);
        if (e) throw e;
        var command = this._getCommand();
        command.Process(sourceImageContainerId, fileName);    
    },   
    _getCommand: function(){
        if(this._commands == null) this._commands = new Array();
        var command = new Efprog.CommandImageDelete();
        this._commands[this._commands.length] = command; 
        return  command;
    }
}
Efprog.ControllerImageDelete._instance = null;
Efprog.ControllerImageDelete.get_instance = function(){
        if(this._instance == null){
            this._instance = new Efprog.ControllerImageDelete();
        }
        return this._instance;
}

Efprog.ControllerImageDelete.registerClass("Efprog.ControllerImageDelete");

Efprog.Point = function(x, y){
    var e = Function._validateParams(arguments, [
        {name: "x", integer: true, mayBeNull: false}, 
        {name: "y", integer: true, mayBeNull: false}
        ]);
    if (e) throw e;

    Efprog.Point.initializeBase(this);
    this._x = x;
    this._y = y;
}

Efprog.Point.prototype = {
    get_x: function(){
        return this._x;
    },
    set_x: function(value){
        var e = Function._validateParams(arguments, [{name: "value", integer: true, mayBeNull: false}]);
        if (e) throw e;    
        this._x = value;
    },
    get_y: function(){
        return this._y;
    },
    set_y: function(value){
        var e = Function._validateParams(arguments, [{name: "value", integer: true, mayBeNull: false}]);
        if (e) throw e;    
        this._y = value;
    },
    compareX: function(point){
        var e = Function._validateParams(arguments, [{name: "point", type: Efprog.Point, mayBeNull: false}]);
        if (e) throw e; 
        if(this._x > point.get_x()) return 1;
        if(this._x < point.get_x()) return -1;
        return 0;    
    },
    compareY: function(point){
        var e = Function._validateParams(arguments, [{name: "point", type: Efprog.Point, mayBeNull: false}]);
        if (e) throw e; 
        if(this._y > point.get_y()) return 1;
        if(this._y < point.get_y()) return -1;
        return 0;    
    },
    shift: function(point){
//        var e = Function._validateParams(arguments, [
//            {name: "point", type: Efprog.Point, mayBeNull: false}, 
//            ]); 
//        if (e) throw e; 
        
        return new Efprog.Point(this._x + point.get_x(), this._y + point.get_y());
    }       
}

Efprog.Point.registerClass("Efprog.Point");

Efprog.Rect = function(){
    Efprog.Rect.initializeBase(this);
    this._tl = new Efprog.Point(0, 0);
    this._br = new Efprog.Point(0, 0);
}

Efprog.Rect.prototype = {
    get_tl: function(){
        return this._tl;
    },
    set_tl: function(value){
        var e = Function._validateParams(arguments, [{name: "value", type: Efprog.Point, mayBeNull: false}]);
        if (e) throw e; 
        this._tl = value;
    },
    get_br: function(){
        return this._br;
    },
    set_br: function(value){
        var e = Function._validateParams(arguments, [{name: "value", type: Efprog.Point, mayBeNull: false}]);
        if (e) throw e; 
        this._br = value;
    },
    shift: function(point){
//        var e = Function._validateParams(arguments, [
//            {name: "point", type: Efprog.Point, mayBeNull: false}
//            ]); 
//        if (e) throw e; 
        var result = new Efprog.Rect();
        result.set_tl(this._tl.shift(point));
        result.set_br(this._br.shift(point));
        return result;
    },    
    isInside: function(point){
        var e = Function._validateParams(arguments, [{name: "point", type: Efprog.Point, mayBeNull: false}]);
        if (e) throw e; 
        return point.compareX(this._tl) >= 0 && 
            point.compareY(this._tl) >= 0 && 
            point.compareX(this._br) <= 0 && 
            point.compareY(this._br) <= 0;     
    }    
}

Efprog.Rect.registerClass("Efprog.Rect");

//Copy from:
//Capturing The Mouse Position
//(C) 2000 www.CodeLifter.com
Efprog.ImageViewController = function(){
    Efprog.ImageViewController.initializeBase(this);
    this._mouseX = 0;
    this._mouseY = 0;
    this._isIE = Sys.Browser.agent == Sys.Browser.InternetExplorer;
    this._mouseMoveHandler = Function.createDelegate(this, this._onMouseMove);
    this._isViewDisplyed = false;
    this._viewerX = 0;
    this._viewerY = 0;
    this._displayRect = null;  
    this._imageListContainer = window.parent.document.getElementById(Efprog.ImageListManagerSettings.get_instance().get_imageListContainerId()); 
    if(!this._imageListContainer) throw "Could not find image list container!";
    this._displayedImageUrl = null;
    this._initialized = false;
}

Efprog.ImageViewController.prototype = {
    Initialize: function(){
        if(this._initialized) return;
        // If NS -- that is, !IE -- then set up for mouse capture
        if (!this._isIE) window.document.captureEvents(Event.MOUSEMOVE)
        // Set-up to use getMouseXY function onMouseMove
        window.document.onmousemove = this._mouseMoveHandler;
        this._initialized = true;
    },
    _onMouseMove: function(e){
        if(!this._initialized) return;
        var curMouseX = 0;
        var curMouseY = 0;
        if(this._isIE){ // grab the x-y pos.s if browser is IE
            curMouseX = event.clientX + window.document.body.scrollLeft;
            curMouseY = event.clientY + window.document.body.scrollTop;
        }else{  // grab the x-y pos.s if browser is NS
            curMouseX = e.pageX;
            curMouseY = e.pageY;
        }  
        // catch possible negative values in NS4
        if (curMouseX < 0) curMouseX = 0;
        if (curMouseY < 0) curMouseY = 0;  
        
        if(this._isViewDisplyed){
            if(!this._displayRect) throw "Display rect is not assigned!";
            
            var actualRect = this._displayRect.shift(new Efprog.Point(-1 * this._imageListContainer.scrollLeft, -1 * this._imageListContainer.scrollTop));
            
            if(!actualRect.isInside(new Efprog.Point(curMouseX, curMouseY)))
                this.Hide();
            else{
                var viewer = window.document.getElementById(Efprog.ImageListManagerSettings.get_instance().get_imageViewerId());
                if(!viewer) throw "ImageViewer is not found!";
                
                if(Math.abs(this._viewerX - curMouseX) > 5 || Math.abs(this._viewerY - curMouseY) > 5){
                    viewer.style.display='none';
                    this._viewerX = curMouseX;
                    this._viewerY = curMouseY;
                    viewer.style.left = String.format("{0}px", this._viewerX.toString());
                    viewer.style.top = String.format("{0}px", this._viewerY.toString());
                    viewer.style.display='block';
                }            
            } 
        }
        this._mouseX = curMouseX;
        this._mouseY = curMouseY;
        return true;
    },
    Display: function(imageHref, obj){
//        var e = Function._validateParams(arguments, [
//            {name: "imageHref", type: String, mayBeNull: false},
//            {name: "obj", mayBeNull: false},
//            ]);
//        if (e) throw e; 
        
        if(!this._initialized) return;
        if(this._isViewDisplyed && this._displayedImageUrl && this._displayedImageUrl == imageHref) return;
        
        var objPos = FindPos(obj);
        var objWidth = obj.offsetWidth;
        var objHeight = obj.offsetHeight;
        var objTl = new Efprog.Point(objPos[0], objPos[1]);
        var objBr = new Efprog.Point(objPos[0] + objWidth, objPos[1] + objHeight);
        if(!this._displayRect)
            this._displayRect = new Efprog.Rect();
        this._displayRect.set_tl(objTl);
        this._displayRect.set_br(objBr);
        
        var viewer = window.document.getElementById(Efprog.ImageListManagerSettings.get_instance().get_imageViewerId());
        if(!viewer) throw "ImageViewer is not found!";
        var image = window.document.getElementById(Efprog.ImageListManagerSettings.get_instance().get_imageViewerImageId());
        if(!image) throw "ImageViewerImage is not found!";
             
        image.src = Efprog.ImageListManagerSettings.get_instance().get_indicatorImageUrl();
        this._viewerX = this._mouseX;
        this._viewerY = this._mouseY;
        viewer.style.left = String.format("{0}px", this._mouseX.toString());
        viewer.style.top = String.format("{0}px", this._mouseY.toString()); 
        viewer.style.display='block';
        image.src = imageHref; 
        this._isViewDisplyed = true; 
        this._displayedImageUrl =  imageHref;          
    },
    Hide: function(){
        if(!this._initialized) return;
        if(!this._isViewDisplyed) return;
        var viewer = window.document.getElementById(Efprog.ImageListManagerSettings.get_instance().get_imageViewerId());
        if(!viewer) throw "ImageViewer is not found!";
        var image = window.document.getElementById(Efprog.ImageListManagerSettings.get_instance().get_imageViewerImageId());
        if(!image) throw "ImageViewerImage is not found!";
  
        image.src = Efprog.ImageListManagerSettings.get_instance().get_indicatorImageUrl();        
        viewer.style.display='none'; 
        this._isViewDisplyed = false;
        this._displayedImageUrl = null;
    }   
}

Efprog.ImageViewController._instance = null;
Efprog.ImageViewController.get_instance = function(){
        if(this._instance == null){
            this._instance = new Efprog.ImageViewController();
        }
        return this._instance;
}

Efprog.ImageViewController.registerClass("Efprog.ImageViewController");
//---
function DisplayViewForImage(){
    Efprog.ImageViewController.get_instance().Display(String.format("{0}?itemId={1}&itemTypeId={2}&imageFileName={3}&sizeFactor=Standard", Efprog.ImageListManagerSettings.get_instance().get_getImageHandlerUrl(), Efprog.ImageListManagerSettings.get_instance().get_contextID(), Efprog.ImageListManagerSettings.get_instance().get_contextTypeID(), this.alt), this);
}
//---
function PreDelete(){
    HideViewer();
    if(!window.confirm('Вы действительно хотите удалить это изображение?'))                       
        return false;        
    return true;        
}