window.addEvent('domready', function() {
	if($('ostoskori_popup')) {
		new Ajax('/cart_dyn.php',{
			method: 'get',
			data: 't=' + new Date().getTime(),
			update: 'ostoskori_popup'
		}).request();
	}
});

window.addEvent('resize', function() {
	if($('ostoskori_popup')) {
		var pos = $('column_right').getPosition();
		if(window.opera || navigator && navigator.vendor && navigator.vendor.indexOf('Apple') != -1) {
			$('ostoskori_popup').setStyle('left',(pos.x+1+(window.getWidth()%2))+'px');
			$('tietoaostamisesta_popup').setStyle('left',(pos.x+1+(window.getWidth()%2))+'px');
		} else {
			$('ostoskori_popup').setStyle('left',(pos.x+1)+'px');
			$('tietoaostamisesta_popup').setStyle('left',(pos.x+1)+'px');
		}
	}
});

var cartHidden = true;
var infoHidden = true;

function isCartHidden() {
	return cartHidden;
}

function isInfoHidden() {
	return infoHidden;
}

function showInfo() {
	if(!isCartHidden()) closeCart();
	if(!$('tietoaostamisesta_popup').fx) {
		$('tietoaostamisesta_popup').fx = new Fx.Style('tietoaostamisesta_popup', 'opacity', {
			duration: 500,
			onComplete: function() {
				if(isInfoHidden()) {
					$('tietoaostamisesta_popup').setStyle('visibility','hidden');
				}
			}
		});
		$('tietoaostamisesta_popup').fx.hide();
	}
	var pos = $('column_right').getPosition();
	if(window.opera || navigator && navigator.vendor && navigator.vendor.indexOf('Apple') != -1) {
		$('tietoaostamisesta_popup').setStyle('left',(pos.x+1+(window.getWidth()%2))+'px');
	} else {
		$('tietoaostamisesta_popup').setStyle('left',(pos.x+1)+'px');
	}
	$('nav_info').addClass('selected');
	$('tietoaostamisesta_popup').fx.start(0.01, 1);
	$('nav_info_link').setProperty('href','javascript:closeInfo()');
	infoHidden = false;
}

function showCart() {
	if(!isInfoHidden()) closeInfo();
	if(!$('ostoskori_popup').fx) {
		$('ostoskori_popup').fx = new Fx.Style('ostoskori_popup', 'opacity', {
			duration: 500,
			onComplete: function() {
				if(isCartHidden()) {
					$('ostoskori_popup').setStyle('visibility','hidden');
				}
			}
		});
		$('ostoskori_popup').fx.hide();
	}
	var pos = $('column_right').getPosition();
	if(window.opera || navigator && navigator.vendor && navigator.vendor.indexOf('Apple') != -1) {
		$('ostoskori_popup').setStyle('left',(pos.x+1+(window.getWidth()%2))+'px');
	} else {
		$('ostoskori_popup').setStyle('left',(pos.x+1)+'px');
	}
	$('nav_ostoskori').addClass('selected');
	$('ostoskori_popup').fx.start(0.01, 1);
	$('nav_ostoskori_link').setProperty('href','javascript:closeCart()');
	cartHidden = false;
}

function closeInfo() {
	$('tietoaostamisesta_popup').fx.start(1, 0.01);
	$('nav_info').removeClass('selected');
	$('nav_info_link').setProperty('href','javascript:showInfo()');
	infoHidden = true;
}

function closeCart() {
	$('ostoskori_popup').fx.start(1, 0.01);
	$('nav_ostoskori').removeClass('selected');
	$('nav_ostoskori_link').setProperty('href','javascript:showCart()');
	cartHidden = true;
}

function addProduct(id) {
	new Ajax('/cart_dyn.php',{
		method: 'post',
		data: 'product_id='+id+'&qty=1&add=1',
		update: 'ostoskori_popup',
		evalScripts: true,
		onComplete: function() {
			var infop = new Element('p', {
				'class': 'ostoskori_success'
			});
			infop.setHTML('Peli lis&auml;tty ostoskoriin');
			infop.injectInside('ostoskori_popup');
			var wasHidden = false;
			if(isCartHidden()) {
				showCart();
				wasHidden = true;
			}
			if(!$('ostoskori_popup').scrl) {
				$('ostoskori_popup').scrl = new Fx.Style('ostoskori_popup', 'top');
			}
			var origscroll = window.getScrollTop();
			if(origscroll > 87) {
				$('ostoskori_popup').scrl.start(87,origscroll);
			}
			(function() {
				if(origscroll > 87) $('ostoskori_popup').scrl.start(origscroll,87);
				if(wasHidden) {
					closeCart();
				}
				infop.remove();
			}).delay(2500);
		}
	}).request();
}

function updateProduct(key) {
	var qty = $('qty_'+key).getValue();
	new Ajax('/cart_dyn.php',{
		method: 'post',
		data: key+'='+qty+'&update=1',
		update: 'ostoskori_popup',
		evalScripts: true,
		onComplete: function() {
			var infop = new Element('p', {
				'class': 'ostoskori_success'
			});
			infop.setHTML('Tuotteen m&auml;&auml;r&auml; p&auml;ivitetty');
			infop.injectInside('ostoskori_popup');
			(function() {
				infop.remove();
			}).delay(2500);
		}		
	}).request();	
}

function removeProduct(key) {
	new Ajax('/cart_dyn.php',{
		method: 'post',
		data: key+'=1&remove=1',
		update: 'ostoskori_popup',
		evalScripts: true,
		onComplete: function() {
			var infop = new Element('p', {
				'class': 'ostoskori_success'
			});
			infop.setHTML('Tuote poistettu ostoskorista');
			infop.injectInside('ostoskori_popup');
			(function() {
				infop.remove();
			}).delay(2500);
		}		
	}).request();	
}

var emailok = false;

function emailOk() {
	return emailok;
}

function validateEmail(email) {
	if(email.match(/^[A-Za-z0-9._%-]+@(?:[A-Za-z0-9-]+\.)+[A-Za-z]{2,4}$/)) {
		$('emailok').setHTML('');
		$('emailsubmit').setStyle('visibility','visible');
		emailok = true;
	} else {
		$('emailok').setHTML('Tarkista s&auml;hk&ouml;postiosoitteesi.');
		$('emailsubmit').setStyle('visibility','hidden');
		emailok = false;
	}
}