function showCinemas(request,div) {
	if ($('errors')) { Element.remove('errors'); }
	new Effect.Appear(div, {duration: 2, queue:'front'});
	
	load_summery();
}

function failure(request) {
	Element.show('errors');
	$('errors').innerHTML = request.responseText;
	new Effect.Highlight('errors',{queue:'end'});
}

function loading(div) {
	new Effect.ScrollTo($('top'), {duration: 0, queue:'front'});
	if ($('notice')) {Element.hide('notice');}
	$(div).innerHTML = '';
	Element.hide(div);
	Element.hide('summery');
	Element.show('loading');
}

function complete(request,div) {
	Element.hide('loading');
    if(request.responseText == "Ihre Selektion ergab kein Ergebnis.") {clearSummery()}

	if (request.status == 200) {showCinemas(request,div)}
	else {failure(request)};
}

function deleteSomething(what,url,id,msg) {
  containerDiv = what+'-'+id;

  if(msg) r=confirm(msg);
  else r = true;

  if(r==true) {
    new Ajax.Updater(
    {success: containerDiv},
    url,
    {asynchronous:true,
    evalScripts:true,
    method: 'post',
    insertion: Insertion.Bottom,
    onComplete: function(request){deleteComplete(request,containerDiv,id)},
    onFailure: function(request){failure(request)}});
  } else {
    return false;
  }
}

function deleteComplete(request,containerDiv,id) {
	if (request.status == 200) {removeSomething(containerDiv,id)}
	else {failure(request)};
}

function removeSomething(containerDiv,id) {
	if ($('errors')) { Element.remove('errors'); }
	if ($('spacer-project-'+id)) { Element.remove('spacer-project-'+id); }
	new Effect.Fade($(containerDiv), {duration: 2, queue:'end'});
}

function changeStatus(url,status,id,msg,root_url,target) {
  containerDiv = 'project-'+id;

  if(msg) r=confirm(msg);
  else r = true;

  if(r==true) {
    ajax = new Ajax.Updater(
	{success: containerDiv},
    url+status+'/',
    {asynchronous:true,
    evalScripts:true,
    method: 'post',
    insertion: Insertion.Bottom,
    onComplete: function(request){changeComplete(request,containerDiv,id,status,root_url,target)},
    onFailure: function(request){failure(request)}});
  } else {
    return false;
  }
}

changeComplete = function(request,containerDiv,id,status,root_url,target) {
	if (request.status == 200) {
		if(target) {
		  Element.remove('spacer-project-'+id);
		  Element.remove('status-project-'+id);
		  $('action-project-'+id).innerHTML = '';
		  var newActions = '&nbsp; &nbsp;<a href="'+root_url+'project/edit/'+id+'/">'+
		  '<img src="'+root_url+'images/icons/edit_project.jpg"'+
          'border="0" alt="Projekt bearbeiten"'+
          'title="header=[Projekt bearbeiten] body=[Über diesen Button können Sie Ihre Anfrage Einstellungen bearbeiten.]" /></a> '+

          '<a href="'+root_url+'selections/'+id+'/">'+
          '<img src="'+root_url+'images/icons/edit_selection.jpg"'+
          'border="0" alt="Kinos auswählen"'+
          'title="header=[Kinos auswählen] body=[Über diesen Button können Sie Ihre Kino Auswahl bearbeiten.]" /></a> '+

          '<a href="'+root_url+'projectsummery/'+id+'/">'+
          '<img src="'+root_url+'images/icons/overview.jpg"'+
          'border="0" alt="Zusammenfassung"'+
          'title="header=[Zusammenfassung] body=[Über diesen Button erhalten Sie eine gesamt Übersicht Ihrer Anfrage, mit allen selektierenten Kinos und dem Gesamtpreis.]" /></a> '+

          '<img src="'+root_url+'images/icons/delete.jpg"'+
          'border="0" alt="Projekt löschen"'+
          'title="header=[Projekt löschen] body=[Über diesen Button löschen Sie das aktuelle Projekt, mit allen Kinos.]"'+
	      'onClick="deleteSomething('+
		  '\'project\','+
	      '\''+root_url+'ajax/delete_project/'+id+'/\','+
	      '\''+id+'\','+
	      '\'Das Projekt Tooltipp Test wirklich löschen?\')" /> '+

          '<a href="'+root_url+'projects/book/'+id+'/">'+
          '<img src="'+root_url+'images/icons/book.jpg"'+
          'border="0" alt="Projekt buchen"'+
          'title="header=[Projekt buchen] body=[Über diesen Button buchen Sie Ihr Projekt, mit allen Daten, die Sie in der Zusammenfassung sehen.]" /></a> ';

		  $('action-project-'+id).injectBottom(newActions).innerHTML;
		  divContent = $(containerDiv).innerHTML;
		  newDiv = '<tr id="'+containerDiv+'" style="display: none;">'+divContent+'</tr>'+
		  '<tr id="spacer-project-'+id+'"><td colspan="5"><hr size="1" style="margin: 10px 0 15px 0" /></td></tr>';
		}
		
		removeSomething(containerDiv,false);
		
		if(target) {
		  if($('no_projects')) {Element.remove('no_projects');}
		  Element.remove(containerDiv);
		  $(target).injectBottom(newDiv);
		  new Effect.Appear($(containerDiv), {duration: 2, queue:'end'});
		  new Effect.Highlight($(containerDiv), {startcolor:'#f0f0f0', endcolor:'#ffffff', duration: 2.5, queue:'end'});
		}
		showNotice();
	}
	else {failure(request)};
}

showNotice = function() {
	if($('notice')) {
		$('notice').innerHTML = ' ';
		$('notice').innerHTML = 'Die Ã„nderungen wurden erfolgreich gespeichert.';
	}
}

clearSummery = function() {
	$('total_charge').value = "";
	$('total_cinemas').value = "";
	$('total_visitors').value = "";
	$('pro_kino').value = "";
	$('production').value = "";
}

function load_summery() {
	new Effect.Appear($('summery'), {duration: 2, queue:'parallel'});
	
	charge = $('total_charge').value;
	$('tmp_charge').value = charge*1;
	$('final_charge').innerHTML = charge*1;
	
	cinemas = $('total_cinemas').value;
	$('tmp_cinemas').value = cinemas*1;
	$('final_cinemas').innerHTML = cinemas*1;
	
	cinemas_rel = $('relevant_cinemas').value;
	$('tmp_cinemas_rel').value = cinemas_rel*1;
	
	visitors = $('total_visitors').value;
	$('tmp_visitors').value = visitors*1;
	$('final_visitors').innerHTML = visitors*1;

	copies = $('pro_kino').value;
	production = $('production').value;
	copies = copies*1;
	copies = copies*cinemas_rel;
	if(copies < 0) copies = 0;
	$('total_copies').value = copies*1;
	if(production)
	  copies -= 3;
	if(copies < 0) copies = 0;
	$('final_copies').innerHTML = copies*1;

	if(copies > 45)
	  copy_charge_per_copy = 35;
	if(copies < 46)
	  copy_charge_per_copy = 40;
	if(copies < 16)
	  copy_charge_per_copy = 55;
	if(copies < 6)
	  copy_charge_per_copy = 70;

	copy_charge = copy_charge_per_copy*copies;
	$('final_copy_charge').innerHTML = copy_charge;
}

function update_summery(checked,disabled,charge,visitors,digi) {
  var sep=".";
  tmp_charge = $('tmp_charge').value;
  tmp_cinemas = $('tmp_cinemas').value;
  tmp_cinemas_rel = $('tmp_cinemas_rel').value;
  tmp_visitors = $('tmp_visitors').value;
  tmp_copies = $('pro_kino').value;
  tmp_production = $('production').value;

  tmp_charge = tmp_charge*1;
  tmp_cinemas = tmp_cinemas*1;
  tmp_cinemas_rel = tmp_cinemas_rel*1;
  tmp_visitors = tmp_visitors*1;
  tmp_copies = tmp_copies*1;

  charge = charge*1;
  visitors = visitors*1;
  
  if(checked && !disabled) {
    tmp_charge += charge;
    tmp_cinemas += 1;
    tmp_visitors += visitors;
	if(!digi) {
		tmp_cinemas_rel += 1;
	}
  } else {
    tmp_charge -= charge;
    tmp_cinemas -= 1;
    tmp_visitors -= visitors;
	if(!digi) {
		tmp_cinemas_rel -= 1;
	}
  }

  if(tmp_charge < 0) tmp_charge = 0;
  if(tmp_cinemas < 0) tmp_cinemas = 0;
  if(tmp_cinemas_rel < 0) tmp_cinemas_rel = 0;
  if(tmp_visitors < 0) tmp_visitors = 0;
  if(tmp_cinemas == 0) {
	tmp_charge = 0;
	tmp_visitors = 0;
	tmp_cinemas_rel = 0;
  }
  
  $('tmp_charge').value = tmp_charge;
  $('final_charge').innerHTML = tmp_charge;
  
  $('tmp_cinemas').value = tmp_cinemas;
  $('final_cinemas').innerHTML = tmp_cinemas;

  $('tmp_cinemas_rel').value = tmp_cinemas_rel;
  
  $('tmp_visitors').value = tmp_visitors;
  $('final_visitors').innerHTML = tmp_visitors;

  tmp_copies = tmp_copies*tmp_cinemas_rel;
  $('total_copies').value = tmp_copies*1;
  if(tmp_production)
    tmp_copies -= 3;
  if(tmp_copies < 0) tmp_copies = 0;
  $('final_copies').innerHTML = tmp_copies;

  if(tmp_copies > 45)
    copy_charge_per_copy = 35;
  if(tmp_copies < 46)
    copy_charge_per_copy = 40;
  if(tmp_copies < 16)
    copy_charge_per_copy = 55;
  if(tmp_copies < 6)
    copy_charge_per_copy = 70;

	copy_charge = copy_charge_per_copy*tmp_copies;
	$('final_copy_charge').innerHTML = copy_charge;
}

function get_cinemas() {
  cinemas = $('total_cinemas').value;
  if(cinemas < 0) cinemas = 0;
  $('final_cinemas').innerHTML = cinemas*1
  center = $('total_center').value;
  if(center < 0) center = 0;
  $('final_center').innerHTML = center*1
}

function kick_center(checked,center,count) {
  box = document.getElementsByClassName(center);
  this.disabled=true;
  for (i=0;i<count;i++) {
    if (checked) {
      if(box[i].checked==false) {
        box[i].checked=true;
        box[i].onclick(this.event);
      }
    } else {
      if(box[i].checked==true) {
        box[i].checked=false;
        box[i].onclick(this.event);
      }
    }
  }
  this.disabled=false;
}

function kick_city(cchecked,city,ccount) {
  cbox = document.getElementsByClassName(city);
  this.disabled=true;
  for (ii=0;ii<ccount;ii++) {
    if (cchecked) {
      if(cbox[ii].checked==false) {
        cbox[ii].checked=true;
        cbox[ii].onclick(this.event);
      }
    } else {
      if(cbox[ii].checked==true) {
        cbox[ii].checked=false;
        cbox[ii].onclick(this.event);
      }
    }
  }
  this.disabled=false;
}

function rollierende(checked,center,count,charge,visitors) {
  box = document.getElementsByClassName(center);
  for (i=0;i<count;i++) {
    if (checked) {
      box[i].disabled=true;
      if(box[i].checked==true)
      box[i].onclick(this.event);
    } else {
      box[i].disabled=false;
      if(box[i].checked==true)
      box[i].onclick(this.event);
    }
  }
  if (checked) {
  update_summery(true,false,charge,visitors);
  } else {
  update_summery(false,false,charge,visitors);
  }
}

function toggle(gid,agid) {
  var div=$(gid);
  var a=$(agid);
  
  if(div.style.display=='none') {
    div.style.display='block';
    a.className = 'toggle toggle-open';
  } else if(div.style.display=='block') {
    div.style.display='none';
    a.className = 'toggle toggle-closed';
  }
}