/*  Scripts used by Eleos website
 *  (c) 2008 WIS Services.
 *
 *  These scripts are built on the Prototype Javascript framework.
 *  For details, see the Prototype web site: http://www.prototypejs.org/
 *
 *--------------------------------------------------------------------------*/

/**
 * Set cookie variables
 */
var Cookie = {
	set: function(name, value, daysToExpire) {
    	var expire = '';
    	if (daysToExpire != undefined) {
      		var d = new Date();
      		d.setTime(d.getTime() + (86400000 * parseFloat(daysToExpire)));
      		expire = '; expires=' + d.toGMTString();
    	}
    	return document.cookie = escape(name)+"="+escape(value || '') + expire+"; path=/";
    	
  	},
  	get: function(name) {
    	var cookie = document.cookie.match(new RegExp('(^|;)\\s*' + escape(name) + '=([^;\\s]*)'));
    	return (cookie ? unescape(cookie[2]) : null);
  	},
  	erase: function(name) {
    	var cookie = Cookie.get(name) || true;
    	Cookie.set(name, '', -1);
    	return cookie;
  	},
  	accept: function() {
    	if (typeof navigator.cookieEnabled == 'boolean') {
      		return navigator.cookieEnabled;
    	}
    	Cookie.set('_test', '1');
    	return (Cookie.erase('_test') == '1');
	}
}; 

/**
 * Initialize boxes on the right
 */ 
function initRightboxes() {
	var rightboxes = $$('div.rightbox div.head'); 	
	rightboxes.each(function(rightbox){
		if(!rightbox.hasClassName('noexpand')){
			var thisContent = rightbox.next(0);
			Event.observe(rightbox, 'click', function(){
				if(thisContent.visible()){
					Effect.SlideUp(thisContent, {duration: 0.5});
					rightbox.setStyle({
						backgroundImage: 'url('+jsImgPath+'rightbox_header_icon_down.gif)'
					});
				} else {
					Effect.SlideDown(thisContent, {duration: 0.5});
					rightbox.setStyle({
						backgroundImage: 'url('+jsImgPath+'rightbox_header_icon.gif)'
					});
				}
			});
		}
	});
}

/**
 * Initialize and build floating menu on the right
 */
function initFloatingMenu() {
	if($('floating_menu_container')){
		// Get H2 headers 
		var headers = $$('.tmpl_2 h2');
		headers.each(function(header, index){
			var newLi = document.createElement('li');
			var newA = document.createElement('a');
			newA.innerHTML = header.innerHTML; 
			newA.href = '#'+index;
			newLi.appendChild(newA);
			$('floating_menu').appendChild(newLi);
			header.update('<a name="'+index+'">'+header.innerHTML+'</a>');
		});
		
		var startY = Element.cumulativeOffset($('floating_menu_container')).last();
		
		// Fix position when page is visited via back-button 
		var scrollOffsetY = Element.cumulativeScrollOffset($('floating_menu_container')).last();
		if(scrollOffsetY != 0){
			$('floating_menu_container').setStyle({
				top: startY+scrollOffsetY+'px'
			});
		}
		
		// On scroll move dialog (always on top) 
		Event.observe(window, 'scroll', function(){
			var scrollOffset = Position.realOffset($('floating_menu_container'));
			$('floating_menu_container').setStyle({
				top: scrollOffset[1]+startY+'px'
			});
			
		});
	}
}

/**
 * Increase the size of the text and store factor in cookie
 */

function increaseText() {
	var textsizeAddition = Cookie.get('TEXTSIZE');
	if(textsizeAddition == ''){
		textsizeAddition = 0;
	}
	var texts = $$('td.middle');
	texts.each(function(thisText){
		var allTextelems = thisText.descendants();
		// update each element 
		allTextelems.each(function(textelem){
			var currentSize = textelem.getStyle('fontSize'); 
			var newSize = parseInt(currentSize.sub(/px/, '')) + 1;
			textelem.setStyle({
				fontSize: newSize + 'px'
			});
		});
	});
	var updateCookie = parseInt(textsizeAddition) + parseInt(1);
	Cookie.set('TEXTSIZE', updateCookie);
}

/**
 * Decrease the size of the text and store factor in cookie
 */
function decreaseText() {
	var textsizeAddition = Cookie.get('TEXTSIZE');
	if(textsizeAddition == ''){
		textsizeAddition = 0;
	}
	var texts = $$('td.middle');
	texts.each(function(thisText){
		var allTextelems = thisText.descendants();
		// update each element
		allTextelems.each(function(textelem){
			var currentSize = textelem.getStyle('fontSize'); 
			var newSize = parseInt(currentSize.sub(/px/, '')) - 1;
			textelem.setStyle({
				fontSize: newSize + 'px'
			});
		});
	});
	var updateCookie = parseInt(textsizeAddition) - parseInt(1);
	Cookie.set('TEXTSIZE', updateCookie);
}

/**
 * Initialize departments map on right size of the site
 */
function initMaps() {
	var maps = $$('map[name="vestigingen"] area');
	maps.each(function(map){
		
		//Event.observe(map, 'click', toggleTip.bindAsEventListener(this, map.id, 'show'));
		
		Event.observe(map, 'mouseover', toggleTip.bindAsEventListener(this, map.id, 'show'));
	});
	
	var centers = $$('map[name="wooncentra"] area');
	centers.each(function(center){
		Event.observe(center, 'click', toggleTip.bindAsEventListener(this, center.id, 'show'));
		Event.observe(center, 'mouseover', toggleTip.bindAsEventListener(this, center.id, 'show'));
	});
	
	var atcs = $$('map[name="atcs"] area');
	atcs.each(function(atc){
		Event.observe(atc, 'click', toggleTip.bindAsEventListener(this, atc.id, 'show'));
		Event.observe(atc, 'mouseover', toggleTip.bindAsEventListener(this, atc.id, 'show'));
	});
}

/**
 * Initialize flipsections (on homepage: wachttijden, faq)
 */
function initFlips() {
	var flipSections = $$('div.wait h4');
	flipSections.each(function(flipSection){
		var thisContent = flipSection.next(0);
		Event.observe(flipSection, 'click', function(){
			if(thisContent.visible()){
				Effect.SlideUp(thisContent, {duration: 0.3});
				flipSection.up(0).setStyle({
					backgroundImage: 'url('+jsImgPath+'plus_icon.gif)'
				});
			} else {
				Effect.SlideDown(thisContent, {duration: 0.3});
				flipSection.up(0).setStyle({
					backgroundImage: 'url('+jsImgPath+'min_icon.gif)'
				});
			}
		});
	});	
}

/**
 * Prints the content of the page
 */
function printContent() {
	if($$('td.middle').length > 0){
		var pageContent = $$('td.middle').first().innerHTML;
		pageContent = pageContent.gsub(/(display|DISPLAY):\s*none/, 'display: block');
		var stylesheets = $$('link');
		var popupWin = window.open("", "newwin");
		// Add title
		popupWin.document.write('<html><head><title>Pagina - Printversie</title>');
		// Add stylesheets
		stylesheets.each(function(stylesheet){
			if(stylesheet.media == 'print'){
				popupWin.document.write('<link href="'+stylesheet.href+'" rel="stylesheet" type="text/css" media="screen, print"/>');
			}
		});
		popupWin.document.write('</head>');
		
		// Add body and content
		popupWin.document.write('<body>'+pageContent+'</body></html>');
		popupWin.document.close();
		popupWin.print();
	}
}

/**
 * Show/hide tooltip on department map
 * 
 * @param tipTxt HTML content tooltip
 * @param mode Expand or collapse
 */
function toggleTip(e){
	var data = $A(arguments);
	if(!e) var e = window.event;
	if (e.stopPropagation) {
		e.stopPropagation();
	}else{
		e.cancelBubble = true;
	}
	if(data[2] == 'show'){
		$('info_tip').update(data[1]);
		new Effect.Appear('info_tip', {afterFinish: function(){
			Event.observe(document.body, 'click', toggleTip.bindAsEventListener('',''));	
		}});
		
	} else {
		new Effect.Fade('info_tip');
	}
}

/**
 * Handles employee data (interview)
 */
function initEmployees() {
	// Get H2 headers 
	var headers = $$('#hidden_story h2');
	var answers = new Array();
	headers.each(function(header, index){
		var headerNextSiblings = header.nextSiblings();
		var arraycontent = "";
		headerNextSiblings.each(function(elm){
			if(elm.tagName == 'H2'){
				throw $break;
			} else {
				arraycontent += elm.innerHTML;
			}
		});
		var questionLink = document.createElement('button');
		questionLink.innerHTML = header.innerHTML;
		questionLink.href = 'javascript: void(0)';
		$(questionLink).observe('click', function(){
			if(index == (headers.length - 1)){
				$('employee_answer').innerHTML = '<div><h2>' + header.innerHTML + '</h2>' +arraycontent + '</div>';	
			} else {
				$('employee_answer').innerHTML = '<div><h2>' + header.innerHTML + '</h2>' +arraycontent + '<br/><br/></div>';
				var nextLink = document.createElement('a');
				nextLink.href = 'javascript: void(0)';
				nextLink.innerHTML = 'volgende vraag';
				nextLink.className = 'next_question';
				$(nextLink).observe('click', function(){
					$(questionLink).next(0).click();
				});
				$('employee_answer').down(0).appendChild(nextLink);
			}
			Effect.SlideDown('employee_answer', {duration: 0.3});
		});
		$('employee_questions').appendChild(questionLink);
		answers.push(arraycontent);
		
		// Onload show first answer 
		if(index == 0){
			if(headers.length > 1){
				$('employee_answer').innerHTML = '<div><h2>' + header.innerHTML + '</h2>' +arraycontent + '<br/><br/></div>';
				var nextLink = document.createElement('a');
				nextLink.href = 'javascript: void(0)';
				nextLink.innerHTML = 'volgende vraag';
				nextLink.className = 'next_question';
				$(nextLink).observe('click', function(){
					$(questionLink).next(0).click();
				});
				$('employee_answer').down(0).appendChild(nextLink);
			} else {	
				$('employee_answer').innerHTML = '<div><h2>' + header.innerHTML + '</h2>' +arraycontent + '</div>';
			}
			Effect.SlideDown('employee_answer', {duration: 0.3});	
		}
		
	});
}
 
 /**
  * Window onload functions
  */
 Event.observe(window, 'load', function(){
 	initRightboxes();
 	initMaps();
 	initFlips();
 	initFloatingMenu();
 	
 	// Set textsize 
 	var texts = $$('td.middle');
 	
 	if(texts.length > 0){
 		
 		var initSize = Cookie.get('TEXTSIZE');
		if(initSize == '' || initSize == null){
			initSize = 0;
		}
		
		
		
			texts.each(function(thisText){
				// Place content header 
				thisText.insert({top: $('content_heading')});
				$('content_heading').show();
				
				var allTextelems = thisText.descendants();
				// update each element 
				allTextelems.each(function(textelem){
					var currentSize = textelem.getStyle('fontSize'); 
					var newSize = parseInt(currentSize.sub(/px/, '')) + parseInt(initSize);
					textelem.setStyle({
						fontSize: newSize + 'px'
					});
				});
				
				// Fix sorted lists (surround content with span's)
				var allOls = thisText.select('ol li');
				allOls.each(function(sortedListItem){
					sortedListItem.update('<span>'+sortedListItem.innerHTML+'</span>')
				});
			});
			
		
 	}
 	
 	// On employees page ... 
 	if($('hidden_story')){
 		initEmployees();
 	}
 });
