String.extend({
	parseUri: function(){
		var bits = this.match(/^(?:([^:\/?#.]+):)?(?:\/\/)?(([^:\/?#]*)(?::(\d*))?)((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[\?#]|$)))*\/?)?([^?#\/]*))?(?:\?([^#]*))?(?:#(.*))?/);
		return (bits)
			? bits.associate(['uri', 'scheme', 'authority', 'domain', 'port', 'path', 'directory', 'file', 'query', 'fragment'])
			: null;
	}
});

Element.extend({
	getBackgroundColor: function() {
		var bg;
		var el = this;
		do {
			bg = el.getStyle("backgroundColor");
			el = el.getParent();
		}
		while(bg == "transparent");
		return bg;
	}
});
var SWE_DISH = {
	adjustToViewport: function() {
		if(window.ie6) {
			var window_height = window.getScrollHeight();
			var document_height = $('wrapper').getSize().scrollSize.y;
			if(document_height > window_height) {
				$('outer-left').setStyle('height',document_height+'px');
				$('outer-right').setStyle('height',document_height+'px');
			}
		}
		var viewport_height = window.getHeight();
		$(document.body).setStyle('minHeight', viewport_height+'px');
		$('outer-left').setStyle('minHeight', viewport_height+'px');
		$('outer-right').setStyle('minHeight', viewport_height+'px');
	},
	adjustMeasurements: function() {
		if(window.ie6) {
			$$('.rounded').each(function(el) {
				var offsets = {
					top: el.getStyle('paddingTop').toInt() + el.getStyle('borderTop').toInt(),
					bottom: el.getStyle('paddingBottom').toInt() + el.getStyle('borderBottom').toInt(),
					left: el.getStyle('paddingLeft').toInt() + el.getStyle('borderLeft').toInt(),
					right: el.getStyle('paddingRight').toInt() + el.getStyle('borderRight').toInt()
				};
				var height = el.getSize().size.y;
				var h = height-offsets.top-offsets.bottom;
				if(h%2 != 0) {
					h++;
				}
				el.setStyle('height',h+'px');
				
				var width = el.getSize().size.x;
				var w = width-offsets.left-offsets.right;
				if(w%2 != 0) {
					w--;
				}
				el.setStyle('width',w+'px');
			});
		}
	},
	adjustHeights: function() {
		var content = $('content');
		var right_bar = $('right-bar');
		if(!$(document.body.hasClass('news')) && content && right_bar) {
			var content_height = content.getSize().size.y;// - content.getStyle('paddingTop').toInt() - content.getStyle('paddingBottom').toInt() - content.getStyle('borderTop').toInt() - content.getStyle('borderBottom').toInt();
			var right_bar_height = right_bar.getSize().size.y;// - right_bar.getStyle('paddingTop').toInt() - right_bar.getStyle('paddingBottom').toInt() - right_bar.getStyle('borderTop').toInt() - right_bar.getStyle('borderBottom').toInt();
			var difference = content_height - right_bar_height;
			if(difference <= 20) {
				var h = (right_bar_height + 20 - content.getStyle('paddingTop').toInt() - content.getStyle('paddingBottom').toInt() - content.getStyle('borderTop').toInt() - content.getStyle('borderBottom').toInt());
				if(window.ie6) {
					if(h%2 != 0) {
						h++;
					}
					content.setStyle('height', h+'px');
				}
				else {
					content.setStyle('minHeight', h+'px');
				}
			}
		}
	},
	adjustSubmenu: function() {
		var submenu = $('submenu');
		submenu.setStyle('height','auto');
		var height = submenu.getSize().size.y;
		var offsets = {
			top: submenu.getStyle('paddingTop').toInt() + submenu.getStyle('borderTop').toInt(),
			bottom: submenu.getStyle('paddingBottom').toInt() + submenu.getStyle('borderBottom').toInt()
		};
		var h = height-offsets.top-offsets.bottom;
		if(h%2 != 0) {
			h++;
		}
		submenu.setStyle('height',h+"px");
	}
};

function setupFlashSB() {
	var lbc = $('lbCenter');
	if(lbc) {
		$('lbCenter').adopt(new Element('div', {
			id: 'lbFlash'
		}));
		return true;
	}
	setTimeout(setupFlashSB, 50);
	return false;
}

window.addEvent('resize', SWE_DISH.adjustToViewport);
window.addEvent("domready", function() {
	setupFlashSB();
	$$('a[href]').each(function(a) {
		var filetypes = /\.(pdf|doc|docx|pot|ppt|pptx|xls|xlsx)$/;
		
		//if(a.hasClass('external') || !/swe-dish\.(com|se)/.test(a.href) || filetypes.test(a.href)) {
		if(a.hasClass('external') || filetypes.test(a.href)) {
			a.addEvent('click', function(e) {
				new Event(e).stop();
				window.open(this.href);
			});
		}
		else {
			if(a.hasClass('isFlashContent') && /\.swf$/.test(a.href)) {
				a.addEvent('click', function(e) {
					new Event(e).stop();
					var h = this.className.match(/h([0-9]+)/)[1];
					var w = this.className.match(/w([0-9]+)/)[1];
					swfobject.embedSWF(this.href, 'lbFlash', w, h, '9.0.0');
					Lightbox.show();
					Lightbox.center.effects($extend({
						duration: Lightbox.options.resizeDuration,
						onComplete: function() {
							$('lbFlash').style.visibility = "visible";
						}
					}, Lightbox.options.resizeTransition ? {transition: Lightbox.options.resizeTransition} : {})).start({
						height: h,
						width: w,
						marginLeft: -w/2
					});
				});
			}
		}
	});
	
	SWE_DISH.adjustToViewport();
	
	//Rounded corners
	$$('.rounded').each(function(el) {
		//var corners = ['tl','tr','bl','br'];
		var corners = el.className.split(" ").filter(function(item) {
			return item == "tl" || item == "tr" || item == "bl" || item == "br";
		});
		if(corners.length < 1) {
			corners = ['tl','tr','bl','br']; 
		}
		for(var i=0,n=corners.length;i<n;i++) {
			el.adopt(new Element('span', {
				'class': 'corner '+corners[i]
			}));
		}
	});
	
	if($('submenu')) {
		var url = window.location.pathname;
		var current_a = $$('div#submenu a[href='+url+']').getLast();
		if(current_a) {
			var li = current_a.getParent();
			while(li.getTag().toLowerCase() != 'div') {
				if(li.getTag().toLowerCase() == 'li') {
					li.getElement('a').addClass('parent-here');
				}
				li = li.getParent();
			}
		}
		
		$$('div#submenu ul li a').each(function(a) {
			var ul = a.getNext();
			var li = a.getParent();
			if(ul && ul.getTag().toLowerCase() == "ul") {
				var child = ul.getElement('a[href='+a.getProperty('href')+']');
				if(child || li.hasClass('toggleable')) {
					a.addEvent('click', function(e) {
						new Event(e).stop();
						this.getNext().toggleClass('remove');
						this.getParent().toggleClass('hasChild');
						if(window.ie6) {
							SWE_DISH.adjustSubmenu();
						}
					});
				}
				if(current_a) {
					if((a == current_a && !li.hasClass('level1')) || ul.hasChild(current_a)) {
						li.addClass('hasChild');
					}
					else if(a != current_a && !ul.hasChild(current_a)) {
						ul.addClass('remove');
					}
				}
			}
			else if(!li.hasClass('level1')) {
				li.addClass('noArrow');
			}
		});
	}

	//sifr
	if(typeof sIFR == "function"){
		
		if($('solutions')) {
			if($$('div#solutions h2 span.bold').length > 0) {
				sIFR.replaceElement('div#solutions h2 span.bold', named({
					sFlashSrc: '/akzidenzbold.swf',
					sColor: $('solutions').getElement('h2 span.bold').getStyle('color'),
					sBgColor: $('solutions').getElement('h2 span.bold').getBackgroundColor(),
					sFlashVars: 'textalign=center&offsetLeft=-15', 
					sWmode: 'transparent'
				}));
			}
			
			if($$('div#solutions h2 span.light').length > 0) {
				sIFR.replaceElement('div#solutions h2 span.light', named({
					sFlashSrc: '/akzidenzlight.swf',
					sColor: $('solutions').getElement('h2 span.light').getStyle('color'),
					sBgColor: $('solutions').getElement('h2 span.light').getBackgroundColor(),
					sFlashVars: 'textalign=center',
					nPaddingTop: (window.ie)?1:0,
					nPaddingBottom: (window.ie)?1:0,
					sCase: 'upper',
					sWmode: 'transparent'
				}));
			}
			
			if($$('div#solutions div.slide p').length > 0) {
				sIFR.replaceElement('div#solutions div.slide p', named({
					sFlashSrc: '/akzidenzmedium.swf',
					sColor: $('solutions').getElement('div.slide p').getStyle('color'),
					sBgColor: $('solutions').getElement('div.slide p').getBackgroundColor(),
					nPaddingRight: 9,
					nPaddingLeft: 9,
					sWmode: 'transparent'
				}));
			}
			
			if(window.ie6) {
				$('solutions').getElements('div.solution').addEvent('mouseenter',function() {
					this.getElement('.slide').setStyle('visibility','visible');
				}).addEvent('mouseleave', function() {
					this.getElement('.slide').setStyle('visibility','hidden')
				});
			}
		}
		if($('sections')) {
			var solutions = ['government','commercial'];
			for(var i=0,n=solutions.length;i<n;i++) {
				sIFR.replaceElement('li#'+solutions[i], named({
					sFlashSrc: '/akzidenzbold.swf',
					sColor: $('sections').getElement('li#'+solutions[i]).getStyle('color'),
					sBgColor: $('sections').getElement('li#'+solutions[i]).getBackgroundColor(),
					sFlashVars: 'textalign=center&offsetLeft=8',
					nPaddingTop: 1,
					nPaddingRight: 10,
					nPaddingBottom: 0,
					sWmode: 'transparent'
				}));
			}
		}
		
		if($$('div#content h1').length > 0 && !$(document.body).hasClass('news')) {
			sIFR.replaceElement('div#content h1', named({
				sFlashSrc: '/eurostile.swf',
				sColor: $('content').getElement('h1').getStyle('color'),
				sBgColor: $('content').getElement('h1').getBackgroundColor(),
				nPaddingLeft: 13,
				nPaddingRight: (window.ie6)?30:7,
				nPaddingBottom: -2,
				sWmode: 'transparent'
			}));
		}
		
		if($$('div#right-bar-inner h2').length > 0) {
		 	sIFR.replaceElement('div#right-bar-inner h2', named({
				sFlashSrc: '/eurostile.swf',
				sColor: $('right-bar-inner').getElement('h2').getStyle('color'),
				sBgColor: $('right-bar-inner').getElement('h2').getBackgroundColor(),
				nPaddingRight: 13,
				nPaddingLeft: 15,
				nPaddingBottom: (window.ie)?3:0,
				sFlashVars: 'offsetTop=2',
				sCase: 'upper',
				sWmode: 'transparent'
			}));
		}
		
		if($(document.body).hasClass('event')) {
			if($$('div#right-bar h2').length > 0) {
				sIFR.replaceElement('div#right-bar h2', named({
					sFlashSrc: '/eurostile.swf',
					sColor: $('right-bar').getElement('h2').getStyle('color'),
					sBgColor: $('right-bar').getElement('h2').getBackgroundColor(),
					nPaddingTop: 0,
					nPaddingRight: 13,
					nPaddingBottom: 0,
					nPaddingLeft: 13,
					sFlashVars: 'offsetTop=2',
					sCase: 'upper',
					sWmode: 'transparent'
				}));
			}
		}
		
		if($(document.body).hasClass('product')) {
			if($$('div#right-bar h2').length > 0) {
				sIFR.replaceElement('div#right-bar h2', named({
					sFlashSrc: '/eurostile.swf',
					sColor: $('right-bar').getElement('h2').getStyle('color'),
					sBgColor: $('right-bar').getElement('h2').getBackgroundColor(),
					nPaddingTop: 9,
					nPaddingRight: 7,
					nPaddingBottom: 10,
					nPaddingLeft: 7,
					sFlashVars: 'offsetTop=2',
					sCase: 'upper',
					sWmode: 'transparent'
				}));
			}
			if($$('div.download h2').length > 0) {
				sIFR.replaceElement('div.download h2', named({
					sFlashSrc: '/eurostile.swf',
					sColor: $('content').getElement('div.download h2').getStyle('color'),
					sBgColor: $('content').getElement('div.download h2').getBackgroundColor(),
					sCase: 'upper',
					sWmode: 'transparent'
				}));
			}
		}
		
		
		if($$('ul#menu li span').length > 0) {
			sIFR.replaceElement('ul#menu li span', named({
				sFlashSrc: '/akzidenz.swf',
				sColor: $('menu').getElement('li span').getStyle('color'),
				sBgColor: $('menu').getElement('li span').getBackgroundColor(),
				nPaddingTop: 8,
				nPaddingRight: (window.ie)?19:17,
				nPaddingBottom: (window.ie)?6:7,
				nPaddingLeft: 17,
				sCase: 'upper',
				sWmode: 'transparent'
			}));
		}
	};	
	
		$('menu').getElements('div').each(function(div) {
			div.getParent().addEvent('mouseenter', function() {
				this.getElement('div').setStyle('visibility','visible');
			}).addEvent('mouseleave', function() {
				this.getElement('div').setStyle('visibility','hidden');
			});
		});
	
	
	if($('locations')) {
		$('locations').getElements('area').each(function(area) {
			var image = $('worldmap'+area.id.replace('locations',''));
			if(!image) {
				return;
			}
			image.addEvent('mouseout', function() {
				$$('.worldmap-overlay').setStyle('visibility','hidden');
				$('worldmap-'+document.body.className).setStyle('visibility','visible');
			}).addEvent('click', function(e) {
				new Event(e).stop();
				var office = this.id.replace('worldmap-','');
				new Ajax(window.location.href, {
					data: {
						office: office
					},
					update: $('contact-information'),
					onComplete: function() {
						document.body.className = office;
					}
				}).request();
			});
			area.addEvent('mouseover', function() {
				$$('.worldmap-overlay').setStyle('visibility','hidden');
				$('worldmap'+this.id.replace('locations','')).setStyle('visibility','visible');
			});
		})
	}
	
	try {
		$('content').getElements('.view-slideshow').setStyle('display','none')[0].setStyles({
			'position':'absolute',
			'display':'block'
		}).getElement('a').setHTML('View slideshow');
	}catch(e){}
	/*if(typeof Lightbox != undefined && $('product-scroll') && $('product-scroll').getElement('ul') && !$(document.body).hasClass('communicase')) {
		$('content').adopt(new Element('p', {
			'class':'view-slideshow'
		}).adopt(new Element('a', {
			href:'#',
			events: {
				'click': function(e) {
					new Event(e).stop();
					Lightbox.open($$('div#product-scroll ul li img').map(function(img) {
						return [img.getProperty("src"), img.getProperty("title")];
					}),0);
				}
			}
		}).setHTML('View slideshow')));
	}*/
	
	if($('big-puff') && $('big-puff').getElement('img')) {
		carousel.reset();
		carousel.go();
	}
	else if($('big-piff')) {
		$('big-puff').setStyle('backgroundImage','none');
	}
	
	SWE_DISH.adjustHeights();
	SWE_DISH.adjustMeasurements();
});

var carousel = {
	max: 0,
	getParts: function(url,name) {
		var matches = url.match(/([0-9]+)\.jpg/);
		var image = parseInt(matches[1],10);
		var next = image + 1;
		var basedir = url.replace(matches[0],"");
		var obj = {
			image: image,
			next: next,
			basedir: basedir
		}
		if(name) {
			return obj[name];
		}
		return obj;
	},
	
	makeImage: function(nr,src) {
		var err = function() {
			errorTimer = $clear(errorTimer);
			carousel.max = nr;
			this.remove();
			carousel.first();
		};
		var err2 = function() {
			img.removeEvents();
			err.attempt('',img);
		};
		var img = new Element('img', {
			id: 'big-puff-'+nr,
			src: src,
			'class':'active'
		}).setStyles({
			opacity: 0.01 //Opera bugg with 0, load-event not fired
		}).addEvent('load', function() {
			errorTimer = $clear(errorTimer);
			this.effect('opacity', {
				onComplete: carousel.go
			}).start(0,1);
		}).addEvent('error', err);
		
		var errorTimer = err2.delay(7000);
		
		$('big-puff').adopt(img);
		return img;
	},
	
	first: function() {
		$('big-puff').getElement('img').addClass('active').effect('opacity', {
			onComplete: carousel.go
		}).start(0,1);
	},
	
	reset: function() {
		$$('div#big-puff img.active').removeClass('active');
		$('big-puff').getElement('img').addClass('active');
	},
	
	go: function() {
		(function(){
			var img = $('big-puff').getElement('img.active');
			img.effect('opacity', {
				delay: 200,
				onComplete: function() {
					this.removeClass('active');
					var parts = carousel.getParts(this.src);
					if(carousel.max > 0 && parts.next >= carousel.max) {
						carousel.first();
						return;
					}
					var src = parts.basedir + ((parts.next<10)?'0'+parts.next:parts.next) + '.jpg';
					var imgs = $('big-puff').getElement('img[src='+src+']');
					if(imgs) {
						imgs.addClass('active').effect('opacity', {
							onComplete: carousel.go
						}).start(0,1);
					}
					else {
						imgs = carousel.makeImage(parts.next, src);
					}
				}.bind(img)
			}).start(1,0);
		}).delay(7000);
	}
};