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.parentNode = function(el, tag) {
	var tag = tag.toUpperCase();
	while(el = el.parentNode) if (el.tagName.toUpperCase() == tag) return el;
	return null;
}

Utils.checkAll = function(aformname, aelname, acheck, ignore_disabled) {
	if (!ignore_disabled) ignore_disabled = false;
	var el =  (typeof aformname == 'string' ? document.forms[aformname] : 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]();
		});
	});
}

Utils.addHash = function(x, x2, prefix) {
	if (!x) x = {};
	Utils.each(x2 || {}, function(value, key) {
		var pos = key.indexOf('[');
		if (pos == -1) x[prefix+'['+key+']'] = value;
		else x[prefix+'['+key.substr(0, pos)+']'+key.substr(pos)] = value;
	});
	return x;
}

Utils.toArray = function(x) {
	if (typeof x != 'object') return [(x || '').toString()];
	var a = [];
	Utils.each(x, function(value, key) {
		a.push(value);
	});
	return a;
}

Utils.effectMoveTo = function(el, x, y, duration, method) {
	var left1 = parseInt(el.style.left) || 0;
	var top1 = parseInt(el.style.top) || 0;
	return new Tween({
		startValue: 0,
		endValue: 1,
		duration: duration || 1000,
		method: method || 'strongEaseOut',
		update: function(newValue) {
			el.style.left = parseInt(left1 + (x - left1) * newValue) + 'px';
			el.style.top = parseInt(top1 + (y - top1) * newValue) + 'px';
		},
		autorun: true
	});
}

Utils.effectAppear = function(el, duration, method) {
	return new Tween({
		startValue: 0,
		endValue: 1,
		duration: duration || 1000,
		method: method || 'strongEaseOut',
		update: function(newValue) {
			Element.setOpacity(el, newValue);
		},
		autorun: true
	});
}

Array.prototype.shuffle = function () {
    this.sort(function() {return 0.5 - Math.random();});
    return true;
}

Utils.arrayIntersect = function(arr1, arr2, returnOnEmptyArray) {
	if (!arr1.length || !arr2.length) return returnOnEmptyArray;
	for(var i = 0; i < arr1.length; i++)
		for(var j = 0; j < arr2.length; j++)
			if (arr1[i] == arr2[j]) return true;
	return false;
}

Utils.VerticalScroller = function(ainterval, divtxt, pic1txt, pic2txt) {
	var period = ainterval || 500;
	var myDiv = document.getElementById(divtxt);
	var pic2 = document.getElementById(pic2txt);
	pic2.innerHTML = document.getElementById(pic1txt).innerHTML;
	var myDivScrollTop;
	var picMarquee = function() {
		if(pic2.offsetHeight - myDiv.scrollTop <= 0){
			myDiv.scrollTop = 0;
		}else{
			myDivScrollTop = myDiv.scrollTop;
			myDiv.scrollTop++;
			if(myDivScrollTop == myDiv.scrollTop)
				myDiv.scrollTop =0;
		}
	}
	var timer = window.setInterval(picMarquee, period);
	myDiv.onmouseover = function() {
		window.clearInterval(timer);
	}
	myDiv.onmouseout=function() {
		timer = window.setInterval(picMarquee, period);
	}
}

Utils.HorizontalScroller = function(scrollEl, divEl, prefix, newtext) {
 this.scrollEl = document.getElementById(scrollEl);
 this.divEl = document.getElementById(divEl);
 var that = this;
 this.divEl.onmouseover = function() { that.stop();};
 this.divEl.onmouseout = function() { that.runAgain();};
 this.prefix = prefix;
 if (newtext !== undefined) this.updateHTML(newtext);
}

Utils.HorizontalScroller.prototype.updateHTML = function(newtext) {
 this.stop();
 var lwidth = this.divEl.offsetWidth;
 var ltext = (newtext === undefined ? this.scrollEl.innerHTML : newtext);
 if (ltext.match(/^\s*$/)) return;
 ltext = this.prefix + ltext;
 var ltext2 = ltext + ltext;
 this.scrollEl.innerHTML = ltext2;
 this.origwidth = this.scrollEl.offsetWidth / 2;
 var counter = 0;
 while(this.scrollEl.offsetWidth < lwidth + this.origwidth) {
	ltext2 += ltext;
	this.scrollEl.innerHTML = ltext2;
	if (++counter > 30) break;
 }
 this.scrollEl.style.left = Math.round(lwidth / 2) + 'px';
 this.runAgain();
}

Utils.HorizontalScroller.prototype.runAgain = function() {
	if (!this.stopped) return;
	this.stopped = false;
	this.run();
}

Utils.HorizontalScroller.prototype.stop = function() {
	if (this.timer) window.clearTimeout(this.timer);
	this.timer = null;
	this.stopped = true;
}

Utils.HorizontalScroller.prototype.run = function() {
	if (this.stopped) return;
 var lnew = Math.round(parseInt(this.scrollEl.style.left) - 1);
 if (lnew <= -this.origwidth) {
  lnew = 0;  
 } 
 this.scrollEl.style.left = lnew + 'px';
 var that = this;
 //alert();
 this.timer = window.setTimeout(function() {that.run()}, 50);
}

