/*
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').load('?c='+ajaxClass+'&ajax=1&service='+encodeURIComponent(service)+'&from='+encodeURIComponent(from), function (data) {
			$('#appointment-container').dialog({
				autoOpen: true,
				modal: true
			}).dialog('open');
		});

		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 = [];

	if (divId && divId.getElementsByTagName) {
		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").get(0)); // jquery
        //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) {
	if (window.global_prevActiveRow) {
		global_prevActiveRow.className = global_prevActiveRow.className.replace('active', '');
	}
	self.className += ' active';
	global_prevActiveRow = self;
	$('#company-details').load('', 'c=Companies&ajax=1&company='+companyID, function () {
		//afterAjaxCallback();
	});
}

(function( $ ) {
	$.fn.slideLoad = function (urlParams) {
		return this.each(function () {
			var $this = $(this);
			$this.slideUp();
			$.get('', urlParams, function (response) {
				//var h = $(response).hide();
				$this.html(response);
				$this.slideDown();
			});
		});
	}
})( jQuery );

function ajaxSubmitForm(self) {
	self = $(self);
	self.parent().load(
		self.attr('action'),
		self.serialize(),
		function(data){
		}
	);
	return false;
}

function ajaxAppear(a, target) {
	$.get(a.attr('href'), function (data) {
		$(data).hide().insertAfter(target).slideDown();
	});
}
function ajaxAppearInside(a, target) {
	$.get(a.attr('href'), function (data) {
		//var al = $('<div class="ajaxLoaded"></div>').html(data);
		//al.hide().appendTo(target).slideDown();
		target.html(data).slideDown();
	});
}

$(document).ready(function () {
	// automatic ajax processing
	$('.ajaxBelow').live('click', function (e) {
		var target = $(this);
		ajaxAppear($(this), target);
		e.preventDefault();
	});
	$('.ajaxBelowParent').live('click', function (e) {
		var target = $(this).parent();
		ajaxAppear($(this), target);
		e.preventDefault();
	});
	$('.ajaxBelowParentParent').live('click', function (e) {
		var target = $(this).parent().parent();
		ajaxAppear($(this), target);
		e.preventDefault();
	});

	$('.ajaxInsideParent').live('click', function (e) {
		var a = $(this);
		var target = $(this).parent();
		target.slideUp(function () {
			ajaxAppearInside(a, target);
		});
		e.preventDefault();
	});
	$('.ajaxInsideParentParent').live('click', function (e) {
		var a = $(this);
		var target = $(this).parent().parent();
		target.slideUp(function () {
			ajaxAppearInside(a, target);
		});
		e.preventDefault();
	});
	$('.ajaxInsideParentParentParent').live('click', function (e) {
		var a = $(this);
		var target = $(this).parent().parent().parent();
		target.slideUp(function () {
			ajaxAppearInside(a, target);
		});
		e.preventDefault();
	});	
});

