/*
	NOTE:
	This script will be turned into a jQuery plugin at some point.
	Watch http://www.hybridchill.com/projects/jquery.html for details.
*/

var Zooming = false;

var Options = { BorderWidth: '0.5em' };
var Counter     = Options.InitCounter || 0;
var ImgIdPrefix = Options.ImgIdPrefix || 'simple-zoom-img-'

$j(document).ready(initSimpleZoom);

function initSimpleZoom()
{
	$j('body').append('<div id="simple-zoom"></div>');
	$j('#simple-zoom')
		.append('<div class="container" style="position:absolute;left:0;top:0;"></div>')
		.append('<div class="images" style="visibility:hidden;overflow:hidden;width:0;height:0;"></div>')
		;

	$j('#simple-zoom .container').click( unzoom );
	$j('a.photo,a.image').each( makeZoomable );
}


function makeZoomable()
{
	Counter++;
	var Src = jQuery(this).attr('href');
	var Id  = ImgIdPrefix+Counter;
	var Alt = '';
	if ( jQuery(this).attr('data-alt') != 'undefined' )      Alt = jQuery(this).attr('data-alt')
	else if ( jQuery(this).attr('data-alt') != 'undefined' ) Alt = jQuery(this).attr('alt');

	jQuery('#simple-zoom .images')
		.append('<img id="'+Id+'" src="'+Src+'" alt="'+Alt+'"/>');

	jQuery(this)
		.attr( 'href' , '#'+Id )
		.click( zoomImage )
		;
}

function zoomImage(e)
{
	if (Zooming) return false;
	Zooming = true;

	if( $j('#simple-zoom .container').is(':visible') )
	{
		$j('#simple-zoom .container').hide().empty();
	}

	var Img = jQuery( jQuery(this).attr('href') );

	var WindowSize = getWindowSize();

	var Zoom =
		{ Width  : Options.Width || Img.width()
		, Height : Options.Height || Img.height()
		};

	var CurPos =
		{ Left : e.pageX
		, Top  : e.pageY
		};

	/*
	var StartPos1 =
		{ Left   : WindowSize.Width/2 + WindowSize.Left
		, Top    : WindowSize.Height/2 + WindowSize.Top
		, Width  : 0
		, Height : 0
		};

	var StartPos2 =
		{ Left   : CurPos.Left
		, Top    : CurPos.Top
		, Width  : jQuery(this).width()
		, Height : jQuery(this).height()
		};
	*/

	var StartPos =
		{ Left   : CurPos.Left
		, Top    : CurPos.Top
		, Width  : 0
		, Height : 0
		};

	var EndPos =
		{ Left   : WindowSize.Width/2 - Zoom.Width/2 + WindowSize.Left
		, Top    : Math.max( WindowSize.Height/2 - Zoom.Height/2 + WindowSize.Top , 0 )
		, Width  : Zoom.Width
		, Height : Zoom.Height
		};

/*
	console.log
	(
		{ WindowSz : WindowSize
		, Zoom     : Zoom
		, CurPos   : CurPos
		, StartPos : StartPos
		, EndPos   : EndPos
		}
	);
*/

	$j('#simple-zoom .container')
		.css
		(
			{ left   : CurPos.Left + 'px'
			, top    : CurPos.Top + 'px'
			, width  : StartPos.Width + 'px'
			, height : StartPos.Height + 'px'
			, borderWidth: 0
			}
		)
		.append( Img.clone() )
		.show()
		.animate
		(
			{ left   : EndPos.Left + 'px'
			, top    : EndPos.Top + 'px'
			, width  : EndPos.Width + 'px'
			, height : EndPos.Height + 'px'
			, borderWidth: Options.BorderWidth
			}
		,
			300
		,
			function()
			{
				Zooming = false;
			}
		)

	return false;
}


function getWindowSize()
{
	var Result =
		{ Width  : window.innerWidth || (window.document.documentElement.clientWidth || window.document.body.clientWidth)
		, Height : window.innerHeight || (window.document.documentElement.clientHeight || window.document.body.clientHeight)
		, Left   : window.pageXOffset || (window.document.documentElement.scrollLeft || window.document.body.scrollLeft)
		, Top    : window.pageYOffset || (window.document.documentElement.scrollTop || window.document.body.scrollTop)
		};

	return Result;
}


function unzoom ()
{
	if ( Zooming ) return false;
	Zooming = true;

	var WindowSize = getWindowSize();


	$j('#simple-zoom .container')
		.animate
		(
			{ left   : WindowSize.Width/2 + WindowSize.Left
			, top    : WindowSize.Height/2 + WindowSize.Top
			, width  : '1px'
			, height : '1px'
			, borderWidth: 0
			}
		,
			100
		,
			function()
			{
				$j('#simple-zoom .container').hide().empty();
				Zooming = false;
			}
		)
		;
}
