// launches a post inline
var launch = function(e)
{
	e.preventDefault();
	
	var $t = $(this),
		data = e.data || {};
	
	$t.append('<div id="loader"><span>Loading</span></div>').find('.images').mouseleave();

	$('#grid').gridmap.deactivate();
};

var setupLoadMore = function(getURL, preload, timeout, callback, nomore)
{
	var isLoading = false,
		to, 
		url = getURL();
	
	$(window).scroll(function(e)
	{
		if(!isLoading)
		{
			if($(window).scrollTop() == $(document).height() - $(window).height()) // at bottom of page
			{
				if(url)
				{
					isLoading = true;
					
					preload();
					
					// slow loading msg after 10 secs
					to = setTimeout(timeout, 10000);
					
					$.ajax({
						url: url,
						data:{ajax:true, blog:true},
						type:'GET',
						success: function(xml)
						{	
							clearTimeout(to);
							isLoading = false;
							
							var $xml = $(xml);
							callback( $xml );
							
							url = getURL($xml);
							if(!url)
							{
								$(window).unbind('scroll');
								nomore();
							}
						},
						error: function (XMLHttpRequest, textStatus, errorThrown) {
							log('error',textStatus);
							isLoading = false; // try again
						}
					});
					
				}
				else
				{
					$(window).unbind('scroll');
					nomore();
				}
				
			}
		}
	
	});
	
	// call first time
	$(window).scroll();
}

$.extend($.ui.draggable.prototype, (function (orig) {
	return {
		_mouseDrag: function (event,noPropagation) {
			
			var positionHelper = this.helper.position(),
				result = orig.call(this, event, noPropagation);
			
			positionHelper.left += (this.positionAbs.left - positionHelper.left) /5;
			positionHelper.top += (this.positionAbs.top - positionHelper.top) /5;
			
			if(!this.options.axis || this.options.axis != "y") this.helper[0].style.left = positionHelper.left + 'px';
			if(!this.options.axis || this.options.axis != "x") this.helper[0].style.top = positionHelper.top + 'px';
			
			return result;
		}
	};
})($.ui.draggable.prototype["_mouseDrag"]));

$(document).ready(function(){
	$('.panels').slideToggle();
	
	$('#grid').css({margin:0, left:$('#overlay').offset().left });
			
	$('#grid').vertexLayout('#grid .post',
		{centre_offset:{x:600,y:400}, min_width:50, min_height:50, debug:false});
	
	$('#grid').gridmap().bind( "activate", function(e, group){

		if($.browser.msie && !$('#grid').gridmap.isActive() ) return false;
		
		group = group || $('#grid .post');
		
		// fading of post imgs
		var mouseleave = function(e){
			var $p = $(this).parent(),
				$post = $p.closest('.post');
			
			if($post.hasClass('tint-active') ) $('.tint', $p ).stop(true,true).fadeIn();
			$('.info', $p ).stop(true,true).hide('fast');

		},
			mouseenter = function(e){
				var $p = $(this).parent(),
					$post = $p.closest('.post');
				
				if($post.hasClass('tint-active') ) $('.notint', $p).show();
				$('.tint', $p ).stop(true,true).fadeOut();

				if ($('.info', $p ).hasClass("unchanged"))
				{
					var id = $('.info', $p).attr("id")
					sIFR.replace(dinmed, {
					    selector: '#' + id
					    ,css: {
					      '.sIFR-root': { 'font-size': '10px', 'color': '#ffffff', 'background-color': '#e74d24' }
					    }
					});
					
				    $('#' + id).removeClass("unchanged");
				}
			    $('.info', $p ).stop(true,true).show('fast');
		};
		
		$('.images', group).mouseenter(mouseenter).mouseleave(mouseleave);

	}).bind("deactivate", function(e){
		$('#grid .post .images').unbind('mouseenter').unbind('mouseleave');
		$('#grid .welcome').unbind('mouseenter').unbind('mouseleave').stop(true).animate( { backgroundColor: '#ccc' }, 500);
		
	
	}).bind("update",function(e, group){ 
		
		group = group || $('#grid .post');

		group.filter('.tint-active, .tint-inactive' ).find('.tint').each(function(i){
			if( $(this).width() ) $(this).fadeIn();
			else $(this).load(function(e){
				$(this).fadeIn();
			});
				
		});
		
		group.filter( '.tint-inactive' ).find('.notint').each(function(i){
			if( $(this).width() ) $(this).fadeIn();
			else $(this).load(function(e){
				$(this).fadeIn();
			});
				
		});
	})
	.trigger('update')
		.gridmap.loadMore( $('#nav-next a').attr('href') ); // load more items and attach
	
	if(($is_home && $.cookie('overlay-hidden')!='false' )){
		$('#grid').gridmap.activate(); 
	} 

	$('.close').click(function(e){
		e.preventDefault();
		
		$('#grid .tint-inactive .tint').fadeOut();

		$('.panels').animate({height:0},'def', function(e){
				$(this).remove();
				$('#grid').gridmap.activate();
		});
	});		
});