$(document).ready(function () {
	
  function breite() { return $(window).width(); }
  function hoehe() { return $(window).height(); }

  var rahmenOffsetVert = 800;
  var bildOffsetVert = 1000;
  var textOffsetVert = 3000;
  var titelOffsetVert = 4300;

  var rahmenOffsetHor = 1800;
  var bildOffsetHor = 2000;
  var textOffsetHor = 4000;
  var titelOffsetHor = 5300;

  function slidePagesUp(newPage, curPage, newPageWithIdx, curPageWithIdx) {
    var hoehe = $(window).height();

    $.get("/" + newPage + "/includes/1.php", function (data) {

      if ($("#" + newPageWithIdx).length <= 0) $('.pages').append(data).children().last().attr("id", newPageWithIdx);

      $("#" + newPageWithIdx + ".rahmen").css({ top: hoehe + rahmenOffsetVert + 'px', 'margin-top': ($("#" + newPageWithIdx + ".rahmen").height() / 2) * (-1) });
      //$("#" + newPageWithIdx + " .bild").css({ top: hoehe + bildOffsetVert + 'px', 'margin-top': ($("#" + newPageWithIdx + " .bild").height() / 2) * (-1) });
	  $("#" + newPageWithIdx + " .bild").css({ top: hoehe + bildOffsetVert + 'px', 'margin-top': ($("#" + newPageWithIdx + " .text").height() / 2) * (-1) });
      $("#" + newPageWithIdx + " .text").css({ top: hoehe + textOffsetVert + 'px', 'margin-top': ($("#" + newPageWithIdx + " .text").height() / 2) * (-1) });
      $("#" + newPageWithIdx + " .titel").css({ top: hoehe + titelOffsetVert + 'px', 'margin-top': ($("#" + newPageWithIdx + " .titel").height() / 2) * (-1), display: 'block' });

      if (curPage != undefined) {
        var rahmenHoeheHalb = $(".bg").height();
        $("#" + curPageWithIdx + ".rahmen").css({ top: hoehe + rahmenOffsetVert + 'px', 'margin-top': (hoehe / 2 + rahmenOffsetVert + rahmenHoeheHalb + 8) * (-1) });
        $("#" + curPageWithIdx + " .bild").css({ top: hoehe + bildOffsetVert + 'px', 'margin-top': (hoehe / 2 + bildOffsetVert + rahmenHoeheHalb + 21) * (-1) });
        $("#" + curPageWithIdx + " .text").css({ top: hoehe + textOffsetVert + 'px', 'margin-top': (hoehe / 2 + textOffsetVert + rahmenHoeheHalb + 21) * (-1) });
        $("#" + curPageWithIdx + " .titel").css({ top: hoehe + titelOffsetVert + 'px', 'margin-top': (hoehe / 2 + titelOffsetVert + rahmenHoeheHalb + 21) * (-1) });
      }

      $(".rahmen, .bild, .text, .titel").animate({ top: '50%' }, 3000, function () {
        $(".titel").delay(250).fadeOut(5000);
		enableEmail();
		$(".lightbox").lightBox({txtImage: 'Bild', txtOf: 'von'});
		//$("a[@rel*=lightbox]").lightBox({txtImage: 'Bild', txtOf: 'von'});
      });
    });

    curSubPage = 1;
    $(".forward").fadeIn(1000);
	$(".back").fadeOut(1000);
  }

  function slidePagesDown(newPage, curPage, newPageWithIdx, curPageWithIdx) {
    var hoehe = $(window).height();

    $.get("/" + newPage + "/includes/1.php", function (data) {

      if ($("#" + newPageWithIdx).length <= 1) $('.pages').append(data).children().last().attr("id", newPageWithIdx);

      $("#" + newPageWithIdx + ".rahmen").css({ top: rahmenOffsetVert * (-1) + 'px', 'margin-top': ($("#" + newPageWithIdx + ".rahmen").height() / 2) * (-1) });
      //$("#" + newPageWithIdx + " .bild").css({ top: hoehe + bildOffsetVert * (-1) + 'px', 'margin-top': ($("#" + newPageWithIdx + " .bild").height() / 2) * (-1) });
	  $("#" + newPageWithIdx + " .bild").css({ top: hoehe + bildOffsetVert * (-1) + 'px', 'margin-top': ($("#" + newPageWithIdx + " .text").height() / 2) * (-1) });
      $("#" + newPageWithIdx + " .text").css({ top: hoehe + textOffsetVert * (-1) + 'px', 'margin-top': ($("#" + newPageWithIdx + " .text").height() / 2) * (-1) });
      $("#" + newPageWithIdx + " .titel").css({ top: hoehe + titelOffsetVert * (-1) + 'px', 'margin-top': ($("#" + newPageWithIdx + " .titel").height() / 2) * (-1), display: 'block' });

      if (curPage != undefined) {
        var rahmenHoeheHalb = $(".bg").height() / 2;

        $("#" + curPageWithIdx + ".rahmen").css({ 'top': (hoehe / 2 + rahmenOffsetVert + rahmenHoeheHalb + 8) * (-1) + 'px', 'margin-top': hoehe - rahmenHoeheHalb + rahmenOffsetVert });
        $("#" + curPageWithIdx + " .bild").css({ 'top': (hoehe / 2 + bildOffsetVert + rahmenHoeheHalb + 21) * (-1) + 'px', 'margin-top': hoehe - rahmenHoeheHalb + bildOffsetVert });
        $("#" + curPageWithIdx + " .text").css({ 'top': (hoehe / 2 + textOffsetVert + rahmenHoeheHalb + 21) * (-1) + 'px', 'margin-top': hoehe - rahmenHoeheHalb + textOffsetVert });
        $("#" + curPageWithIdx + " .titel").css({ 'top': (hoehe / 2 + titelOffsetVert + rahmenHoeheHalb + 21) * (-1) + 'px', 'margin-top': hoehe - rahmenHoeheHalb + titelOffsetVert, display: 'block' });
      }

      $(".rahmen, .bild, .text, .titel").animate({ top: '50%' }, 3000, function () {
        $(".titel").delay(250).fadeOut(5000);
		enableEmail();
		$(".lightbox").lightBox({txtImage: 'Bild', txtOf: 'von'});
		//$("a[@rel*=lightbox]").lightBox();
      });
    });

    curSubPage = 1;
    $(".forward").fadeIn(1000);
	$(".back").fadeOut(1000);
  }

  function slidePagesLeft(curPage, newSubPage, curPageWithIdx, newPageWithIdx, anzSubPages) {
	var anzBreite = breite();
	$.get("/" + curPage + "/includes/" + newSubPage + ".php", function (data) {
      if ($("#" + newPageWithIdx).length <= 0) $('.pages').append(data).children().last().attr("id", newPageWithIdx);

      var rahmenBreiteHalb = $(".bg").width() / 2;

      $("#" + newPageWithIdx + ".rahmen").css({ left: anzBreite + rahmenOffsetHor + 'px', 'margin-left': $("#" + newPageWithIdx + ".rahmen").width() / 2 * (-1) });
      //$("#" + newPageWithIdx + " .bild").css({ left: anzBreite + bildOffsetHor + 'px', 'margin-left': $("#" + newPageWithIdx + " .bild").width() / 2 * (-1) });
	  $("#" + newPageWithIdx + " .bild").css({ left: anzBreite + bildOffsetHor + 'px', 'margin-left': $("#" + newPageWithIdx + " .text").width() / 2 * (-1) });
      $("#" + newPageWithIdx + " .text").css({ left: anzBreite + textOffsetHor + 'px', 'margin-left': $("#" + newPageWithIdx + " .text").width() / 2 * (-1) });
      $("#" + newPageWithIdx + " .titel").css({ left: anzBreite + titelOffsetHor + 'px', 'margin-left': $("#" + newPageWithIdx + " .titel").width() / 2 * (-1), display: 'block' });


      $("#" + curPageWithIdx + ".rahmen").css({ left: anzBreite / 2 + rahmenOffsetHor + 'px', 'margin-left': ($("#" + curPageWithIdx + ".rahmen").width() + rahmenOffsetHor) * (-1) });
      $("#" + curPageWithIdx + " .bild").css({ left: anzBreite / 2 + bildOffsetHor + 'px', 'margin-left': ($("#" + curPageWithIdx + " .bild").width() + bildOffsetHor) * (-1) });
      $("#" + curPageWithIdx + " .text").css({ left: anzBreite / 2 + textOffsetHor + 'px', 'margin-left': ($("#" + curPageWithIdx + " .text").width() + textOffsetHor) * (-1) });
      $("#" + curPageWithIdx + " .titel").css({ left: anzBreite / 2 + titelOffsetHor + 'px', 'margin-left': ($("#" + curPageWithIdx + " .titel").width() + titelOffsetHor) * (-1), display: 'block' });

      $(".rahmen, .bild, .text, .titel").animate({ left: '50%' }, 3000, function () {
        $(".titel").delay(250).fadeOut(5000);
		enableEmail();
		$(".lightbox").lightBox({txtImage: 'Bild', txtOf: 'von'});
		//$("a[@rel*=lightbox]").lightBox();
      });
    });

    curSubPage = newSubPage;
    if (curSubPage != 1) $(".back").fadeIn(1000);
    else $(".back").fadeOut(1000);
	
	if(curSubPage == anzSubPages) $(".forward").fadeOut(1000);
	else $(".forward").fadeIn(1000);
  }

  function slidePagesRight(curPage, newSubPage, curPageWithIdx, newPageWithIdx, anzSubPages) {
    var anzBreite = breite();
    $.get("/" + curPage + "/includes/" + newSubPage + ".php", function (data) {
      if ($("#" + newPageWithIdx).length <= 0) $('.pages').append(data).children().last().attr("id", newPageWithIdx);

      var rahmenBreiteHalb = $(".bg").width() / 2;

      $("#" + newPageWithIdx + ".rahmen").css({ left: rahmenOffsetHor * (-1) + 'px', 'margin-left': $("#" + newPageWithIdx + ".rahmen").width() / 2 * (-1) });
      //$("#" + newPageWithIdx + " .bild").css({ left: bildOffsetHor * (-1) + 'px', 'margin-left': $("#" + newPageWithIdx + " .bild").width() / 2 * (-1) });
	  $("#" + newPageWithIdx + " .bild").css({ left: bildOffsetHor * (-1) + 'px', 'margin-left': $("#" + newPageWithIdx + " .text").width() / 2 * (-1) });
      $("#" + newPageWithIdx + " .text").css({ left: textOffsetHor * (-1) + 'px', 'margin-left': $("#" + newPageWithIdx + " .text").width() / 2 * (-1) });
      $("#" + newPageWithIdx + " .titel").css({ left: titelOffsetHor * (-1) + 'px', 'margin-left': $("#" + newPageWithIdx + " .titel").width() / 2 * (-1), display: 'block' });

      $("#" + curPageWithIdx + ".rahmen").css({ left: (rahmenOffsetHor + anzBreite / 2) * (-1) + 'px', 'margin-left': ($("#" + curPageWithIdx + ".rahmen").width() + rahmenOffsetHor) });
      $("#" + curPageWithIdx + " .bild").css({ left: (bildOffsetHor + anzBreite / 2) * (-1) + 'px', 'margin-left': ($("#" + curPageWithIdx + " .bild").width() + bildOffsetHor) });
      $("#" + curPageWithIdx + " .text").css({ left: (textOffsetHor + anzBreite / 2) * (-1) + 'px', 'margin-left': ($("#" + curPageWithIdx + " .text").width() + textOffsetHor) });
      $("#" + curPageWithIdx + " .titel").css({ left: (titelOffsetHor + anzBreite / 2) * (-1) + 'px', 'margin-left': ($("#" + curPageWithIdx + " .titel").width() + titelOffsetHor), display: 'block' });

      $(".rahmen, .bild, .text, .titel").animate({ left: '50%' }, 3000, function () {
        $(".titel").delay(250).fadeOut(5000);
		enableEmail();
		$(".lightbox").lightBox({txtImage: 'Bild', txtOf: 'von'});
		//$("a[@rel*=lightbox]").lightBox();
      });
    });

    curSubPage = newSubPage;
    if (curSubPage != 1) $(".back").fadeIn(1000);
    else $(".back").fadeOut(1000);
	
	if(curSubPage == anzSubPages) $(".forward").fadeOut(1000);
	else $(".forward").fadeIn(1000);
  }

  function navForward() {
    var curPage = $(".nav ul li.active").attr("id");
    var curPageIdx = jQuery.inArray(curPage, menu);
    var anzSubPages = subPages[curPageIdx];
    var newPageIdx = parseInt(curSubPage) + 1;
    var curPageWithIdx = curPage + "_" + parseInt(curSubPage);
    var newPageWithIdx = curPage + "_" + newPageIdx;

    if (newPageIdx <= anzSubPages) slidePagesLeft(curPage, newPageIdx, curPageWithIdx, newPageWithIdx, anzSubPages);
    else slidePagesLeft(curPage, 1, curPageWithIdx, curPage + "_1");
  }

  function navBack() {
    var curPage = $(".nav ul li.active").attr("id");
    var curPageIdx = jQuery.inArray(curPage, menu);
    var anzSubPages = subPages[curPageIdx];
    var newPageIdx = parseInt(curSubPage) - 1;
    var curPageWithIdx = curPage + "_" + parseInt(curSubPage);
    var newPageWithIdx = curPage + "_" + newPageIdx;

    if (newPageIdx >= 1) slidePagesRight(curPage, newPageIdx, curPageWithIdx, newPageWithIdx, anzSubPages);
    else slidePagesRight(curPage, anzSubPages, curPageWithIdx, curPage + "_1");
  }

  //Email-Links aktivieren
  //ART-ist : Michael A. Vesely
  function enableEmail() {
    $('.email').each(function () {
      $(this).text($(this).text().replace('[at]', '@').replace('[dot]', '.').replace('email: ', 'mailto:')).attr('href', $(this).attr('href').replace('[at]', '@').replace('[dot]', '.').replace('email: ', 'mailto:'));
    });
  }

  var curSubPage = 1;

  var menu = new Array();
  menu[1] = "our_stores";
  menu[2] = "einrichtung";
  menu[3] = "moebel";
  menu[4] = "licht";
  menu[5] = "accessoires";
  menu[6] = "stoffkollektionen";
  menu[7] = "hochzeitslisten";
  menu[8] = "our_services";
  menu[9] = "planung";
  menu[10] = "bauleitung";
  menu[11] = "kontakt";
  menu[12] = "presse";
  menu[13] = "impressum";

  //              -  1  2  3  4  5  6  7  8  9 10 11 12 13
  var subPages = [0, 2, 7, 8, 9,11, 8, 2, 6, 6, 6, 1, 2, 1]; 	//Anzahl von Pages pro Kapitel (erste Zahl (0) gilt nicht)

  $(".nav ul li").bind("click", function (e) {
    e.preventDefault();

    var newPage = $(this).attr("id");
    var curPage = $(".nav ul li.active").attr("id");

    var newPageIdx = jQuery.inArray(newPage, menu);
    var curPageIdx = jQuery.inArray(curPage, menu);

    var newPageWithIdx = newPage + "_1";
    var curPageWithIdx = curPage + "_" + curSubPage;

    if (newPageIdx == -1) return false;
    else {
      if (newPageIdx > curPageIdx) slidePagesUp(newPage, curPage, newPageWithIdx, curPageWithIdx);
      else if (newPageIdx < curPageIdx) slidePagesDown(newPage, curPage, newPageWithIdx, curPageWithIdx);
      else return false;
    }

    $("#" + curPage).removeClass("active");
    $("#" + newPage).addClass("active");

    $(".fbbutton, .gbutton").delay(1000).fadeIn(1000);
  });

  $(".forward").bind("click", function (e) {
    navForward();
  });

  $(".back").bind("click", function (e) {
    navBack();
  });

  $(document).keyup(function (e) {
    if (e.keyCode == '39') {
      e.preventDefault();
      navForward();
    }
	else if (e.keyCode == '37') {
      e.preventDefault();
      navBack();
    }
  });
  
  $(".logo, .logo_big").click(function(){
	  document.location = "index.php";
  });
  
  $(".back, .back_label").hover(function(){
	  $(".back_label").fadeIn();
  }, function(){
	  $(".back_label").fadeOut();
  });
  
  $(".forward, .forward_label").hover(function(){
	  $(".forward_label").fadeIn();
  }, function(){
	  $(".forward_label").fadeOut();
  });
  
  $("#hliste1").live("click", function(e){
	  e.preventDefault();
	  slidePagesLeft("hochzeitslisten", "2", "hochzeitslisten_1", "hochzeitslisten_2", 3);
  });
  
  $("#hliste2").live("click", function(e){
	  e.preventDefault();
	  slidePagesLeft("hochzeitslisten", "3", "hochzeitslisten_1", "hochzeitslisten_3", 3);
  });
  
  $("#presse1").live("click", function(e){
	  e.preventDefault();
	  slidePagesLeft("presse", "2", "presse_1", "presse_2", 2);
  });
});
