function initBlocks()
{
	$('div.boatblocks/.block').click(function()
		{
			$("#frmChooseBlock/#inpBlockID").val($(this).attr('rel'));
			document.frmChooseBlock.submit();
		}
	)
	.hover(
		function()
		{
			$(this).children('span').addClass('block-hover');
		},
		function()
		{
			$(this).children('span').removeClass('block-hover');
		});
		
		
	$("#chooseslottable")
		.click( function(event)
		{
			var slot = $(event.target);
			if (slot.attr("class") != 'slot_taken')
			{
				$("#inpBlockX").val(slot.attr('rel'));
				$("#inpBlockY").val(slot.parent().attr('rel'));
				document.frmMain.submit();
			}
			else slot.get(0).blur();
		});
		
	helper = $('<div class="helper"></div>')
			// hide it at first
			.hide()
			// add to document
			.appendTo('body');

	initCropper();

}

/*

 	Hover over image script adapted from jquery tooltip:
 	http://bassistance.de/jquery-plugins/jquery-plugin-tooltip/
 	
 	(not very expertly, but it works) 
 	
*/

function slthvr(event, photoid)
{
	event = event || window.event; // IE sucks!
	if ($.browser.msie) 
	{
		event.target = event.srcElement;
		event.pageX = event.clientX + document.body.scrollLeft;
		event.pageY = event.clientY + document.body.scrollTop;
	}
	
	event.target.to = setTimeout( "showHelper("+photoid+","+event.pageX+","+event.pageY+")", 350);
}

var helper;

function showHelper(photoid, posX, posY)
{
	//alert('id: '+photoid+' x: '+posX+' y: '+posY);
	
	$.get(rootpath+"getphoto.php",
	  { id: photoid },
	  function(data){
	    
	    if (data!='')
	    {
	  		helper.remove();
	  		helper = $(data).hide().appendTo('body');
/*
		    switch (data)
		    {
		    	case 'RESERVED':
			    	//helper.addClass('reserved-tooltip');
			    	//helper.children('span.reservednote').empty().append('This slot has not been released yet - please try another');
			    	helper.empty().append(data);
			    	break

		    	case 'LOCKED':
			    	//helper.addClass('reserved-tooltip');
			    	//helper.children('span.reservednote').empty().append('This slot is locked while someone tries to put their face in - if you really want it, try back in 30 minutes.');
			    	helper.empty().append(data);
			    	break
	
			  	default:
			  		

			    	//helper.addClass('photohover').children('img').attr("src",rootpath+'admin/'+data);
				}
				*/
				helper.show();

				var left = posX + 5;
				var top = posY + 5;
				
				//alert(left);				
				
				helper.css({
					left: left + 'px',
					top: top + 'px'
				});	
				
				update();
				$('body').bind('mousemove', update);
			}
	  }
	);
	
}

function sltoff(event, photoid)
{

	if ($.browser.msie) event.target = event.srcElement;

	//alert(event.target.to);
	clearTimeout(event.target.to);
	if(helper!=null) helper.hide().removeClass('photohover').removeClass('reserved-tooltip').children('img').attr("src","");
}


function update(event)	{
		
		
		// stop updating when tracking is disabled and the tooltip is visible
		if ( !helper.is(":visible")) {
			$('body').unbind('mousemove', update)
		}

		var left = helper[0].offsetLeft;
		var top = helper[0].offsetTop;
		if(event) {
			// position the helper 15 pixel to bottom right, starting from mouse position
			left = event.pageX + 5;
			top = event.pageY + 5;
			helper.css({
				left: left + 'px',
				top: top + 'px'
			});
		}
		var v = viewport(),
			h = helper[0];
		// check horizontal position
		if(v.x + v.cx < h.offsetLeft + h.offsetWidth) {
			left -= h.offsetWidth + 20 + 5;
			helper.css({left: left + 'px'});
		}
		// check vertical position
		if(v.y + v.cy < h.offsetTop + h.offsetHeight) {
			top -= h.offsetHeight + 20 + 0;
			helper.css({top: top + 'px'});
		}
	}
	
	function viewport() {
		return {
			x: $(window).scrollLeft(),
			y: $(window).scrollTop(),
			cx: $(window).width(),
			cy: $(window).height()
		};
	}
	
	
	function initCropper()
	{
		
		if ($('#resize_container').length > 0)
		{

			$('#resize_container').css('width',image_width+"px").css('height',image_height+"px").css('background-image',"url("+image_path+")");
			$('#resize_containerwrapper').css('width',image_width+"px").css('height',image_height+"px");
			$('#resize_spacer').css('height',image_height+"px").css('width',image_width+"px");
			
			// size and position the resize me
			// if ratio is less than block_ratio, the image is too fat, more it's too tall
			
			if (ratio < block_ratio)
			{
				var boxheight = image_height-20;
				var boxwidth = boxheight/block_ratio;
			}
			else
			{
				var boxwidth = image_width-20;
				var boxheight = boxwidth * block_ratio;
			}
			
			// find out how far left and down the resizeer should go
			var resizerBGLeft = (Math.floor((image_width-boxwidth) / 2));
			var resizerBGTop = (Math.floor((image_height-boxheight) / 2));

			var resizerLeft = resizerBGLeft + parseInt($('#resize_container').css('left'));
			var resizerTop = resizerBGTop + parseInt($('#resize_container').css('top'));

			$('#resizeMe')
				.css('width',boxwidth+"px")
				.css('height',boxheight+"px")
				.css('background-image',"url("+image_path+")")
				.css('top',resizerTop+"px")
				.css('left',resizerLeft+"px")
				.css('background-position', '-'+resizerBGLeft+'px -'+resizerBGTop+'px');
			
			$("#inpCropX").val(boxwidth);
			$("#inpCropY").val(boxheight);
			$("#inpSrcX").val(resizerBGLeft);
			$("#inpSrcY").val(resizerBGTop);

			if (boxheight < min_height)
			{
				$("#resize_containerwrapper").css('background-color','#f00');
				$("#size_warning").show();
			}
			
			$('#resizeMe').Resizable(
				{
					minWidth: 20,
					minHeight: 30,
					maxWidth: 400,
					maxHeight: 400,
					minTop: 230,
					minLeft: 10,
					maxRight: image_width+10,
					maxBottom: image_height+230,
					dragHandle: true,
					ratio: 1.25,
					onDrag: function(x, y)
					{
						this.style.backgroundPosition = '-' + (x - 10) + 'px -' + (y - 230) + 'px';
						resize_offset_x = x - 10;
						resize_offset_y = y - 230;
					},
					onStop: function()
					{
						$("#inpCropX").val($(this).width());
						$("#inpCropY").val($(this).height());
						
					},
					onDragStop: function()
					{
						
						$("#inpSrcX").val(resize_offset_x);
						$("#inpSrcY").val(resize_offset_y);
						
					},
					handlers: {
						se: '#resizeSE',
						e: '#resizeE',
						ne: '#resizeNE',
						n: '#resizeN',
						nw: '#resizeNW',
						w: '#resizeW',
						sw: '#resizeSW',
						s: '#resizeS'
					},
					onResize : function(size, position) {
						this.style.backgroundPosition = '-' + (position.left - 10) + 'px -' + (position.top - 230) + 'px';
						if (size.height < min_height)
						{
							$("#resize_containerwrapper").css('background-color','#f00');
							$("#size_warning").show();
						}
						else
						{
							$("#resize_containerwrapper").css('background-color','#000');
							$("#size_warning").hide();
						}
					}
					
				}
			);
		}
	}
	
	
	function recrop()
	{
		document.frmMain.elements['inpAction'].value = 'RECROP';
		document.frmMain.submit();
	}
	
	function uploadnew()
	{
		document.frmMain.elements['inpAction'].value = 'UPLOADPHOTO';
		document.frmMain.submit();
	}
	
	function doUpload()
	{
		$("#uploadline").append('<div class="uploading"><h3>Image uploading...</h3><p>Please be patient while your image uploads.  If you have used a very large image (2MB or greater) this could take several minutes even if you have broadband.  So stick with it, and don\'t click anywhere for at least five minutes!</p></div>');
		
		document.frmMain.submit();
	}
	
	function showMe()
	{
		var offset = $('#chooseslottable .slotrow:eq('+(prow-1)+') a:eq('+(pcol-1)+')').offset();
		var bigoffset = $('#showyourphoto img').offset();
		if (!showing)
		{
			showing = true;
			var moveleft = (offset.left + 1);
			var movetop = (offset.top + 1);
			$('#showyourphoto img').clone().
					insertAfter('#showyourphoto img').
					hide().
					addClass('clonedImage').
					css('left', bigoffset.left + 5).
					css('top', bigoffset.top + 5).
					show().
					animate( { width: 9, height: 12, left: moveleft, top: movetop } , 2000 , null, function() {$(this).remove(); $('#showme a').show(); showing = false; } );

		}
		return false;
	}