jQuery(function($) {	
	// setEqualHeight($(".equal"));
	
	// $(".equal-modules").each(function(e) {
		// setEqualHeight($(this).children('.module'));
	// });

	turnBlockElementsToLinks($(".block-link"));
	addSpaceBelowImages($("#recommended-products").find('.hproduct'));
	
	$('#nav a.dropdown').live('click', function(e) {
		$('#nav a.dropdown').removeClass('active');
		$(this).addClass('active');
		
		var id = $(this).attr('id').substring(5);
		
		$.ajax({
			url: '/new/modules/dropdown' + id + '.php',
			success: function(data) {
				$('#dropdown').html(data);
			}
		});
		
		$('#dropdown').toggle();
	
		e.preventDefault();
	});
	
	/**
	 * Initiate superfish
	 */
	
	$('ul#my-account').superfish({
  	autoArrows:   false,
	  dropShadows:  false,
    delay:      400,
  	speed:      0
	});
	
	/**
	 * Balance page. Make the last module in each of the
	 * columns contained in #main end at the same height
	 */

	/*
	var columns = $('#main').find('#content').add('#aside-left').add('#sidebar');
	var modules = [];
		
	$(columns).each(function() {
		modules.appendChild($(this).find('.module:last'));
	});
	
	console.log(modules);
	*/

	/**
	 * Toggle disabling inputs
	 */

	$('a.edit').live('click', function(e) {
		$(this).closest('.module').find('form .editable').each(function() {
			$(this).removeAttr('disabled').removeClass('disabled');
		});
		$(this).html('Spara').addClass('save red').removeClass('green');
		
		e.preventDefault();
	});

	$('a.save').live('click', function(e) {
		$(this).closest('.module').find('form .editable').each(function() {
			$(this).attr('disabled', 'disabled').addClass('disabled');
		});
		$(this).html('Redigera').removeClass('save red').addClass('green');

		// Simulate saving
		if ($(this).hasClass('ok')) {
			// Simulate OK response
			$(this).closest('.module').find('.content .user-flash').html('<p><span class="ok icon"></span> Formuläret sparades</p>');
			$(this).closest('.module').find('.content .user-flash').toggle();
			$(this).closest('.module').find('.content .user-flash').animate({opacity: 0}, 2000, function() {
				$(this).closest('.module').find('.content .user-flash').toggle().css({opacity: 1});
			});
		} else if ($(this).hasClass('error')) {
			// Simulate error response
			$(this).closest('.module').find('.content .user-flash').html('<p><span class="warning icon"></span> Ett fel uppstod</p>');
			$(this).closest('.module').find('.content table td:first .info-message').html('Skriv din e-post på formatet namn@domain.com');
			$(this).closest('.module').find('.content .user-flash').toggle();
			$(this).closest('.module').find('.content .user-flash').animate({opacity: 0}, 2000, function() {
				$(this).closest('.module').find('.content .user-flash').toggle().css({opacity: 1});
			});

		}

		e.preventDefault();
	});
	
	/**
	 * Init tooltips for forms
	 */
	
	$('.enable-disable-form').find('.editable').live('focus', function() {
		$(this).closest('tr').find('td.label p').addClass('visible');
	});
	$('.enable-disable-form').find('.editable').live('blur', function() {
		$(this).closest('tr').find('td.label p').removeClass('visible');
	});
	
	/**
	 * This function takes a set of columns (or modules)
	 * and makes sure they are of equal height
	 */
	
	function setEqualHeight(columns) {
		var maxHeight = 0;
		columns.each(function() {
			maxHeight = ($(this).height() > maxHeight) ? $(this).height() : maxHeight;
		});
		columns.height(maxHeight);
	}
	
	/**
	 * This function adds space below images in order to make
	 * text below these images align.
	 */
	
	function addSpaceBelowImages(modules) {
		var sibling, maxHeight;
		modules.each(function() {		
			sibling = $(this).siblings(':eq(' + $(this).index() + ')');			
			if (0 >= sibling.length) {
				sibling = $(this).siblings(':eq(0)');
			}
			difference = $(this).find('img').height() - sibling.find('img').height();
			
			if (difference > 0) {
				// Positive difference, resize sibling
				sibling.find('img').css({'margin-bottom': difference+'px'});
			} else if (difference < 0) {
				// Negative difference, resize this
				difference = Math.abs(difference);
				$(this).find('img').css({'margin-bottom': difference+'px'});
			}
		});
	}	
	
	/**
	 * This function takes a set of modules and wraps them in
	 * a-tags. This makes it possible to make an entire module
	 * clickable while still conforming to XHTML 1.0 Strict.
	 */
	
	function turnBlockElementsToLinks(modules) {
		modules.each(function() {
			var link = $('<div>').append($(this).find('a:first').clone()).find('a');
			link.html('')
		
			$(this).find('a').each(function() {
				$(this).replaceWith($(this).html());
			});
			
			var content = $('<div>').append($(this).children().clone()).remove();
			
			$(this).html('').append(link.append(content));
		});
	}
});
