var MC_Configurator = {
   cameraZoom: 0
  ,cameraRot: 0
  ,viewState: ''
  ,toggleView: null
  ,slideZoom: null
  ,slideRotate: null
  
  ,camZoomSlide2Val: function(v)
  {
    return 28 - v.toInt();
  }
  
  ,camZoomVal2Slide: function(v)
  {
    return 28 - v.toInt();
  }
  
  ,camRotSlide2Val: function(v)
  {
	if (v>=116)
	{
		return (v.toInt()-116);
	}
	else
	{
    	return -116+v.toInt();
	}	
  }
  
  ,camRotVal2Slide: function(v)
  {
	if (v>=0)
	{
		return (v.toInt()+116);
	}
	else
	{
    	return 116+v.toInt();
	}
  }
  
  ,evtChgRotate: function(step)
  {
//    console.log(step, this.camRotSlide2Val(step), this.camRotVal2Slide(this.camRotSlide2Val(step)));
//    $('upd').set('html', step); 
  }
  
  ,evtCplRotate: function(step)
  {
    setRotate(this.camRotSlide2Val(step));
  }
  
  ,evtChgZoom: function(step)
  {
//    $('upd').set('html', step); 
  }
  
  ,evtCplZoom: function(step)
  {
    setZoom(this.camZoomSlide2Val(step));
  }
  
  ,changedCoat: function(coat)
  {
    if ((el = $('mit_husse')))
    {
      switch (coat.mc_bezuege_art)
      {
        case 'stoff' :
          el.disabled = false;
          break;
        case 'leder' :
          el.disabled = true;
          el.checked = false;
          break;
      }
    }
  }
  
  ,changedFoot: function()
  {

  }
  
  ,init: function()
  {
    if ($('zoom_area'))
    {
      this.slideZoom = new Slider($('zoom_area').getFirst(), $('zoom_knob'), {
         steps: 28
        ,onChange: this.evtChgZoom.bind(this)
        ,onComplete: this.evtCplZoom.bind(this)
      });
      this.slideZoom.set(this.camZoomVal2Slide(this.cameraZoom));
      
      
      this.slideRotate = new Slider($('rotate_area').getFirst(), $('rotate_knob'), {
         steps: 232
        ,onChange: this.evtChgRotate.bind(this)
        ,onComplete: this.evtCplRotate.bind(this)
      });
      this.slideRotate.set(this.camRotVal2Slide(this.cameraRot));


			if (this.viewState == 'perspektivisch')
			{
				$('camera_ansicht').setProperty('class', 'camera_ansicht_pers');
			}
			else
			{
				$('camera_ansicht').setProperty('class', 'camera_ansicht_top');
			}

 			$('camera_ansicht').addEvents({
				'click': function() {
					if (this.viewState=='aufsicht')
					{
						$('camera_ansicht').setProperty('class', 'camera_ansicht_pers')
						this.viewState = 'perspektivisch';
						setAnsicht('perspektivisch');
					}
					else
					{
						$('camera_ansicht').setProperty('class', 'camera_ansicht_top')
						this.viewState = 'aufsicht';
						setAnsicht('aufsicht');
					}
				}	
			
			});
    }
    

    if ($('the_card'))
    {
      new Request ({url: '/plugs/cart/moebel_cart_controller.php',
        method: 'post',
        onComplete: getCartInnerHtml
      }).send();
    }
    else
    {
      console.log('div[id=the_cart] not present ...')
    }
  }
  
  ,initTips: function()
  {
    $$('.cart-desc').each(function(el){el.destroy();});
  
    new Tips($$('.tip'), {
      className: 'cart-desc'
    });
  }
}
window.addEvent('reInitTips', MC_Configurator.initTips.bind(MC_Configurator));


//Moebel

var calcedGesamtbreite = 100;


function setSitztiefeSchenkel(value, moebel)
{
  new Request ({url: '/plugs/cart/moebel_cart_controller.php',
    data: 'sitztiefe_schenkel=' + value,
    method: 'post',
    onComplete: getCartInnerHtml
    }).send();
}


function toggleKundeExpertModus(value, moebel_uid)
{
  new Request ({url: '/plugs/cart/moebel_cart_controller.php',
   data: 'toggle_kunde_expert='+value,
   method: 'post',
   async: false,
   onComplete: getCartInnerHtml
   }).send();

   location.href="/konfigurator/moebel/konfigurator.php?moebel_uid="+moebel_uid;
}


function toggleOGL(value, moebel_uid)
{
  new Request ({url: '/plugs/cart/moebel_cart_controller.php',
   data: 'toggle_ogl='+value,
   method: 'post',
   onComplete: getCartInnerHtml
   }).send();

   location.href="/konfigurator/moebel/konfigurator.php?moebel_uid="+moebel_uid;
}

function setAnsicht(value)
{

   new Request ({url: '/plugs/cart/moebel_cart_controller.php',
   data: 'camera_ansicht=' + value,
   method: 'post',
   onComplete: getCartInnerHtml
   }).send();  
}


function setZoom(value)
{

  	new Request ({url: '/plugs/cart/moebel_cart_controller.php',
    	data: 'camera_zoomon=' + value,
    	method: 'post',
    	onComplete: getCartInnerHtml
    	}).send();
}

function setRotate(value)
{
	
	new Request ({url: '/plugs/cart/moebel_cart_controller.php',
    	data: 'camera_rotation=' + value,
    	method: 'post',
    	onComplete: getCartInnerHtml
    	}).send();
	//   if (value == 'on')
	//   {
	//   	new Request ({url: '/plugs/cart/moebel_cart_controller.php',
	//     	data: 'camera_rotation=' + value,
	//     	method: 'post',
	//     	onComplete: getCartInnerHtml
	//     	}).send();
	//    }
	//    else
	//    {
	// new Request ({url: '/plugs/cart/moebel_cart_controller.php',
	//     	data: 'camera_rotation=' + value,
	//     	method: 'post',
	//     	onComplete: getCartInnerHtml
	//     	}).send();
	// 
	//     }
}

// function addRemoveMaterialMuster(value, art, struktur, moebel_uid)
// {
//     new Request ({url: '/plugs/cart/moebel_cart_controller.php',
//     data: 'materialmuster_auswahl=1&moebel_uid=' + moebel_uid + '&muster_struktur=' + struktur +'&muster_art=' + art + '&muster_id=' + value,
//     method: 'post',
//     onComplete: getCartInnerHtml
//     }).send();
// }


function addRemoveMaterialMuster(value, art, moebel_uid)
{
    new Request ({url: '/plugs/cart/moebel_cart_controller.php',
    data: 'materialmuster_auswahl=1&moebel_uid=' + moebel_uid + '&muster_art=' + art + '&muster_id=' + value,
    method: 'post',
    onComplete: getCartInnerHtml
    }).send();
}



function setSitztiefe(value, moebel)
{

  var displayCorrection = false;
  if (moebel == 'hocker')
  {
      var val_sitztiefe = $('txt_sitztiefe_hocker').get('value');
      
      if (val_sitztiefe < 50)
      {
        val_sitztiefe = 50;
        displayCorrection = true;
      }
      if (val_sitztiefe > 150)
      {
        val_sitztiefe = 150;
        displayCorrection = true;
      }
      
      var val_gesamtlaenge = $('txt_gesamtbreite').get('value');
      
      // if (val_gesamtlaenge < val_sitztiefe)
      //       {
      //         var tmp = val_gesamtlaenge;
      //         val_gesamtlaenge = val_sitztiefe;
      //         val_sitztiefe = tmp;
      //         $('txt_sitztiefe_hocker').value = val_sitztiefe;
      //         $('txt_gesamtbreite').value = val_gesamtlaenge;
      //         displayCorrection = true;
      //         
      //       }
      
      if (displayCorrection == true)
      {
        $('txt_sitztiefe_hocker').value = val_sitztiefe;
        displayWarningByColorFade('txt_sitztiefe_hocker');
      }
      
      value = val_sitztiefe;
  }

   new Request ({url: '/plugs/cart/moebel_cart_controller.php?sitztiefe='+value, 
    data: 'sitztiefe=' + value,
    method: 'post',
    onComplete: getCartInnerHtml
    }).send();
}




function setLehneArt(value)
{
  new Request ({url: '/plugs/cart/moebel_cart_controller.php',
   data: 'lehne_art=' + value,
   method: 'post',
   async: false,
   onComplete: getCartInnerHtml
   }).send();  
}

function setSchenkelmontage(value)
{
  new Request ({url: '/plugs/cart/moebel_cart_controller.php',
   data: 'schenkel_montage=' + value,
   method: 'post',
   onComplete: getCartInnerHtml
   }).send();  
}

function setAbschlussbreite(value)
{
  new Request ({url: '/plugs/cart/moebel_cart_controller.php',
   data: 'abschlussbreite=' + value,
   method: 'post',
   onComplete: getCartInnerHtml
   }).send();
}

function setLehnenbreite(value)
{
  new Request ({url: '/plugs/cart/moebel_cart_controller.php',
   data: 'lehnenbreite=' + value,
   method: 'post',
   onComplete: getCartInnerHtml
   }).send();
}

function setSegmentlaenge(value)
{
  new Request ({url: '/plugs/cart/moebel_cart_controller.php',
   data: 'max_segment_laenge=' + value,
   method: 'post',
   onComplete: getCartInnerHtml
   }).send();
}




function setBodenabstand(value)
{
  new Request ({url: '/plugs/cart/moebel_cart_controller.php',
   data: 'bodenabstand=' + value,
   method: 'post',
   onComplete: getCartInnerHtml
   }).send();
}


function setFusshoehe(value)
{
  new Request ({url: '/plugs/cart/moebel_cart_controller.php',
   data: 'fussHoehe=' + value,
   method: 'post',
   onComplete: getCartInnerHtml
   }).send();
}

function setFussbreite(value)
{
  new Request ({url: '/plugs/cart/moebel_cart_controller.php',
   data: 'fussBreite=' + value,
   method: 'post',
   onComplete: getCartInnerHtml
   }).send();
}


function setAbschlusshoehe(value)
{
   //el = $('sel_rueckenhoehe').getAllNext();
   var new_value = value;
   sel_all_abschlusshoehen_options = $('sel_abschlusshoehe');
	

   versatz_rueckenhoehe = 0;
   if (MoebelModellID != 3 && MoebelModellID != 4) //wenn es keine Monos sind
   {
	  versatz_rueckenhoehe = 15;
   }
 
   sel_options=	$('sel_rueckenhoehe').getSelected(); //die momentan selektierte Rückenhöhe
   sel_rueckenhoehe = sel_options[0].value;
   gesamt_abschlusshoehe = value.toInt()+versatz_rueckenhoehe;

   if (gesamt_abschlusshoehe > sel_rueckenhoehe)
   {

	  //die Armlehnenhöhe ist größer als die Lehnenhöhe...
	  //Jetzt muss ermittelt werden, welcher Wert für die Armhöhe genutzt werden soll...
	  var new_value = 5;
	  var new_value_index = 0;

  	  for (var cnt=0; cnt < sel_all_abschlusshoehen_options.options.length; cnt++)
	  {
		if ((sel_all_abschlusshoehen_options.options[cnt].value.toInt() + versatz_rueckenhoehe) <=sel_rueckenhoehe)
		{
			
			new_value = sel_all_abschlusshoehen_options.options[cnt].value.toInt();
			new_value_index = cnt;
            sel_all_abschlusshoehen_options.setValue(new_value);

		}
	  }
	
	  // all_val_divs = sel_all_abschlusshoehen_options.getNext();
	  //  	  all_injected_divs = all_val_divs.getChildren();	
	  //  	  var all_val_divs2 = all_injected_divs.getChildren();
	  // 	  var all_val_divs3 = all_val_divs2[0];
	  //  	  console.log(all_val_divs2);
	  // 
	  //  	  console.log(all_val_divs3);
	  // 	  console.log(all_val_divs3.length);
	  // 	  console.log('-------------');
	  // 	  console.log(new_value_index);
	  // 	  console.log('-------------');
	  //     for (var cnt = 0; cnt < all_val_divs3.length; cnt++)
	  //  	  {
	  // 		console.log('cnt...');
	  // 		console.log(all_val_divs3[cnt]);
	  // 		if (all_val_divs3[cnt].hasClass('act'))
	  // 		{
	  // 			all_val_divs3[cnt].removeClass('act');
	  // 		}
	  //  	  }	
//	  all_val_divs3[new_value_index].addClass('act');
  }
 

  new Request ({url: '/plugs/cart/moebel_cart_controller.php',
   data: 'abschlusshoehe=' + new_value,
   method: 'post',
   onComplete: getCartInnerHtml
   }).send();
}


function setSitzhoehe(value)
{
  new Request ({url: '/plugs/cart/moebel_cart_controller.php',
   data: 'sitzhoehe=' + value,
   method: 'post',
   onComplete: getCartInnerHtml
   }).send();
}

function setRueckenhoehe(value)
{
	
  new Request ({url: '/plugs/cart/moebel_cart_controller.php',
   data: 'rueckenhoehe=' + value,
   method: 'post',
   onComplete: getCartInnerHtml
   }).send();
}


function setGesamtlaengeSchenkel(moebel_art, lehne_art)
{
  
  var value = $('txt_gesamtlaenge_schenkel').get('value');
  // var displayCorrection = false;
  //   var min_value = 178;
  //   
  //   if (lehne_art == 'lehne_konfigurierbar')
  //   {
  //     min_value = 210;
  //   }
  //   
  //   if (value < min_value)
  //   {
  //     value = min_value;
  //     $('txt_gesamtlaenge_schenkel').value = value;
  //     displayCorrection = true;
  //   }
  // 
  //   if (value > 400)
  //   {
  //     value = 400;
  //     $('txt_gesamtlaenge_schenkel').value = value;
  //     displayCorrection = true;
  //   }
  //  
  //   
  //   if (displayCorrection == true)
  //   {
  //     displayWarningByColorFade('txt_gesamtlaenge_schenkel');
  //   }
  //   
  //   
  console.log(value);
  new Request ({url: '/plugs/cart/moebel_cart_controller.php',
   data: 'gesamtlaenge_schenkel=' + value,
   method: 'post',
   onComplete: getCartInnerHtml
   }).send();
}

function setLehnenlaengeSchenkel(value)
{
  var value = $('txt_lehnenlaenge_schenkel').get('value');
  new Request ({url: '/plugs/cart/moebel_cart_controller.php',
   data: 'lehnenlaenge_schenkel=' + value,
   method: 'post',
   onComplete: getCartInnerHtml
   }).send();
  
}

function initCouch()
{
   console.log('init');
  
  if ((el = $('txt_gesamtbreite')))
  {
    var value = el.get('value');
    console.log(value);
    new Request ({url: '/plugs/cart/moebel_cart_controller.php?gesamtbreite=' + value, 
      data: 'gesamtbreite=' + value,
      method: 'post',
      async: true,
      onComplete: getCartInnerHtml
    }).send(); 
  }
  else
  {
    console.log('can not init');
  }
}


function checkMinimalbreite(moebel_art)
{
  
}


function setGesamtbreite(moebel_art)
{

  var value = $('txt_gesamtbreite').get('value');
  var displayCorrection = false;

  // 
  // 
  // if (moebel_art == 'gerade_couch')
  // {
  //   if (value < 120)
  //   {
  //     value = 120;
  //     $('txt_gesamtbreite').value = value;
  //     displayCorrection = true;
  //   }
  // 
  //   if (value > 800)
  //   {
  //     value = 800;
  //     $('txt_gesamtbreite').value = value;
  //     displayCorrection = true;
  //   }
  // 
  // }
  // else if (moebel_art == 'sessel')
  // {
  //   if (value < 78)
  //   {
  //     value = 78;
  //     $('txt_gesamtbreite').value = value;
  //     displayCorrection = true;
  //   }
  // 
  //   if (value > 125)
  //   {
  //     value = 125;
  //     $('txt_gesamtbreite').value = value;
  //     displayCorrection = true;
  //   }
  // } 
  // if (moebel_art == 'hocker')
  // {
  // 
  //   var val_sitztiefe = $('txt_sitztiefe_hocker').get('value');
  // 
  //   if (value < val_sitztiefe)
  //   {
  //     alert('Sitztife ist größer als gesamtlänge');
  //     var tmp = value;
  //     value = val_sitztiefe;
  //     val_sitztiefe = tmp;
  //     $('txt_sitztiefe_hocker').value = val_sitztiefe;
  //     $('txt_gesamtbreite').value = value;
  //     displayCorrection = true;
  //     setSitztiefe(0, 'hocker');  
  //   }
  // }



  // 
  //   if (value > 150)
  //   {
  //     value = 150;
  //     $('txt_gesamtbreite').value = value;
  //     displayCorrection = true;
  //   }
  // } 
  // else if (moebel_art == 'eckcouch_links' || moebel_art == 'eckcouch_rechts')
  // {
  //   if (value < 178)
  //   {
  //     value = 178;
  //     $('txt_gesamtbreite').value = value;
  //     displayCorrection = true;
  //   }
  // 
  //   if (value > 400)
  //   {
  //     value = 400;
  //     $('txt_gesamtbreite').value = value;
  //     displayCorrection = true;
  //   }
  // }
  // 
  // if (displayCorrection == true)
  // {
  //   displayWarningByColorFade('txt_gesamtbreite');
  // }

  new Request ({url: '/plugs/cart/moebel_cart_controller.php',
   data: 'gesamtbreite=' + value,
   method: 'post',
   async: true,
   onComplete: getCartInnerHtml
   }).send();
}


function setFussHolzart(which)
{
  new Request ({url: '/plugs/cart/moebel_cart_controller.php',
    data: 'fussHolzart=' + which,
    method: 'post',
    onComplete: getCartInnerHtml
    }).send();
}



//aktiviert den Radio-Button, wenn auf das Bild einer Holzart für den Fuß angeklickt wurde
function activateHolzart(which)
{
  $('fuss_holzarten_id_'+which).setProperty('checked', 'checked');
  setFussHolzart(which);
}


//aktiviert den Radio-Button, wenn auf das Bild eines Bezugs geklickt wurde
function activateBezug(which)
{
  $('bezug_arten_id_'+which).setProperty('checked', 'checked');
  setCouchBezug(which);
}



function setMaterialBezug(which)
{
  console.trace();
  new Request ({url: '/konfigurator/bezug/bezug_controller.php', 
    data: 'bezug_material=' + which,
    method: 'post',
    onComplete: getBezugInnerHtml
    }).send();
}

function setMaterialFuss(which)
{
  new Request ({url: '/konfigurator/fuss/fuss_controller.php', 
    data: 'materialFuss=' + which,
    method: 'post',
    onComplete: getFussInnerHtml
    }).send();
}


function syncFurniture()
{
    new Request ({url: '/plugs/cart/couch_cart_controller.php', 
    data: 'syncFurniture=' + 1,
    method: 'post',
    onComplete: getCartInnerHtml
    }).send();
}

 
function setStrukturBezug(which)
{
  console.trace();
  new Request ({url: '/konfigurator/bezug/bezug_controller.php', 
    data: 'bezug_struktur=' + which,
    method: 'post',
    onComplete: getBezugInnerHtml
    }).send();
} 
 

 
 
function setCouchBezug(which)
{
  new Request ({url: '/plugs/cart/moebel_cart_controller.php',
    data: 'bezug_id=' + which,
    method: 'post',
    onComplete: getCartInnerHtml
    }).send();
}


// function getCartInnerHtml(reponse)
// {
//   if (reponse != '')
//   {
//     $('cart').set('html', reponse);
//   }
//   mc_cart_right();
// }

function getBezuegeSamples()
{
  if (!(el = $('getBezugInnerHtml'))) return;
  console.trace();
   new Request ({url: '/konfigurator/bezug/bezug_controller.php', 
      method: 'post',
      onComplete: el
      }).send();
}


function getBezugInnerHtml(response)
{
  if (response != '' && (el = $('couch_bezuege_samples')))
  {
    el.set('html', response);
  }
}



function getFuesseSamples()
{
   new Request ({url: '/konfigurator/fuss/fuss_controller.php', 
      method: 'post',
      onComplete: getFussInnerHtml
      }).send();
}






function getFussInnerHtml(response)
{

  if (response != '')
  {
    $('couch_fuesse_samples').set('html', response);
  }
}







function setSteppnaehte(which)
{
  new Request ({url: '/plugs/cart/moebel_cart_controller.php',
    data: 'naehte=' + which,
    method: 'post',
    onComplete: getCartInnerHtml
    }).send();
}


function setMitHusse(value)
{
  new Request ({url: '/plugs/cart/moebel_cart_controller.php',
    data: 'mit_husse=' + value,
    method: 'post',
    onComplete: getCartInnerHtml
    }).send();
}





function setSitzhaerte(which)
{
  new Request ({url: '/plugs/cart/moebel_cart_controller.php',
    data: 'sitzhaerte=' + which,
    method: 'post',
    onComplete: getCartInnerHtml
    }).send();
}


function old_displayWarningByColorFade(the_element)
{
   new Fx.Style(the_element,'background-color',{duration:2000}).start('#f00', '#fff');
}


function displayWarningByColorFade(the_element)
{
//   new Fx.Style(the_element,'background-color',{duration:2000}).start('#f00', '#fff');
//	var myEffect = new Fx.Tween(the_element);
//	$(the_element).highlight('#f00', '#fff');
//	$('myElement').highlight('#ddf');
//	myEffect.start('background-color', '#f00', '#fff');

	$(the_element).set('tween', {duration: 2000});
	$(the_element).tween('background-color', '#f00', '#fff');
}


function setGesamtlaenge()
{
  var value = $('laenge_gesamt').get('value');

  if (isNaN(value))
  {
    value = 0;
  }
  if (CouchSelector)
  {

    var couch_variante = CouchSelector.activeConfig.stellform;


    if (couch_variante == 100) //gerade Couch
    {
      if (value < (40 + 2*CouchSelector.activeConfig.lehnenbreite))
      {
        value = 40 + 2*CouchSelector.activeConfig.lehnenbreite;

        var myElementsEffects = new Fx.Elements($('laenge_gesamt'));
        displayWarningByColorFade('laenge_gesamt');
      }
    }
  }
  
  $('laenge_gesamt').value = value;

}






function setSchenkellaenge(which)
{
}



/*
	updatePrevPictures dient dazu, die Images in den Preview-Divs auszutauschen, wenn ein Kunde von Stoff <-> Leder wechselt
*/

function updatePrevPictures(resp)
{
	resp_picture = resp.picture;
	isLeder = resp.currentMoebelIsLeder;
	curprev_isLeder = false;
	bigprev_isLeder = false;
	bigprev_isLeder = isLeder;
	if ($('moebel_prev_front'))
	{

		el_prev = $('moebel_prev_front');
		el_src = el_prev.get('src');
		if (el_src.lastIndexOf('/leder-') > 0)
		{
			curprev_isLeder = true;
		}


		if (curprev_isLeder != bigprev_isLeder)
		{
			if (bigprev_isLeder)
			{
				source_string = el_prev.get('src');
				new_src = source_string.replace(/\/stoff-/g, "/leder-");
				el_prev.set('src', new_src);
				if ($('moebel_prev_back'))
				{
					new_src = $('moebel_prev_back').get('src').replace(/\/stoff-/g, "/leder-");
					$('moebel_prev_back').set('src', new_src);
				}
				if ($('moebel_prev_detail'))
				{
					new_src = $('moebel_prev_detail').get('src').replace(/\/stoff-/g, "/leder-");
					$('moebel_prev_detail').set('src', new_src);
				}
			}
			else
			{
				source_string = el_prev.get('src');
				new_src = source_string.replace(/\/leder-/g, "/stoff-");
				el_prev.set('src', new_src);
				if ($('moebel_prev_back'))
				{
					new_src = $('moebel_prev_back').get('src').replace(/\/leder-/g, "/stoff-");
					$('moebel_prev_back').set('src', new_src);
				}
				if ($('moebel_prev_detail'))
				{
					new_src = $('moebel_prev_detail').get('src').replace(/\/leder-/g, "/stoff-");
					$('moebel_prev_detail').set('src', new_src);
				}
			}
		}
				
	}
}


function getCartInnerHtml(resp)
{
  if (resp != '')
  {
    try
    {
      eval('resp = ' + resp);

      $('the_card').set('html', resp.cart);

      if (resp.picture && (el = $('bigprev'))) 
      {
        el.getParent().set('html', resp.picture);

		/*
			updatePrevPictures dient dazu, die Images in den Preview-Divs auszutauschen, wenn ein Kunde von Stoff <-> Leder wechselt
		*/
//		
//		updatePrevPictures(resp.picture); 
		updatePrevPictures(resp); 
       fixIEPNG();

      }

      calcedVals = resp.calcedValues;

      if ((el = $('txt_gesamtbreite')))
      {
        if (calcedVals.gesamtlaenge != el.get('value'))
        {
          $('txt_gesamtbreite').value = calcedVals.gesamtlaenge;
          displayWarningByColorFade('txt_gesamtbreite');
        } 
      }

      if ((el = $('txt_gesamtlaenge_schenkel')))
      {
        if (calcedVals.gesamtlaenge_schenkel != el.get('value'))
        {
          $('txt_gesamtlaenge_schenkel').value = calcedVals.gesamtlaenge_schenkel;
          displayWarningByColorFade('txt_gesamtlaenge_schenkel');
        }
      }

      if ((el = $('txt_lehnenlaenge_schenkel')))
      {
        if (calcedVals.lehnenlaenge_schenkel != el.get('value'))
        {
          $('txt_lehnenlaenge_schenkel').value = calcedVals.lehnenlaenge_schenkel;
          displayWarningByColorFade('txt_lehnenlaenge_schenkel');
        }
      }
      

      
      if ((el = $('sel_rueckenhoehe')))
      {
        var tmp = $('sel_rueckenhoehe');

        if ((el_sel_abschlusshoehe = $('sel_abschlusshoehe')))
        {
          el_sel_abschlusshoehe.setValue(calcedVals.abschlusshoehe);
        }
      }
      
    }
    catch (e)
    {
      if ((el = $('the_card'))) el.set('html', resp);
    }
  } 
  mc_cart_right();
  window.fireEvent('cartUpdated');
}

function find_removeEvents_id(e)
{
  var del_moebel_uid_arr;
  var del_moebel_uid;
  del_moebel_uid_arr = e.id;
  del_moebel_uid_arr = del_moebel_uid_arr.split('moebel_uid_');
  del_moebel_uid = del_moebel_uid_arr[1];
  location.href="/konfigurator/moebel/konfigurator.php?moebel_uid=" + del_moebel_uid;
}


// Dragging Cart
 function find_id(dropped_element)
{
  var child_ele = dropped_element.getChildren(); 

  var del_moebel_uid_arr;
  var del_moebel_uid;
  del_moebel_uid_arr = child_ele.getLast().id;
  del_moebel_uid_arr = del_moebel_uid_arr.split('moebel_uid_');
  del_moebel_uid = del_moebel_uid_arr[1];
  location.href="/plugs/cart/moebel_cart_controller.php?remove_moebel_uid=" + del_moebel_uid;
  // del_moebel_uid = 0;
  //  console.log(dropped_element);
  //  console.log(dropped_element.getNext());
  // if (dropped_element.hasChild())
  //   {
    //     console.log('hallo');
    //   }
}
function _mc_cart_right()
{
  return;
  $$('.cart_item_drag').each(function(item){
    item.addEvent('mousedown', function(e) {
      e = new Event(e);

      var clone = this.clone();
      clone.setStyles(this.getCoordinates()) // this returns an object with left/top/bottom/right, so its perfect
        .setStyles({'opacity': 0.7, 'position': 'absolute'})
        .addEvent('emptydrop', function() {
      this.destroy();
      drop.removeEvents();
      find_removeEvents_id(clone);
    }).inject(document.body);

    var drag = clone.makeDraggable({
      droppables: [drop]
      }); // this returns the dragged element

      drag.start(e); // start the event manual
    });

  });
}


function mc_cart_right()
{
//  if ((el = $('bigprev')))
//  {
//        var nfo = el.getCoordinates();
//        nfo.id = 'bigprev';
//        new Request('/plugs/moebel/paint.php', {
//           data: nfo
//          ,onComplete: function(resp)
//          {
//            if ((el = $('bigprev')))
//            {
//              var el = el.getParent();
//              el.set('html', resp);
//              fixIEPNG();
//            }
//          }
//        }).send();
//  }

  window.fireEvent('reInitTips');


  $$('.delivery-to').each(function(el)
  {
    el.setStyles({
      cursor: 'pointer'
    });
    el.addEvent('click', function()
    {
      var target = /([a-z]{2})\.png$/.exec(this.src)[1];
      new Request({url: '/plugs/cart/moebel_cart_controller.php',
         data: 'lieferLand=' + target
        ,onComplete: getCartInnerHtml
      }).send();
    });
  });

  
  
  if ((drop = $('cart_trash'))) 
  {
    drop.dropFx = new Fx.Tween(drop, {
      property: 'background-color',
      wait: false
    });
    
    $$('.cart_item_drag').each(function(el)
    {
      el.addEvents({
        mouseup: function()
        {

          var del_moebel_uid = this.id.split('moebel_uid_')[1];

          
          //Wenn es sich nicht um ein Muster im Warenkorb handelt, wird auf den Konfigurator gelinkt
          if (del_moebel_uid.indexOf("muster") == -1) 
          {
            document.location.href = "/konfigurator/moebel/konfigurator.php?moebel_uid=" + del_moebel_uid;
          }
          //sonst auf die Musterbestellseite
          else 
          {
            document.location.href = "/muster/muster.php";
          }
        },
        mousedown: function(e)
        {
          e.stop();
          var clone = this.clone();
          clone.cloned = this;
          
          var drag = null;
          drag = clone.makeDraggable({
            droppables: [drop],
            onDrag: function()
            {
              this.drags++;
              if (this.drags == 1) 
              {
                this.clone.style.visibility = 'visible';
              }
            },
            //            onComplete: function()
            //            {
            //              console.log('onComplete', arguments);
            //            },
            //            onEmptydrop: function()
            //            {
            //              console.log('onEmptydrop', arguments);
            //            },
            onDrop: function(element, droppable)
            {
              //              console.log('onDrop', element, droppable);
              if (droppable) 
              {
                droppable.fireEvent('drop', element);
                droppable.fireEvent('leave');
              }
              element.destroy();
            }.bind({
              clone: clone,
              drag: drag
            }),
            
            onEnter: function(element, droppable)
            {
              droppable.fireEvent('over');
            },
            
            onLeave: function(element, droppable)
            {
              droppable.fireEvent('leave');
            }
          });
          drag.clone = clone;
          drag.drags = 0;
          
          clone.setStyles(this.getCoordinates());
          clone.setStyles({
            'opacity': 0.7,
            'position': 'absolute',
            'visibility': 'hidden'
          });
          clone.inject(document.body);
          
          drag.start(e); // start the event manual
        }.bindWithEvent(el)
      });
      
    });
        
    drop.addEvents({
      'drop': function(clone)
      {
        //        this.dropFx.start('7389AE').chain(this.dropFx.start.pass('ffffff', this.dropFx));
        del_moebel_uid = clone.cloned.id.split('moebel_uid_')[1];
        location.href = "/plugs/cart/moebel_cart_controller.php?remove_moebel_uid=" + del_moebel_uid;
      },
      'over': function()
      {
        this.dropFx.start('#98B5C1');
      },
      'leave': function()
      {
        this.dropFx.start('#ffffff');
      }
      
    });
  }
  
  
  if (false)//(drop = $('cart_trash')))
  {

    $$('.cart_item_drag').each(function(el)
    {
      el.addEvents({
        mousedown: function(e)
        {
          e;
          var clone = this.clone();
          clone.cloned = this;

          var drag = clone.makeDraggable({
              droppables: [drop]
              ,onDrag: function()
              {
                this.drags ++;
                if (this.drags == 1)
                {
                  this.clone.style.visibility = 'visible';
                }
              }
          });
          drag.clone = clone;
          drag.drags = 0;
          
          clone.setStyles(this.getCoordinates());
          clone.setStyles({'opacity': 0.7, 'position': 'absolute', 'visibility': 'hidden'});
          clone.addEvent('emptydrop', function()
          {
            if (0 == this.drag.drags)
            {
              var del_moebel_uid = clone.cloned.id.split('moebel_uid_')[1];

              
              //Wenn es sich nicht um ein Muster im Warenkorb handelt, wird auf den Konfigurator gelinkt
              if (del_moebel_uid.indexOf("muster") == -1)
              {
                document.location.href = "/konfigurator/moebel/konfigurator.php?moebel_uid=" + del_moebel_uid;
              }
              //sonst auf die Musterbestellseite
              else
              {
                document.location.href = "/muster/muster.php";
              }
            }
            
            this.clone.destroy();
//            find_removeEvents_id(clone);
          }.bind({clone: clone, drag: drag}));
          clone.inject(document.body);

          drag.start(e); // start the event manual
        }.bindWithEvent(el)
      });
      
    });
    
    
    // var dropFx = drop.effect('background-color', {wait: false});
    
    

            drop.addEvents({
              'drop': function(clone) {

                drop.removeEvents();
                clone.destroy();
//                item.clone().inject(drop);
                dropFx.start('7389AE').chain(dropFx.start.pass('ffffff', dropFx));
                del_moebel_uid = clone.cloned.id.split('moebel_uid_')[1];
                location.href="/plugs/cart/moebel_cart_controller.php?remove_moebel_uid=" + del_moebel_uid;
              },
              'over': function() {
                dropFx.start('98B5C1');
              },
              'leave': function() {
                dropFx.start('ffffff');
              }
              
            });
  }
  
  fixIEPNG();
}

MM_IEfix = {
   done: 0
  ,undone: 0
  ,thisRun: 0
};

function fixIEPNG()
{
  MM_IEfix.thisRun = 0;
  
  if (window.ie && !window.ie7)
  {
    var els = $$('.painted div, .painted')
    
    for (var i = 0; (el = els[i]); i ++)
    {
          
      if (!el.ieFixDone && (bg = el.getStyle('background-image')))
      {
        if ('none' != bg)
        {
          bg = /^url\((.*)\)$/.exec(bg)[1];
          el.setStyles({
             background: 'none'
            ,filter: "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + bg + "', sizingMethod='image')"
          });
          el.ieFixDone = true;
          MM_IEfix.thisRun ++;
          MM_IEfix.done ++;
        }
      }
    }
  }
}

window.addEvent('domready', MC_Configurator.init.bind(MC_Configurator));
window.addEvent('coatChoose_changedCoat', MC_Configurator.changedCoat.bind(MC_Configurator));
window.addEvent('coatChoose_changedFoot', MC_Configurator.changedFoot.bind(MC_Configurator));