// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
var IE = document.all ? true : false;

var WAITING_IMG_PATH = '<span class="spinning_icon" ><img alt="waiting" src="/images/ajax_waiting.gif" /> Favor de esperar un momento...</span>'

function insertWaitingImg(id) {
	$(id).innerHTML = WAITING_IMG_PATH;
}

function Point(x_, y_  ) {
  this.x = x_;
  this.y = y_;
}

Point.prototype.toString = function() {
  return "[" + this.x + ", " + this.y + "]";
}
Point.prototype._add = function(point_) {
  return new Point(this.x + point_.x, this.y + point_.y);
}
Point.prototype._sub = function(point_) {
  return this._add(point_.getNegative());
}
Point.prototype.getNegative = function() {
  return new Point(-this.x, -this.y);
}
Point.prototype.reverse = function() {
  this.x = -this.x;
  this.y = -this.y;
}

Point.prototype.screen2LngLat = function(map) {
	// getting geographical coordinates of border of the map 
	bounds = map.getBounds();
	minLat = bounds.getSouthWest().lat();
	minLng = bounds.getSouthWest().lng();
	maxLat = bounds.getNorthEast().lat();
	maxLng = bounds.getNorthEast().lng();

	diff = findPosition($('map_area_selection_table'))._sub(findPosition($('gmap')))
/*	this */
	gmapDiv = $('gmap')

	geographicalPoint = new Point() 
	geographicalPoint.x= (diff.x * (maxLng - minLng) / gmapDiv.offsetWidth ) + minLng;
	geographicalPoint.y= (diff.y * (maxLat - minLat) / gmapDiv.offsetHeight ) + minLat;
	
}



function toggleDisplay(element) {
    var toVisible = (element.style.display == "none");

    element.style.display = toVisible ? "" : "none";

    return toVisible;
}

function setPosition(obj_, point_) {
  obj_.style.left = point_.x + "px";
  obj_.style.top = point_.y + "px";
}

/**
 * Works only for style in pixels
 */
function getPosition(obj_) {
  var style = obj_.style;
  var left = style.left;
  var top = style.top;

  return new Point(Number(left.substring(0, left.length - 2)), Number(top.substring(0, top.length - 2)));
}

function setSize(obj_, point_) {
  setWidth(obj_, point_.x);
  setHeight(obj_, point_.y);
}

/**
 * Works only for style in pixels
 */
function getSize(obj_) {
  var style = obj_.style;
  var width = style.width;
  var height = style.height;

  return new Point(Number(width.substring(0, width.length - 2)), Number(height.substring(0, height.length - 2)));
}

function setWidth(obj_, width_) {
  obj_.style.width = width_ + "px";
}

function setHeight(obj_, height_) {
  obj_.style.height = height_ + "px";
}

function getMousePosition(e) {
  var ie = IE;

  vX = ie ? (event.clientX + document.documentElement.scrollLeft) : e.pageX; 
  vY = ie ? (event.clientY + document.documentElement.scrollTop) : e.pageY;

  return new Point(vX, vY);
}

function show_delete(id){
    setVisibility('delete_' + id, true);
    return true;
}
function hide_delete(id){
    setVisibility('delete_' + id, false);
    return true;
}
function show_btn(id){
    setVisibility('btn_' + id, true);
    return true;
}
function hide_btn(id){
    setVisibility('btn_' + id, false);
    return true;
}
function addToOnLoad(f_) {
  var oldOnLoad = window.onload;
  
  window.onload = function() {
    if (oldOnLoad) {
      oldOnLoad();
    }
    f_();
  };
}
function setVisibility(id_, true_) {
  $(id_).style.visibility = true_ ? "visible" : "hidden";
}

function removeItemFromArray(item_, arr_) {
  var n = arr_.length;

  while (n-- > 0) {
    if (arr_[n] === item_) {
      arr_.splice(n, 1);
    }
  }
}