var isNS4 = (document.layers) ? true : false;
var isIE4 = (document.all && ! document.getElementById) ? true : false;
var isIE5 = (document.all && document.getElementById) ? true : false;
var isNS6 = (!document.all && document.getElementById) ? true : false;
var isIE5Mac = (navigator.userAgent.indexOf("Mac") != -1 && isIE5);
var debugMode = false;
var deb = new scriptDebugger();
var fades = new Array();
var fadeCounter = 0;
var zoomWindow;
var imageZoomer;
var mozileInterface;

function isMSIE(){
	return(navigator.appVersion.indexOf("MSIE") != -1);
}

function getDOMObject(str){
  if (document.all){
    return(document.all[str]);
  }else if (document.getElementById) {
    return(document.getElementById(str));
  }
}

function cmsZoom(i, iw, ih, title){
	imageZoomer.open("/cms/images/l" + i + ".jpg", iw, ih, title);
}

function removeSelections(){
	if(window.getSelection){
		window.getSelection().removeAllRanges();
	}
}

function writeEmail(name, machine){
	document.write("<a href=:\"mailto:" + name + "@" + machine + "\">");
	document.write(name + "@" + machine + "</a>");
}

function printVersion(page){
	this.printWindow = window.open(page + "?print=true", "printWindow", "width=800,height=600,status=no,resizable=yes,scrollbars=yes");
}

function showFullScreen(page){
	options = "width="+(window.screen.width-8)+",height="+(window.screen.height-26)+",resizable=no,left=0,top=0,scrollbars=no, alwaysRaise=yes, fullScreen=yes";
	fsWindow = window.open(page,"fsWindow",options);
}

function setOpacity(id, x){
	if(getDOMObject(id).filters){
		getDOMObject(id).filters.alpha.opacity = x;
		return(true);
	}else if(getDOMObject(id).style.MozOpacity != null){
		getDOMObject(id).style.MozOpacity = 0.01 * x;
		return(true)
    }
	return(false);
}

function getNewFadeId(){
	fadeCounter += 1;
	fades[fadeCounter] = true;
	return(fadeCounter);
}

function cancelFade(id){
	fades[id] = false;
}

function fadeOut(layer, x, step, fadeId){
	if(fades[fadeId]){
		if(!(x < 0) && setOpacity(layer, x)){
			x -= step;
			window.setTimeout("fadeOut(\"" + layer + "\", " + x + ", " + step + ", " + fadeId + ")", 40);
		}else{
			setOpacity(layer, 0);
			getDOMObject(layer).style.visibility = "hidden";
			setOpacity(layer, 100);
		}
	}
}

function getDOMX(id){
	var x = 0;
	if(getDOMObject(id)){
	// nn4up ---------------
		if (isNS4){
			if ( getDOMObject(id) != window )			x = getDOMObject(id).pageX;
		}
	// gk, iemac, iewin ---------------
		else{
			currentX = 0;
			if ( getDOMObject(id).offsetParent ){
				object = getDOMObject(id);
				while ( object.offsetParent ){
					currentX += object.offsetLeft;
					object = object.offsetParent;
				}
			}else if ( getDOMObject(id).x ){
				currentX += getDOMObject(id).x;
			}
			x = currentX;
			if (isIE5Mac){
				x += parseInt( "0" + document.body.currentStyle.marginLeft, 10  );
			}
		}
	}
	return x;
}

function getDOMY(id){
	var x = 0;
	if(getDOMObject(id)){
	// nn4up ---------------
		if (isNS4){
			if ( getDOMObject(id) != window )			x = getDOMObject(id).pageY;
		}
	// gk, iemac, iewin ---------------
		else{
			currentX = 0;
			if ( getDOMObject(id).offsetParent ){
				object = getDOMObject(id);
				var IECorrection = 0;
				while ( object.offsetParent ){
					currentX += object.offsetTop;
					object = object.offsetParent;
				}
			}else if ( getDOMObject(id).y ){
				currentX += getDOMObject(id).y;
			}
			x = currentX;
			if ( isIE5Mac){
				x += parseInt( "0" + document.body.currentStyle.marginLeft, 10  );
			}
		}
	}
	return x;
}

function getWindowHeight(){
	if(window.innerHeight){
		return(window.innerHeight);
	}else{
		if(document.documentElement.clientHeight){
			return(document.documentElement.clientHeight);
		}else{
			return(document.body.offsetHeight);
		}
	}
}

function getWindowWidth(){
	if(window.innerWidth){
		return(window.innerWidth);
	}else{
		if(document.documentElement.clientWidth){
			return(document.documentElement.clientWidth);
		}else{
			return(width = document.body.offsetWidth);
		}
	}
}

function scriptDebugger(){
  this.anchorIndex = 0;
  if(debugMode){
    this.debugWindow = window.open("", "debugWindow", "width=400,height=350,status=no,resizable=yes,scrollbars=yes");
	  this.debugWindow.document.write("<html><body>\n");
	}
	
	this.alert = function(str){
	  if(debugMode){
	    this.anchorIndex++;
		  this.debugWindow.document.write("<a name=\"" + this.anchorIndex + "\"></a>" + str + "<br>\n");
			this.debugWindow.scrollTo(0, 10000);
		}
	}
	
	this.evtAlert = function(str){
	  if(debugMode){
	    this.anchorIndex++;
	    this.debugWindow.document.write("<a name=\"" + this.anchorIndex + "\"></a><font color=\"#ff0000\">" + str + "</font><br>\n");
			this.debugWindow.scrollTo(0, 10000);
		}
	}
}

function StretchDiv(DomId, top, right, bottom, left){
	this.id = DomId;
	this.t = top;
	this.r = right;
	this.b = bottom;
	this.l = left;
  	this.stretchX = !(left < 0 || right < 0);
	this.stretchY = !(top < 0 || bottom < 0);
	this.showId = function(){
		alert(this.id);
	}
 
	this.setMargins = function(top, right, bottom, left){
		this.t = top;
		this.r = right;
		this.b = bottom;
		this.l = left;
		this.stretchX = !(left == "" || right == "");
		this.stretchY = !(top == "" || bottom == "");
		if(getDOMObject(this.id).style.left){
			getDOMObject(this.id).style.left = left.toString() + "px";
		}else{
			getDOMObject(this.id).style.right = right.toString() + "px";
		}
		if(getDOMObject(this.id).style.top){
			getDOMObject(this.id).style.top = top.toString() + "px";
		}else{
			getDOMObject(this.id).style.bottom = bottom.toString() + "px";
		}
		this.stretch();
	}

	this.stretch = function(){
		var width, height, additional;
		additional = 0;
		additional += ((navigator.appVersion.indexOf("MSIE") != -1) && (navigator.appVersion.indexOf("MSIE 8") == -1)) ? -4 : 0;
		if(this.stretchX){
			width = getWindowWidth() - (this.l + this.r) + additional;
			getDOMObject(this.id).style.width = width.toString() + "px";
		}
		if(this.stretchY){
			height = getWindowHeight() - (this.t + this.b) + additional;
			getDOMObject(this.id).style.height = height.toString() + "px";
		}
		return(true);
	}

	this.addToEventList = function(x){
		var oldHandler = window.onresize;
		function newHandler(){
			x.stretch();
		}
		if(oldHandler){
			window.onresize = function(){ oldHandler(); newHandler(); };
		}else{
			window.onresize = function(){ newHandler(); };
		}
	}
  
	this.addToEventList(this);
	this.stretch();
}

function blinkButton(DomId, cp, onUrl, offUrl){
	this.id = DomId;
	this.isCurrentPage = cp;
	this.actUrl = onUrl;
	this.norUrl = offUrl;
	this.width = getDOMObject(this.id).width;
	this.height = getDOMObject(this.id).height;
	this.blinkOn = new Image(this.width, this.height);
	this.blinkOn.src = this.actUrl;
	this.blinkOff = new Image(this.width, this.height);
	this.blinkOff.src = this.norUrl;

	this.activate = function(){
		if(!this.isCurrentPage){
			getDOMObject(this.id).src = this.blinkOn.src;
		}
	}

	this.deactivate = function(){
		if(!this.isCurrentPage){
			getDOMObject(this.id).src = this.blinkOff.src;
		}
	}

	this.setCurrent = function(){
		this.isCurrentPage = true;
	}

	this.addEvents = function(x){
		eval("function " + this.id + "OnHandler(){ x.activate(); }");
		eval("getDOMObject(this.id).onmouseover = function(){ " + this.id + "OnHandler(); };");
		eval("function " + this.id + "OffHandler(){ x.deactivate(); }");
		eval("getDOMObject(this.id).onmouseout = function(){ " + this.id + "OffHandler(); };");
		eval("function " + this.id + "ClickHandler(){ x.setCurrent(); }");
		eval("getDOMObject(this.id).onclick = function(){ " + this.id + "ClickHandler(); };");
	}

	this.addEvents(this);
		if(this.isCurrentPage){
		getDOMObject(this.id).src = this.blinkOn.src;
	}
}

function addOnResizeHandler(handler){
	var oldHandler = window.onresize;
	eval("function newHandler(){ " + handler + " };");
	if(oldHandler){
		window.onresize = function(){ oldHandler(); newHandler(); };
	}else{
		window.onresize = function(){ newHandler(); };
	}
}

function addOnClickHandler(domId, handler){
	var oldHandler = getDOMObject(domId).onclick;
	eval("function newHandler(){ " + handler + " };");
	if(oldHandler){
		getDOMObject(domId).onclick = function(){ oldHandler(); newHandler(); };
	}else{
		getDOMObject(domId).onclick = function(){ newHandler(); };
	}
}  

function pageExit(){
//	fadeOut("innerText", 100, 5, getNewFadeId());
}

function setOnClickCursors(){
	for(var i=0; i<document.images.length; i++){
		var img = document.images[i];
		if(img.onclick){
			img.style.cursor = "pointer";
		}
	}
}

if(navigator.appVersion.indexOf("MSIE") == -1){
//if(!isUndefinedOrNull(Element)){
	Element.prototype.fadeStart = -1;
	Element.prototype.fadeEnd = 0;
	Element.prototype.fadeStep = 5;
	Element.prototype.isFading = false;
	
	Element.prototype.setOpacity = function(opacity){
		if(this.filters && this.filters.alpha){
			this.filters.alpha.opacity = opacity;
			return(true);
		}
		if(this.style.opacity != null){
			this.style.opacity = 0.01 * opacity;
			return(true);
		}
		return(false);	
	}
	
	Element.prototype.getOpacity = function(){
		if(this.filters && this.filters.alpha){
			return(this.filters.alpha.opacity);
			}
		if(this.style.opacity){
			return(this.style.opacity * 100);
		}
		return(100);
	}
	
	Element.prototype.fade = function(start, end, step){
		this.fadeStart = start;
		this.fadeEnd = end;
		this.fadeStep = step;
		if(!this.isFading){
			this.isFading = true;
			this._fader(this);
		}
	}

	Element.prototype.fadeTo = function(opacity, step){
		this.fadeEnd = opacity;
		this.fadeStep = step;
		this.fadeStart = this.getOpacity();
		deb.alert("fadeTo: fading: " + this.isFading + " start: " + this.fadeStart + " end: " + this.fadeEnd);
		if(!this.isFading){
			this.isFading = true;
			this._fader();
		}
		return(true);
	}

	Element.prototype._fader = function(){
		var me = this;
		if(this.fadeStart == this.fadeEnd){
			this.isFading = false;
		}else{
			if(this.fadeStart > this.fadeEnd){
				this.fadeStart -= this.fadeStep;
				if(this.fadeStart < this.fadeEnd){
					this.fadeStart = this.fadeEnd;
				}
			}else{
				this.fadeStart += this.fadeStep;
				if(this.fadeStart > this.fadeEnd){
					this.fadeStart = this.fadeEnd;
				}
			}
			this.setOpacity(this.fadeStart);
			window.setTimeout(function (){ me._fader() }, 40);
		}
	}
}

function embedMediaPlayer(id, width, height, autoPlay, splashImageFile){
	var placeHolder = "mediaPlaceHolder" + id;
	getDOMObject(placeHolder).innerHTML = "Loading media player";
	var fo = new SWFObject("/flash/FlowPlayerLP.swf", "flowPlayer", width, height, "7", "#ffffff", true);
	fo.addParam("allowScriptAccess", "always");
	fo.addParam("allowFullScreen", "true");
	autoPlay = autoPlay ? "true" : "false";
	splashImageFile = splashImageFile != null ? ", splashImageFile: '" + splashImageFile + "'" : "";
	fo.addVariable("config", "{ autoPlay: " + autoPlay + ", autoBuffering: " + autoPlay + ", loop: false, autoRewind: true, countryCode: 'en', playList: [ {overlayId: 'play' }, { url: '/streammedia.php?id=" + id + "' } ], initialScale: 'fit',  fullScreenScriptURL: '/js/fullscreen.js'" + splashImageFile + " }");

	fo.write(placeHolder);
}

function highlightRide(name, activeState){
	if(activeState){
		document.getElementById(name + "SideButton").className = "active";
	}else{
		document.getElementById(name + "SideButton").className = "";
	}
}

function ImageZoomer(){
	this.xMargin = 3;
	this.yMargin = 28;
	this.imageViewer = null;
	this.imageViewerIndex = 0;
	this.imageLoadedEvent = null;
	
	this.open = function(location, width, height, caption, imageViewer){
		if(imageViewer && imageViewer != null){
			this.imageViewer = imageViewer;
			this.imageViewerIndex = this.imageViewer.getCurrentImageIndex();
		}
		if(navigator.appVersion.indexOf("MSIE") != -1 || navigator.appName.indexOf("Opera") != -1){
			location = location + "?x=" + Math.round(Math.random() * 10000);
		}
		height = parseInt(height);
		width = parseInt(width);
		if(caption == null){
			caption = "";
		}
		var screenWidth = getWindowWidth();
		var screenHeight = getWindowHeight();
		var scaleFactor;
		if(width + 2 * this.xMargin > screenWidth){
			scaleFactor = width / (screenWidth - 2 * this.xMargin);
			width = width / scaleFactor;
			height = height / scaleFactor;
		}
		if(height + 2 * this.yMargin > screenHeight){
			scaleFactor = height / (screenHeight - 2 * this.yMargin);
			width = width / scaleFactor;
			height = height / scaleFactor;
		}
		width = Math.floor(width);
		height = Math.floor(height);
		this.modalLayer.setOpacity(0);
		this.modalLayer.style.visibility = "visible";
		this.popupImageLayer.style.width = width + "px";
		this.popupImageLayer.style.marginLeft = -Math.round(width / 2) + "px";
		this.popupImageLayer.style.height = height + "px";
		this.popupImageLayer.style.marginTop = -Math.round(height / 2) + "px";
		this.caption.innerHTML = caption;
		this.modalLayer.fadeTo(75, 25);
		this.popupImageLayer.style.visibility = "visible";
		this.zoomImage = IMG({"src":location, "width":width, "height": height});
		this.zoomImage.style.width = width + "px"; // internet exploder code
		this.zoomImage.style.height = height + "px"; // internet exploder code 
		this.imageLoadedEvent = connect(this.zoomImage, "onload", this, this.insertZoomImage);
		if(imageViewer != null){
			this.insertNextPrevButtons();
		}
	}
	
	this.insertNextPrevButtons = function(){
		var ci = this.imageViewerIndex;
		if(this.imageViewer.hasNextImage(ci) || this.imageViewer.hasPrevImage(ci)){
			this.nextZoomButton = new NextZoomButton(this);
			this.prevZoomButton = new PrevZoomButton(this);
		}
	}
	
	this.updatePopupImageLayer = function(location, width, height, caption){
		if(this.zoomImage){
			try{
				this.popupImageLayer.removeChild(this.zoomImage);
			}catch(e){}
		}
		if(navigator.appVersion.indexOf("MSIE") != -1 || navigator.appName.indexOf("Opera") != -1){
			location = location + "?x=" + Math.round(Math.random() * 10000);
		}
		height = parseInt(height);
		width = parseInt(width);
		if(caption == null){
			caption = "";
		}
		var screenWidth = getWindowWidth();
		var screenHeight = getWindowHeight();
		var scaleFactor;
		if(width + 2 * this.xMargin > screenWidth){
			scaleFactor = width / (screenWidth - 2 * this.xMargin);
			width = width / scaleFactor;
			height = height / scaleFactor;
		}
		if(height + 2 * this.yMargin > screenHeight){
			scaleFactor = height / (screenHeight - 2 * this.yMargin);
			width = width / scaleFactor;
			height = height / scaleFactor;
		}
		width = Math.floor(width);
		height = Math.floor(height);
		this.popupImageLayer.style.width = width + "px";
		this.popupImageLayer.style.marginLeft = -Math.round(width / 2) + "px";
		this.popupImageLayer.style.height = height + "px";
		this.popupImageLayer.style.marginTop = -Math.round(height / 2) + "px";
		this.caption.innerHTML = caption;
		this.zoomImage.src = location;
		this.zoomImage.width = width;
		this.zoomImage.style.width = width + "px";
		this.zoomImage.height = height;
		this.zoomImage.style.height = height + "px";
		this.imageLoadedEvent = connect(this.zoomImage, "onload", this, this.insertZoomImage);
	}
	
	this.updateZoomButtonActiveStates = function(){
		if(this.nextZoomButton){
			this.nextZoomButton.updateActiveState();
		}
		if(this.prevZoomButton){
			this.prevZoomButton.updateActiveState();
		}	
	}
	
	this.hasNextZoom = function(){
		return(this.imageViewer.hasNextImage(this.imageViewerIndex));
	}
	
	this.loadNextZoom = function(){
		var data = this.imageViewer.getNextZoomData(this.imageViewerIndex);
		if(data != null){
			this.imageViewerIndex = data.id;
			this.updatePopupImageLayer(data.location, data.width, data.height, data.caption);
		}
		this.updateZoomButtonActiveStates();
	}
	
	this.hasPrevZoom = function(){
		return(this.imageViewer.hasPrevImage(this.imageViewerIndex));
	}
	
	this.loadPrevZoom = function(){
		var data = this.imageViewer.getPrevZoomData(this.imageViewerIndex);
		if(data != null){
			this.imageViewerIndex = data.id;
			this.updatePopupImageLayer(data.location, data.width, data.height, data.caption);
		}
		this.updateZoomButtonActiveStates();
	}
	
	this.insertZoomImage = function(e){
		this.popupImageLayer.appendChild(this.zoomImage);
		disconnect(this.imageLoadedEvent);
	}
	
	this.close = function(e){
		this.modalLayer.setOpacity(0);
		this.modalLayer.style.visibility = "hidden";
		this.popupImageLayer.style.visibility = "hidden";
		try{
			this.popupImageLayer.removeChild(this.zoomImage);
			this.caption.innerHTML = "";
		}catch(e){}
	}
	
	this.popupImageLayer = DIV({"class": "popupImage"});
	this.closeButton = DIV({"class": "popupImageClose"}, "Close");
	this.caption = DIV({"class": "popupImageCaption"}, "");
	this.popupImageLayer.appendChild(this.closeButton);
	this.popupImageLayer.appendChild(this.caption);
	this.modalLayer = DIV({"class": "modalLayer", "style": "background-color: black;"});
	connect(this.closeButton, "onclick", this, this.close);
	connect(this.modalLayer, "onclick", this, this.close);
	document.body.appendChild(this.popupImageLayer);
	document.body.appendChild(this.modalLayer);
}


/**
BEGIN Zoombuttons
**/

function ZoomButton(imageZoomer){
	this.imageZoomer = imageZoomer;
	this.active = false;
	this.button = null;
}

ZoomButton.prototype.show = function(e){
	if(this.active){
		if(!isMSIE()){
			this.button.fadeTo(100, 20);		
		}else{
			this.button.fadeTo(75, 15);
			this.button.firstChild.fadeTo(100, 20);
		}
	}
}

ZoomButton.prototype.hide = function(e){
	this.button.fadeTo(0, 20);
	if(isMSIE()){
		this.button.firstChild.fadeTo(0, 20);
	}
}

ZoomButton.prototype.updateVisibility = function(){
	if(this.button){
		this.button.style.visibility = this.active ? "inherit" : "hidden";
	}
}

ZoomButton.prototype.create = function(buttonId, iconURL){
	this.button = DIV({"id": buttonId});
	this.button.appendChild(IMG({"src": iconURL}));
	this.imageZoomer.popupImageLayer.appendChild(this.button);
	if(!isMSIE()){
		this.button.setOpacity(0);
	}else{
		try{
			this.button.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=0)";
			this.button.firstChild.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=0)";
		}catch(e){
			try{
				this.button.firstChild.style.filter = "alpha(opacity=0)";
			}catch(e){}
		}
	}
	this.updateVisibility();
	connect(this.button, "onclick", this, this.go);
	connect(this.button, "onmouseover", this, this.show);
	connect(this.button, "onmouseout", this, this.hide);
}

NextZoomButton.prototype = new ZoomButton();
NextZoomButton.prototype.constructor = NextZoomButton;

NextZoomButton.prototype.go = function(e){
	if(this.active){
		this.imageZoomer.loadNextZoom();
	}
	removeSelections();
}

NextZoomButton.prototype.updateActiveState = function(){
	this.active = this.imageZoomer.hasNextZoom();
	this.updateVisibility();
}

function NextZoomButton(imageZoomer){
	this.imageZoomer = imageZoomer;
	this.button = null;
	this.updateActiveState();
	this.create("zoomNextButton", "/images/zoomnext.png");
}

PrevZoomButton.prototype = new ZoomButton();
PrevZoomButton.prototype.constructor = PrevZoomButton;

PrevZoomButton.prototype.go = function(e){
	if(this.active){
		this.imageZoomer.loadPrevZoom();
	}
	removeSelections();
}

PrevZoomButton.prototype.updateActiveState = function(){
	this.active = this.imageZoomer.hasPrevZoom();
	this.updateVisibility();
}

function PrevZoomButton(imageZoomer){
	this.imageZoomer = imageZoomer;
	this.button = null;
	this.updateActiveState();
	this.create("zoomPrevButton", "/images/zoomprev.png");
}

/**
EIND Zoombuttons
**/

function init(ba, ab){
	docInit();
	setOnClickCursors();
	imageZoomer = new ImageZoomer();
	if(document.getElementById("googleMap") && mozileInterface == null){
		var latlng = new google.maps.LatLng(52.980002, 5.916641);
		var myOptions = {
		  zoom: 7,
		  center: latlng,
		  mapTypeId: google.maps.MapTypeId.ROADMAP
		};

		var map = new google.maps.Map(document.getElementById("googleMap"), myOptions);
		
		var marker = new google.maps.Marker({
			position: latlng, 
			map: map,
			title:"Mondial headquarters"
		}); 
	}	

	try{
		if(mozile){
			mozile.setSaveOption("custom", "url", document.location.href);
			mozile.setSaveOption("custom", "content", "editor");
		}
	}catch(e){}// MSIE zuigt!
}
