var elements_animated = false;
var content_loaded = false;
//////////////////////////////////////////////////////////////////
/////////////////// ANIMATE ELEMENTS //////////////////////////
//////////////////////////////////////////////////////////////////

var instant_loader_links;
function trigger_links() {
	if(
		(typeof instant_loader_links!='undefined') 
		&& 
		(instant_loader_links.length>0)
		) 
		{ 
		// output(instant_loader_links.length+' reset');
		instant_loader_links.removeEvents('click');
		instant_loader_links.length=0; 
	}
	instant_loader_links = $$('a.instant_load');
	instant_loader_links.addEvent('click', function() { 
		instant_link_action(this); 
		return false; 
	});
}
function instant_link_action(link) {
	var link_rel = link.getProperty('rel');
	if(link_rel!=null) {
		animate_to_state(link.rel);
	} else {
		elements_animated = true;
		show_loader();
	}
	load_content(link);
	return false;
}
function animate_to_state(state) {
	if(document.body.id!=state) {
		elements_animated = false;
		// ALLEEN PANELEN ZICHTBAAR
		document.body.id = state;
		hide_others();
		// pak cur positie, en komende positie
		var cur_beton_left = EL_beton_content.getStyle('left').toInt();
		var cur_brand_right = EL_brand_content.getStyle('right').toInt();
		var next_positions = position_stuff(false, state);
		var next_beton_left = next_positions.beton_left;
		var next_brand_right = next_positions.brand_right;
		// output(state+ 'beton: '+cur_beton_left+' -> '+next_beton_left);
		// output('brand: '+cur_brand_right+' -> '+next_brand_right);
		// animeer daarnaar
		animate_element(EL_beton_content, cur_beton_left, next_beton_left, 'left', false);
		animate_element(EL_brand_content, cur_brand_right, next_brand_right, 'right', true);
	} else {
		elements_animated = true;
	}
}

function hide_others() {
	if($('home_content')!=null) { $('home_content').dispose();	}
	if($('center_content')!=null) { $('center_content').dispose(); }
	if($('focus_content')!=null) { $('focus_content').dispose(); }
	if($('focus_bg_image')!=null) { $('focus_bg_image').dispose(); }
	if($$('.menu_list').length>0) { $$('.menu_list').each(function(element) { element.dispose(); }); }

	// HIDE NAVIGATION & FOOTER MENU
	EL_footer_menu.setStyle('display', 'none');
	EL_navigation.setStyle('display', 'none');
}
function show_others() {
	// SHOW NAVIGATION & FOOTER MENU
	EL_footer_menu.setStyle('display', 'block');
	EL_navigation.setStyle('display', 'block');
}

function animate_element(element, cur_pos, end_pos, property, update_pos) {
	cur_duration=450;
	var cur_pos_st = cur_pos+'px';
	var end_pos_st = end_pos+'px';
	if(update_pos==true) {
		var nav_animator = new Fx.Tween(
				element, 
				{ property: property, duration: cur_duration, transition: Fx.Transitions.Quart.easeIn }
			).start(cur_pos_st, end_pos_st).chain( function() { 
				elements_animated = true;
				// output('content_loaded = '+content_loaded);
				if(content_loaded==true) {
					insert_content();
				} else {
					show_loader();
				}
				position_stuff();
				show_others();
			});
	} else {
		var nav_animator = new Fx.Tween(
			element, 
			{ property: property, duration: cur_duration, transition: Fx.Transitions.Quart.easeIn }
		)
		.start(cur_pos_st, end_pos_st).chain(function() {
			elements_animated = true;
			// alert('done')
		});	
	}
}
//////////////////////////////////////////////////////////////////
/////////////////// LOAD CONTENT //////////////////////////
//////////////////////////////////////////////////////////////////
function load_content(url) {

	content_loaded = false;
	var url_to_load = escape(url.href);
	var load_file = 'http://www.boksduiven.com/_inc/_content_readers/content_reader.inc.php?load_page='+url_to_load;
	// output('load_file = '+load_file);
	sendRequest(load_file, handleRequest);
}

//////////////////////////////////////////////////////////////////
/////////////////// AJAX  //////////////////////////
//////////////////////////////////////////////////////////////////
/* -----------------------------------------------------
	INSTANT EDIT CALLBACK FUNCTIONS
 -----------------------------------------------------*/
var html_to_show;
function handleRequest(req) {
	html_to_show = req.responseText;
	content_loaded = true;
	if(elements_animated==true) {
		insert_content();
	}
	// output('elements_animated = '+elements_animated);
	/*
	hide_inserted_loader();
	*/
}
function insert_content() {
	hide_loader();
	var content_target = document.body.id;
	if(content_target=='home') {
		// CHECK OF HOME-CONTENT BESTAAT
		if($('home_content')!=null) {
			$('home_content').innerHTML = html_to_show;
		} else {
			var EL_home_content = new Element('div', {
			    id: 'home_content',
			    html: html_to_show
			});
			// INSERT
			EL_home_content.inject(EL_site_wrapper);
		}
		init_slideshows();
	} else if(content_target=='center_pages') {
		// CHECK OF CENTER-CONTENT BESTAAT
		if($('center_content')!=null) {
			$('center_content').innerHTML = html_to_show;
			$('center_content').scrollTo(0);
		} else {
			var EL_center_content = new Element('div', {
			    id: 'center_content',
			    html: html_to_show
			});
			// INSERT
			EL_center_content.inject(EL_site_wrapper);
		}
		init_slideshows();
	} else {
		var holding_element;
		if(content_target=='betonboringen') {
			holding_element = EL_beton_content;
		} else {
			holding_element = EL_brand_content;
		}
		// alert('holding_element ='+holding_element.id)
		var content_data = html_to_show.split('[##--##]');
		var focus_content = content_data[0];
		var sub_menu = content_data[2];
		var bg_img_html = content_data[1];
		// SUB MENU
		if($('page_sub_menu')!=null) { $('page_sub_menu').dispose(); }
		if(sub_menu!='') {
			var EL_sub_menu = new Element('ul', { 
				id: 'page_sub_menu',
				'class': 'menu_list',
				html: sub_menu 
			});
			EL_sub_menu.inject(holding_element, 'bottom');	
		}
		// FOCUS CONTENT
		if($('focus_content')!=null) { $('focus_content').dispose(); }
		if(focus_content!='') {
			var EL_focus_content = new Element('div', { 
				id: 'focus_content',
				html: focus_content 
			});
			EL_focus_content.inject(holding_element, 'top');	
			set_focus_top();
			init_gallery();
		}
		// BACKGROUND IMAGE
		if($('focus_bg_image')!=null) { $('focus_bg_image').dispose(); }
		if(bg_img_html!='') {
			var EL_bg_image = new Element('img', { 
				src: bg_img_html,
				id: 'focus_bg_image'
			});
			EL_bg_image.inject(EL_site_wrapper);	
		}
	}
	if(in_iHell==true) { position_stuff(true, document.body.id, true); }
	trigger_links();
}
var EL_loader;
function show_loader() {
	if(typeof EL_loader=='undefined') { EL_loader = $('ajax_loading'); }
	// ZET IN CENTRUM
	var top_val = Math.round((page_dims.window_h / 2)) - 20; 
	var left_val = Math.round((page_dims.window_w / 2)) - 20;
	EL_loader.setStyles({
		top: top_val,
		left: left_val,
		display: 'block'
	});
}
function hide_loader(){
	if(typeof EL_loader=='undefined') { EL_loader = $('ajax_loading'); }
	EL_loader.setStyle('display', 'none');
} 
/* -----------------------------------------------------
	XMLHTTP Functions
	As found on Quirksmode. You hero.
 -----------------------------------------------------*/

function sendRequest(url,callback,postData) {
	var req = createXMLHTTPObject();
	if (!req) return;
	var method = (postData) ? "POST" : "GET";
	req.open(method,url,true);
	req.setRequestHeader('User-Agent','XMLHTTP/1.0');
	if (postData)
		req.setRequestHeader('Content-type','application/x-www-form-urlencoded');
	req.onreadystatechange = function () {
		if (req.readyState != 4) return;
		if (req.status != 200 && req.status != 304) {
		//	alert('HTTP error ' + req.status);
			return;
		}
		callback(req);
	}
	if (req.readyState == 4) return;
	req.send(postData);
}

function XMLHttpFactories() {
	return [
		function () {return new XMLHttpRequest()},
		function () {return new ActiveXObject("Msxml2.XMLHTTP")},
		function () {return new ActiveXObject("Msxml3.XMLHTTP")},
		function () {return new ActiveXObject("Microsoft.XMLHTTP")}
	];
}

function createXMLHTTPObject() {
	var xmlhttp = false;
	var factories = XMLHttpFactories();
	for (var i=0;i<factories.length;i++) {
		try {
			xmlhttp = factories[i]();
		}
		catch (e) {
			continue;
		}
		break;
	}
	return xmlhttp;
}

