var shop = {

	busy : 0,

	get_cart_total : function () {
		new Ajax.Updater('cart_total_price','/webshop/get_cart_total/');
	},
	
	saveReview : function ()
	{
		if ($('review_body').value.length > 6) {
			$('review').request({
				onComplete : function(t) {
					if (t.responseText == 'succes') {
						
						location.reload(true);
						
					}
				}
			});	
		} else {
			alert('Uw eigen omschrijving van dit product is te kort');
		}
	},
	
	gotoReviews : function ()
	{
		tab.toggle('selector_reviews');
		
		new Effect.ScrollTo('post-review');
	},
	
	add_to_cart : function (product_id) {
		
		var option_id = new Object();
		var option_id;
		var sendVar = '';
		var x = 0;
	
		for (i = 0; i < document.getElementsByTagName('input').length; i++)
		{
			if (document.getElementsByTagName('input')[i].type == 'radio')
			{
				if (document.getElementsByTagName('input')[i].checked == true)
				{
					option_id[x] = document.getElementsByTagName('input')[i].value;
					x++;
				}
			}
		}
	
		for (keyVar in option_id)
		{
			sendVar += option_id[keyVar] + ',';
		}
	
		sendVar = sendVar.substr(0, sendVar.length - 1);
		
		if ( this.busy == 0 ) {
			
			this.busy = 1;
			
			$('product_label').show();
			
			new Effect.Fade('product_label');
			
			new Effect.Move('product_label',{ x: 520, y: -50, afterFinish : function () {
				$('product_label').setStyle({
				  top: '0',
				  left: '0'
				});
				
				if (sendVar == '')
				{
					new Ajax.Request('/webshop/update_cart/', { method: 'post', postBody: 'product_id='+product_id+'&amount=1', onSuccess: function(t) { 
						shop.get_cart_total();
						shop.busy = 0;
					} } );
				}
				else
				{
					new Ajax.Request('/webshop/update_cart/', { method: 'post', postBody: 'product_id='+product_id+'&amount=1&option_ids='+sendVar, onSuccess: function(t) { 
						shop.get_cart_total();
						shop.busy = 0;
					} } );
				}
				
			} });
		
		}
	}

}

var tab = {

	active_button : false,
	active_target : false,

	toggle : function (node) {

		var parent = node;
		var target = node.replace('selector', 'product');
	
		
		if (this.active_button && this.active_target) {
			
			$(this.active_target).hide();
			$(this.active_button).removeClassName('tab_active');
			
			$(parent).addClassName('tab_active');
			$(target).show();
			
			this.active_button = parent;
			this.active_target = target;
			
		} else {
				
			$(parent).addClassName('tab_active');
			$(target).show();
			
			this.active_button = parent;
			this.active_target = target;
		
		}
		
	}

}

Event.observe(window, 'load', function() {
	if ($('price_incl'))
		setupZoom();
});



// cached id-ref getter
var idCache = {};
function getById(id)
{
	if (!(id in idCache))
		idCache[id] = document.getElementById(id);

	return idCache[id];
}

// generic eventhandling
var addEvent = function()
{
	if (document.addEventListener)
	{
		function checkMouseLeaveOrEnter(element, handler)
		{
			return function(e)
			{
				var target = e.relatedTarget || (e.type == 'mouseover' ? e.fromElement : e.toElement);
				while (target)
				{
					if (target == element)
						return;

					target = target.parentNode;
				}

				return handler.call(this, e);
			}
		}

		return function(element, type, handler)
		{
			if (type == 'mouseenter')
			{
				type = 'mouseover';
				handler = checkMouseLeaveOrEnter(element, handler);
			}
			else if (type == 'mouseleave')
			{
				type = 'mouseout';
				handler = checkMouseLeaveOrEnter(element, handler);
			}

			element.addEventListener(type, handler, false);
		}
	}
	else
	{
		function handleEvent(event)
		{
			event = event || fixEvent(window.event);

			// defeat IE resize bug
			if (event.type == 'resize')
			{
				var page = getPageDimensions();
				var currentDimensions = page.innerWidth + 'x' + page.innerHeight;

				if (this.__dimensions && this.__dimensions == currentDimensions)
					return true;

				this.__dimensions = currentDimensions;
			}

			var handlers = this.events[event.type], returnValue;
			for (var i in handlers)
			{
				if (handlers.hasOwnProperty(i) && handlers[i].call(this, event) === false)
					returnValue = false;
			}

			return returnValue;
		}
		function fixEvent(event)
		{
			event.preventDefault = preventDefault;
			event.stopPropagation = stopPropagation;
			event.target = event.srcElement;

			return event;
		}
		function preventDefault() { this.returnValue = false; }
		function stopPropagation() { this.cancelBubble = true; }
		function removeAllEvents()
		{
			while ((cachedEvent = eventCache.pop()))
				removeEvent(cachedEvent.element, cachedEvent.type, cachedEvent.handler);

			// cleanup id-cache
			if (window.idCache)
				window.idCache = null;
		}

		var guid = 1;
		var eventCache = [];

		return function(element, type, handler)
		{
			if (!handler.$$guid) handler.$$guid = guid++;
			if (!element.events) element.events = {};
			if (!element.events[type])
			{
				element.events[type] = {};
				if (element['on' + type]) element.events[type][0] = element['on' + type];
				element['on' + type] = handleEvent;
			}

			element.events[type][handler.$$guid] = handler;

			if (type != 'unload')
			{
				if (!eventCache.length)
					addEvent(window, 'unload', removeAllEvents);

				eventCache.push(
					{
						element: element,
						type: type,
						handler: handler
					}
				);
			}
		}
	}
}();

var removeEvent = function()
{
	if (document.removeEventListener)
	{
		return function(element, type, handler)
		{
			element.removeEventListener(type, handler, false);
		}
	}
	else
	{
		return function(element, type, handler)
		{
			if (element.events && element.events[type] && handler.$$guid)
				delete element.events[type][handler.$$guid];
		}
	}
}();



var news = {
	load : function (data, first) {
		var newsA = data.evalJSON();
		var id = 0;
		var delay = 5000;
		var last = newsA.length;
				
		newsA.each(function(t) {
			id ++ ;
			
			var delay_item = id * delay;
			var rewind = last == id ? true : false;
			
			setTimeout("news.showitem('"+t.slug+"', '"+t.title+"', "+rewind+")", delay_item);
		});
		
		this.data = data;
		
		if (first) {
			this.first(first);
		}
	},
		
	showitem : function (slug, title, rewind) {
		if ( rewind ) {
			this.load(this.data);
		}
		
		new Effect.Fade('newscontainer', { duration : 0.3, afterFinish : function () {
			$('newscontainer').update('<a href="/news/'+slug+'" style="color: #515151; text-decoration: none; opacity:1.0;">'+title+'</a>');
			new Effect.Appear('newscontainer', { duration : 0.3 } );
		} } );
	},
	
	first : function (data) {
		post = data.evalJSON();
		$('newscontainer').update('<a href="/news/'+post.slug+'" style="color: #515151;text-decoration: none; opacity:1.0;">'+post.title+'</a>');
	}
};

