function initJcrop(iw, ih ,imgId) 
{	
	$('#cropbox'+imgId).Jcrop({
		onSelect: showCoords,
		onChange: showCoords,
		aspectRatio: 1,
		trueSize: [iw,ih]
	});			
	// event handlers, as per the Jcrop invocation above
	function showCoords(c)
	{			
		var pre_width = iw;
		var pre_height = ih;				
		var rx = 100 / c.w;
		var ry = 100 / c.h;
		$('#x'+imgId).val(c.x);
		$('#y'+imgId).val(c.y);				
		$('#w'+imgId).val(c.w);
		$('#h'+imgId).val(c.h);
		if(Math.round(rx *  pre_width) < 100 || Math.round(ry * pre_height) < 100)
		{					
			pre_width = '100px';
			pre_height = '100px';
			marginLeft= '0px',
			marginTop= '0px'
		}
		else
		{					
			pre_width = Math.round(rx *  pre_width) + 'px';
			pre_height = Math.round(ry * pre_height) + 'px';
			marginLeft= Math.round(rx * c.x) + 'px',
			marginTop= Math.round(ry * c.y) + 'px'
		}
		if(c.h > 0){
			$('#preview'+imgId).css({
				width: pre_width,
				height: pre_height,
				marginLeft: '-' + marginLeft,
				marginTop: '-' + marginTop
			});
		}
	}
};
function saveCropImage()
{
	var param	='';
	var imgCount=0;
	j=1;
	for(i=1;i<=$('#imgCount').val();i++)
	{
		var x 		= $('#x'+i).val();
		var y 		= $('#y'+i).val();			
		var w 		= $('#w'+i).val();
		var h 		= $('#h'+i).val();
		var imgName = $('#imageName'+i).val();
		
		if(x!='' && y!=''  && h!='' && w!='0' && h!='0')
		{
			param = param + '&x'+j+'=' + x + '&y'+j+'=' + y  + '&w'+j+'=' + w + '&h'+j+'=' + h + '&imgName'+j+'=' + imgName;
			imgCount++;	
			j++;				
		}
	}
	param = 'imgCount=' + imgCount + param;
	var completeUrl = location.href;
	var splitUrl = completeUrl.split('/');
	var totalUrl = splitUrl[0] + '//' + splitUrl[2];

	//retrieve the points using Ajax
	if(document.getElementById('pId') && $('#pId').val()!='')		
		var url = totalUrl+'/product/jcropSave/pId/'+$('#pId').val();
	else
		var url = totalUrl+'/product/jcropSave';
	
	var queryString = param;
	$('#crop_status').val('Complete');
	$.ajax({
		'type': 'POST',
		'data': queryString,
		'url': url,
		'success': function(){
			tb_remove();
			if(document.getElementById('pId') && $('#pId').val()!='')			
				location.href = totalUrl+'/seller/updateitem/pId/'+$('#pId').val();
			else
				document.additem.submit();
		}				
	});	
}
function changeCropDiv(pre_width, pre_height, divId)
{
	$('#cropId_'+divId).css('display','block');
	$('#imgDiv_'+$('#hidImgDiv').val()).attr('class','sub_menu');
	$('#imgDiv_'+divId).attr('class','sub_menu_sel');
	initJcrop(pre_width, pre_height, divId);
	$('#cropId_'+$('#hidImgDiv').val()).css('display','none');	
	$('#hidImgDiv').val(divId);
	return false;
	
}