/**
 * @version: 0.4
 * @date   : 20.09.2010 18:36:01
 * @autor  :  bernd göbel, strich-komma
 * 
 */
  var openStage = false;
  var scrollPos;
 /* redirect ******************************************************************/
 function getUrlVars(){
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
  }

  function open_redirect(tmpURL){
    if( tmpURL == 'news.html'){
      open_page(tmpURL);
    }
    else if( tmpURL == 'index.html'){
      windows.location.href = "index.html";
    }
    else if( tmpURL == 'kontakt.html'){
      open_page(tmpURL);
    }
    else if( tmpURL == 'login.html'){
      open_page(tmpURL);
    }
    else{
      open_project(tmpURL);
    }
  }

 /* intro *********************************************************************/
 
 function fadeout_intro(){
    $('#intro').animate({opacity: 0}, 850,function(){
        $(this).remove();
    }); 
  }
 
 /* filter ********************************************************************/
 
 function show_subfilter( me ){
  var tmpStr = me.parent('li').attr('class');
  tmpStr = tmpStr.split('-');
  var tmpID = '#'+tmpStr[1];
  if( $(tmpID).css('display') == 'block' ){
    $(tmpID).css('display','none');
    $('#access_content').removeClass('active');
    $('#subFilter').animate({'height' : '0'}, 'fast', function() { $('#subFilter').css('display','none'); });
    me.removeClass('current');
  }
  else{
    $('#access_content').addClass('active');
    $('ul.nav-sub-filter').css('display','none');
    $(tmpID).css('display','block');
    $('#subFilter').css('display','block');
    var tmpHeight = $(tmpID).height() + 80; // 80 = margin-top, margin-bottom
    $(tmpID).css('display','none');
    $('#subFilter').animate({'height' : tmpHeight}, 'fast');
    $(tmpID).css('display','block');
    me.addClass('current');
  }
 }
 
 function set_filter( me ){
  reload_content();
  reset_filter_display();
  $( 'dl.project-box' ).each(function () {
      $(this).css('opacity','0.1');
  });
  $('ul.nav-sub-filter').css('display','none');
  $('#subFilter').animate({'height' : '0'}, 'fast', function() { $('#subFilter').css('display','none'); });
  
  
  var tmpFilterSTR    = me.html();
  var tmpSearchSTR    = tmpFilterSTR.toLowerCase();
  var tmpID           = me.parent().parent().attr('id');
  var tmpSearchTarget = '#content dl.project-box .' + tmpID;
  $( tmpSearchTarget ).each( function ( j, myFiltertarget) {
      var tmpTargetSTR  = $(myFiltertarget).html().toLowerCase();
      if( tmpTargetSTR.indexOf(tmpSearchSTR) > -1 ){
        $(myFiltertarget).parent('dl').css('opacity','1');
        $(myFiltertarget).parent('dl').prependTo('#content');
      }
    });
  arrange();// grid-a-licius
  $( 'li.filtertab-'+tmpID+ ' a' ).addClass('selected');
  $( 'li.filtertab-'+tmpID+ ' a span' ).html(pre_selectet);
  $( 'li.filtertab-'+tmpID+ ' a strong' ).html(tmpFilterSTR);
  }

 function reload_content(){
  $('#content').html(contentHTML);
  $( '.project-box' ).click( function () {
    var href =  $(this).children('dt').children('a').attr('href');
    open_project( href );
    return false; 
  });
 }
 function reset_filter(){
  reset_filter_display();
  $('#content').html(contentHTML);
  $('ul.nav-filter a').removeClass('current');
  $('ul.nav-filter a').removeClass('selected');
  $('ul.nav-sub-filter').css('display','none');
  $('#subFilter').animate({'height' : '0'}, 'fast', function() { $('#subFilter').css('display','none'); });
  var position = $('#header').position();
  $('html, body').animate({scrollTop: position.top -10 }, 'slow'); // 10 = margin-top #wrapper
  arrange();// grid-a-licius
 }
 
 function reset_filter_display(){
  $('ul.nav-filter a').removeClass('current');
  $('ul.nav-filter a').removeClass('selected');
  $( 'li.filtertab-subject a span' ).html(pre_subject);
  $( 'li.filtertab-subject a strong' ).html(txt_subject);
  $( 'li.filtertab-customer a span' ).html(pre_customer);
  $( 'li.filtertab-customer a strong' ).html(txt_customer);
  $( 'li.filtertab-history a span' ).html(pre_history);
  $( 'li.filtertab-history a strong' ).html(txt_history);
 }
 
 
 /* Load Content **************************************************************/
 
 function open_page( href ){
  var tmpLoadSTR = href;
  tmpLoadSTR    += ' #subContent';
  $('html, body').animate({scrollTop:0}, 'slow');
  if( href == 'index.html' ){
      window.location.href = "index.html";
      return false;
    }
  $('#pageWrapper').load(tmpLoadSTR, function(){
    if(  href == 'login.html'){
      init_loginForm();
    }
    var tmpHeight = $('#stage').height();
    $('#stage').css('display','block');
    $('#stage').css('height','auto');
    var newHeight = $('#stage').height() + 10;
    $('#stage').css('height',tmpHeight);
    $('#stage').animate({'height' : newHeight}, 'fast');
    
    $('#stage a.project').click( function() {
      var href =  $(this).attr('href');
      open_project( href );
      return false; 
    });
  });
  
  $( 'ul.nav-page li a' ).each(function () {
      $(this).removeClass('current');
      if ( $(this).attr('href') == href  ){
        $(this).addClass('current');
      }
  });
  arrange();// grid-a-licius
  openStage = true;
 }
 
 function open_project ( href ){
  var tmpLoadSTR = href;
  tmpLoadSTR    += ' #subContent';
  scrollPos = $(window).scrollTop();
  $('html, body').animate({scrollTop:0}, 'slow');
  $('#pageWrapper').load(tmpLoadSTR, function(){
      $(".showRoom li:first").addClass('firstItem');
      $(".showRoom li:first img").onImagesLoad({ 
            selectorCallback: open_project_part2
        });
       $('#nav-goBack a')
       .html('Zurück <br>zur Auswahl')
       .click( function() {
          goBack_selection();
          return false;
        }
       );
    });
  $( 'ul.nav-page li a' ).each(function () {
    $(this).removeClass('current');
  });
  $('#stage ul.nav-page').css('display','none');
 }
 
 function open_project_part2 (){
  var tmpHeight = $('#stage').height();
    $('#stage').css('display','block');
    $('#stage').css('height','auto');
    var newHeight = $('#stage').height() + 10;
    scrollPos =  (openStage == false) ? scrollPos + newHeight: scrollPos;
    $('#stage').css('height',tmpHeight);
    $('#stage').animate({'height' : newHeight}, 'fast', function() {
        showRoomWidth = ( $(".showRoom li:first img").width() != 0 )? $(".showRoom li:first img").width() : $(".showRoom li:first").innerWidth();
        showRoomHeight = $(".showRoom li:first").innerHeight();
        $(".showRoom").simplecarousel({
           slidespeed: 700,
           auto: false,
           prev: $('#scrollPrev'),
           next: $('#scrollNext'),
           width: showRoomWidth,
           height: showRoomHeight
          });
          $(".showRoom li").css('display', 'block');
      });
      arrange();// grid-a-licius
      openStage = true;
 }
 
 function goBack_selection(){
  $('html, body').animate({scrollTop:scrollPos  }, 'slow');
 }
 
 
 /* form **********************************************************************/
 
 function init_loginForm(){
    $('div.form form').submit( function() {
    submit_form();
    return false;
    });
    $('div.form input.text').each(function() {
      $(this).data('previous', $(this).val()); 
    });
  
    $('div.form input.text').blur( function (){
      reset_input( $(this) );
    });
    $('div.form input.text').focus( function () {
      set_input( $(this) );
    });
  }

  function submit_form(){
    $('p.error-message').css('display', 'block');
    $('#stage').css('height','auto');
  }
  
  function set_input(me){
    if ( me.attr('id') == 'amt_password' ) {
      me.css('display','none');
      $('#amt_passwordInput').css('display', 'block');
      $('#amt_passwordInput').focus();
    }
    if( me.attr('id') != 'amt_password' && me.attr('id') != 'amt_passwordInput' ){
      me.val('');
    } 
  }
  
  function reset_input(me){
    if ( me.val() == '' ) {
      me.val(me.data('previous'));
      if ( me.attr('id') == 'amt_passwordInput' ) {
        me.css('display','none');
        $('#amt_password').css('display', 'block');
      }
     }
    } 

