function Advertisement(host) {
	var self = this;
	this.user_id = 0;
	this.host = '.' + host;
	this.cookie = new Cookie();
	this.cookie_name = 'advertisement';
	this.getCookie = function() {
		var cookie = this.cookie.get(self.cookie_name);
		if (cookie != false) {
			return cookie;
		} else {
			return 'ad_sell';
		}
	}

	this.setCookie = function(value) {
		this.cookie.set(this.cookie_name, value, null, '/', this.host);
	}

	this.change = function(obj) {
		this.setCookie(obj.id);
		this.Post(obj.id);
		return false;
	}
	
	this.changefriends = function(obj) {
		this.setCookie(obj.id);
		this.Postfriends(obj.id);
		return false;
	}	

	this.show = function(user_id) {
		this.user_id = user_id || 0;
		this.Post(this.getCookie());
	}
	
	this.showfriends = function(user_id) {
		this.user_id = user_id;
		this.Postfriends(this.getCookie());
	}	

	this.Post = function(val) {
		var params = {};
		params.type = val;
		params.time = new Date().getTime();
		params.userid = this.user_id;

		jQuery.get('/advert/ajaxshow/', params, this.render, 'json');
		this.toggle(val);

	}
	
	this.Postfriends = function(val) {
		var params = {};
		params.type = val;
		params.time = new Date().getTime();
		params.userid = this.user_id;

		jQuery.get('/advert/ajaxshowfriends/', params, this.render, 'json');
		this.toggle(val);

	}	

	this.render = function(obj) {
		$('#advert-item').html(obj.html);
		$('#ad_count').html('');
		if (obj.count != 0) {
			$('#ad_count').html(obj.count);
		} else {
			$('#ad_count').html('0');
			$('#advert-item').html('Объявлений нет');
		}					
	}

	this.toggle = function(value) {
		if (value == 'ad_sell') {
			$('#ad_buy').parent().removeClass().addClass('ilink type')
			$('#'+value).parent().removeClass().addClass('ilink type-bg');
		} else {
			$('#ad_sell').parent().removeClass().addClass('ilink type')
			$('#'+value).parent().removeClass().addClass('ilink type-bg');
		}

	}

	this.add_watch = function(advert, user) {
		var advert_id = '#watch_' + advert;
		$(advert_id).removeClass('watch_on');
		$(advert_id).addClass('watch_off');
		$(advert_id).empty();
		$(advert_id)
		.append(
				'<a href="#" onclick="return advertisement.delete_watch(\''
				+ advert + '\',\'' + user
				+ '\');">не отслеживать</a>');
		$.ajax( {
			type :"POST",
			url :"/advert/addwatch/",
			dataType :'json',
			cache :'false',
			data : {
			advert :advert,
			user :user
		},
		success : function onAjaxSuccess(obj) {
			//
		}
		});
		return false;
	}

	this.delete_watch = function(advert, user) {
		var advert_id = '#watch_' + advert;
		$(advert_id).removeClass('watch_off');
		$(advert_id).addClass('watch_on');
		$(advert_id).empty();
		$(advert_id).append(
				'<a href="#" onclick="return advertisement.add_watch(\''
				+ advert + '\',\'' + user + '\');">отслеживать</a>');
		$.ajax( {
			type :"POST",
			url :"/advert/deletewatch/",
			dataType :'json',
			cache :'false',
			data : {
			advert :advert,
			user :user
		},
		success : function onAjaxSuccess(obj) {
			//
		}
		});
		return false;
	}

}

function Cookie() {
	this.cooks = [];
	var self = this;
	if ("" != document.cookie) {
		var p = document.cookie.split(/\s*;\s*/);
		var pL = p.length;
		for (i = 0; i < pL; i++) {
			var parts = p[i].split(/\s*=\s*/);
			this.cooks[parts[0]] = parts[1] ? unescape(parts[1]) : '';
		}
	}

	this.get = function(name) {
		if (this.cooks[name]) {
			return this.cooks[name]
		} else {
			return false;
		}
	}

	this.set = function(name, value, expire, path, domain, secure) {
		if (" " != name) {
			document.cookie = name
			+ "="
			+ escape(value)
			+ ((path == null) ? "" : ";path=" + path)
			+ ((expire == null) ? "" : ";expires="
				+ ((typeof (expire) == 'object') ? expire
						.toGMTString() : new Date(new Date()
						.getTime()
						+ expire * 1000).toGMTString()))
						+ ((domain == null) ? "" : ";domain=" + domain)
						+ ((secure == null) ? "" : ";secure");
			this.cooks[name] = escape(value);
			return false;
		}
		return false;
	}
	this.isSet = function(name) {
		return this.cooks[name] ? true : false;
	}

	this.del = function(name, path, domain) {
		if (this.isSet(name)) {
			document.cookie = name + "="
			+ ((path == null) ? "" : "; path=" + path)
			+ ((domain == null) ? "" : "; domain=" + domain)
			+ "; expires=Thu, 01-Jan-70 00:00:01 GMT";
			delete this.cooks[name];
			return true;
		}
		return false;
	}

}
