// Include the following javascript libraries
// prototype.js
// effects.js
// effects-phase.js
// dragdrop.js
// behaviour.js
// helper.js
//
// Application specific stuff goes into this file. 
// See behaviour.js for event-behaviour rules
// Use the $ and Ajax helper functions in prototype.js
// Form validation and other general helpers can be found in helper.js

Object.extend(Element, {
  // find the first node with the given tagName, starting from the
  // given element; traverses the DOM upwards
  findParentWithTagName: function(element, tagName) {
    return findParentWithTagName(element, tagName);
  },
  // find child element with id
  findChildWithId: function(element, id) {
    return findChildWithId(element, id);
  }
});

function insertImage(editor, url, text) {
  if (!text)
    text = "description"
  textbar_insertText(editor, "![" + text + "](" + url + ")");
}
function insertLink(editor, url, text) {
  if (!text)
    text = "description"
  textbar_insertText(editor, "[" + text + "](" + url + ")");
}
function applyItalics(editor) {
  textbar_insertTags(editor, "*", "*", "Italic");
}
function applyBold(editor) {
  textbar_insertTags(editor, "**", "**", "Bold");
}
function applyH1(editor) {
  textbar_insertTags(editor, "#", "", "Heading 1");
}
function applyH2(editor) {
  textbar_insertTags(editor, "##", "", "Heading 2");
}
function applyH3(editor) {
  textbar_insertTags(editor, "###", "", "Heading 3");
}
function getPanel(editor, id) {
  return Element.findChildWithId(Element.findParentWithTagName($(editor), 'table'), id);
}
function showPanel(editor, id) {
  getPanel(editor, "images").style.display = "none";
  getPanel(editor, "help").style.display = "none";
  if (id)
    getPanel(editor, id).style.display = "block";
}

var grid_rules = {
    'table.grid' : function(element) {
        element.hasSelection = function () {
          return $A(this.getElementsByTagName("input")).any( function (e) {
            return (e.type == "checkbox" && e.checked);
            });
        }
    },
    'table.grid th' : function(element) {
        if (!element.id)
            return;
        element.style.cursor = "pointer";
        element.onclick = function() {
          params = window.location.search.parseQuery();
          if (params['order'] == this.id)
            params['order'] = "-" + this.id;
          else
            params['order'] = this.id;
          querystring = '';  
          for(key in params)
            if (key && key.length>0)
              querystring += key + '=' + params[key] + '&';
          querystring = querystring.substr(0, querystring.length-1);
          window.location.search = querystring;
        }
    },
    'table.grid th input.grid-selector' : function(element) {
        element.onclick = function() { 
          var grid = Element.findParentWithTagName(this, "table");

          var checks = $A(grid.getElementsByTagName("input")).findAll( function (e) {
            return (e.type == "checkbox" && e.className != 'grid-selector');
            });
          var all_checked = $A(checks).all( function (e) {
            return (e.checked);
            });
          checks.each(function(e) {
            e.checked = !all_checked;
          });
          return false;
        }
    },
    'table.grid tbody tr' : function(element) {
        element.onmouseover = function() { 
          this.className = "selected";
        }
        element.onmouseout = function() { 
          this.className = "";
        }
        if (element.getAttribute('href')) {
          element.onclick = function() {
            window.location = this.getAttribute('href');
          }
        }
    },
    'table.grid td input' : function(element) {
        element.onclick = function() {
          event.cancelBubble = true;
          return true;
        }
    },
    'table.grid td a' : function(element) {
        element.onclick = function() {
          event.cancelBubble = true;
          return true;
        }
    },
    '.grid-pager span': function(element) {
        if (element.className != 'selected')
        {
          element.onmouseover = function() {
            this.style.backgroundColor = "orange";
          }
          element.onmouseout = function() {
            this.style.backgroundColor = "";
          }
          element.onclick = function() {
            window.location.search = "page=" + (parseInt(this.innerHTML, 10) - 1);
          }
        }
    }
}

var filter_rules = {
    'select.filterbox' : function(element) {
        element.onchange = function() {
          /* reload page with new filter-parameter */
          params = window.location.search.parseQuery();
          params[element.id] = element.value;
          querystring = '';  
          for(key in params)            
            if (key && key.length>0)
              querystring += key + '=' + params[key] + '&';
          querystring = querystring.substr(0, querystring.length-1);
          window.location.search = querystring;
        }
    }
}


Behaviour.register(grid_rules);
Behaviour.register(filter_rules);