/******************************************************/
// - Javascript functions.js from sociaalraadslieden.nl 
// - written by Bas de Winter @since Fri Nov 24 2006 
// - dependent on prototype.js version 1.4.0
/******************************************************/

function loadFunction() {
	resizeImageResolutions();

	var viewportHeight = pixelToEm( getWindowHeight() );
	var viewportWidth = pixelToEm( getWindowWidth() );
	
	var topmenuHeight = 8.8125;
	var rightfootHeight = 2.187;
	if( $("homecontent") ) {
		var rightfootHeight = 6.1875;
	}
	var homecontentHeight = 12.5;
	var contentHeight = viewportHeight - topmenuHeight;
	var tekstcontentWidth = 39.5;
	var homecontentWidth = 26.25; 
	var imgWidth800 = 19.875;
	var imgWidth1024 = 22.0625;

	if(getResolution() == 800) {
		imgWidth = imgWidth800;
	} else {
		imgWidth = imgWidth1024;
	}

	if ( $("homecontent") ) {
		contentWidth = homecontentWidth;
	} else {
		contentWidth = tekstcontentWidth;
	}

	contentHeight = contentHeight - rightfootHeight;
	if ( $("content") ) {
		Element.setStyle( "content", {"height":  contentHeight + "em"} );
	}
	else {
		contentHeight = contentHeight - homecontentHeight;
		if (contentHeight < 4) {
			contentHeight = 4;
		}
		Element.setStyle( "midfooter", {"height":  contentHeight + "em"} );
	}

	var contentmarginWidth = viewportWidth - (contentWidth + imgWidth);
	if(contentmarginWidth < 0) {
		contentmarginWidth = 0;
	}
	if( $("contentmargin") ) {
		Element.setStyle( "contentmargin", {"width":  contentmarginWidth + "em"} );
	}

	if ( $("image") ) {
		Element.setStyle( "image", {"width":  imgWidth + "em"} );
	}
}

function pixelToEm(strValue, pixelPerEm) {
	pixelPerEm = pixelPerEm | 16;
	strValue = strValue + "";
	
	if (strValue.match(/em/i) != null) {
		strValue = parseFloat(strValue.replace( /em/i, '' ) );
	}
	else {
		strValue = parseFloat(strValue.replace(/px/i, '') );
		strValue = strValue / pixelPerEm;
	}
	return strValue;
}

function emToPixel(strValue, pixelPerEm) {
	pixelPerEm = pixelPerEm | 16;
	strValue = strValue + "";
	if (strValue.match(/em/i) != null) {
		strValue = parseFloat(strValue.replace( /em/i, '' ) );
		strValue = strValue * pixelPerEm;
	}
	return strValue;
}

function getWindowHeight() {
	var windowHeight=0;
	
	/* check which method is supported by the used browser and use that method to retrieve the viewPort width */
	
	if ( typeof( window.scrollHeight )=='number' ) {
		windowHeight=window.scrollHeight;
	}
	else if ( document.documentElement && document.documentElement.clientHeight ) {
		windowHeight = document.documentElement.clientHeight;
	}
	else if ( document.body&&document.body.clientHeight ) {
		windowHeight=document.body.clientHeight;
	}
	return windowHeight;
}

function getWindowWidth() {
	var windowWidth=0;
	
	/* check which method is supported by the used browser and use that method to retrieve the viewPort width */
	
	if ( typeof( window.scrollWidth )=='number' ) {
		windowWidth=window.scrollWidth;
	}
	else if ( document.documentElement && document.documentElement.clientWidth) {
		windowWidth = document.documentElement.clientWidth;
	}
	else if ( document.body&&document.body.clientWidth ) {
		windowWidth=document.body.clientWidth;
	}
	return windowWidth;
}

function showDetails( event ) {
	var element = Event.element( event );
	var oudActive = document.getElementsByClassName( "agendaActive" );
	for ( i=0; i<oudActive.length; i++ ) {
	Element.removeClassName( oudActive[i] , "agendaActive" );
	}
	
	Element.addClassName( element.parentNode.parentNode.parentNode , "agendaActive" );
}

/*
 * resizeImageResolutions
 *
 * Replaces the images for the 800x600 and 1024x768 ones
 *
 */
var previousResolution = 1024;
function resizeImageResolutions() {
	images = document.images;
	for(i=0; i<images.length; i++) {
		if(previousResolution==800) {
			images[i].src = images[i].src.replace(/800/g, getResolution());
		} else {
			images[i].src = images[i].src.replace(/1024/g, getResolution());
		}
	}
	previousResolution = getResolution();
}

/*
 * getResolution
 *
 * Fetches the right resolution
 *
 */
function getResolution(scale) {
	resolution = getWindowWidth();
	if (scale == "1") {
		if (resolution<=800) {
			return 800;
		} else if (resolution>=950) {
			return 1024;
		} else {
			return 900;
		}
	} else {
		if (resolution<=800) {
			return 800;
		} else {
			return 1024;
		}
	}
}

var homeSliderOffset = 0;
var homeSliderCount = 5;
var homeSliderTimeout = null;
var homeSliderInit = null;
/**
 *
 *
 *
 *
 **/
function homeSlider() {
	if (!homeSliderInit) {
		homeSliderInit = setTimeout("homeSlider()", 1000);
	}
	else {
		homeSliderOffset++;
		if (homeSliderOffset < homeSliderCount) {
			elementId = "homeSlider" + homeSliderOffset;
			Element.setStyle(elementId, {"top": (emToPixel(Element.getHeight("midfooter") - (20 * homeSliderOffset) ) ) + "px"} );
			Element.setStyle(elementId, {"visibility": "visible"} );
			
			moveSlider(elementId, 15, 3000, 30);
		}
	}
}

function moveSlider(elementId, toPos, time, steps) {
	interval = time / steps;
	moveY = toPos - parseInt($(elementId).style.top);
	diffY = moveY / steps;
	
	moveSliderDiff(elementId, diffY, toPos, interval);
}

function moveSliderDiff(elementId, diffY, endPos, interval) {
	elementTop = parseInt($(elementId).style.top);
	diffY = parseInt(diffY);
	$(elementId).style.top = (elementTop + diffY) + "px";
	
	if (parseInt($(elementId).style.top) <= parseInt(endPos) ) {
		$(elementId).style.top = endPos + "px";
		clearTimeout(homeSliderTimeout);
		homeSlider();
	}
	else {
		homeSliderTimeout = setTimeout("moveSliderDiff('" + elementId + "', '" + diffY + "', '" + endPos + "', '" + interval + "')", interval);
	}
}
