var officeActive = "";

var Site = {

	start : function() {
		Site.behaviour();
		Site.showAddress('address_'+officeActive);
	},
	
	behaviour : function() 
	{
		$$('#enilsson_email').each(function(el,i){
			el.innerHTML = Site.getEmail();
		});
		$$('#services div#hAccordian').each(function(el,i){
			new HorizontalAccordian(el,{tabWidth:30});
		});
		$$('#products div#hAccordian').each(function(el,i){
			new HorizontalAccordian(el,{tabWidth:30});
		});
		$$('.addressTitle').each(function(el) {
			el.observe('click', function(e) { 
				Event.stop(e);
				Site.showAddress(el.parentNode.id); 
			});
		});
	},
	
	showAddress : function(city)
	{
		$$('#addressBox li').each(function(el) { 
			el.removeClassName('sel');
		});		
		$(city).addClassName('sel');
	},
	
	getEmail : function()
	{
		var l=new Array();
		l[0]='>';l[1]='a';l[2]='/';l[3]='<';l[4]='|109';l[5]='|111';l[6]='|99';l[7]='|46';l[8]='|110';l[9]='|111';l[10]='|115';l[11]='|115';l[12]='|108';l[13]='|105';l[14]='|110';l[15]='|101';l[16]='|64';l[17]='|111';l[18]='|108';l[19]='|108';l[20]='|101';l[21]='|104';l[22]='>';l[23]='"';l[24]='|109';l[25]='|111';l[26]='|99';l[27]='|46';l[28]='|110';l[29]='|111';l[30]='|115';l[31]='|115';l[32]='|108';l[33]='|105';l[34]='|110';l[35]='|101';l[36]='|64';l[37]='|111';l[38]='|108';l[39]='|108';l[40]='|101';l[41]='|104';l[42]=':';l[43]='o';l[44]='t';l[45]='l';l[46]='i';l[47]='a';l[48]='m';l[49]='"';l[50]='=';l[51]='f';l[52]='e';l[53]='r';l[54]='h';l[55]=' ';l[56]='a';l[57]='<';
	
		var email = "";	
		for (var i = l.length-1; i >= 0; i=i-1){
			if (l[i].substring(0, 1) == '|'){
				email += "&#"+unescape(l[i].substring(1))+";";
			} else {
				email += unescape(l[i]);
			};
		};
		
		return email;
	}
	
};

document.observe('dom:loaded', Site.start);	

var currTab;

var HorizontalAccordian = Class.create({

	initialize: function(element) {
		this.element = $(element);
        this.options = Object.extend({
        	transition: Effect.Transitions.sinoidal,
        	duration: 1,
        	defaultOpenBox: 1,
        	tabWidth: 30
        }, arguments[1] || {});
        this.setup();
	},
	
	setup: function()
	{
		this.element.setStyle({ 
			'overflow' : 'hidden'
		});
		this.boxes = $$('#' + this.element.id + ' > div');
		this.contentWidth = this.element.getWidth() - this.boxes.length * (this.options.tabWidth+3);
		this.boxes.each(function(el, i)
		{
			// set some styles on the tab box
			el.setStyle({ 
				'float' 	: 'left', 
				'position' 	: 'relative',
				'height' 	: this.element.getHeight() + 'px',
				'overflow' 	: 'hidden'
			});
			
			// grab the tab heading and set some styles needed for the animation
			var h2 = el.select('h2').first();
			h2.setStyle({ 
				'textIndent' 	: '-1666px', 
				'cursor' 		: 'pointer',
				'width' 		: this.options.tabWidth + 'px', 
				'height' 		: this.element.getHeight() + 'px',
				'position' 		: 'absolute',
				'top' 			: '0px',
				'left' 			: '0px'
			});
			// add a click handler to the heading
			h2.observe('click', this.moveTabs.bindAsEventListener(this));
			
			// manipulate the content box for each tab
			var box = el.select('div').first();
			box.setStyle({ 
				'overflow' : 'hidden', 
				'height' : this.element.getHeight() + 'px',
				'width' : this.contentWidth+3 + 'px',
				'position' : 'absolute',
				'top' : '0px',
				'left' : h2.getWidth().toString() + 'px'
			});
			
			// set which tab is the open one
			if(i==(this.options.defaultOpenBox-1)){
				el.setStyle({ 'width' : (box.getWidth() + h2.getWidth()).toString() + 'px' });
			} else {
				el.setStyle({ 'width' : h2.getWidth().toString() + 'px' });
			}

		}.bind(this));
	},
	
	moveTabs: function(event)
	{
	
		
		// loop through the boxes and identify the one to opened and the currently opened one
		this.boxes.each(function(el, i){
			var h2 = el.select('h2').first();
			var box = el.select('div').first();
			if(h2 == Event.element(event))
			{
				h2.addClassName("boxselect");
				h2.removeClassName("nonselect");
				this.openbox = el;
			} else {
				h2.addClassName("nonselect");
				h2.removeClassName("boxselect");
			};
			if(el.getWidth() > (this.options.tabWidth + 5)){
				this.closebox = el;
			}			
		}.bind(this));
		
		
		if (this.openbox == this.closebox) {
			return;
		}
		
		// run the opening and close animations in parallel
		new Effect.Parallel([
				new Effect.Morph( this.openbox, {
					style : { 
						'width' : (this.contentWidth+3 + this.options.tabWidth).toString() + 'px' 
					},
					duration : this.options.duration
				}),		
				new Effect.Morph(this.closebox, {
					style : { 'width' : this.options.tabWidth + 'px' },
					duration : this.options.duration
				})			
		]);
	}
	
});