
Type.registerNamespace('SfImage');

SfImage.CachedImageStatus = function()
{
    Error = "Error";
    Complete = "Complete";
    Loading = "Loading";
}

SfImage.ImageCache = function(container)
{
	this.Container = container;
	this.CachedImages = new Array();
	this.NumItemsToCache = 1;
 	
	this.AddImage = function(url, shouldDelayLoad)
	{
		if (shouldDelayLoad == true)
		{
			// random time between 2 and 4 seconds
			var randomTime = Math.floor(2000*Math.random() +2000);
			setTimeout(this.Container + '.Internal_AddImage("' + url + '")', randomTime);
		}
		else
		{	
			this.Internal_AddImage(url);
		}
	}
		
	this.Internal_AddImage = function(url)
	{
		var length = this.CachedImages.length;
		var currentIndex = 0;
		if (length >= this.NumItemsToCache)
		{
			currentIndex = length-1;
		}
		else
		{
			currentIndex = length;
		}
		
		var cachedImage = new CachedImage(url, this.Container + ".CachedImages[" + currentIndex + "]");
		this.CachedImages[currentIndex] = cachedImage;
		cachedImage.Load();
	}
	
	this.FindInCache = function(url)
	{
		var i;
		for (i=0; i<this.CachedImages.length; ++i)
		{
			if (this.CachedImages[i].Source == url)
			{
				return this.CachedImages[i]; 
			}
		}
		return null;
	}
	
	function CachedImage(source, container)
	{
		this.Source = source;
		this.Container = container;
		this.Status = SfImage.CachedImageStatus.Loading;
		
		this.Load = function()
		{
			this.Img = new Image();
			this.Img.onerror = new Function("", this.Container + ".OnError()");
			this.Img.onload = new Function("", this.Container + ".OnLoad()");
			this.Img.src = this.Source;
		}
		
		this.OnError = function()
		{
			this.Status = SfImage.CachedImageStatus.Error;
		}	
		
		this.OnLoad = function()
		{
			this.Status = SfImage.CachedImageStatus.Complete;
		}
		
		this.toString = function()
		{
			var retVal =
				"Source: " + this.Source + 
				", Status: " + this.Status
			return retVal;
		}
	}
}

Type.registerNamespace('SfImage.Popup');

SfImage.Popup.Position = function()
{
	Top = 0;
	Right = 1;
	Left = 2;
	Bottom = 3;
}

SfImage.Popup.PositionCalculator = function(containerDimension, boxDimension)
{
	this._separation = 4;// is the gap
	this._paddingLeft = 5;//padding shortens the container
	this._paddingRight = 20;
	this._paddingTop = 0;
	this._containerDimension = containerDimension;
	this._boxDimension = boxDimension;
}

SfImage.Popup.PositionCalculator.prototype =
{
	_CreateParams : function(location)
	{
		var params = new Object();
		params.t = location.Top - this._paddingTop - this._separation;
		params.l = location.Left - this._paddingLeft - this._separation;
		params.w = location.Width + 2 * this._separation;
		params.h = location.Height + 2 * this._separation;
		
		params.CW = this._containerDimension.Width - (this._paddingLeft + this._paddingRight);
		params.CH = this._containerDimension.Height - this._paddingTop;
		
		params.W = this._boxDimension.Width;
		params.H = this._boxDimension.Height;
		
		return params;
	},
	
	GetPopupPosition : function(location)
	{
		var params = this._CreateParams(location);
		var calc = new SfImage.Popup.RawPopupCalculator(params);
		var pos = calc.GetPosition();
		pos.Left = pos.Left + this._paddingLeft;
		pos.Top = pos.Top + this._paddingTop;
		return pos;
    }
}

SfImage.Popup.RawPopupCalculator = function(params)
{
	this._params = params;
	// right	
	//	popup.style.top = t + h/2 - H/2;
	//	popup.style.left = l + w;	

	// left
	//	popup.style.top = t + h/2 - H/2;
	//	popup.style.left = l - W;
		
	// top	
	//	popup.style.top = t - H;
	//	popup.style.left = l + w/2 - W/2;
		
	// bottom
	//	popup.style.top = t + h;
	//	popup.style.left = l + w/2 - W/2;
}

SfImage.Popup.RawPopupCalculator.prototype =
{
	GetPosition : function()
	{
		var position = new Object();
		if (this._CanPlaceOnTop() == true)
		{
			//position.Top = this._params.t - this._params.H;
			//position.Left = this._CalculateLeftOffset();
			//position.Where = SfImage.Popup.Position.Top;
			position.Top = this._params.t - this._params.H + 22;
			position.Left = this._params.l + this._params.w;
			position.Where = SfImage.Popup.Position.Right;
			return position;
		}
		if (this._CanPlaceOnRight() == true)
		{
			position.Top = this._CalculateTopOffset();
			position.Left = this._params.l + this._params.w;
			position.Where = SfImage.Popup.Position.Right;
			return position;
		}
		if (this._CanPlaceOnLeft() == true)
		{
			position.Top = this._CalculateTopOffset();
			position.Left = this._params.l - this._params.W;
			position.Where = SfImage.Popup.Position.Left;
			return position;
		}
		
		position.Top = this._params.t + this._params.h;
		position.Left = this._CalculateLeftOffset();
		position.Where = SfImage.Popup.Position.Bottom;
		return position;
	},
	
	_CanPlaceOnTop : function()
	{
		return (this._params.t - this._params.H >= 0);
	},
	
	_CanPlaceOnRight : function()
	{
		return (this._params.CW >= this._params.l + this._params.w + this._params.W);
	},
	
	_CanPlaceOnLeft : function()
	{
		return (this._params.l - this._params.W >= 0);
	},
	
	_CalculateLeftOffset : function()
	{
		var intendedLeft = this._params.l + this._params.w/2 - this._params.W/2;
		if (intendedLeft <=0)
		{
			return 0;
		}
		var resultingRight = intendedLeft + this._params.W;
		if (resultingRight <= this._params.CW)
		{
			return intendedLeft;
		}
		else
		{
			return this._params.CW - this._params.W;
		}
	},
	
	_CalculateTopOffset : function()
	{
		var intendedTop = this._params.t + this._params.h/2 - this._params.H/2;
		if (intendedTop >=0)
		{
			return intendedTop;
		}
		else
		{
			return 0;
		}
	}
}

SfImage.Popup.PopupHelper = function(input)
{
	this._input = input;
	
	this._zoomElement = null;
	this._imageElement = null;
	this._positionCalculator = null;
	this._borderWidth = 1;
	this._padding = 3;
	this._isInitialized = false;
	this._CreateZoomElement();
	
}
SfImage.Popup.PopupHelper.prototype = 
{
	GetZoomElement : function()
	{
		return this._zoomElement;
	},
	
	_CreateZoomElement : function()
	{
		this._zoomElement = $(document.createElement('div'));
		this._zoomElement.className = 'imagePopupZoomElement';
		this._zoomElement.setStyle({'z-index':'2','position':'absolute','top':'0px','left':'0px','visibility':'hidden','border-width':''+this._borderWidth + 'px','padding':'' + this._padding + 'px'});
		this._imageElement = document.createElement('img');
		this._imageElement = $(this._imageElement);
		this._zoomElement.appendChild(this._imageElement);
		this._imageElement.setStyle({'width':this._input.ZoomWidth + 'px','height':this._input.ZoomHeight+'px','left':'0px','display':'block'});
	},
	
	_Initialize : function()
	{
		var containerDimension = this._input.ContainingElement.getDimensions();

		var boxDimension = new Object();
		boxDimension.Width = this._input.ZoomWidth + 2 * this._borderWidth + 2 * this._padding;
		boxDimension.Height = this._input.ZoomHeight + 2 * this._borderWidth + 2 * this._padding;

		this._positionCalculator = new SfImage.Popup.PositionCalculator({Width:containerDimension.width,Height:containerDimension.height}, boxDimension);
		this._isInitialized = true;
	},

	ShowSlideNumber : function(slideNumber, anchorInfo)
	{
		if (this._isInitialized == false)
		{
			this._Initialize();
		}
	
		this._imageElement.setAttribute("src", mPlayer.GetImageLocationUsingWidthAndHeight(slideNumber, this._input.ZoomWidth, this._input.ZoomHeight));
		
		var scrollOffsets = {Top:this._input.ContainingElement.scrollTop,Left:this._input.ContainingElement.scrollLeft};

		anchorInfo.Top = anchorInfo.Top - scrollOffsets.Top;
		anchorInfo.Left = anchorInfo.Left - scrollOffsets.Left;

		var displayPos = this._positionCalculator.GetPopupPosition(anchorInfo);
		displayPos.Top = displayPos.Top + scrollOffsets.Top;
		displayPos.Left = displayPos.Left + scrollOffsets.Left;
		
		var initial;
		if (displayPos.Where == SfImage.Popup.Position.Top)
		{
			initial = {Top:displayPos.Top, Left:displayPos.Left, Width:this._input.ZoomWidth, Height:this._input.ZoomHeight};
		}
		else if (displayPos.Where == SfImage.Popup.Position.Right)
		{
			initial = {Top:displayPos.Top, Left:displayPos.Left, Width:this._input.ZoomWidth, Height:this._input.ZoomHeight};
		}
		else if (displayPos.Where == SfImage.Popup.Position.Left)
		{
			initial = {Top:displayPos.Top, Left:displayPos.Left+this._input.ZoomWidth-30, Width:this._input.ZoomWidth, Height:this._input.ZoomHeight};
		}
		else
		{
			initial = {Top:displayPos.Top, Left:displayPos.Left, Width:this._input.ZoomWidth, Height:this._input.ZoomHeight};
		}

		this.SetBounds({Top:initial.Top + 'px', 
			Left:initial.Left + 'px',
			Width:initial.Width + 'px',
			Height:initial.Height + 'px'
			});
		this._zoomElement.style.visibility = 'visible';
		this._zoomElement.style.zIndex = '2';
		
	},
	

	SetBounds: function(bounds)
	{
		this._zoomElement.style.top = bounds.Top;
		this._zoomElement.style.left = bounds.Left;
		this._imageElement.style.width = bounds.Width;
		this._imageElement.style.height = bounds.Height;
		this._zoomElement.style.width = bounds.Width;
	},

	Clear : function(e)
	{
		this.SetBounds({Top:0 + 'px', 
			Left:0 + 'px',
			Width:0 + 'px',
			Height:0 + 'px'
			});
		this._zoomElement.style.visibility = 'hidden';
        this._imageElement.setAttribute('src', LayoutOptions.ThemeImageBase + '/1x1.gif');
	}
}

SfImage.ImageUpdater = function(container, imageElement, extraWidth, extraHeight, minWidth, minHeight)
{	
	this.Container = container;
	this.ImageElement = imageElement;

	this.ExtraWidth = extraWidth;
	this.ExtraHeight = extraHeight;
	
	this.MinWidth = minWidth;
	this.MinHeight = minHeight;
	
	this.PreviousImageWidth = null;
	this.PreviousImageHeight = null;
		
	this.ChangeImage = function(imageSrc)
	{
		this.ImageElement.onload = new Function("", this.Container + ".ChangeSizes();");
		this.ImageElement.src = imageSrc;
	}

	this.ChangeSizes = function()
	{
		var imageDimension = this.GetImageDimension();
		if (!this.ValidateDimension(imageDimension))
		{
			return;
		}
		
		if (this.IsChangeNecessary(imageDimension.Width, imageDimension.Height))
		{    		
    		this.PreviousImageWidth = imageDimension.Width;
    		this.PreviousImageHeight = imageDimension.Height;		

    		this.ChangeWindowSize(imageDimension.Width, imageDimension.Height);
		}				
	}
	
	this.ChangeWindowSize = function(imageWidth, imageHeight)
	{
		var dimension = this.GetOptimumWindowDimension(imageWidth, imageHeight);
		if (dimension == null)
		{
			return;
		}
		
        var currentSize = document.viewport.getDimensions();                              
        window.resizeBy((dimension.Width - currentSize.width),(dimension.Height - currentSize.height));
		
	}
	
	this.GetImageDimension = function()
	{
		var ret = new Object();
		if (!this.ImageElement)
		{
			return null;
		}
		
		ret.Width = this.ImageElement.width;
		ret.Height = this.ImageElement.height;

		return ret;
	}
	
	this.ValidateDimension = function(dimension)
	{
		if (dimension == null)
		{
			return false;
		}
		
		if (!dimension.Width)
		{
			return false;
		}
		if (dimension.Width < 100)
		{
			return false;
		}
		
		if (!dimension.Height)
		{
			return false;
		}
		if (dimension.Height < 100)
		{
			return false;
		}
		
		return true;
	}
	
	this.IsChangeNecessary = function(imageWidth, imageHeight)
	{
		if (this.PreviousImageWidth == null || this.PreviousImageHeight == null)
		{
			return true;
		}
		
		if (this.PreviousImageWidth != imageWidth || this.PreviousImageHeight != imageHeight)
		{
			return true;
		}
		
		return false;	
	}
	
	this.GetOptimumWindowDimension = function(imageWidth, imageHeight)
	{		
		var ret = new Object();

		var availWidth = screen.availWidth;
		var availHeight = screen.availHeight;

		var windowWidth = imageWidth + this.ExtraWidth; 
		if (windowWidth > availWidth)
		{
			ret.Width = availWidth;
		}
		else if(windowWidth < this.MinWidth)
		{
		    ret.Width = this.MinWidth;
		}
		else
		{
			ret.Width = windowWidth;
		}
		
		var windowHeight = imageHeight + this.ExtraHeight;
		if (windowHeight > availHeight)
		{
			ret.Height = availHeight;
		}
		else if(windowHeight < this.MinHeight)
		{
		    ret.Height = this.MinHeight;
		}
		else
		{
			ret.Height = windowHeight;
		}
		
		return ret;				
	}
			
	this.GetMainPlayer = function()
	{
		if (opener.closed)
		{
			return null;
		}
		
		if (!opener.mPlayer)
		{
			return null;
		}
		return opener.mPlayer;
	}
}
