/*
 * News Optionen anzeigen
 *
 * @author kscheffelmeier
 */
function showDropDown(id, mode) {

  if ($("#"+id).css("display") == 'block') {

    /*
     * Auf der Home muss das Dropdown angepasst werden beim oeffnen
     */
    if (mode == 'project_selections') {

      $("#monitor").css("background-image", "url(/images/monitor.gif)");
      $("#monitor").css('z-index', '2');
    }

    window.setTimeout(function() { $("#"+id).css("display", "none"); }, 200);
  } else {

    /*
     * Auf der Home muss das Dropdown angepasst werden beim oeffnen
     */
    if (mode == 'project_selections') {

      $("#monitor").css("background-image", "url(/images/monitor_open.gif)");
      $("#monitor").css('z-index', '2');
    }

    window.setTimeout(function() { $("#"+id).css("display", "block"); }, 200);
  }
}

/*
 * News Option waehlen
 *
 * @author kscheffelmeier
 */
function selectNewsOption(elm) {

  $("#selected_news_option").val(elm.html());
  $("#selected_news_option_value").val(elm.attr('id'));

  showDropDown("news_options");

  $("#select_news").submit();
}

/*
 * Projekt Option waehlen
 *
 * @author kscheffelmeier
 * @author sweber
 */
function selectProjectOption(elm) {

  /*
   * DD schliessen, da der Text aktualisiert wird
   */
  $("#project_selections").css("display", "none");

  var id     = elm.attr('id').substr(7);
  tt_current = id;

  $.ajaxSetup({cache: false});

  $('#topteaser_headline').fadeOut('slow',     function() { });
  $('#topteaser_description').fadeOut('slow',  function() { });
  $('#topteaser_picture_link').fadeOut('slow', function() { });
  $('#topteaser_picture').fadeOut('slow',      function() {

    /*
     * Text aktualisieren und das Bild im Monitor tauschen
     */
    $.ajax({url:      '/show/ajaxShowTopTeaser/'+id,
            dataType: 'json',
            success:  function(data) { $('#topteaser_picture').attr('src', data['picture']).attr('title', data['img_desc']).attr('alt', data['img_desc']);
                                       $('#topteaser_headline').html(data['headline']);
                                       $('#topteaser_description').html(data['description']);
                                       $('#topteaser_picture_link').attr('href', data['link']);

                                       $('#topteaser_headline').fadeIn('slow',     function() { });
                                       $('#topteaser_description').fadeIn('slow',  function() { });
                                       $('#topteaser_picture_link').fadeIn('slow', function() { });
                                       $('#topteaser_picture').fadeIn('slow',      function() { });
                                     }});
  });

  $("#monitor").css("background-image", "url(/images/monitor.gif)");
  $("#monitor").css('z-index', '2');

}

/*
 * Navigationspunkt als aktiv makieren
 *
 * @author kscheffelmeier
 */
function makeNaviActive(elm) {

  /*
   * Hintegrund aktiv
   */
  elm.className = 'navi_btn_act';

  /*
   * Seperator aktiv
   */
  $("#"+elm.id+"_sep").attr("src", "/images/menu_btn_bg_sep_active.jpg");
}

/*
 * Navigationspunkt als inaktiv makieren
 *
 * @author kscheffelmeier
 */
function makeNaviInactive(elm) {

  /*
   * Hintegrund inaktiv
   */
  elm.className = 'navi_btn';

  /*
   * Seperator inaktiv
   */
  $("#"+elm.id+"_sep").attr("src", "/images/menu_btn_bg_sep_inactive.jpg");
}

var tt_holder  = null;
var tt_current = null;

/**
 * Initialisiert JS-Funktionen nach dem load()-Event.
 */
function initSHP() {

  tt_holder = $('#tti');

  if (tt_holder) {

    tt_holder = tt_holder.val().split(',');

    window.setTimeout('rotateTopTeaser()', 6000);
  }
}

/**
 * Rotiert die TopTeaer per Zufall.
 *
 * @author sweber
 */
function rotateTopTeaser() {

  var tt_local    = new Array();
  var tt_holfer_c = tt_holder.length;

  for (i = 0; i < tt_holfer_c; i++) {

    if (tt_holder[i] != tt_current) {

      tt_local.push(tt_holder[i]);
    }
  }

  tt_local.sort(zufall);

  selectProjectOption($('#teaser_'+tt_local[0]));

  window.setTimeout('rotateTopTeaser()', 6000);
}

/**
 * Funktion, die eine Zufallssortierung erlaubt
 *
 * @author sweber
 */
function zufall(a, b) {

  return Math.random()-Math.random()
}

/*
 * Border links und rechts beim Dropdown anpassen
 */
function correctDropDownBorder(id) {

  /*
   * Vorerst fix
   */
  id = '#sub_nav_portfolio';

  /*
   * Box Content Hoehe
   */
  content_height     = $(id).height();

  /*
   * Hoehe des Borders
   */
  border_height      = 137;

  /*
   * Neue Hoehe ausrechnen
   */
  new_border_height  = (content_height - border_height);

  /*
   * Border links und rechts ausrichten
   */
  $('#portfolio_border_left_nav_portfolio').css("top", new_border_height+"px");
  $('#portfolio_border_right_nav_portfolio').css("top", new_border_height+"px");
}

/**
 * schicke Fileuploads. Taken from http://www.quirksmode.org/dom/inputfile.html
 */
var W3CDOM = (document.createElement && document.getElementsByTagName);

function initFileUploads() {

  if (!W3CDOM) { return; }

  var fakeFileUpload       = document.createElement('div');
  fakeFileUpload.className = 'fakefile';
  fakeFileUpload.appendChild(document.createElement('input'));

  var image       = document.createElement('div');
  image.className = 'filechoose';
  image.innerHTML = 'Datei wählen';

  fakeFileUpload.appendChild(image);

  var x = document.getElementsByTagName('input');

  for (var i = 0; i < x.length; i++) {

    if (x[i].type != 'file') { continue; }
    if (x[i].parentNode.className != 'fileinputs') { continue; }

    x[i].className = 'file hidden';
    var clone     = fakeFileUpload.cloneNode(true);

    x[i].parentNode.appendChild(clone);
    x[i].relatedElement = clone.getElementsByTagName('input')[0];

    x[i].onchange = x[i].onmouseout = function () {

      this.relatedElement.value = this.value;
    }
  }
}

