// scriptaculous patch

Effect.ScrollHorizontalTo = function(element) {
  var options = arguments[1] || { },
  scrollOffsets = document.viewport.getScrollOffsets(),
  elementOffsets = $(element).cumulativeOffset();
  
  if (options.offset) elementOffsets[0] += options.offset;
  
  return new Effect.Tween(null,
    scrollOffsets.left,
    elementOffsets[0],
    options,
    function(p){ scrollTo(p.round(), scrollOffsets.top); }
  );
};

// /scriptaculous patch

BaseJS = {
  
  RESPONSE_TYPE_HTML: 1,
  RESPONSE_TYPE_JS: 2,
  
  _jsResponse: null,
  
  loadHTML: function(divId, file, params, func) {
    
    BaseJS.load(divId, file, params, BaseJS.RESPONSE_TYPE_HTML, func);
    
    return;
    
  },
  
  loadJS: function(file, params, func) {
    
    BaseJS.load("", file, params, BaseJS.RESPONSE_TYPE_JS, func);
    
    return BaseJS._jsResponse;
    
  },
  
  load: function(divId, file, params, type, successFunc) {
    
    new Ajax.Request(
        "./content/" + file,
        {
            method: "post",
            parameters: params,
            onCreate: function() {
                //$(divId).innerHTML = '<div id="ajax-loader"></div>' + $(divId).innerHTML;
                },
            onSuccess: function(response) {
                BaseJS.evalResponse(divId, response, type);
                successFunc();
                }
            }
        );
    
    return;
    
  },
  
  evalResponse: function(divId, response, type) {
    
    if (type == BaseJS.RESPONSE_TYPE_JS)
      BaseJS._jsResponse = response.responseText.evalJSON();
    else
      $(divId).update(response.responseText);
    
    return;
    
  },
  
  getJSResponse: function() {
    
    return BaseJS._jsResponse;
    
  }
  
};

Trigger = {
  
  triggerPool: [],
  
  registerTrigger: function(name, func) {
    
    Trigger.triggerPool[name] = func;
    
    return;
    
  },
  
  callTrigger: function(name, params) {
    
    return Trigger.triggerPool[name](params);
    
  }
  
};

sizeViewportBoxes = function() {
  
  var viewDim = document.viewport.getDimensions();
  
  $$("div.box").invoke("setStyle", { width: (viewDim.width) + "px" });
  
  var mainWidth = 4 * viewDim.width;
  
  if (navigator.appVersion.search(/msie\s6/i) != -1)
    mainWidth += 165;
  
  $("main").setStyle({ width: mainWidth + "px" });
  
};

var interval, args, img;

Lightbox = {
  
  lightboxId: "lightbox",
  lightboxContentId: "lightbox-content",
  
  show: function(content, width, height) {
    
    Viewport.lock();
    
    this.loadContent(content, width, height);
    
  },
  
  showAjax: function(file, width, height, params) {
    
    Viewport.lock();
    
    this.centerLightbox(width, height);
    
    $(this.lightboxContentId).update("");
    BaseJS.loadHTML(this.lightboxContentId, file, params);
    
    $(this.lightboxId).setStyle({ display: "block" });
    
  },
  
  loadContent: function(content, width, height, recursive) {
    
    this.centerLightbox(width, height);
    
	if (navigator.appVersion.search(/msie\s6/i) != -1 && recursive != 1) {
	  
	  var matches = content.match(/src='(.*?)'/i);
	  
	  if (matches[1]) {
	    
	    img = new Image();
		img.src = matches[1];
		
		args = [content, width, height];
		
		interval = window.setInterval("checkImageIE6()", 250);
		
		return;
	    
	  }
	  
	}
	
    $(this.lightboxContentId).update(content);
    
    $(this.lightboxId).setStyle({ display: "block" });
    
  },
  
  centerLightbox: function(width, height) {
    
    var view = document.viewport.getDimensions();
    var scroll = document.viewport.getScrollOffsets();
    
    $(this.lightboxId).setStyle(
        {
            left: Math.round(((view.width - width) / 2) + scroll.left).toString() + "px",
            top: Math.round(((view.height - height) / 2) + scroll.top).toString() + "px",
            width: width + "px"
            }
        );
    
  },
  
  close: function() {
    
    $(this.lightboxId).setStyle({ display: "none" });
    
    Viewport.unlock();
    
  }
  
};

function checkImageIE6() {
  
  if (!img.complete)
	return;
  
  if (typeof img.naturalWidth != "undefined" && img.naturalWidth == 0)
    return;
  
  window.clearInterval(interval);
  
  Lightbox.loadContent(args[0], args[1], args[2], 1);
  
  return;
  
}

Viewport = {
  
  screenId: "lock-screen",
  
  lock: function() {
    
    var htmlTag = document.getElementsByTagName("html")[0];
    
    var viewWidth = $("main").getWidth();
    var viewHeight = document.viewport.getHeight() > Element.getHeight(htmlTag) ? document.viewport.getHeight() : Element.getHeight(htmlTag);
    
    if (navigator.appVersion.search(/msie/i) != -1)
      $$("select").invoke("setStyle", { display: "none" });
    
    $(this.screenId).setStyle(
        {
            width: viewWidth.toString() + "px",
            height: viewHeight.toString() + "px", 
            display: "block"
            }
        );
    
  },
  
  unlock: function() {
    
    if (navigator.appVersion.search(/msie/i) != -1)
      $$("select").invoke("setStyle", { display: "inline" });
    
    $(this.screenId).setStyle(
        {
            display: "none"
            }
        );
    
  },
  
  resize: function() {
	  
    if ($(this.screenId) && $(this.screenId).getStyle("display") == "block")
      this.lock();
    
  }
  
};

function sendOrderForm() {
  
  var checkFields = ["firstname", "name", "street", "housenr", "zip", "city", "email"];
  var fieldNames = ["Vorname", "Name", "Straße", "Hausnr.", "PLZ", "Ort", "E-Mail"];
  
  var errorMsg = "";
  
  for (var i = 0; i < checkFields.length; i++)
    if ($("input-" + checkFields[i]).value == "")
      errorMsg += fieldNames[i] + "\n";
  
  if (!$("order-overflow"))
    window.alert("Es ist keine Bestellung angegeben.");
  else if (errorMsg != "")
    window.alert("Bitte füllen Sie alle Felder aus:\n\n" + errorMsg);
  else {
    
    $("input-order-submit").disabled = "disabled";
    
    var sendFields = {
        submit: 1,
        reciever: $("input-reciever").value,
        firstname: $("input-firstname").value,
        name: $("input-name").value,
        company: $("input-company").value,
        street: $("input-street").value,
        housenr: $("input-housenr").value,
        zip: $("input-zip").value,
        city: $("input-city").value,
        phone: $("input-phone").value,
        fax: $("input-fax").value,
        email: $("input-email").value,
        message: $("input-message").value
        };
    
    var orderArray = [], piecesArray = [];
    
    for (i = 1; i < 13; i++) {
      
      if ($("input-order-id-" + i)) {
        
        orderArray[i] = i;
        piecesArray[i] = $("input-order-id-" + i).value;
        
      }
      
    }
    
    sendFields.order = orderArray.toJSON();
    sendFields.pieces = piecesArray.toJSON();
    
    BaseJS.loadHTML("box-3-inner", "bestellung.php", sendFields);
    
  }
  
  return false;
  
}

function sendGBookForm() {
  
  var checkFields = ["firstname", "name", "email", "comment"];
  var fieldNames = ["Vorname", "Name", "E-Mail", "Kommentar"];
  
  var errorMsg = "";
  
  for (var i = 0; i < checkFields.length; i++)
    if ($("input-gbook-" + checkFields[i]).value == "")
      errorMsg += fieldNames[i] + "\n";
  
  if (errorMsg != "")
    window.alert("Bitte füllen Sie alle Felder aus:\n\n" + errorMsg);
  else {
    
    var sendFields = {
        submit: 1,
        firstname: $("input-gbook-firstname").value,
        name: $("input-gbook-name").value,
        email: $("input-gbook-email").value,
        comment: $("input-gbook-comment").value
        };
    
    BaseJS.loadHTML("box-4-inner", "gaestebuch.php", sendFields);
    
    Lightbox.close();
    
  }
  
  return false;
  
}

window.onresize = function() { sizeViewportBoxes(); Viewport.resize(); };