/*
* Cache van javascripts.script.js
* Created: 2010-12-09 15:36:28
*/

/*
$Author: sjors $
$LastChangedDate: 2006-10-19 14:05:31 +0200 (do, 19 okt 2006) $
$Id: javascripts.js 28 2006-10-19 12:05:31Z sjors $
$Rev: 28 $
*/

var google = 'UA-11186320-1';
if (google) {
	var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
	document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
}

var shopcolors = ['b51f1f', 'd2c396'];

window.addEvent('domready',function() {

	$$('body').addClass('js');

	new pngSupport().replace();
});

window.addEvent('load',function() {

	if (google && _gat) {
		var pageTracker = _gat._getTracker(google);
		pageTracker._initData();
		pageTracker._trackPageview();
	}

	new imageZoom($$('img.zoom'));

	document.addEvent('click', function(e) {
  	var e = new Event(e);
  	var el = $(e.target)
  	if (el.tagName.toLowerCase() != 'a') {
  		el = el.getParent('a');
  	}
  	var newwin = false;

		if (el && el.get('href')) {
			newwin = newwin || el.hasClass('newwindow');
			newwin = newwin || el.hasClass('externlink');

			if (el.get('href').substring(0, 7) == 'http://' || el.get('href').substring(0, 8) == 'https://') {
				newwin = newwin || el.get('href').indexOf('http://www.winkelcentrumhogevucht.nl');
			}

			var downloadsurl = '/downloads/';
			newwin = newwin || el.get('href').substring(0, downloadsurl.length) == downloadsurl;
		}
		if (newwin) {
			e.stop();
			window.open(el.href, '_blank');
		}
	});

	if ($('shops')) {
		var flash = true;

		if (flash) {
			var shopflash = new Swiff('/images/plattegrond.swf', {
				'width': '300px',
				'height': '510px',
				'container': $('shopflash'),
				'params': {
					'wmode': 'transparent',
					'quality': 'high',
					'allowScriptAccess': 'always'
				}
			}).toElement();
		}

		new scrollBar($('shops'));

		var shops = $$('ul#shops ul li');
		shops.each(function(shop) {
			shop.getFirst('a').addEvent('click', function(e) {
				e.stop();

				if (flash) {
					Swiff.remote(shopflash, 'setcolors', shopcolors[0], shopcolors[1]);
					Swiff.remote(shopflash, 'resetselection');
					Swiff.remote(shopflash, 'selectshop', this.getParent('li').get('id').substring(7));
				}

				new Request.HTML({
					'update': $('shopdetails_cont'),
					'autoCancel': true,
					'onComplete': function() {
						shops.removeClass('current');
						shop.addClass('current');
					}
				}).get(this.get('href')+'/0/ajax');
			});
		});

		if ($('brandsearch')) {
			$('brandsearch').addEvent('submit', function(e) {e.stop()});
			var thereq;
			$('brand').addEvent('keyup', function(e) {
				if (thereq) {
					thereq.cancel();
				}
				if (this.get('value').length == 0) {
					$('brandhint').empty();
					shops.removeClass('current');
					if (flash) {
						Swiff.remote(shopflash, 'resetselection');
					}
					shops.removeClass('brandmatch');
					$('shopdetails_cont').empty();
					if (flash) {
						Swiff.remote(shopflash, 'resetbrandselection');
					}
				} else {
					thereq = new Request.HTML({
						'update': $('brandhint'),
						'autoCancel': true,
						'onComplete': function() {
							$$('div#brandhint a').each(function(el) {
								el.addEvent('click', function(e) {
									e.stop();
									$('brand').set('value', this.get('text'));
									shops.removeClass('current');
									if (flash) {
										Swiff.remote(shopflash, 'resetselection');
									}
									$('shopdetails_cont').empty();
									if (flash) {
										Swiff.remote(shopflash, 'resetbrandselection');
									}

									new Request({
										'autoCancel': true,
										'url': this.get('href')+'/ajax',
										'method': 'get',
										'onSuccess': function(responseText, responseXML) {
											var shoparr = responseText.split(',');
											shops.removeClass('brandmatch');
											shoparr.each(function(shop) {
												if ($('shopid_'+shop)) {
													$('shopid_'+shop).addClass('brandmatch');
													if (flash) {
														Swiff.remote(shopflash, 'setcolors', shopcolors[0], shopcolors[1]);
														Swiff.remote(shopflash, 'selectbrandshop', shop);
													}
												}
											});
										}
									}).send();
								});
							});
						}
					}).get($('brandsearch').get('action')+encodeURIComponent(this.get('value'))+'/0/ajax');
				}
			});
		}
	}
});

var mouseOvers = new Class({
	initialize: function(elements) {
		elements = elements || $$('img.mouseover');
		elements.each(function(image) {
			if (image.hasClass('specialmo')) {
				image.set('src_mouseover', image.get('class').replace(/.*mouseover=/, ''));
				image.set('src_mouseout',  image.get('src'));
				image.addEvent('mouseenter', this.mouseover);
				image.addEvent('mouseleave', this.mouseout);
				new Asset.image(image.get('src_mouseover'));
			} else {
				var xsrc = image.src;
				if (xsrc.substring(xsrc.length-7, xsrc.length-3) == '_up.')	{
					image.set('src_mouseover', image.get('src').replace(/_up\.([a-z]{3})/, '_ov.$1'));
					image.set('src_mouseout',  image.get('src'));

					image.addEvent('mouseenter', this.mouseover);
					image.addEvent('mouseleave', this.mouseout);

					// Preload
					new Asset.image(image.get('src_mouseover'));
				}
			}
		}, this);
	},
	mouseover: function() {
		this.set('src', this.get('src_mouseover'));
	},
	mouseout: function()	{
		this.set('src', this.get('src_mouseout'));
	}
});

var pngSupport = new Class({

	Implements: [Options],

	options: {
		transparentImage: '/images/pnghack/transparent.gif'
	},

	initialize: function(options) {
		this.setOptions(options);
		this.needed = Browser.Engine.trident4 && Browser.Platform.win;
	},

	replace: function(parentel) {
		if (!this.needed) return;
		if (!parentel) parentel = document;

		parentel.getElements('img').each(function(img) {
			if (img.src.indexOf(this.options.transparentImage) == -1 && img.src.indexOf("png") >= 0) {
				var oldsrc = img.get('src');
				img.set('src', this.options.transparentImage);
				img.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + oldsrc + "',sizingMethod='scale')";
			}
		}, this);
	}
});

var overlayBg = new Class({

	Implements: [Options],

	options: {
		'id': 'bodyoverlay',
		'opacity': 0.8
	},

	initialize: function(options) {

		var windowscroll = window.getScrollSize();
		this.setOptions(options);

		if (!$(this.options.id)) {
			new Element('div', {
				'id': this.options.id,
				'styles': {
					'opacity': 0,
					'width': windowscroll.x+'px',
					'height': windowscroll.y+'px'
				}
			})
				.injectInside(document.body);
		}

		this.fx = new Fx.Tween($(this.options.id), {duration: 1000});
		//$(this.options.id).addEvent('click', this.hide.bindWithEvent(this));
	},

	show: function() {
		var windowscroll = window.getScrollSize();
		$(this.options.id).setStyles({
			'width': windowscroll.x+'px',
			'height': windowscroll.y+'px'
		});
		this.fx.start('opacity', this.options.opacity);
	},

	hide: function() {
		this.fx.start('opacity', 0);
	}
});

var imageZoom = new Class({

	Implements: [Options],

	options: {
		zoomdiv: 'zoomdiv',
		padding: [15, 10, 15, 10],
		textheight: [15, 15]
	},

	initialize: function(images, options) {
		this.elements = images;
		if (this.elements.length == 0 || !$(this.options.zoomdiv)) {
			return;
		}
		this.setOptions(options);

		this.elements.each(function(thumb, i) {
			var a = thumb.getParent('a');
			a.addEvent('click', this.showImage.bindWithEvent(this, i));
		}, this);

		var windowscroll = window.getScrollSize();

		this.overlay = new overlayBg();

		$(this.options.zoomdiv)
			.setStyles({
				'display': 'block',
				'top': windowscroll.y / 2,
				'left': windowscroll.x / 2
			})
			.store('fx',  new Fx.Morph($(this.options.zoomdiv), {duration: 1000, wait: false}))
			.store('fx2', new Fx.Morph($(this.options.zoomdiv), {duration: 1000, wait: false}));

		var me = this;
		$(this.options.zoomdiv).getElement('.close').addEvent('click', function(e) {
			e.stop();
			$(me.options.zoomdiv).retrieve('fx2').start({
				'top': [$(me.options.zoomdiv).getTop(), windowscroll.y / 2],
 				'height': [me.previmgsize.y+'px', 0]
			});
			$(me.options.zoomdiv).retrieve('fx').start.delay(600, $(me.options.zoomdiv).retrieve('fx'), {
				'width': [me.previmgsize.x+'px', 0],
		 		'padding': 0,
		 		'left': [$(me.options.zoomdiv).getLeft(), windowscroll.x / 2]
			});
			me.previmgsize = {x: 0, y: 0};
			me.overlay.hide.delay(1200, me.overlay);
			$(me.options.zoomdiv+'_img').getElement('img').dispose.delay(1200, $(me.options.zoomdiv+'_img').getElement('img'));
			$(me.options.zoomdiv).getElement('.close')   .setStyle('visibility', 'hidden');
			$(me.options.zoomdiv).getElement('.next')    .setStyle('visibility', 'hidden');
			$(me.options.zoomdiv).getElement('.previous').setStyle('visibility', 'hidden');
		});
		$(this.options.zoomdiv).getElement('.next').addEvent('click', function(e) {
			e.stop();
			if (me.elements[me.curimg+1]) {
				me.showImage(e, me.curimg+1);
			}
		});
		$(this.options.zoomdiv).getElement('.previous').addEvent('click', function(e) {
			e.stop();
			if (me.elements[me.curimg-1]) {
				me.showImage(e, me.curimg-1);
			}
		});
	},

	showImage: function(e, idx) {
		if (e && e.target) e.stop();

		var windowscroll = window.getScrollSize();

		this.overlay.show();

		if (!this.previmgsize) {
			this.previmgsize = {x: 0, y: 0};
		}

		var removeimg = $(this.options.zoomdiv+'_img').getElement('img');
		if (removeimg) {
			removeimg.setStyle('z-index', 2);
		}

		this.curimg = idx;

		var me = this;
		var images = new Asset.images([this.elements[idx].getParent('a').getProperty('href')], {onComplete: function() {
			var image = images[0];
			image.setStyles({
				'opacity': 0,
				'z-index': 3
			});
			image.injectInside($(me.options.zoomdiv+'_img'));
			new Fx.Tween(image, {onComplete: function(){if(removeimg) {removeimg.dispose()}}}).start('opacity', 1);
			var imgsize = image.getSize();
			imgsize = {x: imgsize.x, y: imgsize.y+me.options.textheight[0]+me.options.textheight[1]};

			var paddingtb = me.options.padding[0] + me.options.padding[2];
			var paddinglr = me.options.padding[1] + me.options.padding[3];

			$(me.options.zoomdiv).getElement('.close').setStyle('visibility',    'visible');
			$(me.options.zoomdiv).getElement('.next').setStyle('visibility',     me.elements[idx+1]?'visible':'hidden');
			$(me.options.zoomdiv).getElement('.previous').setStyle('visibility', me.elements[idx-1]?'visible':'hidden');
			$(me.options.zoomdiv).getElement('.info').set('html', me.elements[idx].get('alt'));

			var newtop  = (windowscroll.y - imgsize.y - paddingtb - me.options.textheight[0] - me.options.textheight[1]) / 2;
			var newleft = (windowscroll.x - imgsize.x - paddinglr) / 2;
			if (newtop  < 0) newtop  = 0;
			if (newleft < 0) newleft = 0;

			$(me.options.zoomdiv).retrieve('fx').start({
				'left': [$(me.options.zoomdiv).getLeft(), newleft],
				'width': [me.previmgsize.x+'px', imgsize.x+'px'],
				'padding-top': [$(me.options.zoomdiv).getStyle('padding-top').toInt(), me.options.padding[0]],
				'padding-right': [$(me.options.zoomdiv).getStyle('padding-right').toInt(), me.options.padding[1]],
				'padding-bottom': [$(me.options.zoomdiv).getStyle('padding-bottom').toInt(), me.options.padding[2]],
				'padding-left': [$(me.options.zoomdiv).getStyle('padding-left').toInt(), me.options.padding[3]]
			});
			$(me.options.zoomdiv).retrieve('fx2').start.delay(800, $(me.options.zoomdiv).retrieve('fx2'), {
				'top': [$(me.options.zoomdiv).getTop(), newtop],
				'height': [me.previmgsize.y+'px', imgsize.y+'px']
			});
			me.previmgsize = imgsize;
		}});
	}
});

var scrollBar = new Class({
	
	Implements: [Options],

	options: {
		orientation: 'vertical',
		ignoreMouse: false,
		classes: {
			horizontal: {
				bar: 'scrollbar-hor',
				handle: 'handle-hor'
			},
			vertical: {
				bar: 'scrollbar-vert',
				handle: 'handle-vert'
			}
		}
	},

	initialize: function(element, options) {
		this.element = element;
		this.setOptions(options);

		if (this.options.orientation == 'horizontal') {
			this.steps = this.element.getScrollSize().x - this.element.getSize().x;
		} else {
			this.options.orientation = 'vertical';
			this.steps = this.element.getScrollSize().y - this.element.getSize().y;
		}

		var relativeElement = this.element.getParent();
		while (relativeElement.getStyle('position') != 'absolute' && relativeElement.getStyle('position') != 'relative') {
			if (relativeElement == $(document.body)) {
				break;
			}
			relativeElement = relativeElement.getParent();
		}

		var scrollbar = new Element('div', {
			'class': this.options.classes[this.options.orientation].bar,
			'styles': {
				'position': 'absolute'
			}
		}).inject(this.element, 'after');
		var handle = new Element('div', {
			'class': this.options.classes[this.options.orientation].handle
		}).inject(scrollbar);

		if (this.options.orientation == 'horizontal') {
			scrollbar.setStyles({
				'width': this.element.getSize().x,
				'top': this.element.getPosition(relativeElement).y + this.element.getSize().y,
				'left': this.element.getPosition(relativeElement).x
			});
		} else {
			scrollbar.setStyles({
				'height': this.element.getSize().y,
				'top': this.element.getPosition(relativeElement).y,
				'left': this.element.getPosition(relativeElement).x + this.element.getSize().x
			});
		}

		var me = this;
		this.slider = new Slider(scrollbar, handle, {
			steps: me.steps,
			mode: this.options.orientation,
			onChange: function(step) {
				var x = (me.options.orientation == 'horizontal' ? step : 0);
				var y = (me.options.orientation == 'horizontal' ? 0 : step);
				me.element.scrollTo(x,y);
			}
		}).set(0);

		if(!this.options.ignoreMouse) {
			// Scroll the content element when the mousewheel is used within the
			// content or the scrollbar element.
			$$(this.element, scrollbar).addEvent('mousewheel', function(e) {
				e.stop();
				var step = me.slider.step - e.wheel * 30;
				me.slider.set(step);
			});
		}
		// Stops the handle dragging process when the mouse leaves the document body.
		$(document.body).addEvent('mouseleave', function() {
			me.slider.drag.stop()
		});
	}

});

