// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

/* 
By Piotr Wlodarek

Adds the following:
- Cookie#get(), Cookie#set()

*/

function commonInitialize()
{
  createDivTooltips();      // MUST be before createTitleTooltips()
  //createTitleTooltips();
  externalLinks();
  highlightRows();
  copyTdOnclick();
  setHourglassHandlers();
}

Event.observe( window, 'load', commonInitialize );

function externalLinks()
{
  if (!document.getElementsByTagName) return;
  var anchors = document.getElementsByTagName("a");
  for (var i=0; i<anchors .length; i++) 
  {
    var anchor = anchors[i];
    if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") 
    {
      anchor.target = "_blank";
      anchor.title = ( (anchor.title != "") ? anchor.title+" (w nowym oknie...)" : "w nowym oknie...");
      anchor.className = (anchor.className != '') ? anchor.className+' external' : 'external';
    }
  }
}

function assignHighlight() { $(this).addClassName( 'highlight' ); }
function removeHighlight() { $(this).removeClassName( 'highlight' ); }
function highlightRows()
{
  var rows = $$('.highlighted tbody tr');

  for ( var i=0; i < rows.length; i++ ) 
  {
    

    Event.observe( rows[i], 'mouseover', assignHighlight.bindAsEventListener( rows[i] ) );
    Event.observe( rows[i], 'mouseout', removeHighlight.bindAsEventListener( rows[i] ) );
  }
}

/* 
   For highlighted tables, copies onclick event 
   from the first TD, to all TDs, except "actions" TDs.
*/
function copyTdOnclick()
{
  var rows = $$( '.highlighted tbody tr' );

  rows.each( function ( row ) {
    var tds = row.getElementsBySelector( 'td' );
    if ( tds.length > 0 && tds[0].onclick )
    {
      tds.each( function ( td ) {
        if ( td.className.indexOf( 'actions' ) == -1 )
          Event.observe( td, 'click', tds[0].onclick.bindAsEventListener( td ) );
      } );
    }
  } );
}

/*
 * Create Tooltips for elements having title attribute
 */
function createTitleTooltips()
{
  $$("*").findAll( function( node ) {
    return node.getAttribute( 'title' );
  } ).each( function(node ) {
    new Tooltip( node, node.title );
    node.removeAttribute( "title" );
  } );
}

function createDivTooltips()
{
  var helpDivs = document.getElementsByClassName( 'tooltip' );
  helpDivs.each( function( helpDiv ) {
    var helpID = helpDiv.id.toString();
    if ( !helpID.endsWith( '_help' ) )
      alert( 'Help DIV id must end with "_help", got ' + helpID + ' instead.' );
    else
    {
      var targetID = helpID.substring( 0, helpID.length - 5 );
      new Tooltip( targetID, helpID );
    }
  } );
}

function setHourglassHandlers()
{
  var myGlobalHandlers = {
    onCreate: function(){
      Element.show( 'general_hg' );
    },

    onComplete: function() {
      if(Ajax.activeRequestCount == 0){
        Element.hide( 'general_hg' );
      }
    }
  };
  Ajax.Responders.register( myGlobalHandlers );
  Event.observe( document, "mousemove", followHourglass );
}

/* Introduce global 'progress' handlers */
function followHourglass( e ) 
{
    // Wrapped in try-catch, because on page unload
    // hg stops existing at some undefined point
    // and all actions on it result in error.
    try
    {
        hg = $( 'general_hg' )
        hg.style.position = 'absolute'
        hg.style.top = Event.pointerY( e ) + 10 + "px"
        hg.style.left = Event.pointerX( e ) + 10 + "px"
    }
    catch ( e )
    {
        if ( !(e instanceof ReferenceError) )
            throw e;
    }
}

var Cookie = {

 set: function (name, value, expires, path, domain, secure) {
    document.cookie= name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
 },

 get: function (name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    } else {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
 },

 remove: function (name, path, domain) {
    if (Cookie.get(name)) {
        document.cookie = name + "=" +
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
 }
}
