﻿var _imgZoomFactorCtrl, _imgFitTypeCtrl, _imgRotFactorCtrl, _imgHandlerNameCtrl, _imgStubNameCtr, _timeLabel, _imgCtrl, _imgContainer, _imgWidthCtrl, _imgHeightCtrl;
var _imgStubName;
var _timeBegin, _timeEnd;
var _imageSource;
var arrImages 
var preImages
var coverage=0
var currCount=0
var loaded
var i,covered,timerID

function preInit(widgetID, imgStubName)
{
    _imgStubName = imgStubName;
    _imgRotFactorCtrl = document.getElementById(widgetID + '_ImgRotFactorCtr');
    _imgZoomFactorCtrl = document.getElementById(widgetID + '_ImgZoomFactorCtrl');
    _imgCtrl = document.getElementById(widgetID + '_DocImage');
    _imgContainer = document.getElementById(widgetID + '_ContentContainer1');
    _timeLabel = document.getElementById(widgetID + '_LoadingLabel');
    _imgWidthCtrl = document.getElementById(widgetID + '_ImgWidthCtrl'); 
    _imgHeightCtrl = document.getElementById(widgetID + '_ImgHeightCtrl');
    _imgFitTypeCtrl = document.getElementById(widgetID + '_ImgFitTypeCtr');
    /////////////////////////////////////////////////////////////////////////
    DragImg.init(_imgCtrl, _imgContainer, '');
    loadSize();
}

function loadSize()
{
    if(_imgContainer.style.width != 0 || _imgContainer.style.height != 0)
    {
        loadImages();
    }
    else
    {
        timerID = setTimeout("loadSize();",10) 
    }
}

function loadImages()
{
    _imageSource = _imgStubName;
    _imageSource += "&ZOOMFACTOR=" + _imgZoomFactorCtrl.value;
    _imageSource += "&ROTFACTOR=" + _imgRotFactorCtrl.value;
    _imageSource += "&FITTYPE=" + _imgFitTypeCtrl.value;
    _imageSource += "&WIDTH=" + _imgContainer.style.width.replace("px", "");
    _imageSource += "&HEIGHT=" + _imgContainer.style.height.replace("px", "");

    arrImages = new Array(_imageSource);
    if (document.images) 
    {
         preImages = new Array();
         currCount = 0;
         loaded = new Array();//,i,covered,timerID
         
     for (i = 0; i < arrImages.length; i++) { 
          preImages[i] = new Image();
          preImages[i].src = arrImages[i];
     }
    /////////////////////////////////////////////////////////////////////////
     for (i = 0; i < preImages.length; i++)
     { 
          loaded[i] = false;
          var d = new Date();
          _timeBegin = d.getTime();
     }
     /////////////////////////////////////////////////////////////////////////
     checkLoad();
  }
}
function checkLoad() 
{
     if (currCount == preImages.length)
     { 
        if(_imgCtrl == null || _imgCtrl == undefined)
            return;
        _imgCtrl.src = _imageSource;
        return;
     }
     //////////////////////////////////////////////////////////////////////
     for (i = 0; i <= preImages.length; i++) 
     {
          if (loaded[i] == false && preImages[i].complete)
           {
               loaded[i] = true;
               currCount++;
               var d1 = new Date();
               _timeEnd = d1.getTime();
               if(_timeLabel != null)
                _timeLabel.innerHTML = (_timeEnd - _timeBegin)/1000;
          }
     }
     timerID = setTimeout("checkLoad();",10) 
}

function fitToBestImage()
{
    _imgCtrl.src = "App_Images/ImageView/loading.gif";
    _imgRotFactorCtrl.value = 1;
    _imgZoomFactorCtrl.value = 1;
    _imgFitTypeCtrl.value = "Page";
}

function fitToHeightImage()
{
    _imgCtrl.src = "App_Images/ImageView/loading.gif";
    _imgRotFactorCtrl.value = 1;
    _imgZoomFactorCtrl.value = 1;
    _imgFitTypeCtrl.value = "Height";
     loadImages();
}

function fitToWidthImage()
{
    _imgCtrl.src = "App_Images/ImageView/loading.gif";
    _imgRotFactorCtrl.value = 1;
    _imgZoomFactorCtrl.value = 1;
    _imgFitTypeCtrl.value = "Width";
     loadImages();
}

function rotate270Image()
{
    _imgCtrl.src = "App_Images/ImageView/loading.gif";
    var lastRotate = parseFloat(_imgRotFactorCtrl.value);
    _imgRotFactorCtrl.value = lastRotate - 1;
    if(parseFloat(_imgRotFactorCtrl.value) <= 0)
        _imgRotFactorCtrl.value = '4';
    loadImages();
}

function rotate90Image()
{
    _imgCtrl.src = "App_Images/ImageView/loading.gif";
    var lastRotate = parseFloat(_imgRotFactorCtrl.value);
    _imgRotFactorCtrl.value = lastRotate + 1;
    if(parseFloat(_imgRotFactorCtrl.value) > 4)
        _imgRotFactorCtrl.value = '1';
    loadImages();
}

function zoomOutImage()
{
    _imgCtrl.src = "App_Images/ImageView/loading.gif";
    var lastZoom = parseFloat(_imgZoomFactorCtrl.value);
    _imgZoomFactorCtrl.value = lastZoom / 1.2;
    if(parseFloat(_imgZoomFactorCtrl.value) < 0.3)
        _imgZoomFactorCtrl.value = lastZoom;
    loadImages();
}

function zoomInImage()
{
    _imgCtrl.src = "App_Images/ImageView/loading.gif";
    var lastZoom = parseFloat(_imgZoomFactorCtrl.value);
    _imgZoomFactorCtrl.value = lastZoom * 1.2;
    if(parseFloat(_imgZoomFactorCtrl.value) > 2)
        _imgZoomFactorCtrl.value = lastZoom;
    loadImages();
}

//////////////////////////////////////////////////////////////////////////
var DragImg = {
	obj : null,
	objRoot : null,
    blocker : null,
    
	init : function(o, oRoot, blockid)
	{
	  if (o != null && oRoot != null)
	  {
		o.onmousedown = DragImg.start;
		o.onDragImgStart = new Function();
		o.onDragImgEnd	= new Function();
		o.onDragImg	 = new Function();
		objRoot = oRoot;
	    if(blockid != null)
	    {
	       if(document.getElementById(blockid))
	       {
	          blocker = document.getElementById(blockid);
		      blocker.onmousedown =  DragImg.start;
		   }
        }
	  }
	},

	start : function(e)
	{
		
		var o = DragImg.obj = this;
		
		if (o == null) return false;
	  
		e = DragImg.fixE(e);
		o.lastMouseX	= e.clientX;
		o.lastMouseY	= e.clientY;
		o.onmousemove	= DragImg.drag;
		o.onmouseup		= DragImg.end;
		
		//window.event.cancelBubble = true;
		
		return false;
	},

	drag : function(e)
	{
		e = DragImg.fixE(e);
		var o = DragImg.obj;
		var ey	= e.clientY;
		var ex	= e.clientX;
        objRoot.scrollTop += DragImg.obj.lastMouseY - ey;
        objRoot.scrollLeft += DragImg.obj.lastMouseX - ex;
		
		
		DragImg.obj.lastMouseX	= ex;
		DragImg.obj.lastMouseY	= ey;

		return false; 
	},

	end : function()
	{
		var o = DragImg.obj;
		o.onmousemove = null;
		o.onmouseup   = null;
		o = null;
	},

	fixE : function(e)
	{
		if (typeof e == 'undefined') e = window.event;
		if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;
		if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;
		return e;
	}
};
