$(function(){

	$('#Fields_Comment')
		.keydown(limitTextarea)
		.keyup(limitTextarea)
		.blur(limitTextarea)
		.focus(clearTextarea);
	
	$('.pictureGrid.enabled div.block.off')
	.live('mouseout',
		function(e) {
			$(this).stop().animate({opacity: 1}, 300).removeClass('highlight');
		}
	)
	.live('mouseover',
		function(e) {
			$(this).stop().css({opacity: 0}).addClass('highlight');
		}
	)
	.live('click',
		function(e){
			$(this).css({opacity: 1});
			$('#Field_Square').val($(this).attr('id'));
			showUI('contentBox-Add');
		}
	);
	
	
	$('.pictureGrid.enabled').live('mousemove',
		function(e) {
		
			var to = $(this).data('cleanup');

			var oThis = this;
			if(!to){
				to = setTimeout(
					function(){
						$('div.off:not(.highlight)', oThis).css({opacity: 1});
						$(oThis).data('cleanup', null);
					},
					 600
				);
				$(this).data('cleanup', to);
			}
		}
	);
	
	$('.pictureGrid.enabled div.on')
	.live('mouseover',
		function(e){
		
			var c = $('.commentPop');
			
			var parts = this.id.substr(('square-').length).split('-');
			
			var name = squares[parts[1]][parts[2]]['firstname'] ? squares[parts[1]][parts[2]]['firstname'] + ' ' + squares[parts[1]][parts[2]]['lastname'] : '';
			var comment = squares[parts[1]][parts[2]]['comment'] || '';
			
			c.find('.who').text(name);
			c.find('.what').text(comment);

			if(name || comment){
				c.stop().show().animate({opacity: 1});
				updateCommentBoxPos(e);
			}
		
		}
	)
	.live('mouseout',
		function(e){
			var c = $('.commentPop');
			c.stop().animate({opacity: 0}, function(){ $(this).hide();});
		}
	)
	.live('mousemove',updateCommentBoxPos);
	
	$('.commentPop').live('mousemove', updateCommentBoxPos);
	
	
	$('.add').click(
		function(){
			hideUI();
			return false;
		}
	);

	$('div.contentBox a.close').live('click',
		function(){
			hideUI();
			return false;
		}
	);

	$('.bar a').click(function(){
		var box = this.href.substr(HTTP.length+1);
		var target = 'contentBox-' + (box.length ? box : 'Intro');
		
		var vis = $('.contentBox:visible:not(.' + target + ')');
		
		if(vis.length){
			vis.hide('puff', {percent: 10}, 
				function(){
					showUI(target);
				}
			);
		}else {
			showUI(target);
		}
		
		return false;
		
	});
});

function limitTextarea(){
	if($(this).val().length > 140){
	//	$(this).val($(this).val().substr(0, 140));
		$('#Fields_Comment_Error').text('Comments must be less than 140 characters').show();
	}else {
		$('#Fields_Comment_Error').hide();
	}
}

function hideUI(){
	$('.header h1').css('position', 'absolute').animate({top: -80}, 300, 'swing');
	$('div.contentBox:visible').hide('puff', {percent: 10});
	$('.pictureGrid').addClass('enabled');
}

function showUI(contentBox){
	contentBox = contentBox || 'contentBox-Intro';
	$('.header h1').css('position', 'absolute').animate({top: 0}, 300, 'swing');
	$('.' + contentBox + ':hidden').show('puff',
		function(){
			$(this).find('img').attr('style', '')
		}
	);
	$('.pictureGrid').removeClass('enabled');
}

function updateCommentBoxPos(e){
	var c = $('.commentPop');
	var w = c.width() /2;
	c.css(
		{
			top: e.pageY + 10,
			left: e.pageX - w
		}
	);
}

function clearTextarea(e){
	if($(this).val() == 'Vær vennlig å gi oss dine tanker om hvalfangst og hval.'){
		$(this).val('');
	}

}