var siteRoot = "http://www.soupjurk.nl";

window.addEvent('domready', function() 
{
	/**********************
	    OPEN NEW WINDOW
	**********************/
	
	$$('a.newWindow').each(function(item, index)
	{
		item.addEvent('click', function(e)
		{
			new Event(e).stop();
			window.open(this.get('href'));
		});
	});
	
	
	/**********************
	    REMOVE ARTICLE
	**********************/
	
	$$('a.removeArticle').each(function(item, key)
	{
		item.addEvent('click', function(e)
		{
			// check confirmation
			if(confirm('Weet je zeker dat je dit artikel wilt verwijderen uit je winkelmandje?'))
			{
				// send request
				new Request(
				{
					url: siteRoot + "/requests/managecart.php",
					method: "post",
					data: "action=remove" + "&key=" + key,
					onSuccess: function(response)
					{
						var result = JSON.decode(response, true);
						
						if(result.valid)
						{
							var myTween = new Fx.Morph(item.getParent('tr'), {'duration': '250'});
							
							myTween.start({'opacity': [1,0]}).chain(function()
							{
								window.location.href = window.location.href;
							});	
						}
						else
						{
							alert(result.message);
						}
					}
				}).send();
			}
		});
	});
	
	
	/**********************
	   ARTICLE IMG SWITCH
	**********************/
	
	if($('showArticle'))
	{
		// preload images
		$$('img.shopimg').each(function(item)
		{
			if(item.src.match("_thumb"))
			{
				var loadImg = new Image();
				loadImg.src = item.src.replace("_thumb", "");
			}
		});
		
		// add events
		$('articlesubimg').getElements('a').each(function(item)
		{
			var image = item.getElement('img');
			
			item.addEvent('click', function(e)
			{
				if(!image.hasClass('current'))
				{
					// switch focus
					$('articlesubimg').getElements('img').each(function(subItem)
					{
						subItem.removeClass('current');
					});
					image.addClass('current');
					
					// change current image
					var myTween = new Fx.Morph($('mainImg'), {'duration': '250'});
					myTween.start({'opacity': [1,0]}).chain(function()
					{
						$('mainImg').src = image.src.replace("_thumb", "");
						myTween.start({'opacity': [0,1]});
					});
				}
			});
		});
	}
	
	/**********************
	    CUSTOMER FORM
	**********************/
	
	if($("customerForm"))
	{
		$("customerForm").addEvent('submit', function(e)
		{
			// stop submit event
			new Event(e).stop();
			
			// attach request
			this.set('send', 
			{
				url: siteRoot + "/requests/validateform.php",
				method: 'post',
				onSuccess: function(response)
				{
					var result = JSON.decode(response, true);
					
					if(result.valid)
					{
						window.location.href = $("customerForm").action;
					}
					else
					{
						$('formWarning').innerHTML = result.message;
						$('formWarning').removeClass('hidden');
					}
				},
				onFailure: function()
				{
					alert("Oeps, er is iets misgegaan. Probeer het opnieuw!");
				}
			});
			
			// send request
			this.send();
		});
	}
	
	/**********************
	     COMMENT FORM
	**********************/
	
	if($("newCommentForm"))
	{
		$("newCommentForm").addEvent('submit', function(e)
		{
			// disable button
			$('submitbtn').disabled = true;
			
			// stop submit event
			new Event(e).stop();
			
			// attach request
			this.set('send', 
			{
				url: siteRoot + "/requests/validateform.php",
				method: 'post',
				onSuccess: function(response)
				{
					var result = JSON.decode(response, true);
					
					if(result.valid)
					{
						window.location.href = window.location.href;
					}
					else
					{
						$('formWarning').innerHTML = result.message;
						$('formWarning').removeClass('hidden');
					}
				},
				onFailure: function()
				{
					alert("Oeps, er is iets misgegaan. Probeer het opnieuw!");
				}
			});
			
			// send request
			this.send();
		});
	}
});
