window.addEvent("domready",function(){				
	$$("select").each(function(select){
		select.setStyles({"margin-left":"-9999px","position":"absolute"});
		if (select.options.length > 0) {
			var newSelect = new Element("div").addClass("select").setStyle("width",select.getCoordinates().width).set("title",select.options[select.selectedIndex].text).store("open","false").injectAfter(select);
			var textSelect = new Element("p", {id:select.id+"Selected"}).set("html",select.options[select.selectedIndex].text).setStyle("width",select.getCoordinates().width-15).inject(newSelect);
			if(select.hasClass("margin"))
				newSelect.setStyle("margin-left",3);
				newSelect.addEvents({
				"mouseenter":function(){
					newSelect.setStyle("border-color","#133D7E");
				},
				"mouseleave":function(){
					if(newSelect.retrieve("open")=="false")
						newSelect.setStyle("border-color","");
				},
				"click":function(e){						
					e.stop();
					$$(".selectContainer").each(function(container){
						var prev = container.getPrevious("div");
						if(prev!=newSelect){
							container.getPrevious("div").store("open","false");
							container.destroy();
						}
					});						
					if(newSelect.retrieve("open")=="false"){							
						newSelect.store("open","true");							
						var container = new Element("div").setStyles({
							"overflow":"hidden",
							"border":"1px solid #133D7E",
							"background":"#fff",
							"height":0
						});	
						container.set("tween",{
							transition:Fx.Transitions.Expo.easeOut,
							onComplete:function(){
								
							}
						});
						container.addClass("selectContainer").setStyles({						
							"position":"absolute",
							"top":newSelect.getCoordinates().top + newSelect.getCoordinates().height,
							"left":newSelect.getCoordinates($("wrapper")).left
						}).inject(newSelect,'after');
						select.getElements("option").each(function(option,index){						
							var span = new Element("span").set("html",option.get("text"))
							.addEvents({
								"mouseenter":function(){
									this.addClass("over");
								},
								"mouseleave":function(){
									this.removeClass("over");
								},
								"click":function(){
									textSelect.set("html",this.get("html"));
									newSelect.set("title",this.get("html"));
									container.destroy();
									newSelect.store("open","false");
									select.selectedIndex = index;
									newSelect.setStyle("border-color","#0078C8");
									
									if (select.name == 'ligplaats') {
										nieuweref = window.location.href;
										nieuweref = nieuweref.replace('default.asp', '');
										nieuweref = nieuweref.replace('?lang=de', '');
										
										url = new URI(nieuweref);
										if ( url.getData('subject') == null ) {
											nieuweref = nieuweref +  'default.asp?subject=pagina&keuze=onze_vloot'
										}
										strpos = nieuweref.indexOf("ligplaatsindex");
										if (strpos > 0) { nieuweref = nieuweref.substring(0,strpos-1); }
										nieuweref = nieuweref +  '&ligplaatsindex=' + (index);	
										
										window.location.href= nieuweref;
									} else if (select.name == 'schip') {
										var text = select.options[index].value;
										var req = new Request({
											url: "/includes/getBouwjaren.asp",
											method: 'post',
											data: {groep:text},
											onSuccess: function(response){
												var results = JSON.decode(response).results;
												if(results.length > 0) {
													$("bouwjaar").empty();
													new Element("option", { text: "alle jaren", value: "" }).inject($("bouwjaar"));
													$("bouwjaarSelected").set("text","alle jaren");
													results.each(function(result) {
														new Element("option", { text: result.bouwjaar, value: result.bouwjaar }).inject($("bouwjaar"));
													});
												}
											}
										}).send();
									}
								}
							})
							.inject(container);
							if(option.get("text")==select.options[select.selectedIndex].text) {
								span.addClass("selected");								
							}
						});
						if(container.getCoordinates().width < newSelect.getCoordinates().width)
							container.setStyle("width",newSelect.getCoordinates().width-2);
						container.tween("height",container.getScrollSize().y);
					} else {
						newSelect.store("open","false");
						var el = newSelect.getNext("div");
						el.set("tween",{
							transition:Fx.Transitions.Expo.easeOut,
							onComplete:function(){
								el.destroy();
								newSelect.setStyle("border-color","#0078C8");
							}
						});
						el.tween("height",0);
					}
				}
						
			});
		}
	});
	document.body.addEvent("click",function(e){
		var el = e.target;					
		if(!$(el).hasClass("select")){
			$$(".selectContainer").each(function(container){
				container.getPrevious("div").store("open","false");
				container.set("tween",{
					transition:Fx.Transitions.Expo.easeOut,
					onComplete:function(){
						container.getPrevious("div").setStyle("border-color","#0078C8");
						container.destroy();						
					}
				});
				container.tween("height",0);
			});
		}
	});
});
