/**
 * Returns the week number for this date.  dowOffset is the day of week the week
 * "starts" on for your locale - it can be from 0 to 6. If dowOffset is 1 (Monday),
 * the week returned is the ISO 8601 week number.
 * @param int dowOffset
 * @return int
 */
Date.prototype.getWeek = function (dowOffset) {
/*getWeek() was developed by Nick Baicoianu at MeanFreePath: http://www.meanfreepath.com */

	dowOffset = typeof(dowOffset) == 'int' ? dowOffset : 0; //default dowOffset to zero
	var newYear = new Date(this.getFullYear(),0,1);
	var day = newYear.getDay() - dowOffset; //the day of week the year begins on
	day = (day >= 0 ? day : day + 7);
	var daynum = Math.floor((this.getTime() - newYear.getTime() - 
	(this.getTimezoneOffset()-newYear.getTimezoneOffset())*60000)/86400000) + 1;
	var weeknum;
	//if the year starts before the middle of a week
	if(day < 4) {
		weeknum = Math.floor((daynum+day-1)/7) + 1;
		if(weeknum > 52) {
			nYear = new Date(this.getFullYear() + 1,0,1);
			nday = nYear.getDay() - dowOffset;
			nday = nday >= 0 ? nday : nday + 7;
			/*if the next year starts before the middle of
 			  the week, it is week #1 of that year*/
			weeknum = nday < 4 ? 1 : 53;
		}
	}
	else {
		weeknum = Math.floor((daynum+day-1)/7);
	}
	return weeknum;
};

var googleMapsLoadScheduled = false;
var googleMapsOnLoadFunctions = [];

function scheduleGoogleMapsLoad(callback) {
	googleMapsLoadScheduled = true;
	if (typeof callback == 'function') {
		googleMapsOnLoadFunctions.push(callback);
	}
}

function googleMapsLoad() {
	for (i in googleMapsOnLoadFunctions) {
		googleMapsOnLoadFunctions[i]();
	}
}

function googleMapsScript() {
	if (googleMapsLoadScheduled) {
		var script = document.createElement("script");
		script.type = "text/javascript";
		script.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + "maps.google.com/maps/api/js?sensor=false&region=BE&async=2&callback=googleMapsLoad";
		document.body.appendChild(script);
		var markerClusterer = document.createElement("script");
		markerClusterer.type = "text/javascript";
		markerClusterer.src = $.siteUrl + "/js/markerclusterer_compiled.js";
		document.body.appendChild(markerClusterer);
	}
}

jQuery.fn.extend({
insertAtCaret: function(myValue){
  return this.each(function(i) {
    if (document.selection) {
      this.focus();
      sel = document.selection.createRange();
      sel.text = myValue;
      this.focus();
    }
    else if (this.selectionStart || this.selectionStart == '0') {
      var startPos = this.selectionStart;
      var endPos = this.selectionEnd;
      var scrollTop = this.scrollTop;
      this.value = this.value.substring(0, startPos)+myValue+this.value.substring(endPos,this.value.length);
      this.focus();
      this.selectionStart = startPos + myValue.length;
      this.selectionEnd = startPos + myValue.length;
      this.scrollTop = scrollTop;
    } else {
      this.value += myValue;
      this.focus();
    }
  })
}
});

var formatDatePeriod = function(startDay, endDay) {
	var period = '';
	var startDate = new Date('01/01/2011').setDayOfYear(startDay);
	var start = startDate.getDate() / startDate.getDaysInMonth(startDate.getMonth());
	if (start <= 1/3) {
		period += 'begin ';
	} else if (start <= 2/3) {
		period += 'midden ';
	} else {
		period += 'eind ';
	}
	period += startDate.getMonthName() + ' en ';
	var endDate = new Date('01/01/2011').setDayOfYear(endDay);
	var end = endDate.getDate() / endDate.getDaysInMonth(endDate.getMonth());
	if (end <= 1/3) {
		period += 'begin ';
	} else if (end <= 2/3) {
		period += 'midden ';
	} else {
		period += 'eind ';
	}
	period += endDate.getMonthName();
	return period;
}

var format = function(value, unit) {
	var x = value;
	if (typeof($.settings.units[unit][$.units]) != 'undefined') {
		return eval($.settings.units[unit][$.units].value.replace('$x', 'x'));
	} else {
		return x;
	}
}

var reformat = function(value, unit) {
	var x = value;
	if (typeof($.settings.units[unit][$.units]) != 'undefined') {
		return eval($.settings.units[unit][$.units].inverse.replace('$x', 'x'));
	} else {
		return x;
	}
}
