$(function(){

  $('.description p:first').livequery(function(){ $(this).addClass('intro'); });

  $(':header').livequery(function(){ $(this).addClass('dontend'); });

  $('a.lightbox').livequery(function() {
    $(this).lightBox({
      imageBlank: '/images/common/lightbox-blank.gif',
      imageLoading: '/images/common/ajax-loader.gif',
      imageBtnClose: '/images/common/lightbox-btn-close.gif',
      imageBtnPrev: '/images/common/lightbox-btn-prev.gif',
      imageBtnNext: '/images/common/lightbox-btn-next.gif'
    });
  });

  $('a[rel*=fullscreen]').livequery('click', function(e){
    window.open(this.href, 'fullscreen', 
      'left=0,top=0,height='+window.screen.availHeight+
      ',width='+window.screen.availWidth+',resizable=1');
    e.preventDefault();
  });

  $('a.cancel').livequery('click', function(e){
    e.preventDefault(); 
    var d = $(this).parents('.ui-dialog-content');
    if(d.length > 0){
      d.dialog('close');
    }
    else{
      history.back(); 
    }
  });

  $('.placeholder').livequery(function(){ $(this).inputHint(); });
  $('.placeholder-value').each(function(i){
    $(this).data("value", this.value);
  }).focus(function(e){
    this.value = "";
  }).blur(function(e){
    if(this.value == ""){
      this.value = $(this).data("value");
    }
  });

  $('textarea.autogrow').autogrow();
  $('textarea.autogrow').livequery(function(){ $(this).autogrow(); });

  $('.tabs').livequery(function(){ $(this).tabs(); });
  $('.slideshow').livequery(function(){ $(this).cycle({ timeout: 10000 }); });
  $('.rating').livequery(function(){
    $(this).rating({ 
      callback: function(value, link){ 
        this.form.submit();
      } 
    });
  });

  if(jQuery.browser.msie) {
    $('.accordion').livequery(function(){ $(this).accordion({ header: 'h2', collapsible: true, animated: false, active: '.active' }); });
  } else {
    $('.accordion').livequery(function(){
      $(this).accordion({ header: 'h2', collapsible: true, autoHeight: false, clearStyle: true, active: '.active' });
    });
  }

  $('.toggle').livequery('click', function(e){ $(new RegExp('(#.+)$').exec(this.href)[1]).toggle('fast'); e.preventDefault(); });

  $('.toggle-with-parent').livequery('click', function(e){ $(new RegExp('(#.+)$').exec(this.href)[1]).toggle('fast'); e.preventDefault(); 
    $(this).parent().toggle();
  });
  
  $('a[rel*=dialog][rel!=remote-dialog][rel!=remote-popup]').livequery('click', function(e){
    var target = $(new RegExp('(#.+)$').exec(this.href)[1]);
    target.clone().dialog({modal: true, draggable: false, width: 480}).show(); 
    e.preventDefault();
  });
  
  $('a[rel*=remote-popup]').livequery('click', function(e){
    e.preventDefault();
    var self = this;
    var regexp = new RegExp('(#.+)$');
    var select_id = "";
    if (regexp.test(this.href)){
      select_id = regexp.exec(this.href)[1];
    }
    $.get(this.href, function(content){
      if (select_id != ""){
        $(select_id, content).clone().dialog({ width: 580, modal: true, draggable: false, title: self.title, beforeclose: function(event, ui) {
          $(this).remove();
        }});
      } else {
        $(content).dialog({ width: 580, modal: true, draggable: false, title: self.title, beforeclose: function(event, ui) {
          $(this).remove();
        }});
      }	
    }); 
  });
  
  $('a[rel*=remote-dialog]').livequery('click', function(e){
    e.preventDefault();
    var self = this;
    var regexp = new RegExp('(#.+)$');
    var select_id = "";
    if (regexp.test(this.href)){
      select_id = regexp.exec(this.href)[1];
    }
    $.get(this.href, function(content){
      if (select_id != ""){
        $(select_id, content).clone().dialog({ width: 580, modal: true, draggable: false, title: self.title, buttons: { "Ok": function() { $(this).dialog("close"); } }, beforeclose: function(event, ui) {
          $(this).remove();
        }});
      } else {
        $(content).dialog({ width: 580, modal: true, draggable: false, title: self.title, buttons: { "Ok": function() { $(this).dialog("close"); } }, beforeclose: function(event, ui) {
          $(this).remove();
        }});
      }	
    }); 
  });

  $('textarea.wysiwyg').each(function(i) {
    this.fckeditor = new FCKeditor(this.id, '100%', '400');
    this.fckeditor.ReplaceTextarea();
  });

  $('textarea.wysiwyg-basic').each(function(i) {
    this.fckeditor = new FCKeditor(this.id, '100%', '200', 'Basic');
    this.fckeditor.ReplaceTextarea();
  });

  $('textarea.wysiwyg-immediate').each(function(i) {
    this.fckeditor = new FCKeditor(this.id, '100%', '400', 'Immediate');
    this.fckeditor.ReplaceTextarea();
  });

  $('textarea.markup').each(function(i) {
    this.fckeditor = new FCKeditor(this.id, '100%', '400');
    this.fckeditor.Config['FullPage'] = true;
    this.fckeditor.Config['StartupShowBlocks'] = true;
    this.fckeditor.ReplaceTextarea();
  });

  $('a[rel*=application]').each(function(i){
    $('#application-container').load(this.href, null, function(){
      $('.application-loader').hide();
    });
  });

  $('.show-on-hover').each(function(i){
    var el = $(this);
    el.hide();
    el.parent().hover(
      function(){
        el.show();
      },
      function(){
        el.hide();
      });
  });

  $('.show-on-click').each(function(i){
    var el = $(this);
    el.hide();
    el.parent().click(function(){
        el.toggle('slow');
    });
  });

  $('.fade-out').livequery(function(){
    $(this).animate({opacity:1.0}, 10000);
    $(this).fadeOut('slow', function(){
      $(this).remove();
    });
  });

  $('input.autosubmit').change(function(){ $(this).parents('form').submit(); });
  $('select.autosubmit').change(function(){ $(this).parents('form').submit(); });
  
  $(".sortable").each(function(){
    $(this).sortable({ 
      handle  : $(this).metadata().handle,
      items   : $(this).metadata().items,
      update  : function(e,ui){
        $.ajax({ url: $(this).metadata().action, data: $(this).sortable('serialize'), type: $(this).metadata().method });
      }
    });
  });
  
  $('.more[rel=next]').livequery('click', function(e){
    e.preventDefault();
    var self = this;
    var dummy = $('<div/>').load(this.href, function(){
      dummy.find('.xoxo > li').appendTo('.xoxo');
      if(dummy.find('.more[rel=next]').size() > 0) {
        self.href = dummy.find('.more[rel=next]').get(0).href;
      } else {
        $(self).remove();
      }
    });
  });
  
  $('.show-all').livequery('click', function(e){ $(new RegExp('(#.+)$').exec(this.href)[1]).children('.hidden').show('fast'); $(this).hide(); e.preventDefault(); });
  
  $('.tooltipped').livequery(function(){
    $(this).tipsy({ gravity: $.fn.tipsy.autoNS });
  });
  
  $('.authenticate').livequery('click', function(e){
    e.preventDefault();
    alert('You need to sign in to be able to do that. If you are already signed in, you might not have the required permissions.');
  });
  
  $('#message, #warning').livequery('click', function(){
    $(this).slideUp('fast', function(){
      $(this).remove();
    });
  });

});