var current_overlay = null;

$(document).ready(function() {

	$('.product').mouseover(function() {
		$(this).find('.admin_hover').show();
	});
	
	$('.product').mouseout(function() {
		$(this).find('.admin_hover').hide();
	});
	
	$('.tile .wrapper').hide();
	$('.tile').mouseover(function() {
		$(this).find('.wrapper').show();
		$(this).addClass('active');
		if ($(this).is('.mouseover')) {
			$(this).find('.white').css('background', '#FFFFFF');
			$(this).find('.white').css('opacity', '0.7');
		}
	});
	$('.tile').mouseout(function() {
		$(this).find('.wrapper').hide();
		$(this).removeClass('active');
		$(this).find('.white').css('background', '');
	});
	
	$('.invisible').css('opacity', '0.5');
	
	if (jQuery.ui && jQuery.ui.sortable) {
		$('div#products').sortable(
			{
				containment: 'parent',
				tolerance: 'pointer',
				axis: 'y',
				items: 'div.product',
				handle: '.sort_handle',
				placeholder: 'placeholder',
				revert: true,
				opacity: 0.5,
				scroll: true,
				delay: 100,
				distance: 10,
				update: function() {
					var hash = $('div#products').sortable('serialize', { key: 'sort_products' });
					$.post(document.location.href, hash, function(return_value) {
						if (return_value != '') {
							alert(return_value);
						}
					});
				},
				start: function() {
					$('div#products div.image').hide('fast');
					$('div#products div.decoration_container').hide();
				},
				stop: function() {
					$('div#products div.image').show();
					$('div#products div.decoration_container').show();
				}
			}
		);
		
		$('div#categories').sortable(
			{
				containment: 'parent',
				tolerance: 'pointer',
				items: 'div.category',
				revert: true,
				opacity: 0.5,
				scroll: true,
				distance: 10,
				delay: 100,
				handle: '.sort_handle',
				update: function() {
					var hash = $('div#categories').sortable('serialize', { key: 'sort_categories' });
					$.post(document.location.href, hash, function(return_value) {
						if (return_value != '') {
							alert(return_value);
						}
					});
				},
				start: function() {
					$('div.new_row').remove();
				}
			}
		);
				
		$('div#product_thumbs').sortable(
			{
				placeholder: 'placeholder',
				revert: true,
				handle: '.sort_handle',
				start: function() {
					//$('div#product_thumbs').addClass('sorting');
					$('div.new_row').remove();
				},
				containment: 'parent',
				items: 'div.product',
				tolerance: 'pointer',
				scroll: true,
				revert: true,
				opacity: 0.5,
				distance: 10,
				delay: 100,
				update: function() {
					var hash = $('div#product_thumbs').sortable('serialize', { key: 'sort_products' });
					$.post(document.location.href, hash, function(return_value) {
						if (return_value != '') {
							alert(return_value);
						}
					});
				}
			}
		);
		
	}
	
	$('div#content img, div.image').rightClick(function() {
		alert(strings['copyright_message']);
		return false;
	});
	
	$('a.page_33').click(function() {
		$('div.tile_settings_container').fadeIn();
		return false;
	});
	
	init_ajaxform($('div.tile_settings_container form'));
	

});

function init_ajaxform(this_form) {
	var container = $(this_form).parent();
	$(this_form).ajaxForm({
		success: function(responseText, statusText) {
			$(container).html(responseText);
			init_ajaxform(container.find('form').get(0));
			container.fadeOut();
		}
	});
	$(this_form).find('.close').click(function() {
		$(container).fadeOut('slow');
		return true;
	});
}