(function() { 
/*************************************************************/
Behaviour.register({
	'div.breadcrumbs': function(el) {
		var crumbs = el.getElementsBySelector('span');
		for(var i = 0; i < crumbs.length; i++) {
			if (i+1 >= crumbs.length) continue;
			new Insertion.After(crumbs[i], ' > ');
		}
	},
	'input[type=image]': function(el) {
		el.observe('focus', function() { el.blur() });
	},
	'a': function(el) {
		el.observe('focus', function() { el.blur() });
	},
	'input': function(el) {
		var type = el.type;
		switch(type) {
			case 'text':
			case 'password':
				el.addClassName('text');
				break;
			default:
				el.addClassName(type);
				break;
		}
	},
	'div.checkout #cd_same': function(el) {
		
		var delivery = el.up('.checkout').down('.delivery');
		var note	= delivery.down('span.cd_same');
		var fields	= delivery.down('.form');

		var showHide = function() {
			if (!el.checked) {
				console.log('not checked');
				note.hide();
				fields.show();
			} else {
				note.show();
				fields.hide();
			}
		}

		showHide();

		el.observe('click', showHide);
	},
	'div.click_div': function(el) {
		var a = el.down('a');
		if (a) {
			el.style.cursor = 'pointer';
			el.onclick = function() {
				document.location.href = a.href;
			}
			el.title = a.innerHTML;
			a.hide();
		}
	},
	'.navlist2 a': function(el) {
		el.observe('mouseover', function() {
			el.up('li').addClassName('active-top');
		});
		el.observe('mouseout', function() {
			el.up('li').removeClassName('active-top');
		});
	},
	'div.menu-main ul li a': function(el) {
		setHover(el.up('li'), 'hover');
	},
	'div.content_list div.blocks': function(el) {

		var totalWidth 	= 0;
		var maxWidth	= el.getWidth() - 25;
		debug("MAX: " + maxWidth);
		el.getElementsBySelector('div.block').each(function(b) {
			totalWidth += (b.getWidth() + 6);
			debug(totalWidth);
			if (totalWidth >= maxWidth) {
				new Insertion.After(b, '<div style="clear: both">&nbsp;</div>');
				totalWidth = 0;
			}
		});
	},
	'.threestate': function(el) {
		setThreeStateButton(el);
	},
	'.cart input.quantity': function(el) {
		new Form.Element.Observer(el, 0.2, function() {
			el.up('td').down('.save').show();
		});
		el.observe('focus', function() {
			el.up('td').addClassName('focus');
		});

	},
	'.overzicht-winkelwagen form.remove': function(el) {
		el.onsubmit = function() {
			new Ajax.Request(el.action, {
				method: 'post',
				parameters: el.serialize(true),
				onComplete: function(req) {
					var info = req.responseText.evalJSON();
					if (info.status) {
						updateCartInfo(info);
						el.up('.overzicht-winkelwagen').down('.total-price').update('&euro; ' + info.content.totals.price);
						new Effect.Fade(el.up('tr'), { 
							duration: 0.5,
							afterFinish: function() {
								if (info.content.totals.quantity == 0) document.location.reload();
							}
						});
					} else {
						alert(info.content);
					}

				}
			});

			return false;
		}
	},
	'.overzicht-winkelwagen .quantity form': function(el) {
		el.onsubmit = function() {

			new Ajax.Request(el.action, {
				method: 'post',
				parameters: el.serialize(true),
				onComplete: function(req) {
					var info = req.responseText.evalJSON();
					if (info.status) {
						try {
							el.enable();

							updateCartInfo(info);

							el.up('.quantity').down('.number').update(info.content.added.quantity);
							el.up('tr').down('td.subtotal').update('&euro; ' + info.content.added.price);
							el.up('.overzicht-winkelwagen').down('.total-price').update('&euro; ' + info.content.totals.price);

							if (new Number(info.content.added.quantity) == 0) {
								el.up('.quantity').down('form.subtract').disable();
							} else {
								el.up('.quantity').down('form.subtract').enable();
							}
						} catch(e) {
							debug(e);
						}
					} else {
						alert(info.content);
					};

				}
			});
			el.disable();
			return false;
		}
	},
	'.cart input.save': function(el) {
		el.observe('click', function() {
			var qField = el.up('td').down('input.quantity');
			new Ajax.Request('/cart/set_quantity', {
				method: 'post',
				parameters: {
					Article: qField.id.replace('quantity_', ''),
					Quantity: qField.value
				},
				onComplete: function(req) {
					var info = req.responseText.evalJSON();
					$$('div.ww-tekst').shift().update('Uw winkelwagen bevat ' + info.content.totals.quantity + ' artikelen voor &euro; ' + info.content.totals.price + '.');
					$$('table.cart tr.totals td.quantity').shift().update(info.content.totals.quantity);
					$$('table.cart tr.totals td.price').shift().update('&euro; ' + info.content.totals.price);
					el.up('tr').down('.unit_price').update('&euro; ' + info.content.added.unit_price);
					el.up('tr').down('.total_price').update('&euro; ' + info.content.added.price);
					el.hide();
				}
			});
		});
	},
	'div.article': function(el) {
		setHover(el, 'article_hover', 'img.poster');
	},
	'div.active_cat': function(el) {
		el.classNames().each(function(c) { 
			var matches;
			if(matches = c.match(/^active_cat-(\d+)$/)) {

				var cat = $('category_' + matches[1]);
				if (cat) {
					cat.down('.title').addClassName('active');
					var i = 0;
					var child;
					while(child = cat.up('li', i++)) {
						child.down('.title').addClassName('active_parent');
					}
				};
			}
		});
	},
	'div.article .contents': function(el) {
		var quantity 	= new Number(el.innerHTML);
		var holder		= el.up('.quantity');
		
		setTimeout(function() {
			updateArticleQuantity(holder, quantity);
		}, 10);

	},
	'form.article_form': function(el) {

		el.onsubmit = function() {

			new Ajax.Request(el.action, {
				method: 'post',
				parameters: el.serialize(true),
				onComplete: function(req) {
					var info = req.responseText.evalJSON();
					if (info.status) {
						try {
							el.enable();
							el.reset();
							updateCartInfo(info);
							var holder = el.up('.article_info').down('.quantity');
							updateArticleQuantity(holder, info.content.added.quantity);
						} catch(e) {
						}
					} else {
					};

				}
			});

			return false;
		};

	},
    '#nieuwsbrief form': function(el) {

        var inp = el.down('input[name=email]');
        if (inp.value == "") {
            inp.value = 'Uw email adres hier';
            inp.addClassName('placeholder');
            inp.onfocus = function() {
                inp.removeClassName('placeholder');
                inp.value   = '';
                inp.onfocus = null;
            }
        }

        el.onsubmit = function() {

            var para = el.serialize(true);
            el.disable();
            new Ajax.Request(el.action, {
                method: el.method,
                parameters: para,
                onComplete: function(req) {
                    var info = eval('(' + req.responseText + ')');
                    if (info.status) {
                        el.up('#nieuwsbrief').update(info.content);
                        $('nieuwsbrief').style.backgroundImage = 'none';
                    } else {
                        el.enable();
                        inp.focus();
                        alert(info.content);
                    }
                }
            });

            return false;
        }
    },
	'div.article div.stock_image': function(el) {

		setTimeout(function() {
		var img = el.getStyle('background-image');
		if (/MSIE/.test(navigator.userAgent) && !window.opera) {

			el.style.backgroundPosition = '-900px';
			img 	= img.replace(/(url\("?|"?\))/g, '');
			var style 	= "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+img+"',sizingMethod='crop');";
			el.style.filter = style;

		}}, 10);

	}

});
function updateCartInfo(info) {
	try {
		$$('div.ww-tekst').shift().update('Producten: ' + info.content.totals.quantity + '<br />Subtotaal: &euro; ' + info.content.totals.price + 
					'<br />Verzendkosten: &euro; ' + info.content.totals.shipping + '<br />' + 
					'--------------------<br />' + 
					'Totaal: &euro; ' + info.content.totals.total);

	} catch(e) {};
}
function setThreeStateButton(el) {
	
	var origImage 	= el.src;
	var hover		= el.src.replace(/.jpg$/, '_over.jpg');
	var down		= el.src.replace(/.jpg$/, '_down.jpg');

	el.observe('mouseover', function() {
		el.src = hover;
	});
	el.observe('mouseout', function() {
		el.src = origImage;
	});
	el.observe('mousedown', function() {
		el.src = down;
	});
	el.observe('mouseup', function() {
		el.src = hover;
	});

}
function setHover(el, className, target) {
	var t = target ? $(el).down(target) : $(el);
	if (!t) return false;

	$(el).observe('mouseover', function() {
		t.addClassName(className);
	});
	$(el).observe('mouseout', function() {
		t.removeClassName(className);
	});
}
var debug = function() {
	// do nothing
}
if (typeof(console) != "undefined") {
	debug = function(str) {
		console.log(str);
	};
}
function updateArticleQuantity(holder, quantity) {
	try {
		if (quantity <= 9) quantity = "0" + quantity;
		var img = holder.getStyle('background-image').replace(/\d+/, quantity);
		holder.style.backgroundImage = img;

		if (/MSIE/.test(navigator.userAgent) && !window.opera) {

			holder.style.backgroundPosition = '-900px';
			var img 	= img.replace(/(url\("?|"?\))/g, '');
			var style 	= "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+img+"',sizingMethod='crop');";
			holder.style.filter = style;

		}
	} catch(e) {
		alert(e.message);
	}


	/*  */
}
/*************************************************************/
})();

function refreshStylesheets(loop) {
	var rnd = Math.round(Math.random()*500);
	$$('link').each(function(l) {
		try {
			l.href = l.href.replace(/\?.*$/, '') + '?rnd = ' + rnd;
		} catch(e) { };
	});

	if (loop > 1000) {
		setInterval(refreshStylesheets, loop);
	}
};
