var functions = {
 	init: function(){
		$$('.dropdown_wrapper').each(function(s){ s.setStyle({display:'none'}); });
		$$('.btn_product').each(function(s){
			if (s.previous(0))
				new Effect.Opacity(s.previous(0).down('.picture'), { from: 1.0, to: 0.7, duration: 0.0 });
			
			Event.observe(s.down('a'), 'mouseover', functions.btn_product_mouseover);
			Event.observe(s.down('a'), 'mouseout', functions.btn_product_mouseout);
			
		});
		
		Event.observe($('menu'), 'mouseover', 	functions.menu_mouseover.bindAsEventListener());
		Event.observe($('menu'), 'mouseout', 	functions.menu_mouseout.bindAsEventListener());
		Event.observe($('dealers_form'), 'click', 	functions.dropdown);
	},
	menu_mouseover: function(e){
 		tag = Event.element(e);
		if (tag.className != 'active' && tag.up('ul').id == 'menu')
			new Effect.Morph(tag, {style: 'color:#f9f5e9;',duration: 0.2});
	},
	menu_mouseout: function(e){
 		tag = Event.element(e);
		if (tag.className != 'active' && tag.up('ul').id == 'menu')
			new Effect.Morph(tag, {style: 'color:#451e0e;', duration: 0.3});
	},
	dropdown: function(e){
		tag = Event.element(e);
		
		tag.next(0).appear({duration:'0.5'});
		tag.next(0).select('a').each(function(s){
			Event.observe(s,'click',function(){
				s.up('.dropdown_wrapper').previous(0).update(s.innerHTML);
			});
		});
		
		Event.observe(document,'click',function(){tag.next(0).hide()});
	},
	btn_product_mouseover: function(e){
 		tag = Event.element(e);
		
		if (tag.up('div').previous(0))
			new Effect.Opacity(tag.up('div').previous(0).down('.picture'), { from: 0.7, to: 1.0, duration: 0.2 });
		
		new Effect.Morph(tag.up('div').down('.btn_product_header'), {style: 'color:#d15428;', duration: 0.2});
			
	},
	btn_product_mouseout: function(e){
 		tag = Event.element(e);		
		
		if (tag.up('div').previous(0))
			new Effect.Opacity(tag.up('div').previous(0).down('.picture'), { from: 1.0, to: 0.7, duration: 0.2 });
		
		new Effect.Morph(tag.up('div').down('.btn_product_header'), {style: 'color:#f9f5e9;', duration: 0.2});
		
	}
};

var varianter = {
	init: function(){
		if ($('product_types')){
			$('product_types').select('div').each(function(s){
				Event.observe(s.up('li'), 'click', varianter.click);
				s.hide();
			});
			
		}
	},
	click: function(e){
 		tag = Event.element(e);		
		if(tag.down('div').visible()){
			tag.down('div').hide();
			tag.className = '';
		}else{
			tag.down('div').appear({duration: 0.3});
			tag.className = 'active';
		}
	}
	
}

var calendar = {
	init: function(){
		new Ajax.Updater('calendar', 'ajax_get_kalender.asp',{
  			onSuccess: function(){
				$('calendar').appear({duration: 0.3});
			}
		});
	},
	prev: function(tag){
 		new Ajax.Updater('calendar', 'ajax_get_kalender.asp' + tag.rel);
	},
	next: function(tag){
 		new Ajax.Updater('calendar', 'ajax_get_kalender.asp' + tag.rel);
	},
	post: function(tag){
 		new Ajax.Updater('ajax_post', 'ajax_get_kalenderpost.asp?dato=' + tag.rel,{
  			onSuccess: function(){
				if (!$('ajax_post').visible()){
					$('ajax_post').appear({duration: 0.3});
				}
			}
		});
	}
}

var statments = {
	init: function(){
		if ($('statements')){
			new Ajax.Request('ajax_get_statments.asp', {
			  onSuccess: function(transport) {
			    if(transport.responseText != '[]'){
					str_json = transport.responseText.evalJSON();
			  		statments.loop();
				}
			  }
			});
		}
	},
	loop: function(){
		var i = 0;
		
		new PeriodicalExecuter(function() {
			if ((i+1) == str_json.size()){
				i = 0;
				$('statements_header').update(str_json[0].listtitle + ' - <em>' + str_json[0].forfatter_tittel + '</em>');
				$('statements_text').update('<span>&ldquo;</span>' + str_json[0].statement + '<span>&rdquo;</span>');
			}else{
				i += 1;
				$('statements_header').update(str_json[i].listtitle + ' - <em>' + str_json[i].forfatter_tittel + '</em>');
				$('statements_text').update('<span>&ldquo;</span>' + str_json[i].statement + '<span>&rdquo;</span>');
			};
		},15);
	}
}

function galleri(){
	var i = 0;
	var arr = $('flash').childElements();
	
	new PeriodicalExecuter(function() {
		if ((i+1) == arr.size()){
			i = 0;
			Effect.Appear(arr[0], { duration: 1.0 });
			window.setTimeout(function() { arr.invoke('show'); } , 1000);
		}else{
			Effect.Fade(arr[i], { duration: 1.0 });
			i += 1;
		};
	},5);
}	

document.observe("dom:loaded", function() {	
	if($('calendar')){ calendar.init(); };
	if ($('flash')){ galleri(); };
	
	functions.init();
	statments.init();
});