/*
* Cache van javascripts.script.js
* Created: 2011-06-27 17:12:30
*/

/*
$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-6545988-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');
	
	$$('ul#footernav li').each(function(el, i) {
		if (i > 0) {
			new Element('li').set('html', '|').inject(el, 'before');
		}
	});
	$$('ul#supernav li').each(function(el, i) {
		if (i > 0) {
			new Element('li').set('html', '|').inject(el, 'before');
		}
	});
	$$('li.productdetails li').each(function(el, i) {
		if (i > 0) {
			new Element('li').set('html', '|').inject(el, 'before');
		}
	});
	
	$$('div.thumbs').each(function(div) {
		var imglarge = new Element('div').setStyle('width', '3000px').inject($(div.get('id')+'_large'));
		var thumbs = div.getElements('a');
		var imgsfx;
		var imgs;
		function showimage(idx) {
 			imgsfx.start(
 				'margin-left',
 				imglarge.getStyle('margin-left'),
 				-1 * idx * imglarge.getParent('div').getStyle('width').toInt()
 			);
			thumbs.removeClass('current');
			thumbs[idx].addClass('current');
		}
		thumbs.each(function(img, i) {
			new Asset.image(img.get('href')).inject(imglarge);
			img.addEvent('click', function(e) {
				new Event(e).stop();
				showimage(i);
			});
		});
		imgsfx = new Fx.Tween(imglarge, {
			duration: 300,
			transition: Fx.Transitions.Sine.easeInOut
		});
		if (thumbs[0]) {
			showimage(0);
		}
	});

	var imageswitchidx = 0;
	$$('div.imageswitch').each(function(div) {
		var switchidx = imageswitchidx;
		var aimgs = div.getElements('a');
		if (aimgs.length > 0) {
			new Element('img', {
				'id': 'imgswitch_'+imageswitchidx
			}).inject(aimgs[0], 'before');
			
			aimgs.addEvent('click', function(e) {
				if (e) new Event(e).stop();

				$('imgswitch_'+switchidx).set('src', this.get('href'));
				aimgs.removeClass('current');
				this.addClass('current');
			});

			aimgs[0].fireEvent('click');
		}
		imageswitchidx++;
	});
	$$('div.imageswitch-wide').each(function(div) {
		var switchidx = imageswitchidx;
		var aimgs = div.getElements('a');
		if (aimgs.length > 0) {
			new Element('img', {
				'id': 'imgswitch_'+imageswitchidx
			}).inject(div);
			
			aimgs.addEvent('click', function(e) {
				if (e) new Event(e).stop();

				$('imgswitch_'+switchidx).set('src', this.get('href'));
				aimgs.removeClass('current');
				this.addClass('current');
			});

			aimgs[0].fireEvent('click');
		}
		imageswitchidx++;
	});

	/* Menu */
	var menulists = $$('ul#menu > li > ul');
	var menufx = new Fx.Elements(menulists, {
		'link': 'cancel',
		'duration': 500,
		transition: Fx.Transitions.Sine.easeOut
	});
	openmenu = function(e, idx) {
		var obj = {};
		menulists.each(function(el, i) {
			if (i == idx) {
				obj[i] = {
					'height': el.getScrollSize().y
				};
			} else {
				obj[i] = {
					'height': 0
				};
			}
		});
		menufx.start(obj);
	}
	var ulcounter = 0;
	$('menu').getChildren('li').each(function(li) {
		var childul = li.getElement('ul');
		if (childul) {
			childul.setStyles({
				'overflow': 'hidden',
				'position': 'absolute',
				'height': '0'
			});
			li.addEvent('mouseenter', openmenu.bindWithEvent(childul, ulcounter));
			ulcounter++;
		} else {
			li.addEvent('mouseenter', openmenu.bindWithEvent(childul, -1));
		}
	});
	$('menu').addEvent('mouseleave', openmenu.bindWithEvent($('menu'), -1));

	if (false && $('submenu')) {
		var submenulists = $$('ul#submenu > li > ul');
		var submenufx = new Fx.Elements(submenulists, {
			'link': 'cancel',
			'duration': 250,
			transition: Fx.Transitions.Sine.easeOut
		});
		opensubmenu = function(e, idx) {
			var obj = {};
			submenulists.each(function(el, i) {
				if (i == idx) {
					obj[i] = {
						//'width': el.getScrollSize().x
						'width': 200
					};
				} else {
					obj[i] = {
						'width': 0
					};
				}
			});
			submenufx.start(obj);
		}
		var ulcounter = 0;
		$('submenu').getChildren('li').each(function(li) {
			var childul = li.getElement('ul');
			if (childul) {
				childul.setStyles({
					'overflow': 'hidden',
					'position': 'absolute',
					'width': '0'
				});
				li.addEvent('mouseenter', opensubmenu.bindWithEvent(childul, ulcounter));
				ulcounter++;
			} else {
				li.addEvent('mouseenter', opensubmenu.bindWithEvent(childul, -1));
			}
		});
		$('submenu').addEvent('mouseleave', opensubmenu.bindWithEvent($('submenu'), -1));
	}
	/* Menu */

	$$('.expandinglist').each(function(list) {
		var defopen = -1;

		list.getElements('a.toggler').each(function(a, i) {
			if (defopen == i) {
				a.getParent('li').addClass('open');
			}

			a.addEvent('click', function(e) {
				new Event(e).stop();
				if (a.getParent('li').hasClass('open')) {
					list.getElements('li.open').removeClass('open');
				} else {
					list.getElements('li.open').removeClass('open');
					a.getParent('li').addClass('open');
				}
			});

			if (defopen == -1 && document.location.href.substr(document.location.href.indexOf('#')) == a.get('href').substr(a.get('href').indexOf('#'))) {
				defopen = i;
				a.getParent('li').addClass('open');
			}
		});

		new Accordion(list.getElements('a.toggler'), list.getElements('div.details'), {
			'display': defopen,
			'show': defopen,
			'alwaysHide': true
		});
	});

	if ($('breadcrumbtrail')) {
		var split = '....';
		var width = $('breadcrumbtrail').getSize().x;
		$('breadcrumbtrail').setStyle('width', 'auto');
		(function() {
			while (width < $('breadcrumbtrail').getSize().x) {
				// Get biggest element
				var biggestel = null;
				var biggestwidth = 0;
				$$('#breadcrumbtrail a').each(function(el) {
					if (el.getSize().x > biggestwidth) {
						biggestel = el;
						biggestwidth = el.getSize().x;
					}
				});
				var atxt = biggestel.get('text');
				atxt = atxt.replace(split, '');
				var length = (atxt.length / 2);
				atxt = atxt.substring(0, length - split.length) + split + atxt.substring(length + split.length);
				biggestel.set('html', atxt);
			}
		}).delay(100);
	}

});

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

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

	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.slimlinebuildings.com');
			}*/

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

	// Product/principe details
	if ($('productdetail') && $('dl_productdetail')) {
		var pd_timer;
		$('dl_productdetail').setStyle('display', 'none');
		var corr = {x: -20, y: 20};
		if ($('productdetail').hasClass('principedetail')) {
			corr = {x: -5, y: 5};
		}
		new Element('ul', {
			'id': 'productdetails_dl',
			'styles': {
				'display': 'block',
				'opacity': 0
			},
			'events': {
				'mouseenter': function() {
					$clear(pd_timer);
				},
				'mouseleave': function() {
					pd_timer = $('productdetails_dl').fade.delay(500, $('productdetails_dl'), 'out');
				}
			}
		}).inject(document.body);

		$$('#productdetail area').each(function(ar) {
			ar.addEvent('mouseenter', function(e) {
				$clear(pd_timer);
				var pos = ar.retrieve('pos');
				if (!pos) {
					ar.store('pos', e.page);
					pos = ar.retrieve('pos');
				}
				
				$('productdetails_dl').setStyles({
					'top': pos.y + corr.y,
					'left': pos.x + corr.x
				});
				$('productdetails_dl').set('html', $('dl_'+ar.get('id')).get('html'));
				$('productdetails_dl').fade('in');
			});
			ar.addEvent('mouseleave', function() {
				pd_timer = $('productdetails_dl').fade.delay(500, $('productdetails_dl'), 'out');
			});
		});
	}

	$$('.clickrow').each(function(el) {
		el.getParent()
			.setStyle('cursor', 'pointer')
			.addEvent('click', function(e) {
				if (e.target.get('tag') != 'a') {
					e.stop();
					document.location.href = el.get('href');
				}
			});
	});

	$$('.watchover').each(function(el) {
		el.addEvent('mouseenter', function(e) {
			el.addClass('over');
		});
		el.addEvent('mouseleave', function(e) {
			el.removeClass('over');
		});
	});

	new mouseOvers();	
	
});

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');
				var size = img.getSize();
				img.set('src', this.options.transparentImage);
				img.set('width', size.x);
				img.set('height', size.y);
				img.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + oldsrc + "',sizingMethod='scale')";
			}
		}, this);
	}
});


