var hwCallBackNarrow = {
	getNarrowList:function(result)
	{				
		updateListLocations(result.results);	
	}	
}

function handleLocations() {
	this.className = 'handleLocations';
	this.callBack = 'hwCallBackHandleLocations';
	this.dispatcher = new AJAX(url_prefix + 'libs/ajax/v2/server.php', this.className, this.callBack);
}

handleLocations.prototype  = {
	getGeoIPLocation: function() { return this.dispatcher.doCall('getGeoIPLocation', arguments); }
}

function handleDirectory() {
                this.className = 'handleDirectory';
                this.callBack = 'hwCallBackNarrow';
                this.dispatcher = new AJAX(url_prefix + 'libs/ajax/v2/server.php', this.className, this.callBack);
}

handleDirectory.prototype  = {
	getNarrowList: function() { return this.dispatcher.doCall('getNarrowList',arguments); }
}

function handleListings() {
                this.className = 'handleListings';
                this.callBack = 'listings_callback';
                this.dispatcher = new AJAX(url_prefix + 'libs/ajax/v2/server.php', this.className, this.callBack);
}

handleListings.prototype  = {
	addDigg: function() { return this.dispatcher.doCall('addDigg',arguments); },
	rateListing: function() { return this.dispatcher.doCall('rateListing',arguments); },
	insertListingClickToWebsiteStat: function() { return this.dispatcher.doCall('insertListingClickToWebsiteStat',arguments); }
}

function handleMobile() {
	this.className = 'handleMobile';
	this.callBack = 'callback_mobile';
	this.dispatcher = new AJAX(url_prefix + 'libs/ajax/v2/server.php', this.className, this.callBack);
}

handleMobile.prototype  = {
	send: function() { return this.dispatcher.doCall('send',arguments); }
}

function handleMyPage() {
	this.className = 'handleMyPage';
	this.callBack = 'hwCallBackMultimedia';
	this.dispatcher = new AJAX(url_prefix + 'libs/ajax/v2/server.php', this.className, this.callBack);
}

handleMyPage.prototype  = {
	getPictureList: function() { return this.dispatcher.doCall('getPictureList',arguments); },
	getVideosList: function() { return this.dispatcher.doCall('getVideosList',arguments); },
	sendMail: function() { return this.dispatcher.doCall('sendMail',arguments); },
	sendContactFormIfValidWord: function() { return this.dispatcher.doCall('sendContactFormIfValidWord',arguments); }
}

function AJAX(serverURL, className, callBack)
{
	this.serverURL = serverURL;
	this.callBack = callBack;
	this.className = className;
	this.htmlHead = document.getElementsByTagName('HEAD').item(0);
}

AJAX.prototype = {

	counter: 0,

	arguments: [],

	stringify: function (v) {
		var a = [];

		function e(s) {
			a[a.length] = s;
		}

		function g(x) {
			var c, i, l, v;

			switch (typeof x) {
			case 'object':
				if (x) {
					if (x instanceof Array) {
						e('[');
						l = a.length;
						for (i = 0; i < x.length; i += 1) {
							v = x[i];
							if (typeof v != 'undefined' &&
									typeof v != 'function') {
								if (l < a.length) {
									e(',');
								}
								g(v);
							}
						}
						e(']');
						return;
					} else if (typeof x.valueOf == 'function') {
						e('{');
						l = a.length;
						for (i in x) {
							v = x[i];
							if (typeof v != 'undefined' &&
									typeof v != 'function' &&
									(!v || typeof v != 'object' ||
										typeof v.valueOf == 'function')) {
								if (l < a.length) {
									e(',');
								}
								g(i);
								e(':');
								g(v);
							}
						}
						return e('}');
					}
				}
				e('null');
				return;
			case 'number':
				e(isFinite(x) ? +x : 'null');
				return;
			case 'string':
				l = x.length;
				e('"');
				for (i = 0; i < l; i += 1) {
					c = x.charAt(i);
					if (c >= ' ') {
						if (c == '\\' || c == '"') {
							e('\\');
						}
						e(c);
					} else {
						switch (c) {
						case '\b':
							e('\\b');
							break;
						case '\f':
							e('\\f');
							break;
						case '\n':
							e('\\n');
							break;
						case '\r':
							e('\\r');
							break;
						case '\t':
							e('\\t');
							break;
						default:
							c = c.charCodeAt();
							e('\\u00' + Math.floor(c / 16).toString(16) +
								(c % 16).toString(16));
						}
					}
				}
				e('"');
				return;
			case 'boolean':
				e(String(x));
				return;
			default:
				e('null');
				return;
			}
		}
		g(v);
		return a.join('');
	},

	doCall: function(method, arguments) {
		this.arguments = [];
		for (var i = 0; i < arguments.length; i++) {
			this.arguments[this.arguments.length] = arguments[i];
		}

		var url = this.serverURL + '?class=' + this.className + '&method=' + method + '&arguments=' + encodeURI(this.stringify(this.arguments) + '&callback=' + this.callBack) + '&timestamp=' + (new Date()).getTime();

		var script = document.createElement('SCRIPT');
		script.setAttribute('type', 'text/javascript');
		script.setAttribute('charset', 'utf-8');
		script.setAttribute('src', url);
		script.setAttribute('id', '_ajax_' + this.counter);
		this.htmlHead.appendChild(script);
	}
}