var ajax_url = '/konfigurator/web/konfigurator.php/ajax';
var ajax_render_url = '/konfigurator/web/konfigurator.php/ajax_render';

var url_steps = '/konfigurator-steps.html';
var url_popup = '/konfigurator.html';


var fach_x;
var fach_y;

var config_type = 'steps';
var todo = '';



//$(function() {
$(document).ready( function() {
		if(config_type=='popup')
		{
			//alert("popup");
			doOnStart();
		}
		if(config_type=='steps')
		{
			//doOnStartSteps();
		}
		if(config_type=='serien')
		{
			var s = 'http://mooko24.page-center.de/konfigurator/web/konfigurator.php/main/list';
			
			$.ajax({
				type: "GET",
				url: s,
		 		async: true,
		 		success: function(html) {
		      		$('#moebel_liste').empty();
					$('#moebel_liste').append( html);
		  		}
			});
			
		}
	});
function doOnStart()
{
	//alert('doOnStart');//return;
	config_type = 'popup';
	// Show menu when #myDiv is clicked
	
	

	
					$("#m_map_teaser").contextMenu({
						menu: 'myMenu'
					},
						function(action, el, pos) {
						//alert(
						//	'Action: ' + action + '\n\n' +
						//	'Element ID: ' + $(el).attr('id') + '\n\n' + 
						//	'X: ' + pos.x + '  Y: ' + pos.y + ' (relative to element)\n\n' + 
						//	'X: ' + pos.docX + '  Y: ' + pos.docY+ ' (relative to document)'
						//	);
						});
					//});
	
	
	
	//$('.map').maphilight();
	
	$("#config").dialog({
	
			resizable: true,
			draggable: true,
			autoOpen: false,
			height: 600,
			width: 650,
			modal: true,
			close: function() { 
			//$(this).dialog('destroy');
			//destroy();
			}
		});
	refreshImage();
}


function refreshImage()
{
	//alert("refreshImage");
	checkTodo();
	
}

function checkTodo()
{
	
	var s = ajax_url + '/todo?zeit=' + Math.random();

	$.ajax({
		type: "GET",
		url: s,
 		async: true,
 		success: function(html) {
      		handleToDo(html);
  		}
	});
	
}


function handleToDo(html)
{
	
	//alert(html);
	if(html=='moebel_schraege')
	{
		//alert(html);
		renderConfig('moebel_schraege');
		return true;
	}
	
	if(html=='moebel_config')
	{
		//alert(html);
		renderConfig('moebel_config');
		return true;
	}
	
	if(config_type=='steps')
	{
		refreshInfoSteps();
	}
	
	if(html=='moebel_reihen')
	{
		//alert(html);
		renderConfig('moebel_reihen');
		return true;
	}
	if(html=='moebel_sockel_config')
	{
		renderConfig('moebel_sockel_config');
		return true;
	}
	if(html=='moebel_texture_config')
	{
		renderConfig('moebel_texture_config');
		return true;
	}
	if(html=='moebel_griff_config')
	{
		renderConfig('moebel_griff_config');
		return true;
	}
	if(html=='moebel_reihen_config')
	{
		renderConfig('moebel_reihen_config');
		return true;
	}
	
	if((html=="" || html=="ok") && config_type=="steps")
	{
		document.location.href=url_popup;
		return;
	}
	
	if(config_type!='steps')
	{
		
		var s = ajax_render_url + '/image?zeit=' + Math.random();// 'ajax_render.php?aktion=image&val=' + Math.random();
		
		$('#moebel_image').empty();
		$('#moebel_image').append('<img border = "0" id="map_teaser" class="map" usemap="#m_map_teaser" src="'+s+'" alt="" />');
		
		b = refreshMap();
		
		b = refreshInfoShort();
		
		b = refreshInfo();
		
		
	}
}

function renderConfig(url)
{	 
	
	u = ajax_render_url + '/' + url;
	if(u.search(/\?.+/) == -1)
	{
		u = u + '?zeit=' + Math.random();
	}
	else
	{
		u = u + '&zeit=' + Math.random();
	}

	$.ajax({
		type: "GET",
		url: u,
	 	async: true,
	 	success: function(html) {
	      		if(config_type=='steps')
				{
					renderStepOverview(url);
					$('#step-config').empty();
					$('#step-config').append( html);
					
				}
				else
				{
					$('#config').empty();
					$('#config').append( html);
					$('#config').dialog('open');
				}
				initLightBox();
	  	}
	});

	
	
}

function renderStepOverview(url)
{
	var u = ajax_render_url + '/step_overview?akt_step=' + url + '&zeit=' + Math.random();
	
	$.ajax({
		type: "GET",
		url: u,
	 	async: true,
	 	success: function(html) {
	      		if(config_type=='steps')
				{
					$('#step_overview').empty();
					$('#step_overview').append( html);
					
				}
		}
	});
}


function resetMoebel(url)
{
	var b = confirm("Möchten Sie das Möbelstück wirklich zurücksetzen?");
	if(b)
	{
		doAction('moebel_reset');
		if(url) doAction('moebel_reset', url);//document.location.href=url;
		else doAction('moebel_reset', url_steps);//document.location.href=url_steps;
	}
}

function clearDebug()
{
	$('#debug').empty();
	$('#ajax_output').empty();
}

/*function doReq(url)
{

	$('#debug').prepend('http://blackserve' + url + '<br/>');
	 
	var html = $.ajax({
	 url: url,
 	async: false
	}).responseText;

	
	return html;
}*/
/*
*/
$(function() {

	/*$("#config").dialog({

		resizable: true,
		draggable: true,
		autoOpen: false,
		height: 540,
		width: 680,
		modal: true,
		close: function() { 
		//$(this).dialog('destroy');
		//destroy();
		}
	});*/
});




function initLightBox()
{
	//alert('init lightbox');return;
	$(function(){
			$.Lightbox.construct({
				show_linkback:	false,
				text: {
					image:		'',
					close: 'schließen',
					closeInfo:'Klicken Sie irgendwo, um das Bild zu schließen'
				},
				opacity: 0.5,
				show_helper_text: true,
				download_link: false
			});
		});

}

function doAction(url, href)
{	 
//	alert(ajax_url + '/' + url);
	u = ajax_url + '/' + url;
	if(u.search(/\?.+/) == -1)
	{
		u = u + '?zeit=' + Math.random();
	}
	else
	{
		u = u + '&zeit=' + Math.random();
	}
	//alert(u);
	$.ajax({
		type: "GET",
		url: u,
 		async: true,
 		success: function(html) {
 			if(href) document.location.href=href;
      		else checkErrors(html);
  		}
	});
	//var html = doReq(u);
//alert(html);
	//checkErrors(html);
	
	//return html;
}

function doLinkAction(url)
{	 
	
	var html = doAction(url);
	
	//alert(html);
	$('#config').dialog('close');
	$('#config').empty();
	
	
	refreshImage();
}

function renderSubConfig(div_id, url)
{	 
	$('#debug').append(url + '<br/>');
	 
	 u = ajax_render_url + '/' + url;
	 
	 if(u.search(/\?.+/) == -1)
	{
		u = u + '?zeit=' + Math.random();
	}
	else
	{
		u = u + '&zeit=' + Math.random();
	}
	s = '#' + div_id;
	$.ajax({
		type: "GET",
		url: u,
 		async: true,
 		success: function(html) {
 			
      		$(s).empty();
			$(s).append(html);
			initLightBox();
  		}
	});
	
	/*var html = doReq(u);

	s = '#' + div_id;
	
	$(s).empty();
	$(s).append(html);
	initLightBox();*/
}
function saveSubConfig(url)
{	 
	$('#debug').append(url + '<br/>');
	 
	 $.ajax({
		type: "GET",
		url: ajax_url + '/' + url,
 		async: true
	});
	
//	var html = doReq(ajax_url + '/' + url);

}

function checkErrors(html)
{
	$('#hinweis').empty();
	//alert(html);
	if(html!="" && html!="ok")
	{
		alert(html);
		
		if(html.substr(0,8)=="Warnung ")
		{
			
			$('#hinweis').append(html);
		}
	}
}

function doConfig(form)
{	 
	

	var s='';
	var aktion = '';
	
	$('input').each(function(i) {
		if(this.name!='') {
			
			if(this.name=='aktion') aktion = this.value;
			
			if(this.type=='checkbox' || this.type=='radio')
			{
				if(this.checked)
				{
					s += this.name + '=' + this.value + '&';
				}
			}
			else
			{
				s += this.name + '=' + this.value + '&';
			}
		}
	});
      //alert(ajax_url + '/' + aktion + '?zeit='+Math.random()+'&' + s);

	
	$.ajax({
		type: "GET",
		url: ajax_url + '/' + aktion + '?zeit='+Math.random()+'&' + s,
 		async: true,
 		success: function(html) {
 			
			
			$('#config').dialog('close');
	
			$('#config').empty();
			checkErrors(html);
	
			refreshImage();
  		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
 			
			
		  //alert(textStatus);
		  //alert(errorThrown);
  		}
		
	});
	
	//var html = doReq(ajax_url + '/' + aktion + '?zeit='+Math.random()+'&' + s);

	

}

/**/



function refreshMap()
{
	s = ajax_render_url + '/map?zeit=' + Math.random();
	//alert(s);
	//return;
	$.ajax({
	type: "GET",
	 url: s,
 	async: true,
 	success: function(html) {
      		document.getElementById('m_map_teaser').innerHTML = html;
      		$('.map').maphilight();
  	}
	});
}

function refreshPreis()
{
	
	//var s = ajax_render_url + '/preis?zeit=' + Math.random();
	//$('#moebel_preis').empty();
	//$('#moebel_preis').append('Aktueller Preis: ' + doReq(s) + ' €');

}



function refreshInfo()
{

	
	var s = ajax_render_url + '/moebel_info?zeit=' + Math.random();
	//alert(s);
	//return;
	$.ajax({
		type: "GET",
	 	url: s,
 		async: true,
 		success: function(html) {
      		//alert(html);
      		$('#moebel_info').empty();
			$('#moebel_info').append(html);
	
			initLightBox();
  		}
	});


}

/**/

function refreshInfoShort()
{
	
	var s = ajax_render_url + '/moebel_info_short?zeit=' + Math.random();
	
	

	$.ajax({
		type: "GET",
	 	url: s,
 		async: true,
 		success: function(html) {
      		$('#moebel_info_short').empty();
			$('#moebel_info_short').append(html);
  		}
	});
	
}


function refreshInfoSteps()
{
	
	var s = ajax_render_url + '/moebel_info_steps?zeit=' + Math.random();
	
	

	$.ajax({
		type: "GET",
	 	url: s,
 		async: true,
 		success: function(html) {
      		$('#moebel_info_steps').empty();
			$('#moebel_info_steps').append(html);
  		}
	});
	
}



function showHide(id)
{
	if(document.getElementById(id).style.display == "none") document.getElementById(id).style.display="inline";
	else document.getElementById(id).style.display = "none";

}

function setFach(x,y)
{

	kd_do_hide = false;
	fach_x = x;
	fach_y = y;

	//alert(fach_x);
}

function doOnStartSteps()
{
	//config_type = 'steps';
	
	//refreshImage();
}



function setMoebel(id)
{
	 

	u = ajax_url + '/moebel_by_id/id/' + id;
	if(u.search(/\?.+/) == -1)
	{
		u = u + '?zeit=' + Math.random();
	}
	else
	{
		u = u + '&zeit=' + Math.random();
	}
	
	$.ajax({
		type: "GET",
		url: u,
 		async: true,
 		success: function(html) {
      		document.location.href=url_popup;
  		}
	});
	
	

}

