//----------------------------------------------------------------------------------------------------------------------
/*jslint browser:true white:false devel:true onevar:true nomen:false forin:true undef:true */
/*global jQuery,window,BlueMoonIT_ImageResizer */
//----------------------------------------------------------------------------------------------------------------------

function BlueMoonIT_ImageResizer() 
{
	this.images = [];
	this.current = null;

	this.imgNode = null;
}

BlueMoonIT_ImageResizer.prototype.addImage = function (src, width, height) 
{
	var index = this.images.length;
	this.images[index] = new Image(width, height);
	this.images[index].src = src;
};

BlueMoonIT_ImageResizer.prototype.previous = function () 
{
	if( (null === this.current) || (0 === this.current) ) {
		return this.select(this.images.length - 1);
	}

	return this.select(this.current - 1);
};

BlueMoonIT_ImageResizer.prototype.next = function () 
{
	if( (null === this.current) || (this.current + 1 >= this.images.length) ) {
		return this.select(0);
	}

	return this.select(this.current + 1);
};

BlueMoonIT_ImageResizer.prototype.select = function (idx) 
{
	if( (idx < 0) || (this.images.length <= idx) ) {
		return false;
	}

	this.current = idx;

	this.imgNode.src = this.images[this.current].src;
	
	return this.resize();
};

BlueMoonIT_ImageResizer.prototype.resize = function ()
{
	var newWidth, newHeight;

	if (null === this.current)
	{
		return false;
	}

	newWidth = jQuery(document).width();
	newHeight = jQuery(window).height() - 340;

	return this.correctRatio(newWidth, newHeight);
};

BlueMoonIT_ImageResizer.prototype.correctRatio = function (containerWidth, containerHeight)
{
	var imgW, imgH, R, newImgW, newImgH, marginLeft, marginTop, containerR;

	imgW = this.images[this.current].width;
	imgH = this.images[this.current].height;

	if( (imgW < containerWidth) || (imgH < containerHeight) ) {
		marginLeft = ((containerWidth - imgW) / 2);
		marginTop = ((containerHeight - imgH) / 2);


		jQuery(this.imgNode).css({
			width: imgW + 'px',
			height: imgH + 'px',
			marginLeft: marginLeft + 'px',
			marginTop: marginTop + 'px'
		});

		jQuery('#leftbar').css({
			left: (marginLeft - 365) + 'px',
			top: marginTop + 'px',
			height: imgH + 'px'
		});

		jQuery('#centerbar').css({
			width: imgW + 'px',
			height: imgH + 'px',
			left: marginLeft + 'px',
			top: marginTop + 'px'
		});

		jQuery('#rightbar').css({
			left: (marginLeft + imgW) + 'px',
			top: marginTop + 'px',
			height: imgH + 'px'
		});

		return;
	}

	R = imgW / imgH;

	containerR = containerWidth / containerHeight;

	// Assume Ratio is correct
	newImgW = containerWidth;
	newImgH = containerHeight;
	marginLeft = 0;
	marginTop = 0;

	// Check if the ratio is correct, if not, correct it
	// Container is to high
	if ( R > containerR )
	{
		// make the image wider
		newImgW = R * newImgH;

		// move the image left
		marginLeft = (newImgW - containerWidth) / 2;

	}
	// Container is to low
	else if ( R < containerR )
	{
		// make the image higher
		newImgH = newImgW / R;

		// move the image up
		marginTop = (newImgH - containerHeight) / 2;
	}

	jQuery(this.imgNode).css({
		width: newImgW + 'px',
		height: newImgH + 'px',
		marginLeft: -marginLeft + 'px',
		marginTop: -marginTop + 'px'
	});

	return true;
};

