/*
* Cache van javascripts.script.js
* Created: 2010-06-03 13:49:24
*/

/*
$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 cookieopts = {
	'domain': 'www.domusmagnus.com',
	'path': ''
};

var google = 'UA-7751350-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"));
}

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

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

	new imageFader($$('div.imagerotator img'), {duration: 2500, interval: 10000});
	
	if ($('enlarge')) {
		var ul = new Element('ul', {id: 'enlarge'}).replaces($('enlarge'));
		[0, 1, 2].each(function(size) {
			var li = new Element('li', {'class': 'zoom'+size}).inject(ul);
			new Element('a', {
				'href': 'Zoom',
				'html': 'A'
			}).addEvent('click', function(e) {
				new Event(e).stop();
				if ($('zoomstyle')) $('zoomstyle').dispose();
				new Asset.css('/zoom'+size+'.style.css', {'id': 'zoomstyle'});
				Cookie.write('zoom', size, cookieopts);
				$$('ul#enlarge a.current').removeClass('current');
				this.addClass('current');
			}).inject(li);
		});
		var zoom = Cookie.read('zoom');
		if (zoom && zoom.toInt() > 0 && zoom.toInt() <= 2) {
			new Asset.css('/zoom'+zoom.toInt()+'.style.css', {'id': 'zoomstyle'});
			$$('li.zoom'+zoom.toInt()+' a').addClass('current');
		}
	}

	$$('ul#mainmenu ul').each(function(ul) {
		if (ul.getParent('ul').get('id') == 'mainmenu') {
			var parent = ul.getParent('li');
			parent.setStyle('cursor', 'pointer');
	
			if (parent.hasClass('current')) {
				ul.setStyles({
					'overflow': 'hidden',
					'display': 'block'
				});
			} else {
				ul.setStyles({
					'overflow': 'hidden',
					'display': 'block',
					'height': 0
				});
			}
	
			parent.addEvent('click', function(e) {
				new Event(e).stop();
				var curheight = ul.getSize().y;
				var a = this.getFirst('a');
				if (curheight == 0) {
					ul.tween('height', curheight, ul.getScrollSize().y);
					if (a.get('href')) {
						(function(){document.location.href = a.get('href');}).delay(500);
					}
				} else {
					if (a.get('href')) {
						document.location.href = a.get('href');
					} else {
						ul.tween('height', curheight, 0);
					}
				}
			});
		}
	});
	$$('ul#mainmenu ul a').each(function(a) {
		a.addEvent('click', function(e) {
			new Event(e).stopPropagation();
		});
	});
	$$('ul#mainmenu ul ul').each(function(ul) {
		ul.addEvent('click', function(e) {
			new Event(e).stopPropagation();
		});
	});

	new mouseOvers();

	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');
			
			var downloadsurl = '/downloads/';
			newwin = newwin || el.get('href').substring(0, downloadsurl.length) == downloadsurl;
		}
		if (newwin) {
			e.stop();
			window.open(el.href, '_blank');
		}
	});

	new pngSupport().replace();

	if ($('Map')) {
		$('Map').getElements('area').each(function(area) {
			var a = new Element('a', {
				'styles': {
					'display': 'none'
				},
				'href': area.get('href'),
				'rel': area.get('alt'),
				'title': area.get('title')
			}).inject($('Map'), 'before');
			
			area.addEvent('click', function(e) {
				new Event(e).stop();
				e.target = a;
				a.fireEvent('click', e);
			});
		});
	}

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

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 imageFader = new Class({

	Extends: Fx.Elements,

	options: {
		auto: true,
		interval: 10000
	},

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

  this.elements.setStyle('opacity', 0);

		if (this.elements[0]) {
			//this.elements[0].setStyle('opacity', 1);
			if (this.elements[0].getParent().getStyle('position') != 'absolute' && this.elements[0].getParent().getStyle('position') != 'relative') {
				this.elements[0].getParent().setStyle('position', 'relative');
			}
		}

		this.curel = 0;
		if (this.options.auto) {
			this.auto.periodical(this.options.interval, this);
			this.auto.delay(1000, this);
		}
	},

	auto: function() {
		if (!this.elements[this.curel]) {
			this.curel = 0;
		}
		this.display(this.curel);
		
		this.curel += 1;
	},

	display: function(idx) {
		var obj = {};
		this.elements.each(function(el, i) {
			if (el.getStyle('opacity') != 0 && i != idx) {
				obj[i] = {'opacity': 0};
				el.setStyle('z-index', 1);
			} else if (i == idx) {
				obj[i] = {'opacity': 1};
				el.setStyle('z-index', 2);
			}
		});
		return this.start(obj);
	}
});