var alertstr = '';

function startCatZoom () {
	
	$j("#div_large").css('display','block');
	//$j("#div_large").css('opacity',0);
	
	//alert($j("#large_img").width());
	//alert($j("#large_img").width() + "/" + getWidth($j("#large_img")));
	//alert($j("#large_img").height() + "/" + getHeight($j("#large_img")));
	
	if (getWidth($j("#large_img")) > 100) InitCatZoom();
	else {
		showLoading();
		$j("#large_img").load(function(){
			//alert('loaded');
			//alert($j("#large_img").width());
			InitCatZoom();					  
		});
	}
	$j("#div_large").css('display','none');
	//$j("#div_large").css('opacity',1);
}

function showLoading () {
	
	$j("#zoom_frame").mousemove(function(e){
		MoveLoading(e);
	});
	
}

function hideLoading () {
	$j("#catzloading").css('display','none');
}

function MoveLoading(e)
{
	
	$j("#catzloading").css('display','block');
	
	deltax = e.pageX - $j("#productImage").offset().left;
	deltay = e.pageY - $j("#productImage").offset().top;
	
	$j("#catzloading").css('left',deltax  );
	$j("#catzloading").css('top',deltay + 36 );	
}


function InitCatZoom() {
	
	//$j(window).load(function() {
							
	//alert('init catzoom');
	//alert($j("#large_img").width());

	NullState();
	
	$j("#zoom_frame").mousemove(function(e){
		ShowZoom(e);
	});

	$j("#zoom_frame").mouseout(function(e){
		NullState();
	});

	//});
}

function ShowZoom(e)
{
	
	hideLoading();
	$j("#div_large").css('display','block');
	
	deltax = e.pageX - $j("#productImage").offset().left;
	deltay = e.pageY - $j("#productImage").offset().top;

	//alert( deltax + "/" + deltay );

	var smallw = getWidth($j("#productImage"));
	var smallh = getHeight($j("#productImage"));
	var largew = getWidth($j("#large_img"));
	var largeh = getHeight($j("#large_img"));
	
	var divlargeheight = Math.round(getHeight($j("#catone")) - parseInt( $j("#div_large").css('top') ) - 18);

	$j("#div_large").css('height', divlargeheight);

	ratio = largew/smallw;
	
	//alert(smallw + "/" + largew + "/" + ratio);

	//ratio for zoom window
	wratiox = Math.round(largew/getWidth($j("#div_large")));
	wratioy = Math.round(largeh/getHeight($j("#div_large")));
	//dim of zoom frame
	zfw = Math.round(smallw/wratiox);
	zfh = Math.round(smallh/wratioy);

	$j("#zoom_frame").css('width',zfw);
	$j("#zoom_frame").css('height',zfh);
	$j("#zoom_frame").css('border', '1px solid #E5E1D3');
	$j("#zfimg").css('display','block');
	$j("#productImage").css('opacity',0.5);
	
	largeleft = Math.round(-ratio*(deltax-(zfw/2)));
	largetop = Math.round(-ratio*(deltay-(zfh/2)));
	
	//$j("#large_img").css('left',largeleft);
	//$j("#large_img").css('top',largetop);
	
	obj = document.getElementById('large_img');
//	obj.style.position = "absolute";
	obj.style.left = largeleft + "px";
	obj.style.top = largetop + "px";
	
	//alert(largeleft + "/" + largetop + "/" + deltax + "/" + $j("#large_img").css('left'));
	alertstr += "\n" + largeleft + "/" + largetop + "/" + deltax + "/" + obj.style.left + "/" + obj.style.top;

	$j("#zoom_frame").css('opacity', 1 );
	$j("#zoom_frame").css('left', deltax-(Math.round(zfw/2)) );
	$j("#zoom_frame").css('top', deltay-(Math.round(zfh/2)) );
	$j("#zfimg").css('left', -(deltax-(Math.round(zfw/2))+1) );
	$j("#zfimg").css('top', -(deltay-(Math.round(zfh/2))+1) );



}


function NullState()
{
	$j("#div_large").css('display','none');
	pw = $j("#productImage").width();
	if (!pw) pw = 306;
	ph = $j("#productImage").height();
	if (!ph) ph = 306;
	$j("#zoom_frame").css('width',pw);
	$j("#zoom_frame").css('height',ph);
	$j("#zoom_frame").css('border', 'none' );
	$j("#zoom_frame").css('left',0);
	$j("#zoom_frame").css('top',0);

	$j("#zfimg").css('left',0);
	$j("#zfimg").css('top',0);

	$j("#productImage").css('opacity',1);
	
	hideLoading();
	//if (alertstr.length > 0) alert(alertstr);
}

/*ф-ия для смены изображения
	@param
		srcf 	- прямая ссылка на большое изображение
		srcm 	- прямая ссылка на предпросмотровое изображение
	@return
		no
*/
function changeImage(srcf,srcm)
{
	$j('#productImage').attr('src',srcm);
	$j('#zfimg').attr('src',srcm);
	$j('#large_img').attr('src',srcf);

}//function changeImage(src)
