
$('#leftcontent .box h2').click(function(e){
  var name = $(this).attr('id');
  if (!getCookie(name) || $(this).hasClass('opened')) {
    setCookie(name, 1);
    $(this).removeClass('opened').addClass('closed').next().hide();
  } else {
    setCookie(name, 0, -1);
    $(this).removeClass('closed').addClass('opened').next().show();
  }
});


(function($) {

  var
  showForgotpassword = function(){
    $.thickbox.show({
      load : {
        template : 'forgotpassword'
      },
      closeOnEscOrClick: false,
      beforeShown : function(content, target) {
        $(content).find('form').jsonform({
          controller : 'user',
          action : 'newpassword',
          redirect : false,
          reload : false,
          resetOnSuccess : false,
          response : function (status, j) {
            if (status == 'success') {
              $.thickbox.show(j.TBmessage, {
                closeTime : 3,
                onContentClose : function(){
                  setTimeout(function(){
                    showLogin()
                  }, 100);
                }
              });
            }
          }
        });
      }
    });
  },
  showLogin = function(){
    $.thickbox.show({
      load : {
        template : 'login'
      },
      closeOnEscOrClick: false,
      beforeShown : function(content, target) {
        $(content).find('input[type=checkbox]').custCheckBox();
        $(content).find('form').jsonform({
          controller : 'user',
          action : 'login',
          reload : true,
          resetOnSuccess : false
        });
        $(content).find('a.forgotpassword').click(function(e){
          e.preventDefault();
          showForgotpassword();
        });
      }
    });
  };
  $.TBshowLogin = showLogin;
    
  $('#page_head a.login, .showTBlogin').click(function(e){
    e.preventDefault();
    showLogin();
  });
  $('#page_head a.register').thickbox({
    load : {
      template : 'register'
    },
    closeOnEscOrClick: false,
    beforeShown : function(content, target) {
      $(content).find('form').jsonform({
        controller : 'user',
        action : 'register',
        redirect : false,
        reload : false,
        resetOnSuccess : false,
        response : function (status, j) {
          if (status == 'success') {
            $.thickbox.show(j.TBmessage, {
              closeTime : 3
            });
          }
        }
      });
      $(content).find('input[name=email]').blur(function(){
        var ri = $(content).find('input[name=realname]');
        if ($.trim(ri.val()) == '') {
          var n = $(this).val().split('@').shift();
          ri.val(n.charAt(0).toUpperCase() + n.substr(1));
        }
      });
      $(content).find('a.forgotpassword').click(function(e){
        e.preventDefault();
        showForgotpassword();
      });
    }
        
  });

})(jQuery)

if (navigator.userAgent.indexOf('MSIE 6.0') != -1) {
  $('.buttons input').hover(
    function () {
      var c = $(this).attr('class');
      $(this).data('class', c);
      $(this).addClass(c + 'hover');
    },
    function () {
      $(this).removeClass( $(this).data('class') + 'hover');
    }
    );

}
$(document).ready(function() {
  if ($('.helpicon').get(0) && typeof $('.helpicon').tooltip == 'function') {
    var
    data = {},
    loaded = false,
    temphtml = '<div style="height:50px; padding-top:45px; text-align:center;" id="helptemphtml">' + $('#loading_indicator').html() +  '</div>',
    setContentOnLoadHref = null,
    obejcts = {},    
    getContent = function(href) {

      var params = href.replace('#', '').split('/'), marker = params[0], submarker = params[1], content;
      if (data[marker] && data[marker][submarker]) {
        content = data[marker][submarker];
      } else {
        setContentOnLoadHref = href;
        load([href]);
      }
      return content;
    },
    load = function(keys){
      $.json('general', 'loadhelp', {
        keys : keys
      }, function(j){
        var marker, submarker;
        if (j.data) {
          for (marker in j.data) {
            data[marker] = {};
            for (submarker in j.data[marker]) {
              data[marker][submarker] = j.data[marker][submarker];
              $(obejcts['#' + marker + '/' + submarker]).show();
            }
          }
          if (setContentOnLoadHref) {
            $('#helptemphtml').parent().html(getContent(setContentOnLoadHref));
            setContentOnLoadHref = null;
          }
          loaded = true;
        } else {
          reportError('unable load help', {
            keys : keys,
            json : j
          });
        }
      });
    };        
    var keys = [];
    $('.helpicon').each(function() {
      var href = $(this).attr('href');
      keys.push(href);
      obejcts[href] = this;
    });
    load(keys);
    
    $('.helpicon').tooltip({ 
      delay: 100,
      showURL: false,
      bodyHandler: function() {
        var html;
        if (!loaded) {
          setContentOnLoadHref = $(this).attr('href');
          html = temphtml;
        } else if (!(html = getContent($(this).attr('href')))) {
          html = temphtml;
        }
        return html;
      }
    });
  }
});
$('a.lightbox').lightBox();
$('.msg_success a.close, .msg_error a.close, .msg_note a.close').click(function(e){
  e.preventDefault();
  $(this).parent().slideUp('fast', function(){
    $(this).remove();
  });
});

(function($) {
  var
  sessionlifetime = 15 * 1000 * 60,
  beforeend = 60000,
  restoreSession = function(){
    $.post(BASEURL + 'json.php?controller=general&action=visiter', {}, function(){
      setTimeout(function(){
        restoreSession();
      }, sessionlifetime - beforeend);
    });
  };
  setTimeout(function(){
    restoreSession();
  }, sessionlifetime - beforeend);
})(jQuery);
(function($) {
  var
  ip = $('#pagesearchform input'),
  def = $('#pagesearchform input').val(),
  to = null;
  ip.focus(function(){
    if ($.trim(ip.val()) == def)
      ip.val('')
  }).blur(function(){
    if ($.trim(ip.val()) == '')
      ip.val(def)
  });
  $('#pagesearchform').submit(function(e){
    e.preventDefault();
    var val = $.trim(ip.val());
    if (val == def) {
      if (to)
        clearTimeout(to);
      to = setTimeout(function(){
        ip.removeClass('error');
      }, 5000);
      ip.addClass('error');
    } else {
      document.location = $(this).attr('action') + '/' + val;
    }
  });
})(jQuery);

(function($) {
  var p, lastid = getCookie('playlistid');
  $('a.openplaylist').click(function(e){
    var w = getCookie('xspfplaylist')? 450 : 340;
    e.preventDefault();
    if (!lastid) {
      if ($(this).attr('id')) {
        lastid = $(this).attr('id');
      } else {
        lastid = $.uniqId();
        $(this).attr('id', lastid)
      }
      setCookie('playlistid', lastid, 30);      
    }

    if (!p || p.closed) {
      p = window.open('',
        "LAplaylistwindov",
        "resizable=yes,scrollbars=yes,status=no,left=20,top=20,height=500,width=" + w + ",menubar=false,toolbar=false,location=false,personalbar=false,directories=false");
    }
    if ($(this).attr('id') != lastid || p.location.href.indexOf(BASEURL) == -1)  {
      lastid = $(this).attr('id');
      setCookie('playlistid', lastid, 30);
      p.location.href = BASEURL + 'ajax.php?controller=playlist&action=index&id=' + $(this).attr('id');
    }
    if (p)
      p.focus();
    if ($(this).hasClass('refresh')) {
      $(this).text($(this).prev().text());
      $(this).prev().hide();
    }
  });
})(jQuery);
$('div.msg_note a.close').click(function(e){
    e.preventDefault();
    setCookie('livelinknoteclosed', 1, 30);
});

