/*
function $(name) {
	var o = document.getElementById(name);
	return o ? o : {};
}
*/
//function activateAppointmentCell(ahref) {
	var prevActivated;
	/*return */activateAppointmentCell = function(ahref, service, from, ajaxClass) {
		if (prevActivated) {
			prevActivated.className = '';
		}

		$('appointment-container').style.backgroundColor = '#FFFFE0';
		jx.load('?c='+ajaxClass+'&ajax=1&service='+encodeURIComponent(service)+'&from='+encodeURIComponent(from), function (data) {
			$('appointment-container').innerHTML = data;
			$('appointment-container').style.backgroundColor = 'transparent';
		});

		ahref.parentNode.className = "activated";
		prevActivated = ahref.parentNode;
		//console.log(ahref);
	}
//}

function formSerialize(form) {
	var string = [];
	for (var el in form.elements) {
		el = form.elements[el];
		if (el.name) {
			//console.log(el);
			if (el.type == 'checkbox' || el.type == 'radio') {
				string.push(el.name+'='+encodeURIComponent(el.checked ? el.value : 0));
			} else {
				string.push(el.name+'='+encodeURIComponent(el.value));
			}
		}
	}
	string = string.join('&');
	return string;
}

function addListener(element, event, listener, bubble) {
	if (element.addEventListener) {
		if (typeof(bubble) == "undefined") bubble = false;
		element.addEventListener(event, listener, bubble);
	} else if (this.attachEvent) {
		element.attachEvent("on" + event, listener);
	}
}

function evalJSin(divId) {
	global_ajaxCallback = [];

	var x = divId.getElementsByTagName("script");
	//console.log(x);
	var h = document.getElementsByTagName("head")[0];
	for (var i=0; i<x.length; i++) {
		var s = document.createElement("script");
		s.type="text/javascript";
		h.appendChild(s);
		s.text = x[i].text;
		s.src = x[i].src;
	}
}

var global_ajaxCallback = [];
function afterAjaxCallback() {
	//console.log('afterAjaxCallback');
	for (var i in global_ajaxCallback) {
		var cb = global_ajaxCallback[i];
		//console.log(cb);
		var func = cb[0];
		var para = cb.slice(1);
		//console.log(func);
		//console.log(para);
		call_user_func_array(func, para);
	}
}

function StartGMap(address) {
	//console.log('StartGMap('+address+')');
	if (GBrowserIsCompatible()) {
        var map = new GMap2($("map_canvas"));
        //map.setCenter(new GLatLng(< ?= $this->service->company->data['geo'] ?>), 15);
        map.setUIToDefault();

        var geocoder = new GClientGeocoder();
		geocoder.getLatLng(address,
			function(point) {
				if (!point) {
					alert('not found');
				} else {
					map.setCenter(point, 15);
					var marker = new GMarker(point);
					map.addOverlay(marker);
					marker.openInfoWindowHtml(address);
				}
			}
		);
	}
}

function call_user_func_array(cb, parameters) {
    // http://kevin.vanzonneveld.net
    // +   original by: Thiago Mata (http://thiagomata.blog.com)
    // +   revised  by: Jon Hohle
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // *     example 1: call_user_func_array('isNaN', ['a']);
    // *     returns 1: true
    // *     example 2: call_user_func_array('isNaN', [1]);
    // *     returns 2: false

    var func;

    if (typeof cb == 'string') {
        if (typeof this[cb] == 'function') {
            func = this[cb];
        } else {
            func = (new Function(null, 'return ' + cb))();
        }
    } else if (cb instanceof Array) {
        func = eval(cb[0]+"['"+cb[1]+"']");
    }

    if (typeof func != 'function') {
        throw new Error(func + ' is not a valid function');
    }

    return func.apply(null, parameters);
}

/*
function defaultJSONHandler(self, div) {
	jx.load('?'+formSerialize(self), function (data) {
		if (data.redirect) {
			document.location = data.location;
		} else {
			$(div).innerHTML = data.html;
		}
	}, 'json');
	return false;
}
*/

function defaultJSONHandler(self, div) {
	self = $(self);
	jQuery.get(self.attr('action'), self.serialize(), function (response, status, xhr) {
		//console.log(response, status, xhr);
		var data = jQuery.parseJSON(response);
		//console.log(data);
		if (data.redirect) {
			document.location = data.location;
		} else if (data.html) {
			$('#'+div).html(data.html);
		} else {
			$('#'+div).html(response);
		}
	});
	return false;
}

String.prototype.zf = function() {
	return this.length == 1 ? "0" + this : this;
}

function companiesSelectOne(self, companyID) {
	$('#company-details').load('', 'c=Companies&ajax=1&company='+companyID, function () {
		afterAjaxCallback();
	});
	if (window.global_prevActiveRow) {
		global_prevActiveRow.className = global_prevActiveRow.className.replace('active', '');
	}
	self.className += ' active';
	global_prevActiveRow = self;
}