var Utils = {};
Utils.PeriodicalFunction = function(aops) {
	this.maxTimes = 0;
	this.counter = 0;
	this.func = function() {};
	this.period = 1000;
	this.stopped = false;
	if (aops) Object.extend(this, aops);
	this.runFunc();
}
Utils.PeriodicalFunction.prototype.run = function() {
	this.stopped = false;
	this.runFunc();
}
Utils.PeriodicalFunction.prototype.stop = function() {
	this.stopped = true;
}
Utils.PeriodicalFunction.prototype.runFunc = function() {
	if (this.stopped) return;
	this.counter++;
	if (this.maxTimes && this.maxTimes <= this.counter) return;
	this.func();
	var that = this;
	window.setTimeout(function() {that.runFunc()}, this.period);
}
Utils.checkAll = function(aformname, aelname, acheck, ignore_disabled) {
	if (!ignore_disabled) ignore_disabled = false;
	var el = document.forms[aformname].elements[aelname];
	var arr = [];
	if (!el.length) {
		if (el.checked != acheck && !(ignore_disabled && el.disabled)) {
			arr.push(el);
			el.checked = acheck;
		}
	}
	else for(var i = 0; i < el.length; i++) {
		if (el[i].checked != acheck && !(ignore_disabled && el[i].disabled)) {
			arr.push(el[i]);
			el[i].checked = acheck;
		}
	}
	return arr;
}
Utils.collectProperty = function(arr, property) {
	var arr2 = [];
	for(var i = 0; i < arr.length; i++) {
		arr2.push(arr[i][property]);
	}
	return arr2;
}

Utils.each = function(arr, func) {
	if (arr.constructor == Array) {
		for(var i = 0; i < arr.length; i++) func(arr[i], i);
	}
	else {
		for(var i in arr) func(arr[i], i);
	}
}

if (window.GControl) {
Utils.GHtmlControl = Class.create();
Utils.GHtmlControl.prototype = Object.extend(new GControl(), {
	initialize: function(innerHTML, opt) {
		this.initialize = this.initialize_GMap;
		this.innerHTML = innerHTML;
		this.isFloat = false;
		if (opt) Object.extend(this, opt);
	},
	initialize_GMap: function(map) {
		this.container = document.createElement("div");
		this.container.innerHTML = this.innerHTML;
		if (this.isFloat) map.getPane(G_MAP_MARKER_PANE).appendChild(this.container);
		else map.getContainer().appendChild(this.container);
		return this.container;	
	},
	update: function(html) {
		this.container.innerHTML = html;
	},
	show: function() {
		this.container.style.display = 'block';
	},
	hide: function() {
		this.container.style.display = 'none';
	}
});
}

Utils.GMapEventHandler = function(map, fn) {
	var that = this;
	var infoWindow = map.getInfoWindow();
	var events = [
		{event: 'closeclick', object: infoWindow},
		{event: 'maximizeclick', object: infoWindow},
		{event: 'maximizeend', object: infoWindow},
		{event: 'restoreclick', object: infoWindow},
		{event: 'restoreend', object: infoWindow},
		{event: 'infowindowopen', object: map},
		{event: 'infowindowbeforeclose', object: map},
		{event: 'infowindowclose', object: map}
	];
	
	events.each(function(a) {
		that[a.event] = (fn && fn[a.event]) ? fn[a.event] : function() {};
		GEvent.addListener(a.object, a.event,  function() {
			if (that[a.event]) that[a.event]();
		});
	});
}

Array.prototype.shuffle = function () {
    this.sort(function() {return 0.5 - Math.random();});
    return true;
}