/*
###############################################################################
#
# ultralokalpatriotische Fotoserien
# Ulrich Kühn
#
# 22:42 15.03.2009
#
###############################################################################
*/


function Fensterweite()
{
  if (window.innerWidth) 
  {
    return window.innerWidth;
  } 
  else if (document.body && document.body.offsetWidth) 
  {
    return document.body.offsetWidth;
  } 
  else 
  {
    return 0;
  }
}

function Fensterhoehe() 
{
  if (window.innerHeight) 
  {
    return window.innerHeight;
  } 
  else if (document.body && document.body.offsetHeight) 
  {
    return document.body.offsetHeight;
  } 
  else 
  {
    return document.documentElement.clientHeight;
  }
}


/*** Neu anfangen (Bretter löschen) ***********************************************/

function allesNeu()
{
  if (confirm("Möchten Sie von vorne anfangen?"))
  {
    for (var i=1; i<=max_bretter; i++)
    {
      document.getElementById("brett"+i).src = canvas;
    }
  }
  bestellDas();
}


/*** Bestellung ***********************************************/

function bestellDas()
{
  var best = document.getElementById("bestellung");
  var b = 0;
  var inner = "";
  for (var i=1; i<=max_bretter; i++)
  {
    var brett = document.getElementById("brett"+i);
    if (brett.style.visibility == "visible" && brett.src.indexOf("=") != -1)
    {
      b++;
      inner += "<img class=\"minithumb\" src=\""+brett.src+"&thumb=1\"><br>\n";
    }
  }
  if (b != 0)
  {
    inner += "<hr>";
    inner += "<div style=\"font-weight:bold; text-align:center;\">"+Preise[b]+" &euro;</div>";
    //inner += "<img src=\"/ulptest/bb.png\">";
    inner += "<input type=\"button\" style=\"margin-top:20px; padding-top:10px; padding-bottom:10px;\" name=\"bestellung\" value=\""+(b==1?"dieses\nBild":"diese\nBilder")+"\nbestellen\" onclick=\"bestellEs();\">";
  }
  best.innerHTML = inner;
}

function bestellEs()
{
  var b = "";
  var z = "?bilder=";
  for (var i=1; i<=max_bretter; i++)
  {
    var brett = document.getElementById("brett"+i);
    if (brett.style.visibility == "visible")
    {
      var p = brett.src.indexOf("=");
      if (p != -1)
      {
        b += z + brett.src.substr(p+1);
        z = ",";
      }
    }
  }
  if (b != "")
  {
    window.location.href = "bestellung.pl" + b + "&aid="+anmeldung;
  }
  else
  {
    alert("Sie haben noch kein Bild ausgewählt.");
  }
}


/*** Intro-Einblendungen ***********************************************/

function anIntro()
{
  var intro = document.getElementById('intro');
  intro.style.visibility = intro.style.visibility == "hidden"? "visible":"hidden";
}

function ausIntro()
{
  document.getElementById('intro').style.visibility = "hidden";
}


/*** Hilfe-Einblendungen ***********************************************/

function anHilfe()
{
  var hilfe = document.getElementById('hilfe');
  hilfe.style.visibility = hilfe.style.visibility == "hidden"? "visible":"hidden";
}

function ausHilfe()
{
  document.getElementById('hilfe').style.visibility = "hidden";
}


/*** Fenstergroessenanderungen ***********************************************/

function neuAufbau() 
{
  var kopf = document.getElementById('kopf');
  var awahl = document.getElementById('albumwahl');
  var bwahl = document.getElementById('bildwahl');
  var anwahl = document.getElementById('anordnungswahl');
  var wand = document.getElementById('wand');
  var best = document.getElementById('bestellung');
  var hilfe = document.getElementById('hilfe');
  var intro = document.getElementById('intro');

  if (parseInt(anwahl.offsetWidth)>=parseInt(Fensterweite())-parseInt(awahl.offsetWidth)-parseInt(bwahl.offsetWidth))
  {
    //window.resizeTo(parseInt(anwahl.offsetWidth)+parseInt(awahl.offsetWidth)+parseInt(bwahl.offsetWidth)+xchrome+10, parseInt(Fensterhoehe())+ychrome);
  }
  
  hilfe.style.left = Math.floor((parseInt(Fensterweite())-parseInt(hilfe.offsetWidth))/2) + 'px';
  hilfe.style.top = Math.floor((parseInt(Fensterhoehe())-parseInt(hilfe.offsetHeight))/2) + 'px';

  intro.style.left = Math.floor((parseInt(Fensterweite())-parseInt(intro.offsetWidth))/2) + 'px';
  intro.style.top = Math.floor((parseInt(Fensterhoehe())-parseInt(intro.offsetHeight))/2) + 'px';

  //document.getElementById("1x1").style.marginLeft = parseInt(document.getElementById("wsek").offsetWidth)+10 + "px";
  
  anwahl.style.top = parseInt(kopf.offsetHeight)+10 + 'px';
  //anwahl.style.left = (parseInt(Fensterweite())-parseInt(anwahl.offsetWidth))/2 + 'px';
  
  awahl.style.left = parseInt(Fensterweite())-parseInt(awahl.offsetWidth)-parseInt(bwahl.offsetWidth)-5 + 'px';
  //awahl.style.top = '0px'; //parseInt(kopf.offsetHeight) + 'px';

  bwahl.style.height = parseInt(Fensterhoehe())-20 + 'px';
  bwahl.style.left = parseInt(Fensterweite())-parseInt(bwahl.offsetWidth) + 'px';
  //awahl.style.top = parseInt(kopf.offsetHeight) + 'px';
    
  //best.style.top = parseInt(anwahl.offsetHeight) + 'px';

  var xan = parseInt(ano.slice(0,1));
  var yan = parseInt(ano.slice(2,3));
  var brett = 1;
  var wandx = parseInt(Fensterweite())-parseInt(bwahl.offsetWidth)-2*brett_aussen-parseInt(best.offsetWidth);
  var wandy = parseInt(Fensterhoehe())-Math.max(parseInt(awahl.offsetHeight),parseInt(anwahl.offsetHeight))-2*brett_aussen;
  wand.style.width = wandx+'px';
  wand.style.height = wandy+'px';
  wand.style.left = parseInt(best.offsetWidth)+brett_aussen+'px';
  wand.style.top = Math.max(parseInt(awahl.offsetHeight),parseInt(anwahl.offsetHeight))+brett_aussen+'px';
  var xsize = Math.floor(wandx/(xan+(xan-1)*brett_rand));
  var ysize = Math.floor(wandy/(yan+(yan-1)*brett_rand));
  var size = Math.min(xsize,ysize,477);
  var ypos = Math.floor((wandy-size*(yan+(yan-1)*brett_rand))/2);
  for (var y=1; y<=yan; y++)
  {
    var xpos = Math.floor((wandx-size*(xan+(xan-1)*brett_rand))/2);
    for (var x=1; x<=xan; x++)
    {
      var bo = document.getElementById('brett'+brett);
      bo.style.height = size + 'px';
      bo.style.width = size + 'px';
      bo.style.visibility = 'visible';
      bo.style.left = xpos + 'px';
      bo.style.top = ypos + 'px';
      brett++;
      xpos += Math.floor(size+size*brett_rand);
    }
    ypos += Math.floor(size+size*brett_rand);
  }
  bretter = brett;
  for (; brett<=max_bretter; brett++) document.getElementById('brett'+brett).style.visibility = 'hidden';
  bestellDas();
}


/*** Drag and Drop ***********************************************************/

function dragHandler(e)
{
  var htype = '-moz-grabbing';
  if (e == null) { e = window.event; htype = 'move'; } 
  var elem = e.target!=null? e.target : e.srcElement;
  if (elem.className == "thumb")
  {
    var bw = document.getElementById('bildwahl');
    var brett = document.getElementById('brett1');
    var size = Math.min(parseInt(elem.style.height),parseInt(brett.style.height));
    drag.className = elem.className;
    drag.sourceElem = elem;
    drag.src = elem.src;
    drag.title = document.ulpform.ort.options[document.ulpform.ort.selectedIndex].text+", "+
                 document.ulpform.serie.options[document.ulpform.serie.selectedIndex].text+", "+
                 elem.title;
    drag.style.top = parseInt(elem.style.top)-parseInt(bw.scrollTop) + (e.clientY-parseInt(elem.style.top)+parseInt(bw.scrollTop)) * (1-size/parseInt(elem.style.height)) + 'px'; 
    drag.style.left = parseInt(document.getElementById('bildwahl').style.left)+parseInt(elem.offsetLeft) + (e.clientX-parseInt(document.getElementById('bildwahl').style.left)-parseInt(elem.offsetLeft)) * (1-size/parseInt(elem.style.height)) + 'px'; 
    drag.style.height = size + 'px';
    drag.style.width = size + 'px';
    drag.style.cursor = htype;
    drag.style.visibility = 'visible';
    drag.style.opacity = 1;
    drag.style.filter = "alpha(opacity=100)"; 
    dragOK = true;
    dragXoffset = e.clientX - parseInt(drag.style.left);
    dragYoffset = e.clientY - parseInt(drag.style.top);
    document.onmousemove = moveHandler;
    document.onmouseup = dropHandler;
    //loadFoto(elem.id);
    return false;
  }
  if (elem.className == "brett")
  {
    drag.className = elem.className;
    drag.sourceElem = elem;
    drag.src = elem.src;
    drag.title = elem.title;
    var wand = document.getElementById('wand');      
    drag.style.top = parseInt(elem.style.top)+parseInt(wand.style.top) + 'px'; 
    drag.style.left = parseInt(elem.style.left)+parseInt(wand.style.left) + 'px';
    drag.style.height = parseInt(elem.style.height) + 'px';
    drag.style.width = parseInt(elem.style.width) + 'px';
    drag.style.cursor = htype;
    drag.style.visibility = 'visible';
    drag.style.opacity = 1;
    drag.style.filter = "alpha(opacity=100)"; 
    dragOK = true;
    dragXoffset = e.clientX - parseInt(drag.style.left);
    dragYoffset = e.clientY - parseInt(drag.style.top);
    document.onmousemove = moveHandler;
    document.onmouseup = dropHandler;
    return false;
  }
}

function moveHandler(e)
{
  if (e == null) { e = window.event } 
  if (e.button<=1 && dragOK)
  {
    drag.style.left = e.clientX - dragXoffset + 'px';
    drag.style.top = e.clientY - dragYoffset + 'px';
    var wand = document.getElementById('wand');  
    for (var i=1; i<bretter; i++)
    {
      var brett = document.getElementById('brett'+i);
      var brettx = parseInt(brett.style.left)+parseInt(wand.style.left);
      var bretty = parseInt(brett.style.top)+parseInt(wand.style.top);
      /*if (e.clientX >= brettx && e.clientX < brettx+parseInt(brett.style.width) &&
          e.clientY >= bretty && e.clientY < bretty+parseInt(brett.style.height))
      {
        brett.style.outlineWidth = '5px';
      }
      else
      {
        brett.style.outlineWidth = '0px';
      }*/
    }
    return false;
  }
}

function dropHandler(e)
{
  if (e == null) { e = window.event } 
  document.onmousemove = null;
  document.onmouseup = null;
  var wand = document.getElementById('wand');
  var dropped = false;
  if (e.clientX >= parseInt(document.getElementById("bildwahl").style.left) && drag.className == "brett")
  {
    drag.sourceElem.src = canvas;
    drag.sourceElem.title = "";
    fadeOut(drag,fadeSteps/2,fadeTime/2,fadeIO);
    //drag.style.visibility = 'hidden';
    bestellDas();
  }
  else
  {
    for (var i=1; i<bretter; i++)
    {
      var brett = document.getElementById('brett'+i);
      var brettx = parseInt(brett.style.left)+parseInt(wand.style.left);
      var bretty = parseInt(brett.style.top)+parseInt(wand.style.top)
      if (e.clientX >= brettx && e.clientX < brettx+parseInt(brett.style.width) &&
          e.clientY >= bretty && e.clientY < bretty+parseInt(brett.style.height))
      {
        dropped = true;
        brett.style.outlineWidth = '0px';
        if (drag.className == "thumb")
        {
          brett.title = drag.title;
          zoomIn(drag,brett,brettx,bretty,parseInt(brett.style.width),zoomSteps,zoomTime,zoomIO);
        }
        if (drag.className == "brett")
        {
          drag.sourceElem.src = brett.src; brett.src = drag.src;
          drag.sourceElem.title = brett.title; brett.title = drag.title;
          drag.style.visibility = 'hidden';
          bestellDas();
        }
        break;
      }
    }
    if (!dropped)
    {
      fadeOut(drag,fadeSteps,fadeTime,fadeIO);
    }
  }
  dragBild = "";
  dragOK = false;
}


// Animation: Thumbnail auf Brettgröße zoomen
function zoomIn(elem,brett,x,y,w,steps,interval,io)
{
  document.onmousedown = null;
	if (elem.zoomChangeMemInt) window.clearInterval(elem.zoomChangeMemInt);
	var actStep = 0;
	elem.zoomChangeMemInt = window.setInterval(
		function()
    {
			elem.currentX = easeInOut(parseInt(elem.style.left),x,steps,actStep,io);
			elem.currentY = easeInOut(parseInt(elem.style.top),y,steps,actStep,io);
      elem.currentW = easeInOut(parseInt(elem.style.width),w,steps,actStep,io);
			elem.style.left = elem.currentX + 'px';
      elem.style.top = elem.currentY + 'px';
      elem.style.width = elem.currentW + 'px';
      elem.style.height = elem.currentW + 'px';
			actStep++;
			if (actStep > steps)
      {
        window.clearInterval(elem.zoomChangeMemInt);
        brett.src = elem.src;
        brett.src = "bild.pl?bild="+elem.sourceElem.id; // großes Bild nachladen
        elem.style.visibility = 'hidden';
        document.onmousedown = dragHandler;
        bestellDas();
      }
		}
		, interval)
}


// Animation: Grafik ausblenden
function fadeOut(elem,steps,interval,io) // www.hesido.com
{
	if (elem.fadeChangeMemInt) window.clearInterval(elem.fadeChangeMemInt);
	var actStep = 0;
	elem.fadeChangeMemInt = window.setInterval(
		function()
    {
			elem.currentOpacity = easeInOut(100,0,steps,actStep,io);
			elem.style.opacity = elem.currentOpacity/100;
      elem.style.filter = "alpha(opacity=" + elem.currentOpacity + ")"; 
			actStep++;
			if (actStep > steps)
      {
        window.clearInterval(elem.fadeChangeMemInt);
        elem.style.visibility = 'hidden';
      }
		}
		, interval)
}


// Hilfsfunktion für Animationen
function easeInOut(minValue,maxValue,totalSteps,actualStep,powr) // www.hesido.com
{
	var delta = maxValue - minValue;
	var stepp = minValue+(Math.pow(((1 / totalSteps)*actualStep),powr)*delta);
	return Math.ceil(stepp)
}

document.onmousedown = dragHandler;



// Asynchrones Nachladen einer Bildserie
function ladeSerie(ort,serie)
{
  if (ort != 0)
  {
    loadAsync("serien.pl?ort="+ort,document.getElementById('albumwahl'));
  }
  loadAsync("serie.pl?ort="+ort+"&serie="+serie,document.getElementById('bildwahl'));
}

function loadAsync(url,obj)
{
  var req = null;

  try
  {
    req = new XMLHttpRequest();
  }
  catch (ms)
  {
    try
    {
      req = new ActiveXObject("Msxml2.XMLHTTP");
    } 
    catch (nonms)
    {
      try
      {
        req = new ActiveXObject("Microsoft.XMLHTTP");
      } 
      catch (failed)
      {
        req = null;
      }
    }  
  }

  if (req == null) return;
    
  req.open("GET", url, true);

  req.onreadystatechange = function()
  {            
    switch(req.readyState)
    {
      case 4:
        if (req.status!=200)
        {
          alert("Fehler:"+req.status); 
        }
        else
        {    
          obj.innerHTML = req.responseText;
          neuAufbau();
        }
        break;
      
      default:
        return false;
      break;     
    }
  };

  req.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
  req.send(null);
}
