/*===============================================================================
 Red4Net(R) - Content Management System : www.netsoftworks.eu
 Copyright (C) 2000-11 NETSOFTWORKS - Antonio Fonseca All Rights Reserved
 ===============================================================================*/

$(document).ready(function() {

	/**
	 * This function adds a hover and a transfer effect to the addButtons of
	 * applicable contents.
	 * @param {Object} this
	 */
	$('.r4n-button-applicate-content')
	.live('hover', function() {
		$(this).css('cursor', 'pointer');
	}, function() {
		$(this).css('cursor', 'default');
	})
	.live('click', function(e) {
		stopEvent(e);
		$(this).effect('transfer', {
			to: 'div.container_block_shopcart'
		}, 1000);
	});

	/**
	 * dialog mit shopcart
	 * 'Select new job(s)' 'Apply job(s)' I18N e ShopArt
	 */	
	$('#r4n-shopcart-dialog').dialog({
		bgiframe: true,
		zIndex: 3999,
		autoOpen: false,
		height: 400,
		width: 550,
		modal: false,
		zIndex: 2000,
		title: $('div.container_block_shopcart div.block_titel').html(),
		buttons: {
			'Select new job(s)': function() {
				$(this).dialog('close');
			},
			'Apply job(s)': function() {
				location = '/red4net/serv_content_application_form.asp';
			}
		},
		close: function(event, ui) {
			$('#r4n-shopcart-dialog').empty();
			$.unblockUI({});
		}
	});
	
	// Anzeige der ContentAplicationList wenn man angemeldet ist.
	if ($('#WidgetContentApplicationList').length > 0) {
		$('#tabs')
		.prepend('<ul><li><a href="#tabs-1">CONTENT</a></li><li><a href="#tabs-2">APPLICATION</a></li></ul>')
		.tabs();
	}
	
	// Pulsierender Hinweis da� Attachment hinzugef�gt wurde
	$('ul.r4n-validation-error').effect('pulsate', {
		times: 1
	}, 500);

	/**
	 * This function deletes a single or all items from a cart and
	 * updates the corresponding cartWidget through Ajax.
	 * 
	 * @param {Object} e
	 */	
	$('a.r4n-delete-cart-item').live('click', function(e) {
		stopEvent(e);
		if (!confirm('Do you really want to remove this cart item?'))
			return;
		$.url.setUrl($(this).attr('href').replace('&amp;', '&')); // Lazy, huh?
		var groupId = $.url.param('groupId');
		$('body').css('cursor', 'wait');
		$.ajax({
			type: 'GET',
			url: $(this).attr('href').replace('&amp;', '&'),
			data: {
				anti: Math.random()
			},
			success: function(data, textStatus) {
				var objCartWidget = $('#widgetApplicationContainer' + groupId);
				if (0 < objCartWidget.length) 
					updateCartWidget(groupId);
				else
					updateCartBlock(false, false);
			},
			error: onAjaxError,
			complete: function(xhr, textStatus) {
				$('body').css('cursor', 'default');
			}
		});
	});

	/**
	 * Wird ein Button zum Lösen eines Attachment geklickt,
	 * so muß die Aktion zum Lösen aufgerufen werden.
	 * Im Erfolgsfall wird die ID der Gruppe zur�ckgegeben
	 * deren CartWidget dann aktualisiert wird.
	 * 
	 * @param {Object} e
	 */	
	$('a.r4n-unlink-attachment').live('click', function(e) {
		stopEvent(e);
		if (!confirm('Do you really want to remove this attachment?'))
			return;
		$('body').css('cursor', 'wait');
		$.ajax({
			type: 'GET',
			url: $(this).attr('href').replace('&amp;', '&'),
			data: {
				anti: Math.random()
			},
			success: function(data, textStatus) {
				updateCartWidget(data); // action returns groupId as data
			},
			error: onAjaxError,
			complete: function(xhr, textStatus) {
				$('body').css('cursor', 'default');
			}
		});
	});
	
});

/**
 * This function updates (or adds) an item to a cart
 * and updates the *whole* cartWidget through Ajax.
 */
function updateCartItem(cartItemId) {

	var groupId = $('#cartItemGroupId' + cartItemId).val();
	var cartItemAction = $('#cartItemAction' + cartItemId).val();
	
	var qs = {};
	qs['action'] = 'addToCart';
	qs['anti'] = Math.random();
	qs['groupId'] = $('#cartItemGroupId' + cartItemId).val();
	qs['cartItemAction'] = $('#cartItemAction' + cartItemId).val();
	if ($('#cartItemId' + cartItemId).length > 0) 
		qs['cartItemId'] = $('#cartItemId' + cartItemId).val();
	qs['cartItemContentId'] = $('#cartItemContentId' + cartItemId).val();
	qs['cartItemQuantity'] = $('#cartItemQuantity' + cartItemId).val();
	if ($('cartItemColor' + cartItemId).length > 0) 
		qs['cartItemColor'] = $('#cartItemColor' + cartItemId).val();
	if ($('cartItemSize' + cartItemId).length > 0) 
		qs['cartItemSize'] = $('#cartItemSize' + cartItemId).val();
	
	$('body').css('cursor', 'wait');
	$.ajax({
		type: 'GET',
		url: app_url + '/red4net/serv_cart_item_action.asp',
		data: $.param(qs),
		success: function(data, textStatus) {
			if ('insert' == cartItemAction) 
				$('#widgetApplicationContainer' + cartItemId).html(data);
			else updateCartWidget(groupId);
		},
		error: onAjaxError,
		complete: function(xhr, textStatus) {
			if ('insert' == cartItemAction) {
				updateCartBlock(true, true);
			} else {
				// Auf der Seite auf der das WidgetContentApplicationCart dargestellt wird
				// ist kein WidgetContentApplicationCartBlock mehr vorhanden, also muß dieses
				// nicht mehr aktualisiert werden.
			}
			$('body').css('cursor', 'default');
		}
	});
	
}

/**
 * Updates the WidgetApplicationCart as part of the content application form.
 * 
 * @param {Object} groupId
 */
function updateCartWidget(groupId) {
	$.ajax({
		type: 'GET',
		url: app_url + '/red4net/serv_cart_item_action.asp',
		data: {
			action: 'getCartWidget',
			groupId: groupId,
			anti: Math.random()
		},
		beforeSend: function(data, textStatus) {
		},
		success: function(data, textStatus) {
			if ($('#widgetApplicationContainer' + groupId).length < 1) 
				alert('#widgetApplicationContainer' + groupId + ' nicht gefunden');
			$('#widgetApplicationContainer' + groupId).html(data);
			//$('.widgetApplicationContainer').replaceWith(data);
		},
		error: onAjaxError
	});
	
}

/**
 * Updates ShopCartBlock & optionally displays dialog with actual list of CartItems.
 * 
 * @param {boolean} blnForceEnableCheckout
 * 				if button r4n-content-application-checkout should be displayed
 * 				even if there has been none before
 * @param {boolean} blnDisplayDialog
 * 				if dialog should be displayed
 */
function updateCartBlock(blnForceEnableCheckout, blnDisplayDialog) {
	var objCartBlockContainer = $('div.container_block_shopcart');
	if (0 == objCartBlockContainer.length) {
		alert('shopcart not found');
		return;
	}
	$.ajax({
		type: 'GET',
		url: app_url + '/red4net/serv_cart_item_action.asp',
		data: {
			action: 'getCartBlock',
			anti: Math.random(),
			enableCheckout: (blnForceEnableCheckout || 0 < $('.r4n-content-application-checkout', objCartBlockContainer).length) ? 'true' : 'false'
		},
		success: function(data, textStatus) {
			objCartBlockContainer.replaceWith(data);
			if (blnDisplayDialog) {
				// get container again cause its content has changed!
				var content = $('div.container_block_shopcart').html();
				$('#r4n-shopcart-dialog').append(content);
				$.blockUI({
					message: null,
					zIndex: 1600
				});
				$('#r4n-shopcart-dialog').dialog('open');
			}
		},
		error: onAjaxError
	});
}

