/*
 * Opacity GControl by Klokan Petr Pridal (based on XSlider of Mike Williams)
 */
 
function GOpacityControl( op , a ) {
  a = (!a) ? [] : ((typeof(a) != "array") ? [a] : a);
  this.opacity  = (!op) ? 1 : ((op>1) ? op/100 : op);
  this.overlays = a;
  this.width = 58; // pixels
  this.pkg = 'http://cordc.ucsd.edu/projects/charts/lib/pkg/xslider/';
  GControl.call(this,true,false);
}
GOpacityControl.prototype = new GControl(true,false);


GOpacityControl.prototype.addOverlay = function(o) { 
  o.getTileLayer().opacity = this.opacity;
  this.overlays.push(o);
};


GOpacityControl.prototype.removeOverlay = function(o) { 
  for(var i in this.overlays) { 
    if( this.overlays[i] == o ) { 
      this.overlays.splice(i,1);
      break;
    }
  }
};


GOpacityControl.prototype.clearOverlays = function() { 
  this.overlays = [];
};


// This function positions the slider to match the specified opacity
GOpacityControl.prototype.setSlider = function(pos) {
  var left = Math.round((this.width*pos));
  this.slide.left = left;
  this.knob.style.left = left+"px";
};


// This function reads the slider and sets the overlay opacity level
GOpacityControl.prototype.setOpacity = function() {
  if( this.opacity == (this.slide.left/this.width) ) { 
    return;
  }
  this.opacity = this.slide.left/this.width;
  for(var i in this.overlays) { 
    this.overlays[i].getTileLayer().opacity = this.opacity;
    this.map.removeOverlay(this.overlays[i]);
    this.map.addOverlay(this.overlays[i]);
  }
};

// This gets called by the API when addControl(new GOpacityControl())
GOpacityControl.prototype.initialize = function(map) {
  var that = this;
  this.map = map;

  // Is this MSIE, if so we need to use AlphaImageLoader
  var agent = navigator.userAgent.toLowerCase();
  if( ( agent.indexOf("msie") > -1 ) &&
      ( agent.indexOf("opera") < 1 ) ){
    this.ie = true;
  } else {
    this.ie = false;
  }

  // create the background graphic as a <div> containing an image
  var container = document.createElement("div");
  container.style.width = "70px";
  container.style.height = "21px";

  // Handle transparent PNG files in MSIE
  if (this.ie) {
    var loader = "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader("+
      "src='"+this.pkg+"opacity-slider.png', sizingMethod='crop');";
    container.innerHTML = '<div style="height:21px; width:70px; ' +
      loader+ '" ></div>';
  } else {
    container.innerHTML = '<div style="height:21px; width:70px; '+
      'background-image: url('+this.pkg+'opacity-slider.png)" ></div>';
  }

  // create the knob as a GDraggableObject
  // Handle transparent PNG files in MSIE
  if (this.ie) {
    var loader = "progid:DXImageTransform.Microsoft.AlphaImageLoader("+
      "src='"+this.pkg+"opacity-slider.png', sizingMethod='crop');";
    this.knob = document.createElement("div");
    this.knob.style.height = "21px";
    this.knob.style.width = "13px";
    this.knob.style.overflow = "hidden";
    this.knob_img = document.createElement("div");
    this.knob_img.style.height = "21px";
    this.knob_img.style.width = "83px";
    this.knob_img.style.filter = loader;
    this.knob_img.style.position = "relative";
    this.knob_img.style.left = "-70px";
    this.knob.appendChild(this.knob_img);
  } else {
    this.knob = document.createElement("div");
    this.knob.style.height = "21px";
    this.knob.style.width = "13px";
    this.knob.style.backgroundImage = "url("+this.pkg+"opacity-slider.png)";
    this.knob.style.backgroundPosition = "-70px 0px";
  }

  this.knob.style.position = "absolute";
  this.knob.style.top = "0px";
  this.knob.style.left = "0px";
  container.appendChild(this.knob);

  this.slide = new GDraggableObject(this.knob, {container:container});
  this.slide.setDraggableCursor('pointer');
  this.slide.setDraggingCursor('pointer');
  this.container = container;

  // attach the control to the map
  map.getContainer().appendChild(container);

  // init slider
  this.setSlider(this.opacity);

  // Listen for the slider being moved and set the opacity
  GEvent.addListener(this.slide, "dragend", function() {
    that.setOpacity();
  });
  //GEvent.addListener(this.container, "click", function( x, y ) { alert(x, y) });

  return container;
}

// Set the default position for the control
GOpacityControl.prototype.getDefaultPosition = function() {
  return new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(7, 20));
}
