$(function () {
	$('<div id="ajax-spinner"></div>').hide().ajaxStart(function () {
		$(this).show();
	}).ajaxStop(function () {
		$(this).hide();
	}).appendTo("body");
});

$("div.flash").livequery(function () {
	var el = $(this);
	setTimeout(function () {
		el.animate({"opacity": 0}, 2000);
		el.slideUp();
	}, 7000);
});


function handle_detail_close (tthis)
{
	if ( ! tthis._detail ) tthis = this;

	var _detail = tthis._detail;
	var detail_helper = jQuery(tthis).parents('.detail-helper');

	_detail.removeClass ('detail-helper-opened').removeClass ('detail-hover').append ( detail_helper.find (".content").contents() );
	detail_helper.remove ();
}

function handle_detail_open ()
{
	var _detail = jQuery(this).parents('.subcategory-detail');
	var detail_helper = jQuery("#detail-helper").clone (true).removeAttr ('id');

	detail_helper.find (".content").append ( _detail.contents() );
	_detail.addClass ('detail-helper-opened');
	jQuery('body').append (detail_helper);

	var off = _detail.offset ();
	detail_helper.css ('left', off.left).css ('top', off.top).slideDown('fast');

	detail_helper.find ('.close-detail')[0]._detail = _detail;
	detail_helper.find ('.add-to-basket-button')[0].detail_helper = detail_helper;
}

function handle_detail_adding_done (data, status)
{
	if ( ( status !== "success" )  || ( ! data.match ('{"redirect') ) )
	{
		alert ('Error while adding product to basket! Try again...');
		return;
	}

	if (jQuery('span#lang').text() == 'nl')
	{
			flash ('Het nieuwe product is aan uw mandje toegevoegd.', 'info');
	}
	else
	{
		flash ('Product added to basket.', 'info');
	}

}

function handle_detail_add_to_basket ()
{
	var detail_helper = this.detail_helper;

	var quantity = detail_helper.find ('input[name="quantity"]')[0].value;
	var comment = detail_helper.find ('input[name="comment"]')[0].value;
	var _extras = detail_helper.find ('input[name^="extras"]:checked');
	var extras = [];
	for ( var i = 0; i < _extras.length; i++) extras[extras.length] = _extras[i].value;

	handle_detail_close (detail_helper.find ('.close-detail')[0]);		
	jQuery.post (this.href, { 'quantity': quantity, 'comment' : comment,  'extras[]': extras }, handle_detail_adding_done );

	return false;
}

function extend_products ()
{
	var details = jQuery("#products-listing .subcategory-detail").not ('.detail-na');

	details.hover (
		function () { jQuery(this).addClass ('detail-hover'); },
		function () { jQuery(this).removeClass ('detail-hover'); }
	).click (
		function (e) {
			if ( ! jQuery(this).parents('.subcategory-detail').hasClass ('detail-helper-opened') )
			{
				jQuery(this).find ('.open-detail').click ();
			}
		}
	);

	details.find ('.open-detail').bind ( 'click', handle_detail_open );
	details.find ('.close-detail').bind ( 'click', handle_detail_close );
	details.find ('.add-to-basket-button').bind ( 'click', handle_detail_add_to_basket );

	jQuery("#products-listing .highlight .open-detail").click ();
}

function flash (msg, type)
{
	jQuery("#content-inner").prepend ('<div class="flash ' + type + '">' + msg + '</div>');
}

function validate_number ()
{
	this.value = parseInt (this.value) ? parseInt (this.value) : '';
}

function handle_basket_update_done (data, status)
{
	if ( ( status !== "success" )  || ( ! data.redirect ) )
	{
		alert ('Error while updating product in basket! Try again...');
		return;
	}

	window.location.href = data.redirect;
}

function handle_basket_update (frm)
{
	jQuery.post (jQuery(this).parents("form")[0].action, { 'quantity': this.value, 'item-id': this.name}, handle_basket_update_done, "json");
}

jQuery(document).ready ( function () {
	jQuery(".validate-number").bind ('change', validate_number). bind ('keyup', validate_number);
	jQuery("#menu ul li").hover (
		function () { jQuery(this).addClass ('opened').find ("ul").stop(true).css('height', 'auto').slideDown ('slow'); },
		function () { jQuery(this).removeClass ('opened').find ("ul").stop(true).slideUp ('fast'); }
	);
});

