/*!*******************************************************************************
 *
 *  Runway10 (runway10.com)
 *  Copyright notice
 *
 *  (c) 2008-2009 Runway10 (r10JSCore@runway10.com)
 *  All rights reserved
 *
 *  This script is part of the Runway10 JS core library project or R10JSCore.
 *  The R10JSCore project is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  The GNU General Public License can be found at
 *  http://www.gnu.org/copyleft/gpl.html.
 *  A copy is found in the textfile GPL.txt and important notices to the license
 *  from the author is found in LICENSE.txt distributed with these scripts.
 *
 *  This script is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  This copyright notice MUST APPEAR in all copies of the script!
 *
 *********************************************************************************/

IE6 = function() { return Prototype.Browser.IE6 = Prototype.Browser.IE && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5)) == 6}

CssHoverPrefetcher = Class.create({
  initialize: function(options) {
    this.options = Object.extend({
      expression: /url\(.*-hover\..*\)/g
    }, options || {})
    this.styleHanlder = this.onStyle.bind(this);
    this.linkHanlder = this.processLink.bind(this)
    $$('link[type="text/css"]').each(this.linkHanlder)
  },
  processLink: function(link) {
    new Ajax.Request(link.href, { onComplete: this.styleHanlder })
  },
  onStyle: function(res) {
    res.responseText.match(this.options.expression).each(function(e) { this.prefetchUrl(e) }.bind(this) );
  },
  prefetchUrl: function(hover) {
    var url = hover.substring(4, hover.length - 1)
    img = document.createElement("img")
    img.src = url
    $(img).hide()
  }
})

FocusController = Class.create({
  initialize: function(control, size, next) {
    this.next = next
    this.size = size
    this.control = control
    $(control).observe('keyup', this.onKeyUp.bind(this))
    $(control).observe('keydown', this.onKeyDown.bind(this))
  },
  onKeyUp: function(event) {
    var value = $(this.control).value
    if (value && this.size != 0 && value.length == this.size) {
      $(this.next).focus()
    }
  },
  onKeyDown: function(event) {
    if (event.keyCode == Event.KEY_TAB) {
      if (!event.shiftKey) {
        Event.stop(event)
        try {
          $(this.next).focus()
        } catch(e) {
        }
      }
    }
  }
})

SelectFocusController = Class.create({
  initialize: function(control, next) {
    this.next = next
    this.control = control
    $(control).observe('change', this.onChange.bind(this))
  },
  onChange: function(event) {
    if ($(this.control).value) {
      $(this.next).focus()
    }
  }
})

onCapture = function(event) {
  function updateValue(input, fvalue) {
    try{
      $(input).value = fvalue()
    } catch(e) {}
  }
  updateValue('id_country', geoplugin_countryCode)
  updateValue('id_region', geoplugin_region)
  updateValue('id_city', geoplugin_city)
  updateValue('id_areacode', geoplugin_areaCode)
  updateValue('id_dmacode', geoplugin_dmaCode)
}

ObserveAllFilter = Class.create({
  initialize: function() {
    if ($('action-toggle')) {
      this.allField = document.createElement("input")
      $(this.allField).type  = "hidden"
      $(this.allField).id    = $(this.allField).name = "all__elements"
      $(this.allField).value = 0 
      $($('action-toggle').form).appendChild($(this.allField))
      $('action-toggle').observe('change', this.onChange.bind(this))
      var lineChange = this.onLineChange.bind(this)
      $$('.action-select').each(function(e) { $(e).observe('click', lineChange)})
    }
  },
  onLineChange: function(event) {
    $(this.allField).value = 0
    $('action-toggle').checked = ''
  },
  onChange: function(event) {
    $(this.allField).value = $(this.allField).value == 1 ? 0 : 1  
  }
})

/**
 * Mimics a checkbox input with a dom elements that is checked unchecked, connects its value to a
 * hidden checkbox that is added if not yet defined in the form
 */
CheckboxControl = Class.create({
  initialize: function(options) {
    this.options = Object.extend({
      selector: '.checkbox',
      checkedClass: 'checked',
      clickableClass: 'clickable',
      clickgroupClass: 'all',
      clickgroupContainerSelector: '.group'
    }, options || {})
    $$(this.options.selector).each(function(checkbox) {
      var onClick = this.click.bindAsEventListener(this, checkbox); 
      $(checkbox).observe('click', onClick);
      $(checkbox).addClassName(this.options.clickableClass);
      var id = $(checkbox).id;
      if (id && $(id + "_label")) {
        $(id + "_label").observe('click', onClick);
        $(id + "_label").addClassName(this.options.clickableClass);
        var hidden = $("id_" + id);
        if (!hidden) {
          // Is click all (we don't need a hidden field)
          if (!this.options.clickgroupClass || !$(checkbox).hasClassName(this.options.clickgroupClass)) {
            hidden = $.INPUT({ 
              type: 'checkbox', id: 'id_' + id, name: id, 
              checked: $(checkbox).hasClassName(this.options.checkedClass) ? 'checked' : '' 
            });
            $(hidden).hide();
            $(checkbox).appendChild(hidden);          
          }
        }
      } 
    }.bind(this));
  },
  _isChecked: function(checkbox) {
    return $(checkbox).hasClassName(this.options.checkedClass);
  },
  _checkAll: function(elements) {
    $(elements).each(function(e) {
      if (!$(e).hasClassName(this.options.clickgroupClass)) {
        if (!this._isChecked(e)) {
          $(e).simulate('click');
        }
      }
    }.bind(this));
  },
  _uncheckAll: function(elements) {
    $(elements).each(function(e) {
      if (!$(e).hasClassName(this.options.clickgroupClass)) {
        if (this._isChecked(e)) {
          $(e).simulate('click');
        }
      }
    }.bind(this));
  },
  click: function(event, checkbox) {
    Event.stop(event);
    $(checkbox).toggleClassName(this.options.checkedClass);
    var checked = this._isChecked(checkbox);
    // toggles hidden
    if ($(checkbox).id && $("id_" + $(checkbox).id)) {
      $("id_" + $(checkbox).id).checked = checked;
    }
    if (this.options.clickgroupClass && $(checkbox).hasClassName(this.options.clickgroupClass)) {
      if (this.options.clickgroupContainerSelector) {
        var container = $(checkbox).up(this.options.clickgroupContainerSelector);
        if (container) {
          (checked ? this._checkAll : this._uncheckAll).call(this, $(container).select(this.options.selector));
        }
      }
    }
  }
});



