/* TODO: das gehört nicht in comments.js */
function show_loader (version) {
  
  var $slider = $('div.slider-wrapper');
  if ($slider.length==0) return; // hkhl fix f. error statische seiten / $slider php ;-)?
  if (version == 'black') {
    $('#loader-black').css({top: $slider.position().top, left: $slider.position().left, width: $slider.outerWidth(), height: $slider.outerHeight(), display: 'block'});
  } else {
    $('#loader-background').css({top: $slider.position().top, left: $slider.position().left, width: $slider.outerWidth(), height: $slider.outerHeight(), display: 'block'});
  }
}
function hide_loader () {
  $('#loader-background,#loader-black').css({display: 'none'});
}
/* end TODO */
function activateTab (new_tab, callback) {
  var active_tab = null;
  if ($('div.panel.active div.comments-container').css('display') == 'block') {
    active_tab = 'comments';
  } else if ($('div.panel.active div.share-container').css('display') == 'block') {
    active_tab = 'share';
  } else if ($('div.panel.active div.rate-container').css('display') == 'block') {
    active_tab = 'rate';
  }

  if (active_tab == new_tab || new_tab == null) {
    callback = null;
  }
  if (active_tab == null) {
    callback.call();
  } else {
    $('div.panel.active div.tabs').animate({height: 25, top: 448}, 800, 'easeInOutExpo', function(){
      if (location.hash && location.hash.substr(-9) == '/comments') {
        location.hash = location.hash.substring(0, location.hash.indexOf('/comments')+1);
      }
      $('div.panel.active div.tabs div.' + active_tab).removeClass('active');
      $('div.panel.active div.' + active_tab + '-container').css({display: 'none'});
      $('div.panel.active div.comments-box').html('');
      if (typeof(callback) == 'function') {
        callback.call();
      }
    });
  }

  return true;
}
function closeAllTabs () {
  $('div.tabs').css({height: 25, top: 448});
  $('div.tabs>div').removeClass('active');
  $('div.comments-container, div.share-container, div.rate-container').css('display', 'none');
  $('div.panel.active div.comments-box').html('');
}


$(document).ready(function(){
  /* TODO: das gehört nicht in comments.js */
  show_loader('black');
  $('div.matrix-image').live('click', function() {
    location.href = $(this).parent().find ('h3 a').attr('href');
  });

  if ($('div.navigation').length) {
    var nav_width = 0;
    $('div.navigation *').each(function () {
      nav_width += $(this).outerWidth();
    });
    $('div.navigation').css({marginLeft: 319 - (nav_width/2)})
  }

  $('div.searchr').live('click', function() {
    $('#searchform').trigger('submit');
  });
  /* end TODO */

  $('div.container-close').live('click', function(e){
    activateTab(null);
  });

  $('div.panel div.comments').live('click', function(e) {
    activateTab ('comments', function() {
      if ($('div.panel.active div.comments-container').css('display') == 'none') {
        // comments laden
        var post_id = $('div.panel.active').find('div.post').attr('id').substring($('div.panel.active').find('div.post').attr('id').indexOf('-')+1);

        try {
          pageTracker._trackPageview(location.href + location.hash + 'comments');
        } catch (except) {
        }

        $.get('/?comments_form=true&post_id='+post_id, {}, function (response) {


        $('div.panel.active div.comments-box').html(response);
          if (location.hash && location.hash.substr(-9) != '/comments') {
            if (location.hash.substr(-1) != '/')
              location.hash += '/comments';
            else
              location.hash += 'comments';
          }

          needHelp();
          if (typeof(ie6_comments_hook) == 'function') {
            ie6_comments_hook();
          }
          $('div.inputc-email,div.inputc-author,div.inputc-comment').altInputLabel();
          eval($('div.fbc_loginstate_top').next('script').html());

          $('div.panel.active div.comments-container').css({display: 'block'});
          $('div.panel.active div.tabs div.comments').addClass('active');
          $('div.panel.active #si_image').attr('src', '/wp-content/plugins/si-captcha-for-wordpress/captcha-secureimage/securimage_show.php?sid=' + Math.random());

          $('a#new-captcha').click(function() {
            $('div.panel.active #si_image').attr('src', '/wp-content/plugins/si-captcha-for-wordpress/captcha-secureimage/securimage_show.php?sid=' + Math.random());
            return false;
          });

          /* submit comments via AJAX */
          $('form#commentform').unbind('submit').bind('submit',function() {

            var postval = $('div.panel.active form#commentform').serializeArray();
            $.ajax({
              url: '/wp-comments-post.php',
              type: "POST",
              data: postval,
              success: function(){
                /* get post ID */
                var post_id = $('div.panel.active div.post').attr('id');
                post_id = post_id.substring(post_id.indexOf('-')+1);
                $.get('/', {'only_comments':'1', 'post_id': post_id}, function(response) {
                  $('div.panel.active ol.comments-list').replaceWith(response);
                  $('div.panel.active ol.comments-list').scrollTop($('div.panel.active ol.comments-list li:last').offset().top);
                  // Werte leeren
                  $('div.panel.active #commentform input,div.panel.active #commentform textarea').val('').trigger('blur');
                  // Reiter aktualisieren
                  $('div.panel.active div.commentsm a').text($('div.panel.active ol.comments-list li').length+' Comments');
                });

              },
              error: function(req, textStatus, errorThrown) {
                var errormsg = req.responseText.match(/(<p>)(.*?)(<\/p>)/);
                errormsg = errormsg[2];
                alert(errormsg);
              }
            });

            return false;
          });

          $('div.panel.active div.tabs').animate({height: 425, top: 48}, 800, 'easeInOutExpo');

        });
      }
    });
    return false;
  });

  $('div.panel div.share').live('click', function(e) {
    try {
      pageTracker._trackPageview(location.href + location.hash + 'share');
    } catch (except) {
    }
    activateTab ('share', function() {
      if ($('div.panel.active div.share-container').css('display') == 'none') {
        $('div.panel.active div.share-container').css({display: 'block'});
        $('div.panel.active div.tabs div.share').addClass('active');
        $('div.panel.active div.tabs').animate({height: 125, top: 348}, 800, 'easeInOutExpo');
      }
    });
    return false;
  });

  $('div.panel div.rate').live('click', function(e) {
    try {
      pageTracker._trackPageview(location.href + location.hash + 'rate');
    } catch (exception) {
    }
    activateTab ('rate', function() {
      if ($('div.panel.active div.rate-container').css('display') == 'none') {
        $('div.panel.active div.rate-container').css({display: 'block'});
        $('div.panel.active div.tabs div.rate').addClass('active');
        $('div.panel.active div.tabs').animate({height: 125, top: 348}, 800, 'easeInOutExpo');
      }
    });
    return false;
  });

  $('#contactform').submit(function (){
    
    var postval = $('#contactform').serializeArray();


    $.ajax({
      url: $('#contactform').attr('action'),
      type: "POST",
      data: postval,
      success: function(response){
        // reload mit thx-url
        location.href=response;
      },
      error: function(req, textStatus, errorThrown) {
        var errormsg = 'Please fill out all the fields or correct your entry.';
        alert(errormsg);
      }
    });
    
    
    return false;
  });

});

$.fn.altInputLabel = function () {
  return this.each (function() {

    if ($(this).find('input,textarea').val()) {
      $(this).find('label').css({zIndex: 1});
    }

    $(this).find('label').livequery('click', function(){
      $(this).siblings('input,textarea').focus();
    });

    $(this).find('input,textarea').livequery('focus', function (){
      $(this).siblings('label').css({zIndex: 1});
    }).bind('blur', function(e){
      if ($(this).val() == '' && e.keyCode != 9) {
        $(this).siblings('label').css({zIndex: 3});
      }
    });
  });
}


