/**
  * Javascript meuk
  *
  * Copyright Lars Op den Kamp 2008
  * http://lars.opdenkamp.eu/
  */

/*
	0 = mc1 visible, mc2 invisible
	1 = mc2 visible, mc1 invisible
*/

var last_pos = -1;
var next_page = 0;
var ajax_waiting = 0;
var ajax_changing = 0;
var ajax_last_page = 0;
var last_response = '';
var last_height = -1;
var div_status = 0;
var scroll_1;

/*
 * Restarts the scrollbars
 * Changed: executes all javascript that is present in the response
 */
function restart_scrollbars()
{
	return;
	document.observe('dom:loaded', function()
	{
		last_response.evalScripts();
	});
}

function move_active_block(mv)
{
	if (last_pos != mv)
	{
		var dif = (mv - last_pos) * 50;
		var newcolor;
		
		if (mv == 1)
			newcolor = '#759075'; /* groen */
		else if (mv == 2)
			newcolor = '#907575'; /* rood */
		else if (mv == 3)
			newcolor = '#757590'; /* blauw */
		else if (mv == 4)
			newcolor = '#757575'; /* grijs */
		
		last_pos = mv;
		
		new Effect.Parallel([
			new Effect.Move($('nav-blok'), { x: 0, y: dif, mode: 'relative' }), 
  		new Effect.Morph($('nav-blok'), {
  			style: { background: newcolor }}
  		),
  		new Effect.Morph($('container-rechts'), {
  			style: { background: newcolor }}
  		)
		], { 
  		duration: 0.75
		});
	}
}

/**
  * Get a page via ajax
  * @param part The part to change
  * @param page The page to get
  */
function ajax_get(page)
{
	if (ajax_waiting == 1)
	{
		next_page = page;
		return false;
	}
	
	ajax_waiting = 1;
	ajax_last_page = page;
	
	if (scroll_1 != undefined) scroll_1.disable();
	
	setTimeout('ajax_wait_image()', 500);
	
	new Ajax.Request
	(
		'ajax.php',
		{
			method: 'get',
			parameters: 'block=' + page,
			onComplete: change_content
		}
	);
}

/*
 * Finishes the ajax request
 * - clears the previous content
 * - loads next page (if there is any)
 * - restarts scrollbar(s) (if there was no next page)
 */
function ajax_finish()
{
	ajax_clear_wait_image();
	
	if (next_page != 0 && ajax_last_page != next_page)
	{
		var tmp = next_page;
		next_page = 0;
			
		ajax_waiting = 0;
		setTimeout('ajax_get("' + tmp + '")', 250);
	}else
	{
		ajax_waiting = 0;
		next_page = 0;
		restart_scrollbars();
	}
	
	last_height = 0;
}

/**
  * Change content of the container
  * @param request The request
  */
function change_content(request)
{
	ajax_changing = 1;
	last_response = request.responseText;
	var ht = get_window_height() - 200;
	
	last_response = last_response.gsub('{{window_height}}', ht);
	
	$('container-rechts').update(last_response);
	
	ajax_finish();
}

function clear_div(div)
{
	$(div).innerHTML = '';
}

function ajax_wait_image()
{
	if (ajax_waiting && !ajax_changing && $('even_wachten') != undefined)
	{
		$('even_wachten').setStyle('visibility:visible');
	}
}

function ajax_clear_wait_image()
{
	ajax_changing = 0;
	$('even_wachten').setStyle('visibility:hidden');
}

function get_window_height() {
	var myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' )
	{
		//Non-IE
		myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) )
	{
    //IE 6+ in 'standards compliant mode'
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) )
  {
    //IE 4 compatible
    myHeight = document.body.clientHeight;
  }
  
  return myHeight;
}

function change_content_height()
{
	// wait for the changes to complete
	if (ajax_changing == 0 && $('s_content') != undefined && $('main_content') != undefined)
	{
		var ht = get_window_height();
	
		if (last_height != ht)
		{
			$('s_content').setStyle('height: '+(ht - 200)+'px');
			
			restart_scrollbars();
			if (scroll_1 != null)
			{
				scroll_1.scrollTo('top');
			}
			
			last_height = ht;
		}
	}
	
	setTimeout('change_content_height()', 50);
}

function set_cookie( name, value, expires, path, domain, secure )
{
	var today = new Date();
	today.setTime( today.getTime() );
	
	if ( expires ) Expires = expires * 1000 * 60 * 60 * 24;
	
	var expires_date = new Date( today.getTime() + (expires) );

	document.cookie = name + "=" +escape( value ) +
		( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
		( ( path ) ? ";path=" + path : "" ) +
		( ( domain ) ? ";domain=" + domain : "" ) +
		( ( secure ) ? ";secure" : "" );
}

function get_cookie( check_name ) {
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );


		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}

function under_construction()
{
	var cookie = get_cookie('under-construction-shown');
	
	if (cookie == null)
	{
		new Effect.Appear('under-construction');
	}
}