function init_home_preview() {
  $(".home_preview h3").css("cursor","pointer").bind("mouseover", function() {
    $(this).css("background","#EEE");
  }).bind("mouseout", function() {
    $(this).css("background","transparent");
  });

  $("#prev_wd h3").bind("click", function() {expand("prev_wd");});
  $("#prev_aipc h3").bind("click", function() {expand("prev_aipc");});
  $("#prev_pcdoc h3").bind("click", function() {expand("prev_pcdoc");});
}

function expand(ele) {
  var show_div = "#" + ele;
  var show_img = "#" + ele + " img";

  close_all();

  /* aktuelles Element anzeigen */
  $(show_div).animate({height: "15em"}, 500);
  $(show_div + " .innerdiv").fadeIn(1500)
  $(show_img).animate({height: "256px", width: "128px"}, 333);

  init_home_preview(); /* legt zuvor entfernte Event-Handler wieder an, danach:

  Event-Handler vom aktuellen Element entfernen und "normales" Erscheinungsbild herstellen */
  $(show_div + " h3").unbind("click").unbind("mouseover").css("cursor","text");
}

function close_all() {  /* alle Elemente ausblenden */
  $(".home_preview .innerdiv").fadeOut(500);
  $(".home_preview img").animate({height: "80px", width: "40px"}, 500);
  $(".home_preview").animate({height: "40px"}, 500);
}

function init_accordeon() {
  $("#accordeon img").css({
    "width":"24px",
    "height":"24px"
  });
  $("#accordeon h4").css({
    "cursor":"pointer",
    "background":"url(/src/css/acc_open.gif) right bottom no-repeat #EEE"
  }).bind("click",function(){
    toggle_acc_element(this);
  });
  $("#accordeon .innerdiv").slideUp("fast");
}

function toggle_acc_element(ele){
  var ele = "#" + ele.id;
  var div = ele + " + .innerdiv";
  var img = ele + " img";

  if($(ele).css("background").indexOf("open") == -1) {       // Element ist geöffnet -> schließen
    $(div).slideUp(function() {
      $(ele).css("background-image","url(/src/css/acc_open.gif)");
      });
    $(img).animate({
      "height":"24px",
      "width":"24px",
      "top":"-2px",
      "left":"5px"
    });
  }
  else {                                              // Element ist geschlossen -> öffnen
    $(div).slideDown(function() {
      $(ele).css("background-image","url(/src/css/acc_close.gif)");
    });
    $(img).animate({
      "height":"10ex",
      "width":"10ex",
      "top":"2ex",
      "left":"1em"
    });
  }
}
// IE < 8 verkackt die Positionierung der Überschrift -> Accordeon deaktivieren
function set_accordeon_oldie() {
  $("#accordeon h4")
    .unbind("click")
    .css({
      "background-image":"none",
      "cursor":"text"
      });
  $("#accordeon .innerdiv").slideDown("fast");
  $("#accordeon img").css({
      "height":"10ex",
      "width":"10ex",
      "top":"2ex",
      "left":"1em"
    });
}

function init_selector() {
  var helptext = Array();
  helptext["s1"] = $("#helptext_office").text();
  helptext["s2"] = $("#helptext_media").text();
  helptext["s3"] = $("#helptext_game").text();
  $("#helptext_office, #helptext_media, #helptext_game").remove();

  $("#office, #media, #game").slideUp("fast");

  // Hilfetexte
  $(".select")
  .bind("mouseenter",function(event) {
    var ele = this.id;
    var x = event.pageX - 270;
    if (x<10) x = 10;
    var y = event.pageY - 300;
    $("#" + ele).before("<div id='help'>"+helptext[ele]+"</div>");
    $("#help")
    .css({
      "position":"absolute",
      "left":x,
      "top":y,
      "background":"#FFE",
      "color":"black",
      "border":"1px solid #E80",
      "padding":"5px",
      "width":"360px",
      "display":"none"
    })
    .fadeIn(1000);
  })
  .bind("mousemove", function(event) {
      var x = event.pageX -200;
      var y = event.pageY - 300;
      $("#help").css({
        "left":x,
        "top":y
      });
    })
  .bind("mouseleave",function() {
    $("#help").remove();
  })
  .bind("click",function() {
    switch(this.id) {
      case "s1":
        $("#office, #media, #game").slideUp("slow");
        $("#office").slideDown("slow");
        $(".select").css("border-color","#EEEEE0");
        $("#" + this.id).css("border-color","#E80");
        break;
      case "s2":
        $("#office, #media, #game").slideUp("slow");
        $("#media").slideDown("slow");
        $(".select").css("border-color","#EEEEE0");
        $("#" + this.id).css("border-color","#E80");
        break;
      case "s3":
        $("#office, #media, #game").slideUp("slow");
        $("#game").slideDown("slow");
        $(".select").css("border-color","#EEEEE0");
        $("#" + this.id).css("border-color","#E80");
        break;
    }
  });
}

function init_aipc_assist() {
  // workaround für verzögerte Anzeige der Hilfetexte im IE6 ("timer ist nicht definiert")
  var timer;

  $(document).ready(function(){
    $(".option_expert, .next_step").show();

    // Container für Hilfetexte vorbereiten
    $("body").append('<div id="help"></div>');
    $("#expertenansicht").css("position","relative");
    $("#help")
      .css({
        "position":"absolute",
        "background":"url(/src/css/help.gif) 3px 7px no-repeat #FFD",
        "color":"black",
        "border":"1px solid #555",
        "padding":"5px 3px 7px 20px",
        "width":"30%"
      })
    .hide(10);

    // Falls bereits zuvor gewählt: Expertenansicht sofort einblenden
    expertview();
    // und Änderungen überwachen
    $("#show_options, #show_options + label").bind("click", function() {expertview();});

    // -> Schritt 1
    $("#start").bind("click", function(event) {
      $("#step1, #step2, #step3, #step4, #step5, #step6, #step7").slideUp(10);
      $("#step1").fadeIn(1000);
      $(this).hide("fast");
      smoothscroll($(this).attr("href"));
      $(this).attr("href");
      event.preventDefault();
    });

    // -> Schritt 2
    $("#goto_2").bind("click",function(event) {
      if($("#step1 input:checked").length < 1) {          // nichts ausgewählt, "Weiter" verweigert
        alert("Bitte wählen Sie zuerst eine Hauptanwendung aus.");
        event.preventDefault();
        return;
      }
      else {
        $("#step2").fadeIn(1000);
        $(this).hide(10);
        smoothscroll($(this).attr("href"));
        event.preventDefault();
        // Hauptanwendung aus Auswahl entfernen
        smart_check();
        // nachträgliche Änderungen der Hauptanwendung überwachen, nicht bei der ersten Auswahl aufrufen!
        $("#step1 input, #step1 input + label").bind("click", function() {smart_check();});
      }
    });

    // Schritt 3
    $("#goto_3").bind("click",function(event) {
      $("#step3").fadeIn(1000);
      $(this).hide(10);
      smoothscroll($(this).attr("href"));
      event.preventDefault();
    });

    // -> Schritt 4
    $("#goto_4").bind("click",function(event) {
      $("#step4").fadeIn(1000);
      $(this).hide(10);
      smoothscroll($(this).attr("href"));
      event.preventDefault();
    });

    // -> Schritt 5
    $("#goto_5").bind("click",function(event) {
      $("#step5").fadeIn(1000);
      $(this).hide(10);
      smoothscroll($(this).attr("href"));
      event.preventDefault();
    });

    // -> Schritt 6
    $("#goto_6").bind("click",function(event) {
      $("#step6").fadeIn(1000);
      $(this).hide(10);
      smoothscroll($(this).attr("href"));
      event.preventDefault();
    });

    // -> Schritt 7 (Abschluß)
    $("#goto_7").bind("click",function(event) {
      $("#step7").fadeIn(1000);
      $(this).hide(10);
      smoothscroll($(this).attr("href"));
      event.preventDefault();
    });

    // noch nicht benötigte Schritte ausblenden
    $("#step1, #step2, #step3, #step4, #step5, #step6, #step7").slideUp(10);

    // Hilfetexte als tooltip
    $(".aipc_help")
      .bind("mousemove", function(event) {
        var x = event.pageX + 10;
        var y = event.pageY + 5;
        $("#help").css({
          "left":x,
          "top":y
        });
      })
      .bind("mouseenter", function() {
        text = "helptext." + this.id;
        $("#help").html(eval(text));
        timer = window.setTimeout('$("#help").fadeIn(750)',1000);
      })
      .bind("mouseleave", function() {
        window.clearTimeout(timer);
        $("#help").stop(true,true);
        $("#help").fadeOut(500);
      });
  });
}

function smoothscroll (ziel) {
  $('html').animate({scrollTop: $(ziel).offset().top}, 1000);
}

function smart_check() {
  var ele = $("input[name='Hauptanwendung']:checked").attr('id');
  var cb = "#s2" + ele.substr(2);
  var lbl = cb + " + label";
  // alle Elemente (wieder) anzeigen
  var all_cb = $("#step2 input");
  var all_lbl= $("#step2 input + label");

  all_lbl.slideDown(1);
  all_cb
    .css({
      "float":"left",
      "margin-right":"1em"
    })
    .slideDown(1);
  $(lbl).fadeOut(1);
  $(cb).removeAttr("checked").fadeOut(1);
}

function expertview() {
  var is_showing = false;

  if($("#show_options").attr("checked")) {
    $("#expertenansicht").slideDown(10);   // Expertanansicht anzeigen
    smoothscroll($("#expertenansicht"));
    is_showing = true;
  }
  else $("#expertenansicht").slideUp(10);  // bzw. ausblenden

  return is_showing;
}

function check_mail() {
  $("#frm_send").submit(function() {
    var mail = $("#mail").val();
    var error = false;
    var errortext = "";

    if(mail == "") {
      error = true;
      errortext = "Mailadresse ist leer";
    }
    else {
      if(mail.indexOf("\\") != -1) {
        error = true;
        errortext = "ungültige Mailadresse";
      }
      if(mail.indexOf("@") < 0) {
        error = true;
        errortext = "ungültige Mailadresse";
      }
      if(mail.indexOf(".") < 0) {
        error = true;
        errortext = "ungültige Mailadresse";
      }
    }

    if(error) {
      alert("Fehler: "+errortext+"\n\nBitte geben Sie eine gültige Email-Adresse an.");
      $("#mail").focus();
      return false;
    }
  });
}
