window.addEvent('domready', function() {

	var preview = new Request.HTML({url:'cart.php?action=preview',
		onSuccess: function(tmp, tmpb, html) {
			$('preview').set('html', html);
		},
		onFailure: function() {
			$('preview').set('text', 'Koria ei voitu avata!');
		}
	});

	var req = new Request.HTML({url:'cart.php', 
		onSuccess: function(tmp, tmpb, html) {
			$('cart').set('html', html);
		},
		onFailure: function() {
			$('cart').set('text', 'Virhe!');
		}
	});

	$('cart').addEvent('click', function() {
		preview.send();
		var pos = $('cart').getPosition();
		$('preview').setPosition({x: pos.x - 194, y: pos.y + 19});
		$('preview').style.display = ($('preview').style.display == 'none')?(''):('none');
	});

	var siz = $('cats').getSize();
	var sizb = $('main').getSize();
	if (siz.y > sizb.y)
	{
		$('main').setStyle('height', siz.y);
	}

	req.send();

});

function showCopy()
{
	var pos = $('copys').getPosition();
	$('copybar').setPosition({x: pos.x, y: pos.y + 19});
	$('copybar').style.display = ($('copybar').style.display == 'none')?(''):('none');
}

function validateField(ob, type, req)
{
	var text = ob.value;
	var fx = new Fx.Tween(ob, {duration: 750});

	var ret = new Request({url:'validate.php?text='+text+'&type='+type+'&req='+req,
		onSuccess: function(st) {
			if (st == 'valid')
			{
				fx.set('background-color', '#FFF');
				fx.set('background-repeat', 'no-repeat');
				fx.set('background-position', 'right center');
				fx.set('background-image', 'url(images/ok.png)');

			} else {
				fx.set('background-color', '#FFF');
				fx.set('background-repeat', 'no-repeat');
				fx.set('background-position', 'right center');
				fx.set('background-image', 'url(images/fail.png)');
			}
		}
	});
	ret.send();	
}

function clearCart()
{

	var cc = new Request.HTML({url:'cart.php?action=clear',
		onSuccess: function() {

			var updcart = new Request.HTML({url: 'cart.php',
				onSuccess: function(tmpa, tmpb, html) {
					$('cart').set('html', html);
				}
			});
			updcart.send();
			
			var hl = new Fx.Tween($('cart'), {duration: 1000});
			hl.start('background-color', '#eebbbb', '#ffffff');		
		}
	});
	cc.send();

}

function removeProduct(id)
{

	var rp = new Request.HTML({url:'cart.php',
		onSuccess: function(tmp, tmpb, ret) {
			var preview = new Request.HTML({url:'cart.php?action=preview',
				onSuccess: function(tmp, tmpb, html) {
					$('preview').set('html', html);
				},
				onFailure: function() {
					$('preview').set('text', 'Koria ei voitu avata!');
				}
			});

			var req = new Request.HTML({url:'cart.php', 
				onSuccess: function(tmp, tmpb, html) {
					$('cart').set('html', html);
				},
				onFailure: function() {
					$('cart').set('text', 'Virhe!');
				}
			});

			preview.send();
			req.send();

			var hl = new Fx.Tween($('cart'), {duration: 1000});
			hl.start('background-color', '#eebbbb', '#ffffff');					
		}
	}).get({'action': 'delete', 'id': id});
}

function hideModal()
{
	$('modal').setStyle('display', 'none');
	$('modal').set('text', '');
}

function addCart(id, amount, is_modal)
{
	var nogo = 0;
	var optstr = '';
	if ($('options'+id)) {
		var opts = $$('#options'+id+ ' .option select');
		opts.each(function(item, id) {
			if (item.value < 1) {
				nogo = 1;
				item.getParent('.option').set('styles', {'background-color': '#900'});
				var pos = item.getPosition();
				$('infopop').setPosition({
					x: pos.x - 50,
					y: pos.y + 25
				});
				$('infopop').innerHTML = '<br /><p>Valitse haluamasi vaihtoehto ensin!</p>';
				$('infopop').setStyle('display', '');
				var t = setTimeout("$('infopop').setStyle('display', 'none');", 3000);
			} else {
				optstr = optstr + item.name +'='+ item.value +'|';
				item.getParent('.option').set('styles', {'background-color': 'transparent'});
			}
		});
	}
	
	if (nogo)
   	return 0;		
		
	var ac = new Request.HTML({
		url: 'cart.php',
		onSuccess: function(tmpa, tmpb, html){
			
			if (html != 'OK') {
				if (!is_modal) {
					var pos = $('tocart' + id).getPosition();
					var siza = $('tocart' + id).getSize();
					$('modal').setPosition({
						'x': pos.x + ((siza.x - 200) / 2),
						'y': pos.y
					});
					$('modal').set('text', '');
					$('modal').setStyle('display', '');
					
					var modal = new Request.HTML({
						url: 'cart.php',
						onSuccess: function(tmpa, tmpb, html){
							$('modal').set('html', html);
						},
						onFailure: function(){
							$('modal').setStyle('display', 'none');
						}
					}).get({
						'action': 'modal',
						'id': id
					});
				}
					
				return 0;
			}
			
			var updcart = new Request.HTML({
				url: 'cart.php',
				onSuccess: function(tmpa, tmpb, html){
					$('cart').set('html', html);
				}
			});
			updcart.send();			
			window.scrollTo(0, 0);
			
			if (is_modal)
			{
				$('modal').set('text', '');
				$('modal').setStyle('display', 'none');
			}
						
			var hl = new Fx.Tween($('cart'), {
				duration: 1000
			});
			hl.start('background-color', '#eeeebb', '#ffffff');
			
			var pos = $('cart').getPosition();
			$('infopop').setPosition({
				x: pos.x,
				y: pos.y + 25
			});
			$('infopop').innerHTML = '<p>Tuote lis&auml;tty ostoskoriin!</p><br /><p><a class="button" href="kori-checkout.html">Siirry maksamaan</a></p>';
			$('infopop').setStyle('display', '');
			var t = setTimeout("$('infopop').setStyle('display', 'none');", 3000);
			
		}
	}).get({
		'action': 'add',
		'id': id,
		'amount': amount,
		'opts': optstr
	});
	
}
