﻿/* 
* borderWatcher() v0.2
* Copyright (c) 2008 Esa I Ojala
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License. 
*
*
* Author: Esa
* 
* A GMap2()  method that tests if a given GLatLngBounds() is inside maps viewport or which borders does it cross.
* If borders are crossed, one or more of the returned booleans will be true 
* and corresponding CSS classnames will be added to map container node.
* If no borders are crossed, the method removes the added classnames.
* CSS of the page should be written so that each CSS class changes style of one side of the map container element border.
*
*/

/**
*  Version
*  0.2 the first public version 9/9/2008
*  
*  
**/

var BW_JS_VERSION = "0.2";

/**
*  Default CSS classnames.
*/

var BW_DEFAULT_CLASSNAMES = [
  "border-watcher-top",
  "border-watcher-right",
  "border-watcher-bottom",
  "border-watcher-left"
];


/**
* borderWatcher() method
* @ author Esa
* 
* @ param bounds. The bounds that is tested if it is inside viewport bounds of the map
* @ type GLatLngBounds()
* @ param opt_classNames. An optional array of four CSS classnames if the default names won't satisfy.
* @ type Array
* @ retuns Four booleans [top, right, bottom, left]
* @ type Array
*/

GMap2.prototype.borderWatcher = function(bounds, opt_classNames){
  var hiClass = opt_classNames || BW_DEFAULT_CLASSNAMES;
  var cssClass = this.getContainer().className.split(" ")[0]||"äöä";
  var topV = bounds.getNorthEast().lat();
  var rightV = bounds.getNorthEast().lng();
  var bottomV = bounds.getSouthWest().lat();
  var leftV = bounds.getSouthWest().lng();
  var viewport = this.getBounds();
  var topT = viewport.getNorthEast().lat();
  var rightT = viewport.getNorthEast().lng();
  var bottomT = viewport.getSouthWest().lat();
  var leftT = viewport.getSouthWest().lng();  
  var offTop = false;
  if(topT < topV){
    cssClass += " " + hiClass[0]||"";
    offTop = true;
  }
  var offRight = false;
  if(rightT < rightV){
    cssClass += " " + hiClass[1]||"";
    offRight = true;
  }
  var offBottom = false;
  if(bottomT > bottomV){
    cssClass += " " + hiClass[2]||"";
    offBottom = true;
  }
  var offLeft = false;
  if(leftT > leftV){
    cssClass += " " + hiClass[3]||"";
    offLeft = true;
  }
  this.getContainer().className = cssClass;
  return [
    offTop,
    offRight,
    offBottom,
    offLeft
  ];
};



