var BOX_W = 72;
var BOX_H = 72;
var BOX_MARGIN = 5;
var box_mouseover = null;
var box_mouseoff = null;
var box_do_hover = true;
var box_preview_mode = 'pop';

function init_pos_filter(klass)
{
	if( !klass ) return 'top left';
	
	klass = klass.replace('has-big','');
	
	pos = '';
	klass = klass.split(' ')[0];
	if( klass.match('t') ) pos += 'top';
	else pos += 'bottom';
	
	if( klass.match('r') ) pos += ' right';
	else pos += ' left';
	
	return pos;
}


$(function(){
	
	if( $('.boxes .preview').length > 0 )
	{
		box_preview_mode = 'preview';
	}
	
	$('.boxes').each(function(){
		var t = $(this);
		var p = t.parent();
		t.addClass('clearfix');
		var per_row = Math.floor( p.width() / ( 2 * BOX_MARGIN + BOX_W ) );
		var num_boxes = t.children().length;
		t.height( ( 2 * BOX_MARGIN + BOX_H ) * Math.ceil( num_boxes / per_row ) );
		t.show();
	});
	
	$('.boxes li').each(function(){
		var $li = $(this);
		var big_box = $li.children('a');
		var $img = $li.find('img');
		var $a = $li.find('a');
		
		big_box.hide();
		
		$a.css({
			display:'block',
			width:BOX_W,
			height:BOX_H
		});
		$img.hide();
		
		$li.css({
			'display':'block',
			'width':BOX_W,
			'height':BOX_H,
			'margin': BOX_MARGIN,
			'background':'url('+$img.attr('src')+') no-repeat ' + init_pos_filter( $img.attr('class') )
		});
				
		var li_pos = $li.position();
		$li.css({
			'left':li_pos.left,
			'top':li_pos.top
		});			
		
		if (box_preview_mode == 'pop') {
			$li.hover(function(){
				$(this).pop();
			}, function(){
				$(this).unpop();
			});
		}
		else
		{
			$li.hoverIntent(function(){
				$(this).show_preview();
			}, function(){
				//$(this).hide_preview();
				window.status = '';
			});
		}
		
		$('.popped').pop();
		
	}).each(function(){
		$(this).parent().css('position','absolute');
	});
	
	$(window).bind('load', function() { 
		$('.boxes a').each(function(){
			$('<img>').attr('src',$(this).attr('href'));
		});
	});
	
	set_preview_dimensions();
});

function set_preview_dimensions()
{
	var $preview = $('.boxes .preview:first');
	var $img = $preview.find('img');
		
	if( $img.height() == 0 )
	{
		setTimeout('set_preview_dimensions()',500)
		return;
	}
	
	$img.wrap('<a></a>');
	$preview.css({
		'width':$img.width(),
		'height':$img.height()
	});

	$preview.find('a:first').css({
		'display':'block',
		'width':$img.width(),
		'height':$img.height()
	});
}

jQuery.fn.pop = function(){
	return $(this).each(function(){
		var $li = $(this);
		if( !box_do_hover && !$li.hasClass('popped') ) return;
		
		var $img = $li.find('img');

		var init_pos = init_pos_filter($img.attr('class'));

		var w = $img.width();					
		var h = $img.height();
		
		if( $img.hasClass('has-big') )
		{
			src = $img.attr('src').split('/')
			src[src.length - 2] += '/full';
			src = src.join('/')

			var dim = src.split('?')[1].split('&');
			
			w = dim[0].split('=')[1] + 'px';
			h = dim[1].split('=')[1] + 'px';
			
			$li.css('background','url(' + src + ') no-repeat '+ init_pos_filter( $img.attr('class') ) );
		}
		
		var top = init_pos.indexOf('bottom') > -1 ? -h + BOX_H: 0;
		var bottom = init_pos.indexOf('top') > -1 ? -h + BOX_H: 0;
		
		var left = init_pos.indexOf('right') > -1 ? -w + BOX_W: 0;
		var right = init_pos.indexOf('left') > -1 ? -w + BOX_W: 0;

		$li.css({
			'width':w,
			'height':h,
			'marginTop': top + BOX_MARGIN,
			'marginLeft': left + BOX_MARGIN,
			'marginRight': right + BOX_MARGIN,
			'marginBottom': bottom + BOX_MARGIN,
			'z-index':'2'
		});
	});
}

jQuery.fn.unpop = function(){
	return $(this).each(function(){
		var $li = $(this);
		$li.css({
			'width':BOX_W,
			'height':BOX_H,
			'margin': BOX_MARGIN,
			'z-index':'0'
		});
		if ( $li.find('.has-big').length > 0 )
		{
			var $img = $li.find('img');
			$li.css('background', 'url(' + $img.attr('src') + ') no-repeat ' + init_pos_filter($img.attr('class')));
		}
		
	});
}

jQuery.fn.show_preview = function()
{
		var $li = $(this);
		if( !box_do_hover && !$li.hasClass('popped') ) return;
		
		var $img = $li.find('img:first');
		var $a = $li.find('a:first');
		var $preview = $li.siblings('.preview:first');
		var $preview_link = $preview.find('a:first');
		var src = $img.attr('src');
		
		if( $img.hasClass('has-big') )
		{
			src = src.split('/')
			src[src.length - 2] += '/full';
			src = src.join('/')
		}
		
		$preview.css('background','url(' + src + ') center center no-repeat');
		$preview.unbind('click');
		$preview.css('cursor','');	
		$preview_link.attr('href', ($a.length == 0 ? '' : $a.attr('href')) );
		
			
}

jQuery.fn.hide_preview = function()
{
		var li = $(this);
		if( !box_do_hover && !li.hasClass('popped') ) return;
		
		var img = li.find('img');
		var big_box = li.children('a');
		
		var $preview = li.siblings('.preview:first');
		
		$preview.css('background',$preview.attr('orig_bg'));
}
		