$(function() {
  initLoginForm();
  initTinyMCE();
  pingPoller();
});

function initLoginForm() {
  $("#login-form label").each(function() {
    var label = $(this).addClass("js");
    $(this).find("input")
      .focus(function() {
        label.addClass("focus");
      })
      .blur(function() {
        if(!$(this).attr("value")) {
          label.removeClass("focus");
        }
      });
  });
}

function initTinyMCE() {
  if($('textarea.tinymce').length == 0 || typeof($('textarea.tinymce').tinymce) != "function")
    return;

  $('textarea.tinymce').tinymce({
    script_url : '/lib/tinymce/jscripts/tiny_mce/tiny_mce.js',
    content_css : "/style/screen-fckeditor.css",
    theme : "advanced",
    plugins : "safari,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template",
    theme_advanced_buttons1 : "bold,italic,|,bullist,numlist,|,cut,copy,paste,pastetext,pasteword,|,undo,redo",
    theme_advanced_buttons2 : "",
    theme_advanced_buttons3 : "",
    theme_advanced_buttons4 : "",
    theme_advanced_toolbar_location : "top",
    theme_advanced_toolbar_align : "left",
    theme_advanced_statusbar_location : false,
    theme_advanced_resizing : false,
  });
}

function pingPoller() {
  if(readCookie('apdo_polled')) return;

  $.get('poller.php', function() {
    createCookie('apdo_polled', 'true', 1);
  });
}

function createCookie(name, value, hours) {
  if (hours) {
    var date = new Date();
    date.setTime(date.getTime()+(hours*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else var expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

function eraseCookie(name) {
  createCookie(name,"",-1);
}

