var Popup = {};
Popup.ID = 'ctPopup';
Popup.ImageCache = {};
Popup.LoadingImage = null;

Popup.GetDiv = function(){
	var div = document.getElementById( Popup.ID );
	if( div == null ){
		div = document.createElement( 'div' );
		div.id = Popup.ID;
		div.style.backgroundColor = '#000000';
		div.style.display = 'none';
		div.style.padding = '6px';
		div.style.position = 'absolute';

		var img = document.createElement( 'img' );
		div.appendChild( img );

		document.body.appendChild( div );
	}
	return div;
}

Popup.OnMouseOver = function( e, loaded ){
	var div = Popup.GetDiv();
	var img = div.firstChild;

	var imgRef = e.Node;
	var imgObj = Popup.ImageCache[ imgRef.src ];
	if( imgObj == null ){
		var imgRef = e.Target;

		if( loaded ){
			Popup.ImageCache[ imgRef.src ] = Popup.LoadingImage;
			setTimeout(
				function(){ Popup.OnMouseOver( e, false ); },
				0
			);
		}else{
			Popup.LoadingImage = new Image();
			Popup.LoadingImage.onload = function(){
				Popup.OnMouseOver( e, true );
			};
			Popup.LoadingImage.src = imgRef.src;
		}
		return;
	}

	if( img.src != imgObj.src ){
		var rects = getWindowRects();

		var showHeight = imgObj.height;
		var showWidth = imgObj.width;
		if( showHeight > rects.Height ){
			showHeight = parseInt( 0.75 * rects.Height );
			var scale = showHeight / imgObj.height;
			showWidth = parseInt( scale * showWidth );
		}

		//no use showing a smaller image
		if( showHeight < imgRef.height )
			return;

		var diff = rects.Height - showHeight;
		var topAdd = parseInt( diff / 2 );
		

		img.src = imgObj.src;
		img.height = showHeight;
		img.width = showWidth;
	
		div.style.height = showHeight +'px';
		div.style.width = showWidth +'px';
		div.style.display = 'block';
		div.style.top = ( rects.Top + topAdd ) +'px';

		Popup.OnMouseMove( e );
	}
}

Popup.OnMouseMove = function( e ){
	var div = Popup.GetDiv();
	div.style.left = ( e.Point.X + 50 ) +'px';
}

Popup.OnMouseOut = function( e ){
	var div = Popup.GetDiv();
	div.style.display = 'none';
	div.firstChild.src = '';
}

Popup.Scale = function( img ){
	var maxHeight = 250;
	var maxWidth = 250;

	var hScale = img.height / maxHeight;
	var wScale = img.width / maxWidth;
	if( hScale <= 1 && wScale <= 1 )
		return;

	var h = img.height;
	var w = img.width;
	if( hScale > wScale ){
		img.height = parseInt( h / hScale );
		img.width = parseInt( w / hScale );
	}else{
		img.height = parseInt( h / wScale );
		img.width = parseInt( w / wScale );
	}
}

Popup.Setup = function(){
	var body = document.getElementById( 'parts_content' );
	if( !body )
		return;

	var iList = [];
	var images = body.getElementsByTagName( 'img' );
	for( var i = 0; i < images.length; i++ ){
		if( images[i].className == 'content_image' )
			iList.push( images[i] );
	}

	if( !iList.length )
		return;

	for( var i = 0; i < iList.length; i++ ){
		HD.Event.OnMouseOver( iList[i], Popup.OnMouseOver );
		HD.Event.OnMouseMove( iList[i], Popup.OnMouseMove );
		HD.Event.OnMouseOut( iList[i], Popup.OnMouseOut );

		Popup.Scale( iList[i] );
	}
}
