/**
* CIMKA
* Dependencies: Prototype, Scriptaculous, Google Maps
*/

/** Get IE version: */
var IEversion = 0;
var isIE = false;
var isSafari = false;
try {
	if(navigator.userAgent && document.all) {
		var regex = new RegExp(/MSIE ([0-9\.]{3,})\;/);
		var IEversion = regex.exec(navigator.userAgent)[1];
		IEversion = parseInt(IEversion);
		isIE = (IEversion > 0);
	}
	
	if(navigator.userAgent.indexOf('Safari') != -1) {
		isSafari = true;
	}
	
} catch(e) { /* not supported, but not IE. */ }

var language = {
	nl: {
		language: 'nederlands',
		name: 'naam',
		project: 'project',
		client: 'opdrachtgever',
		design: 'ontwerp',
		date: 'datum',
		animation: 'animatie'
	},
	en: {
		language: 'english',
		name: 'name',
		project: 'project',
		client: 'client',
		design: 'design',
		date: 'datum',
		animation: 'animation'
	}
}

language = (lang == 'nl') ? language.nl : language.en;

var menu = null;
var lis = null

var busy = false;

function fadeClick(event, isElement) {
	Spinner.toggle();
	if(!isElement) {
		var element = Event.element(event);
	} else {
		var element = event;
	}
	new Effect.Fade('fadewrapper',{
		afterFinish: function() {
			document.location.href=this.href;
		}.bind(element)
	});
	Event.stop(event);
}


function addContentFadeClicks() {
	if($('map')) {
		return false;
	}
	$A(['content-text','login-text']).each(function (id) {
		if($(id)) {
			$A($(id).getElementsByTagName('a')).each(function(e) {
				try {
					if(e.href.indexOf('mailto:') == -1 && !e.hasClassName('nofade')) {
						Event.observe(e,'click',fadeClick);
					}
				} catch(e) {
					
				}
			});
		}
	});
}


function loadMenu() {
	if(menu == null) {
		menu = $('menu');
		lis = menu.immediateDescendants();
	}

	lis.each(function(li) {
		if(li.getElementsByTagName('a').length > 0) {
			var menuLink = li.getElementsByTagName('a')[0];
			if((theSubList = $(menuLink.parentNode.getElementsByTagName('ul')[0]))) {
				//if(!theSubList.hasClassName('slide')) {
					//alert('slide');
				
				if(!useSlide && theSubList.getElementsByClassName('current').length > 0) {
					theSubList.style.display = 'inline';
				} else {
					theSubList.style.display = 'none';
				}
				theSubList.removeClassName('hidden');
				theSubList.addClassName('hiddenx');
				//}
				//if(theSubList.hasClassName('hidden')) {
				if(useSlide) {
					if(theSubList.getElementsByClassName('current').length > 0) {
						//Spinner.toggle();
						//theSubList.style.display = 'inline';
						new Effect.BlindDown(theSubList, {
							scaleX: true,
							scaleY: false,
							scaleContent: false,
							queue:'end',
							afterFinish: function(){
								this.removeClassName('hiddenx');
								if(!isIE || IEversion >= 7) {// || IEversion >= 7) {
									this.immediateDescendants().each(function(e,index) {
										new Effect.Appear(e,{to:0.99,delay:(index/10),duration:0.5});
									});
								}
								//Spinner.toggle();
								busy = false;
							}.bind(theSubList)
						});
					}
				}
				theSubList.immediateDescendants().each(function(e) {
					Event.observe(e.firstChild,'click',fadeClick);
				});
			}
			
			Event.observe(menuLink, 'click', function(event) {
				var element = Event.element(event);
				element.blur();
				
				// close others: (simple)
				var closeSubList = false;
				Event.element(event).parentNode.siblings().each(function(e) {
					if((closeSubList = $(e.getElementsByTagName('ul')[0]))) {
						//e.removeClassName('current');
						e.getElementsByTagName('a')[0].setStyle({textDecoration: 'none',color:'#888'});
						//closeSubList.addClassName('hidden');
						new Effect.BlindUp(closeSubList, {
							scaleX: true,
							scaleY: false,
							scaleContent: false,
							queue:'end',
							afterFinish: function(){
								this.addClassName('hiddenx');
								busy = false;
							}.bind(closeSubList)
						});
					}
				});
				
				
				if(subList = $(element.parentNode.getElementsByTagName('ul')[0])) {
					
					//element.toggleClassName('current');
					element.setStyle({textDecoration: 'none', color: '#505050'});//addClassName('current');
					
					// close current:
					subList.immediateDescendants().each(function(e,index) {
						if(isIE && IEversion < 7) {
							e.setStyle({visibility:'hidden'});
							//new Effect.Fade(e,{delay:(index/10),duration:0.5});
						} else {
							new Effect.Fade(e,{to:0.01,delay:(index/10),duration:0.5});
						}
					});
					
					if(!busy) {
						busy = true;
						if(!subList.hasClassName('hiddenx')) { // close
						
							
						
							if(!subList.totalWidth) {
								var totalWidth = 0;
								subList.immediateDescendants().each(function(e) { totalWidth += e.getWidth(); });
								subList.totalWidth = totalWidth;
							}
							subList.style.width = subList.totalWidth+'px';

							new Effect.BlindUp(subList, {
								scaleX: true,
								scaleY: false,
								scaleContent: false,
								queue:'end',
								afterFinish: function(){
									this.addClassName('hiddenx');
									busy = false;
								}.bind(subList)
							});
						} else { // open
							Spinner.toggle();

							if(isIE) {// && IEversion < 7) {
								subList.immediateDescendants().each(function(e) {
									e.setStyle({visibility:'visible'});
								});
							}

							//fadeClick(event);

							subList.eventElement = Event.element(event);

							new Effect.BlindDown(subList, {
								scaleX: true,
								scaleY: false,
								scaleContent: false,
								queue:'end',
								afterFinish: function(){
									this.removeClassName('hiddenx');
									if(!isIE || IEversion >= 7) {
										subList.immediateDescendants().each(function(e,index) {
											new Effect.Appear(e,{to:0.99,delay:(index/10),duration:0.5});
										});
									}

									fadeClick(this.eventElement, true);
									
									Spinner.toggle();
									busy = false;
								}.bind(subList)
							});
						}
					}
					
					Event.stop(event);
				} 
				
				// Normal mainmenu link:
				else {
					/*element.blur();
					Spinner.toggle();
					new Effect.Fade('fadewrapper',{
						afterFinish: function() {
							document.location.href=this.href;
						}.bind(element)
					});
					Event.stop(event);*/
					
					fadeClick(event);
				}
			});
		}
	});
}


function setCookie(name,value,expiredays) {
	var exdate = new Date();
	exdate.setDate(exdate.getDate() + expiredays);
	document.cookie = name + "=" + escape(value) + ((expiredays==null) ? "" : ";expires=" + exdate.toGMTString());
}


function initSplash() {
    
	var a = $('splash').firstDescendant();
	var initClick = function(href) {
		new Effect.Fade('splash', { afterFinish: function() {
				window.location.href=href;
			}
		});
	}

	$('splash').getElementsBySelector('a').invoke('observe','click',function(event) {
		var el = Event.element(event);
		el.blur();
		Event.stop(event);
		initClick(el.href);
	});
/*
	Event.observe(a, 'click', function(event) {
		Event.element(event).blur();
		Event.stop(event);
		initClick();
	});*/
}

var image;

var ProjectHandle = Class.create();
ProjectHandle.prototype = {

    baseurl: '/',
	spliturl: 'showcases',

    initialize: function(baseurl) {
        this.baseurl = baseurl;
    },

    get: function(href, loginProject) {
    	if(loginProject) {
    		this.spliturl = 'afbeeldingen';
    	}
        Spinner.toggle();
        new Ajax.Request(baseurl+'/json', {
            method: 'post',
            requestHeaders: {Accept: 'application/json'},
            parameters: {
                location: href.split(this.spliturl)[1],
                action: ((loginProject) ? 'getLogin' : 'get')
            },
            onSuccess: function(t, json) {
                var json = t.responseText.evalJSON(true);
                if(json.error) {
                    alert(json.error);
                    return false;
                } else {
                    this.set(json);
                }
            }.bind(this)
        });
    },

    set: function(project) {

    	var slideshow = $('slideshow');
    	
    	// normal project
    	if(project.image) {
			//slideshow.immediateDescendants().each(function(e) { e.remove(); });
    		//slideshow.appendChild(Builder.node('img',{src:baseurl+'/uploads/projecten/'+project.image}));
    		if(!image) {
    			if($('project-foto').getElementsByTagName('img').length > 0) {
    				image = $('project-foto').getElementsByTagName('img')[0];
				} else {
					//image = Builder.node('img',{src:baseurl+'/uploads/projecten/'+project.image,style:'display:none'});
					new Insertion.Top('project-foto','<img src="'+baseurl+'/uploads/projecten/'+project.image+'" title="'+project.image+'" alt="'+project.image+'" style="display: none" />');//$('project-foto').appendChild(image);
					image = $('project-foto').getElementsByTagName('img')[0];
				}
			} else {

			}
			
			//alert(project.image);
			
			if($('description-dl')) {
				new Effect.Fade('description-dl',{duration: 0.3});
			}

    		new Effect.Fade(image, {duration: 0.5, afterFinish: function() {
    			this.src = baseurl+'/uploads/projecten/'+project.image;
    			$('filename').innerHTML = project.image;
    			if(isIE) {
    				$('filename').setStyle({width: '200px'});
    			}
    			
    			if(isSafari) {
    				this.style.marginTop = '22px';
    			}
    			
				// check if the image is in browser cache (IE)
				// If it is in browsercache the onload event doesn't fire.. so we
				// do it like this:
				if (this.complete) {
	    			new Effect.Appear(this,{queue: 'end'});
					if($('description-dl')) {
						new Effect.Appear('description-dl',{queue: 'end', duration: 0.3});
					}
		
					Spinner.toggle();
					busy = false;
				}
				else {
					this.onload = function() {
		    			new Effect.Appear(this,{queue: 'end'});
						if($('description-dl')) {
							new Effect.Appear('description-dl',{queue: 'end', duration: 0.3});
						}
			
						Spinner.toggle();
						busy = false;
					}.bind(this); 
				}
    		}.bind(image)});
    		return false;
    	}
    	
    	// showcase project:
    	
    	// 'description' get's the meta in IE -_-
        var dl = $('description-dl');
        new Effect.Fade(dl, {duration:0, afterFinish: function() {
            
            if(!SL) {
            	return false;
            }
            
            SL.stop();
           	new Effect.Fade('right-nav');
            new Effect.Fade('slideshow', { afterFinish: function() {
	            var slthumbs = $('showcase-thumbs');

				//if(slthumbs.hasClassName('nojs')) { return false }

	            // clear slideshow:
	            slideshow.immediateDescendants().invoke('remove');
	            // clear dots:
	            $('right-nav').immediateDescendants().invoke('remove');
	            // fill slideshow/dots
	            for(var i=0; i<project.fotos.length; i++) {
	                var display = (i==0 ? 'inline' : 'none')
	                slideshow.appendChild(Builder.node('img',{src:baseurl+'/uploads/showcase/'+project.fotos[i],style:"display:"+display+""}));
	            	if(project.fotos.length>1) {
	            		$('right-nav').appendChild(Builder.node('li',[
	            			Builder.node('a',{ className: (i==0 ? 'current' : ''), href:'#'+(i+1) },'Pagina '+(i+1))
	            		]));
					}
	            }

				if(project.fotos.length > 1) {
					//new Insertion.Top($('right-nav'), '<li><a id="pause">pause</a></li>');
					//Event.observe('pause','click',SL.pause.bind(SL));
				}

	            new Effect.Appear('slideshow', { afterFinish: function() {

	            	new Effect.Appear('right-nav');
		            SL = new SlideShow();
		            // clear details:
		            dl.immediateDescendants().each(function(e) {
		                e.remove();
		            });

//		            if(project.ProjectNr != '') {
		                dl.appendChild(Builder.node('dt',{className: 'title'},project.ProjectNr+' '+project.Titel));
						var empty = Builder.node('dd',{style:'width: 20px'},"");
		                dl.appendChild(empty);
//		            }
		            
/*		            if(project.Titel != '') {
		                dl.appendChild(Builder.node('dt','naam'));
		                dl.appendChild(Builder.node('dd',project.Titel));
		            }*/

		            if(project.Project != '') {
		                dl.appendChild(Builder.node('dt',language.project));
		                dl.appendChild(Builder.node('dd',project.Project));
		            }

		            if(project.Klant != '') {
		                dl.appendChild(Builder.node('dt',language.client));
		                dl.appendChild(Builder.node('dd',project.Klant));
		            }

		            if(project.Ontwerp != '') {
		                dl.appendChild(Builder.node('dt',language.design));
		                dl.appendChild(Builder.node('dd',project.Ontwerp));
		            }

					if (project.DatumZichtbaar == 'y') {
						dl.appendChild(Builder.node('dt', language.date));
						dl.appendChild(Builder.node('dd', project.Date));
					}

					if(project.DField != '') {
		                dl.appendChild(Builder.node('dt',project.DField));
		                dl.appendChild(Builder.node('dd',project.DValue));
		            }
/*
		            if(project.PDF != '') {
		                dl.appendChild(Builder.node('dt','PDF'));
		                var pdfLink = Builder.node('a',{
		                	href:baseurl+'/uploads/projectpdf/'+project.PDF,
		                	onclick: "window.open('"+baseurl+'/uploads/projectpdf/'+project.PDF+"', null); return false"
		                },'download');
		                dl.appendChild(Builder.node('dd',[pdfLink]));
		            }
/*
		            if(project.Animatie != '') {
		                dl.appendChild(Builder.node('dt',language.animation));
		                var animatieLink = Builder.node('a',{
		                	href:baseurl+'/media/'+project.Animatie,
		                	onclick: "window.open('"+baseurl+'/media/'+project.Animatie+"', null,'location=0,status=0,scrollbars=0,width=400,height=200'); return false"
		                },'view');
		                dl.appendChild(Builder.node('dd',[animatieLink]));
		            }

		            if(project.Panorama != '') {
		                dl.appendChild(Builder.node('dt','panorama'));
						var panoramaLink = Builder.node('a',{
		                	href:baseurl+'/panorama/'+project.Panorama,
		                	onclick: "window.open('"+baseurl+'/panorama/'+project.Panorama+"', null,'location=0,status=0,scrollbars=0,width=640,height=480'); return false"
		                },'view');
		                dl.appendChild(Builder.node('dd',[panoramaLink]));
		            }
*/
					if((project.quicktimes) && project.quicktimes != '') {
						project.quicktimes.each(function(s,i) {
							dl.appendChild(Builder.node('dt',language.animation+(i>0 ? ' #'+(i+1) : '')));
							var link = Builder.node('a',{
								target: 'quicktimeplayer',
			                	href:baseurl+'/uploads/media/'+s,
			                	onclick: "window.open(this.href, null); return false" //,'location=0,status=0,scrollbars=0,width=640,height=480'
			                },s);
			                dl.appendChild(Builder.node('dd',[link]));
						});
					}

					if((project.panoramas) && project.panoramas != '') {
						project.panoramas.each(function(s,i) {
							dl.appendChild(Builder.node('dt','panorama'+(i>0 ? ' #'+(i+1) : '')));
							var link = Builder.node('a',{
			                	href:baseurl+'/uploads/media/'+s,
			                	onclick: "window.open(this.href, null); return false" //'location=0,status=0,scrollbars=0,width=640,height=480'
			                },s);
			                dl.appendChild(Builder.node('dd',[link]));
						});
					}

					if((project.pdfs) && project.pdfs != '') {
						project.pdfs.each(function(s,i) {
							dl.appendChild(Builder.node('dt',''));//,'pdf'+(i>0 ? ' #'+(i+1) : '')));
							var link = Builder.node('a',{
			                	href:baseurl+'/uploads/media/'+s,
			                	onclick: "window.open(this.href, null); return false" //'location=0,status=0,scrollbars=0,width=640,height=480'
			                },s);
			                dl.appendChild(Builder.node('dd',[link]));
						});
					}

					if((project.others) && project.others != '') {
						project.others.each(function(s,i) {
							dl.appendChild(Builder.node('dt',''));//,'pdf'+(i>0 ? ' #'+(i+1) : '')));
							var link = Builder.node('a',{
			                	href:baseurl+'/uploads/media/'+s,
			                	onclick: "window.open(this.href, null); return false" //'location=0,status=0,scrollbars=0,width=640,height=480'
			                },s);
			                dl.appendChild(Builder.node('dd',[link]));
						});
					}

		            if((project.Tekst) && project.Tekst != '') {
		                dl.appendChild(Builder.node('dt',{className:'tekst'},''));
		                var textnode = Builder.node('dd',{className:'tekst'});
		                textnode.innerHTML = project.Tekst;
		                dl.appendChild(textnode);//.unescapeHTML(true)
		            }
		            
		            // style bugfix in IE:
		            if(isIE) {
		            	dl.style.marginLeft = '320px';
		            }
  			    if(isSafari) {
				dl.style.marginTop = '22px';
			    }

		            new Effect.Appear(dl,{duration:0,afterFinish: function() { Spinner.toggle(); busy = false; }});
				}});
            }});
        }});
    }
}


var showcaseHeight = 0;
function initShowcaseNav(OnlyOpacity) {
    if(!OnlyOpacity) {
    	$('left-nav').immediateDescendants().each(function(li, index) {
    		Event.observe(li, 'click', function(event) {
    			var a = Event.element(event);
    			a.blur();
    			a.parentNode.parentNode.immediateDescendants().each(function(li) {
    				li.firstDescendant().removeClassName('current');
    			});
    			a.addClassName('current');
    			showcasePage(index);
    			Event.stop(event);
    		});
    	});
    }
    showcaseHeight = $('showcase-wrapper').getHeight(); // 235
	
	var noActions = false;
	/*if($('showcase-wrapper').hasClassName('no-overflow')) {
		noActions = true;
	}*/

	var LoginProject = $('showcase-wrapper').hasClassName('projectnav');
	
		// project nav: (only get images)
	
	//} else {

	// Opacity + hover events
	$A($('showcase-wrapper').getElementsByTagName('a')).each(function(a) {
		a = $(a);

		if(!noActions) {
			Event.observe(a, 'click', function(event) {
			    var el = Event.element(event);
			    el.blur();
				Event.stop(event);
			    if(!busy) {
			    	busy = true;
					    Project.get(el.href, LoginProject);
				    $A($('showcase-wrapper').getElementsByTagName('a')).each(function(a) {
				        a.removeClassName('current');
			        });
			        el.addClassName('current');
			        initShowcaseNav(true);
				}
		    });
		}

			/*if(!a.hasClassName('current')) {
				new Effect.Opacity(a, {from: 0.95, to: 0.75, duration: 0.5});
		}


		Event.observe(a, 'mouseover', function(event) {
			var element = Event.element(event);
			if(!element.hasClassName('current'))
					new Effect.Opacity(element, {from: 0.75, to: 0.95, duration: 0});
		});

		Event.observe(a, 'mouseout', function(event) {
			var element = Event.element(event);
			if(!element.hasClassName('current'))
					new Effect.Opacity(element, {from: 0.95, to: 0.75, duration: 0});
			});*/

	});
	//}
}

function showcasePage(index) {
	page = -index*showcaseHeight;
	page = (page != 0) ? page-(5*index) : page;
	new Effect.Morph('showcase-thumbs',{
		style: { marginTop: page+'px' },
		afterFinish: function() {
			if(isSafari) {
				window.resizeBy(1,0);
			}
		}
	});
}


function initPageNav() {
	$('left-nav').immediateDescendants().each(function(li, index) {
		Event.observe(li, 'click', function(event) {
			var a = Event.element(event);
			a.blur();
			a.parentNode.parentNode.immediateDescendants().each(function(li) {
				li.firstDescendant().removeClassName('current');
			});
			a.addClassName('current');
			textPage(index);
			if(isSafari) {
				window.resizeBy(-1,0);
			}
			Event.stop(event);
		});
	});
}

function textPage(index) {
	page = -index*300;
	new Effect.Morph('text-wrapper',{
		style: { marginLeft: page+'px' }
	});
}


var map = null;
var geocoder = null;

function createMap() {
  if (GBrowserIsCompatible()) {
	map = new GMap2(document.getElementById("map"));
	if(isSafari) {
		$('map').style.marginTop = '22px';
	}
	map.addControl(new GSmallMapControl());
	geocoder = new GClientGeocoder();
	showAddress('Van Nelleweg 1, 3004 HB, Rotterdam, Netherlands');
  }
}

function showAddress(address) {
	if (geocoder) {
		geocoder.getLatLng(
			address,
			function(point) {
				if (!point) {
					alert(address + " not found");
				} else {
					point = new GLatLng(51.924815,4.432077);
					var icon = new GIcon();
					icon.image = "http://labs.google.com/ridefinder/images/mm_20_red.png";
					icon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
					icon.iconSize = new GSize(12, 20);
					icon.shadowSize = new GSize(22, 20);
					icon.iconAnchor = new GPoint(6, 20);
					icon.infoWindowAnchor = new GPoint(5, 1);

					map.setCenter(point, 16);
					var marker = new GMarker(point, icon);
					map.addOverlay(marker);
					map.setMapType(G_SATELLITE_MAP);
					//marker.openInfoWindowHtml(address);
				}
			}
		);
	}
}

var SlideShow = Class.create();
SlideShow.prototype = {
	// Settings:
	interval: 4,
	
	// Working vars:
	paused: false,
	element: null,
	images: $A(),
	current: 0,
	pe: 'bad',
	useDots: false,
	dotElement: null,
	dotLinks: $A(),
	initialize: function() {
		try { this.pe.stop(); } catch(e) { }
		var elements = $('wrapper').getElementsByClassName('slideshow');
		if(elements.length > 0) {
			this.element = $(elements[0]);
			this.images = $A(this.element.getElementsByTagName('img'));
			var newHeight = this.element.getHeight()-(this.element.id == 'full' ? 0 : 0)+'px';
			this.element.style.height = newHeight;
			if($('slideshowwrapper')) {
				$('slideshowwrapper').style.height = newHeight;
			}
		}
		
		for(var i=0; i<this.images.length; i++) {
			Event.observe(this.images[i],'click',function(event) { this.pause(); }.bind(this));
			if(this.images[i].style.display != 'none') {
				this.current = i;
			}
		}
		
		this.useDots = (this.dotElement = $('right-nav'));
		if(this.useDots) {
			
			//var pauseLi = Builder.node('li');
			//var pauseA = Builder.node('a',{className:'pause'},'pauze');
			
			this.dotLinks = $A(this.dotElement.getElementsByTagName('a'));
			this.dotLinks.each(
				function(a) {
				Event.observe(a,'click',function(event) {
					var element = Event.element(event);
					element.blur();
					if(element.id != 'pause') {
						this.show(element);
					}
					Event.stop(event);
				}.bind(this));
			}.bind(this));
			
			if(this.images.length > 1) {
				new Insertion.Top(this.dotElement, '<li><a id="pause">pause</a></li>');
				Event.observe('pause','click',this.pause.bind(this));
			}
			
		}
		
		if(this.images.length > 1) {
			this.start();
			this.active = true;
		}
	},
	start: function() {
		//$('pause').removeClassName('current');
		this.pe = new PeriodicalExecuter(this.next.bind(this), this.interval);
	},
	pause: function(event) {
		try {
			if(this.paused) {
		        this.start();
		        this.paused = false;
				$('pause').removeClassName('current');
			} else {
				this.stop();
				this.paused = true;
				$('pause').addClassName('current');
			}
		} catch(e) {
			//alert("can't pause/resume because: "+e);
		}
	},

	stop: function() {
	    try {
	        this.pe.stop();    
	    } catch(e) {
	        //alert('cant stop because: '+e);// can't stop because it's not started :)
	    }
	},
	next: function(pe) {
		new Effect.Fade($(this.images[this.current]),{
			duration: 0.5,
			queue: 'end',
			afterFinish: function() {
				// double check, everything is hidden:
				$(this.images[this.current]).siblings().invoke('hide');
			}.bind(this)
		});
		
		//////
		
		this.current = (this.images[this.current+1]) ? this.current+1 : 0;
		

		
		new Effect.Appear($(this.images[this.current]),{
			duration: 0.5, 
			queue: 'end'/*,
			afterFinish: function() {
				this.siblings().invoke('hide');
			}.bind($(this.images[this.current]))*/
		});
		
			if(this.useDots) {
				this.dotLinks.each(function(e) { $(e).removeClassName('current'); });
				if(this.dotLinks[this.current]) {
					//alert(this.dotLinks[this.current].id);
					this.dotLinks[this.current+(this.dotLinks[this.current].id == 'pause' ? 2 : 0)].addClassName('current');
				}
			}
		
	},
	show: function(e) {
		this.newIndex = e.href.substr(e.href.indexOf('#')+1,e.href.length)-1;
		//$A(e.parentNode.getElementsByTagName('a')).each(function(e) { $(e).removeClassName('current'); });
		this.stop();
		new Effect.Fade($(this.images[this.current]),{ afterFinish: function() {
			// double check, everything is hidden:
			$(this.images[this.newIndex]).siblings().invoke('hide');
			
			this.current = this.newIndex;
			new Effect.Appear($(this.images[this.newIndex]),{
				afterFinish: function() {
					this.siblings().invoke('hide');
				}.bind($(this.images[this.current]))
			});
			
			if(this.useDots) {
				this.dotLinks.each(function(e) { $(e).removeClassName('current'); });
			}
			if(this.dotLinks[this.current]) {
				this.dotLinks[this.current].addClassName('current');
			}
			if(!this.paused) {
				this.start();
			}
		}.bind(this)});
	}
}

var SpinnerControl = Class.create();
SpinnerControl.prototype = {
    element: null,
    isOn: false,
    initialize: function(el) {
        this.element = $(el);
    },
    
    toggle: function() {
        this.element.style.backgroundImage = (!this.isOn) ? "url('"+baseurl+"/img/spinner.gif')" : "url('')";
        this.isOn = (!this.isOn);
    }
}

// Onload event
var SL;
var Project;
var Spinner;
Event.observe(window, 'load', function() {
	
	// Set the cookie telling the server that this user supports JavaScript:
	setCookie('UserHasJS','yes',365);
	
	loadMenu();
	
	if($('text-wrapper') && $('left-nav')) { initPageNav(); }
	if($('left-nav') && $('left-nav').hasClassName('showcase-nav')) {
	    Project = new ProjectHandle(baseurl);
	    initShowcaseNav();
	}
	
	var splash = false;
	if($('splash')) {
		initSplash();
	} else {
		if($('fadewrapper')) {
			new Effect.Appear('fadewrapper',{ from: 0, to: 0.99, queue:'end', afterFinish: function() {
					if($('map')) { createMap(); }
					SL = new SlideShow();
					// doesn't get initialized while 'appearing':
					if($('showcase-wrapper')) {
						showcaseHeight = $('showcase-wrapper').getHeight(); // 235
					}
				}
			});
		} else {
			if($('map')) { createMap(); }
			SL = new SlideShow();	
		}
	}
    
    Spinner = new SpinnerControl('menu');
	
	addContentFadeClicks();
	
});
