var PointXY = function (x, y){
    if (!x) x = 0;
    if (!y) y = x;
    this.x = x;
    this.y = y;
    this.top = this.y;
    this.left = this.x;
	this.toString = function(){
		return "x: "+this.x + " y: "+this.y;
	};
};


var DimWH = function (w, h){
    if (arguments.length == 1){
		this.w = arguments[0];
        this.h = arguments[0];
    }
	else {
	    this.w = w;
	    this.h = h;
    }
};
var PointFromStractPositions = function(a, b){
	return new PointXY(a.x - b.x, a.y - b.y);

};
var PointLL = function (lat, lng){
    if (!lat) {lat=0;}
    if (!lng) {lng=0;}
    this.lat = lat;
    this.lng = lng;
};
function getParameterByName(name){
  name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
  var regexS = "[\\?&]" + name + "=([^&#]*)";
  var regex = new RegExp(regexS);
  var results = regex.exec(window.location.search);
  if(results == null)
    return "";
  else
    return decodeURIComponent(results[1].replace(/\+/g, " "));
}

var LatLng = function (lat, lng){
    return new google.maps.LatLng(lat, lng);
};

var Bounds = function (sw, ne){
    if ($.isArray(sw)){
        sw = new LatLng (sw[0], sw[1]);
    }
    if ($.isArray(ne)){
        ne = new LatLng (ne[0], ne[1]);
    }

    return new google.maps.LatLngBounds(sw, ne);
};

function asinh (arg) {return Math.log(arg + Math.sqrt(arg*arg+1));}
Number.prototype.getYbyMercator = function (){ return (- asinh(Math.tan(this)) * (k.GMap.Dim.h / Math.PI)) + k.GMap.Dim.h/2};

Number.prototype.degToRad = function (){ return this*0.017453292519943295 };
Array.prototype.clone = function() { return this.slice(0); }
Array.prototype.shuffle = function (){for(var rnd, tmp, i=this.length; i; rnd=parseInt(Math.random()*i), tmp=this[--i], this[i]=this[rnd], this[rnd]=tmp);};
if(!Array.indexOf){Array.prototype.indexOf = function(obj){for(var i=0; i<this.length; i++){if(this[i]==obj){return i;}}return -1;}}
function subtractPos(a, b){return new PointXY(a.left - b.left, a.top - b.top);}
function resolveOrientation() {
	return Math.abs(window.orientation) == 90 ? "h" : "v";
}
//function subtractPos2(a, b){return new PointXY(a.x - b.x, a.y - b.y);}
//function subtractPos(a, b){return {top: a.top - b.top, left: a.left - b.left};}
function filterChecked(i, e){return $(e).is(":checked");}
function getParameterByName(name){
	var regexS = "[\\?&]"+name+"=([^&#]*)", regex = new RegExp( regexS ), results = regex.exec(window.location.href );
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	if( results == null ){
	  return "";
	}
	else{
		return decodeURIComponent(results[1].replace(/\+/g, " "));
	}
}

function readCookie(name) {
	var nameEQ = name + "=", ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function parseForm(form){
	var r = {};
	function parseText(i, el){r[el.id] =  el.value;}
	function parseCheckbox(i, el){r[el.id] = $(el).is(":checked");}
	form.find("input[type='text']").each(parseText);
	form.find("input:checkbox'").each(parseCheckbox);
	return r;
}

Array.prototype.remove = function(from, to) {var rest = this.slice((to || from) + 1 || this.length);this.length = from < 0 ? this.length + from : from;return this.push.apply(this, rest);};
Array.prototype.removeObject = function(o) {var t = this; $.each(t, function(i, item){if(o===item){t.remove(i); return false;}})};
jQuery.fn.outerHTML = function(s) {return (s) ? this.before(s).remove(): jQuery("<p>").append(this.eq(0).clone()).html();};


function fitStringToWidth(str,width,className) {
  // str    A string where html-entities are allowed but no tags.
  // width  The maximum allowed width in pixels
  // className  A CSS class name with the desired font-name and font-size. (optional)
  // ----
  // _escTag is a helper to escape 'less than' and 'greater than'
  function _escTag(s){ return s.replace("<","&lt;").replace(">","&gt;");}

  //Create a span element that will be used to get the width
  var span = document.createElement("span");
  //Allow a classname to be set to get the right font-size.
  if (className) span.className=className;
  span.style.display='inline';
  span.style.visibility = 'hidden';
  span.style.padding = '0px';
  document.body.appendChild(span);

  var result = _escTag(str); // default to the whole string
  span.innerHTML = result;
  // Check if the string will fit in the allowed width. NOTE: if the width
  // can't be determinated (offsetWidth==0) the whole string will be returned.
  if (span.offsetWidth > width) {
    var posStart = 0, posMid, posEnd = str.length, posLength;
    // Calculate (posEnd - posStart) integer division by 2 and
    // assign it to posLength. Repeat until posLength is zero.
    while (posLength = (posEnd - posStart) >> 1) {
      posMid = posStart + posLength;
      //Get the string from the begining up to posMid;
      span.innerHTML = _escTag(str.substring(0,posMid)) + '&hellip;';

      // Check if the current width is too wide (set new end)
      // or too narrow (set new start)
      if ( span.offsetWidth > width ) posEnd = posMid; else posStart=posMid;
    }

    result = '<abbr title="' +
      str.replace("\"","&quot;") + '">' +
      _escTag(str.substring(0,posStart)) +
      '&hellip;<\/abbr>';
  }
  document.body.removeChild(span);
  return result;
}

function setCookie(c_name,value,expiredays){
	var exdate=new Date(); exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +value+((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

String.prototype.printf = function (obj) {
  var useArguments = false;
  var _arguments = arguments;
  var i = -1;
  
  if (typeof _arguments[0] == "string") {
    useArguments = true;
  }
  if (obj instanceof Array || useArguments) {
    return this.replace(/\%s/g,
    function (a, b) {
      i++;
      if (useArguments) {
        if (typeof _arguments[i] == 'string') {
          return _arguments[i];
        }
        else {
          throw new Error("Arguments element is an invalid type");
        }
      }
      return obj[i];
    });
  }
  else {
    return this.replace(/{([^{}]*)}/g,
    function (a, b) {
      var r = obj[b];
      return typeof r === 'string' || typeof r === 'number' ? r : a;
    });
  }
}

if (typeof String.prototype.startsWith != 'function') {
  String.prototype.startsWith = function (str){
    return this.slice(0, str.length) == str;
  };
}

var isOldFirefox = jQuery.browser['mozilla'] && parseFloat(jQuery.browser['version']) < 4;
var VENDOR_SPECIFIC;

if ($.browser.webkit){
	VENDOR_SPECIFIC = "webkit";
}
else if ($.browser.msie){
	VENDOR_SPECIFIC = "ms";
}
else if ($.browser.mozilla){
	VENDOR_SPECIFIC = "moz";
}
else if ($.browser.opera){
	VENDOR_SPECIFIC = "o";
}

function browserSpecificCSS(css) {
	return css.replace("@@", VENDOR_SPECIFIC);
}
