window.addEvent('domready', function(){
	$('shopcart_list').getElement('.close').addEvent('click', function(){
		hideShopcart();
	})
	shopcartLoadEvents()
});
var cloned = '';
function addItemToShopcart (item, units) {
	var imageSizes = $('front').getElement('img').getDimensions();
	var cloned = $('front').getElement('img').clone().inject($('front'), 'top').setStyles({
		position: 'absolute',
		width: imageSizes.x,
		height: imageSizes.y
	}).position({relativeTo: $('front'), offset: {x: 2}}).set('morph', {transition: Fx.Transitions.Expo.bounceOut, duration:1000});
	cloned.morph({
		border: 0,
		top: 0,
		left: 860,
		width: 32,
		height: imageSizes.y*(32/imageSizes.x),
		opacity: 0.2,
		transition: 'expo-out'
	});
	(function(){cloned.dispose();}).delay(900);
	var request = new Request({
		url: base_path+'shopcart/add/'+item+'/'+units,
		onSuccess: function(response){
			if ( ! response.contains('error::nostock')) {
				updateShopcartList();
			} else {
				cloned.hide();
				dp.alerta('Lo sentimos pero no tenemos suficiente stock<br />Unidades disponibles: '+response.replace(/error\:\:nostock\[([0-9].*)\]/, '$1')+'.');
			}
		}
	}).send();
}

function updateItemFromShopcart(item, units)
{
	var request = new Request({
		url: base_path+'shopcart/update/'+item+'/'+units,
		onSuccess: function(response){
			if ( ! response.contains('error::nostock')) {
				updateShopcartList();
			} else {
				$('item_'+item).getElement('input').set('value', response.replace(/error\:\:nostock\[([0-9].*)\]/, '$1'));
				$('item_'+item).getElement('input').fireEvent('keyup');
				dp.alerta('Lo sentimos pero no tenemos suficiente stock<br />Unidades disponibles: '+response.replace(/error\:\:nostock\[([0-9].*)\]/, '$1')+'.');
			}
		}
	}).send();
}

function delItemFromShopcart (item, units) {
	var request = new Request({
		url: base_path+'shopcart/del/'+item+'/'+units,
		onSuccess: function(response){
			updateShopcartList();
		}
	}).send();}

function updateShopcartList () {
	var holder = new Element('div').set('id', 'dataHolder').inject(document.body);
	var request = new Request.HTML({
		url: base_path+'shopcart/refreshShopcart',
		onSuccess: function(tree, elements, data){
			holder.set('html', data);
			$('shopcart_list').getElement('div.updatable').set('html', holder.getElement('div[class=updatable]').get('html'));
			holder.dispose();
			shopcartLoadEvents();
			$('shopcart').getElement('span#price').set('html', $('shopcart_list').getElement('div.total').getElement('span.price').get('html'));
			dp.tooltip();
		}
	}).send();
}

var autorefresh_shopcart = null;
function shopcartLoadEvents()
{
	var el = $('shopcart_list').getElement('.list');
	el.getElements('.item').each(function(el){
		el.getElement('input').addEvent('keyup', function(ev){
			if (autorefresh_shopcart) {
				clearTimeout(autorefresh_shopcart);
			}
			autorefresh_shopcart = setTimeout(function(){updateShopcart(el)}, 1000);
		});
		el.getElement('div.delete').addEvent('click', function(){
			delItemFromShopcart(el.get('id').replace(/item_/, ''), parseInt(el.getElement('input').get('value'))+1);
		});
	});
}

function updateShopcart(el) {
	if (autorefresh_shopcart) {
		clearTimeout(autorefresh_shopcart);
	}
	if (el.getElement('input').get('value') > 0) {
		updateItemFromShopcart(el.get('id').replace(/item_/, ''), el.getElement('input').get('value'));
	}
}

var overlay;
function showShopcart () {
	overlay = new Overlay({click: function(){hideShopcart();}});
	$('shopcart_list').fade('hide').show();
	$('shopcart_list').fade('in');
}

function hideShopcart () {
	overlay.close();
	$('shopcart_list').fade('out');
}
