function playSound(){
    stopSound();
    $('#contentFooterSoundOff').attr('class','soundPassive');
    $('#contentFooterSoundOn').attr('class','soundActive');
    var audioPlayer = new SWFObject("/cms/flash/emff_stuttgart.swf", "audioPlayer", "0", "0", "8", "#3C0000");
    audioPlayer.addParam("movie", "/cms/flash/emff_stuttgart.swf");
    audioPlayer.addParam("FlashVars", "playlist=/cms/flash/xml.php&autostart=yes&repeat=yes&shuffle=yes");
    audioPlayer.write("audioPlayer");
};

function stopSound(){
    $('#contentFooterSoundOff').attr('class','soundActive');
    $('#contentFooterSoundOn').attr('class','soundPassive');
    var audioPlayer=document.createElement("div");
    audioPlayer.setAttribute("id","audioPlayer");
    document.getElementById('soundPlayer').innerHTML="";
    document.getElementById('soundPlayer').appendChild(audioPlayer);
};

function loadURL(hash) {
  var link = document.createElement("a");
  if(hash) {
    var targetUrl=Base64.decode(hash);
    var oldIdcat =  $.getURLParam("idcat",targetUrl);
    var newIdcat = $.getURLParam("idcat");
    $(link).attr("href",targetUrl);
	if (targetUrl) {
		loadAjax(link);
	}
    $(".contentLeftText li").removeClass("active");
    $(".contentLeftText li#idcat" + oldIdcat).addClass("active");
  }
  else{
    var idcat = $.getURLParam("idcat");
    if(idcat !== null){
      $(".contentLeftText li").removeClass("active");
      $(".contentLeftText li#idcat" + oldIdcat).addClass("active");
      $(link).attr("href","/cms/front_content.php?idcat=" + idcat);
      loadAjax(link);
    }
    else{
        if(!($.browser.safari && document.referrer === '')){
          $(".contentLeftText li").removeClass("active");
          hash = "/cms/front_content.php?idcat=1";
          $(link).attr("href",hash);
          hash = Base64.encode(hash.replace(/^.*#/, ''));
          loadAjax(link);
        }
    }
  }
  return false;
};


function loadAjax(link){
    $.ajax({
        type: "POST",
        url: link.href,
        data: "ajax=1&showme=title",
        dataType: "html",
		timeout: 5000,
        success: function(pageTitle){
        pageTitle=pageTitle.match(/<title>(.+)<\/title>/);
			if (pageTitle) {
				document.title = html_entity_decode(pageTitle[1]);
			}
        }
    });
    $.ajax({
        type: "POST",
        url: link.href,
        data: "ajax=1&showme=login",
        dataType: "html",
		timeout: 5000,
        success: function(html){
         $("#contentFooterLogin").html(html);
         if($("#contentFooterLoginPasswordField").attr("id")){
           changePasswordToTextfield();
         }
        }
    });
    $.ajax({
        type: "POST",
        url: link.href,
        data: "ajax=1&showme=content",
        dataType: "html",
		timeout: 5000,
        success: function(html){
         $("#contentCenter").html(html);
         $("#document").css("cursor","auto");
         $("a").css("cursor","pointer");
        }
    });
    return false;
};

var statement = window.setInterval("reloadStatement()", 10000);

function reloadStatement () {
  $.ajax({
      type: "POST",
      url: "/cms/front_content.php",
      data: "ajax=1&showme=statement",
      dataType: "html",
	  timeout: 5000,
      success: function(html){
        $("#contentLeftStatement").html(html);
      }
  });
}
function changePasswordToTextfield(){
  if($("#contentFooterLoginPasswordField").attr("type") == "password") { 
    var textField = document.createElement("input");
    $(textField).attr("type","text").attr("id","contentFooterLoginPasswordField").attr("value","PASSWORT").attr("name","password");
    $("#contentFooterLoginPassword").html(textField);
  }
}
function changeTextToPasswordfield(){
  if($("#contentFooterLoginPasswordField").attr("type") == "text") { 
    var passwordField = document.createElement("input");
    $(passwordField).attr("type","password").attr("id","contentFooterLoginPasswordField").attr("value","PASSWORT").attr("name","password");
    $("#contentFooterLoginPassword").html(passwordField);
    document.getElementById('contentFooterLoginPasswordField').select();
  }
}
$(document).ready(function(){
  $.history.init(loadURL);

  $("#homeLink").click(function(){
    window.location.href="/";
  });

  $("#contentFooterLoginPasswordField").livequery("click", function(){
      changeTextToPasswordfield();
      document.getElementById("contentFooterLoginPasswordField").select();
  });
  $("#contentFooterLoginPasswordField").livequery("select", function(){
      changeTextToPasswordfield();
      document.getElementById("contentFooterLoginPasswordField").select();
  });
  $("#contentFooterLoginPasswordField").livequery("focus", function(){
      changeTextToPasswordfield();
      document.getElementById("contentFooterLoginPasswordField").select();
  });
  $("#contentFooterLoginPasswordField").livequery("keypress", function(){
      changeTextToPasswordfield();
  });


  $(".contentLeftText li a").livequery("click", function(){
    $(".contentLeftText li").removeClass("active");
    $(this).parent().addClass("active");
  });
  
  $("a").livequery("click", function(){
      $("#document").css("cursor","wait");
      $("a").css("cursor","wait");
      if(this.blur) { this.blur(); }
      if($(this).attr('id') == "secimgreload"){
        $("#document").css("cursor","auto");
        $("a").css("cursor","pointer");
        return false;
      }
      if(substr_count($(this).attr('class'),"ui-dialog")) { return false; }

      var linkAs = '';
      if($(this).parent().attr("class") == "extern" || $(this).children().attr("class") == "extern" || $(this).attr("class") == "extern") { linkAs = "extern"; }
      else if($(this).parent().attr("class") == "download" || $(this).children().attr("class") == "download" || $(this).attr("class") == "download") { linkAs = "download"; }
      else if($(this).parent().attr("class") == "pdf" || $(this).children().attr("class") == "pdf" || $(this).attr("class") == "pdf") { linkAs = "download"; }
      else if($(this).parent().attr("class") == "noajax" || $(this).attr("class") == "noajax") { linkAs = "noajax"; }
      else if($(this).parent().attr("class") == "email" || $(this).attr("class") == "email") { linkAs = "email"; }
      else if($(this).parent().attr("class") == "nohistory" || $(this).attr("class") == "nohistory") { linkAs = "nohistory"; }
      else if($(this).parent().attr("class") == "noJS" || $(this).attr("class") == "noJS") { linkAs = "noJS"; }
      else if($(this).parent().hasClass("back") || $(this).hasClass("back")) { linkAs = "history_back"; }
      else { linkAs = "intern"; }

      if(linkAs == 'intern'){
        var hash = this.href;
        hash = Base64.encode(hash.replace(/^.*#/, ''));

        $.history.load(hash);
        //loadAjax(this);
        return false;
      }
      else if(linkAs == "email" || linkAs == "noajax"){
        $("#document").css("cursor","auto");
        $("a").css("cursor","pointer");
        window.location.href=this;
        return false;
      }
      else if(linkAs == "extern"){
        $("#document").css("cursor","auto");
        $("a").css("cursor","pointer");
        window.open(this);
        return false;
      }
      else if(linkAs == "download"){
        $("#document").css("cursor","auto");
        $("a").css("cursor","pointer");
        var openLink=this.href;
        $.ajax({
          type: "POST",
          url: this.href,
          data: "ajax=1&showme=title",
          dataType: "html",
		  timeout: 5000,
          success: function(html){
            if(html.substr(0,4) != "Fehl"){
              window.open(openLink);
              return false;
            }
            else{
              errorPage=document.createElement("a");
              $(errorPage).attr("href","/cms/front_content.php?idart=48&idcat=4&download=" + Base64.encode(openLink) + "&return=" + document.location.href.replace(/^.*#/, '') );
              loadAjax(errorPage);
              return false;
            }
          }
        });
        return false;
      }
      else if(linkAs == "nohistory"){
        loadAjax(this);
        return false;
      }
      else if(linkAs == "history_back"){
        window.history.back();
        return false;
      } else if (linkAs == "noJS") {
	  
	  }
  });


  $('form').livequery("submit",function(){
    $("#document").css("cursor","wait");
    $("a").css("cursor","wait");
    $("input").css("cursor","wait");
    var formAction = $(this).attr("action");
    var queryString = $(this).formSerialize();
    $.ajax({
        type: "POST",
        url: formAction,
        data: queryString + "&ajax=1&showme=login",
        dataType: "html",
		timeout: 5000,
        success: function(html){
            $("#contentFooterLogin").html(html);
        }
    });
    $.ajax({
        type: "POST",
        url: formAction,
        data: queryString + "&ajax=1&showme=title",
        dataType: "html",
		timeout: 5000,
        success: function(pageTitle){
            pageTitle=pageTitle.match(/<title>(.+)<\/title>/);
			if ($pageTitle) {
				document.title = html_entity_decode(pageTitle[1]);
			}
        }
    });
    $.ajax({
        type: "POST",
        url: formAction,
        data: queryString + "&ajax=1&showme=content",
        dataType: "html",
		timeout: 5000,
        success: function(html){
            $("#contentCenter").html(html);
            $("#document").css("cursor","auto");
            $("a").css("cursor","pointer");
            $("input").css("cursor","auto");
        }
    });
    return false;
  });

  $("#secimgreload").livequery("click",function(){
    var secimg = document.createElement("img");
    var secimgSid = Math.random();
    $(secimg).attr("src","./includes/secimg/img.php?sid=" + secimgSid).attr("alt","Sicherheits-Bild").attr("id","secImage");
    $("#secimg").html(secimg);
    return false;
  });
});

