
	var Photobar = {
		
		photosList : null,
		range : {
			current : 0,
			limit : 5
		},
		
		MakePhotobar : function() 
		{
			var html = "";
			//if (this.range.limit > this.photosList.length) this.range.limit = this.photosList.length;
			var j = 0;
			for (var i=this.range.current;j<this.range.limit && i<this.photosList.length;i++) {
					j++;
					html += this.getHtml(this.photosList[i]);
			}
			j = this.range.limit - j;
			if (j > 0) {
				for (var i=0;i<j;i++) {
					html += this.getHtml(this.photosList[i]);
				}
			}
			document.getElementById("photobar").innerHTML = html;
		},
		
		getHtml : function(message) 
		{
			var html = "";
			html += '<div><a href="/board/item/'+message.id+'">';
			html += '<div class="price_overlay">'+ message.price +' руб.</div>';
			html += '<img width="140" height="105" alt="img" src="/files/board/thumb/'+message.image_id+'.jpg" border="0"></a>';
			html += '<br><div class="photobar_autoname"><a href="/board/item/'+message.id+'">' + message.name + " " + message.year + '</a></div>';
			html += '</div>';
			return html;
		},
		
		ScrollDown : function()
		{
			this.range.current++;
			if (this.range.current >= this.photosList.length)
			 	this.range.current = 0;
			this.MakePhotobar();
		},
		
		ScrollUp : function()
		{
			this.range.current--;
			if (this.range.current < 0)
			 	this.range.current = this.photosList.length-1;
			this.MakePhotobar();
		},
		
		Scroll : function(event)
		{
			var delta = 0;
			if (event.wheelDelta) { 
	                delta = event.wheelDelta/120;
	               /* if (OP)
						delta = -delta;*/
	        } else if (event.detail) {
				delta = -event.detail/3;
	        }
			if (delta < 0) this.ScrollDown();
			if (delta > 0) this.ScrollUp();
			if (event.preventDefault) event.preventDefault();
			event.returnValue = false;
		}
		
	}
	
	/*function runBoardTrigger(id)
	{
		var autoDirs = [43];
		switchDisplay("autoOptionsDiv", false);
		if (in_array(autoDirs, id)) {
			//Auto.GetBrandsList();
			setValue("link_type", "auto");
			switchDisplay("additionalParams", true);
			switchDisplay("autoOptionsDiv", true);
		} else {
			setValue("link_type", "");
			switchDisplay("additionalParams", false);
			//document.getElementById("customDiv").innerHTML = "";
		}
	}*/
	function in_array(arr, value)
	{
		for (var i=0;i<arr.length;i++) {
			if (arr[i] == value) return true;
		}
		return false;
	}
	
	var Board = {
		
		boardActions : "/common/actions/board.php",
		
		DeleteByPassword : function()
		{
			var delForm = document.getElementById("message_del_form");
			this.Delete(delForm.message_id.value, delForm.message_pass.value);
		},
		
		Delete : function(id, password)
		{
			if (confirm("Удалить объявление?") == false) return false;
			var vars = "action=delete&id=" + id;
			if (password) vars += "&password=" + password;
			try {
				sendXmlHttpRequest("post", this.boardActions, vars, this.deleteCallback);
			} catch (e) {
				alert(e);
			}
		},
		deleteCallback : function(jsHttp)
		{	
			eval("var json = " + jsHttp.responseText);
			if (json.Message.Type == "error") {
				alert(json.Message.Text);
			} else if (json.Message.Type == "info") {
				//alert(json.Message.Text);
				location.reload();
			}
		},
		
		CheckFields : function()
		{
			var boardForm = document.getElementById("boardAddForm");
			var hasErrors = false;
			var errorText = "";
			var dirid = boardForm.dirid.value;
			if (boardForm.link_type.value == "auto") 
			{
				if (boardForm.year.value < 1950 || boardForm.year.value > 2010) {
					hasErrors = true;
					errorText += "Введите год между 1950 и 2010\n"; 
				}
				if (boardForm.price.value == "") {
					hasErrors = true;
					errorText += "Укажите цену машины\n";
				}
				if (boardForm.auto_brand.value == 0) {
					hasErrors = true;
					errorText += "Выберите марку машины\n";
				} else if (boardForm.auto_model.value == 0 && boardForm.custom_name.value == "") {
					hasErrors = true;
					errorText += "Укажите модель машины\n";
				}
					
			}
			if (dirid == 0) {
				hasErrors = true;
				errorText += "Выберите раздел\n";
			}
			if (boardForm.text.value == "" && boardForm.link_type.value != "auto") {
				hasErrors = true;
				errorText += "Введите текст объявления\n";
			}
			if (boardForm.icq.value == "" && boardForm.email.value == "" && boardForm.tel.value == "") {
				hasErrors = true;
				errorText += "Введите хотя бы один контакт\n";
			}
			if (hasErrors) {
				alert(errorText);
				return false;
			} else return true; 
		}
		
	}
	
	var Auto = {
		
		actionsPath : "/common/actions/auto.php",
		brands_selector_title: "Марка авто",
		hide_custom_name: false,
		
		//TODO: объединить с пред.?
		
		GetBrandsList : function(brandId, modelId, custom_name, show_models_list)
		{
			vars = "action=get_auto_brands";
			sendXmlHttpRequest("get", this.actionsPath, vars, 
				function (jsHttp)
				{
					eval("var json = " + jsHttp.responseText);
					var html = Auto.brands_selector_title + ': <select name="auto_brand" onChange="Auto.GetModelsList(this.value);">';
					html += '<option value="0">---</option>';
					for (var i=0;i<json.Brands.length;i++) 
					{
						var selected = (brandId == json.Brands[i].Id) ? "selected" : "";
						html += '<option value="'+json.Brands[i].Id+'" '+ selected +'>'+json.Brands[i].Name+'</option>';
					}
					html += '</select> <span id="autoModels"></span>';
					//html += '<br> <div style="float: right;">или введите своё название <input class="textbox" type="text" name="custom_name"></div><br>';
					//html += "<br>";
					document.getElementById("customDiv").innerHTML = html;
					if (modelId || custom_name || show_models_list)
						Auto.GetModelsList(brandId, modelId, custom_name);
				}
			);
		},
		
		GetModelsList : function(brandId, modelId, custom_name, hide_custom_name)
		{
			vars = "action=get_auto_models&brand=" + brandId;
			sendXmlHttpRequest("get", this.actionsPath, vars, 
				function (jsHttp)
				{
					eval("var json = " + jsHttp.responseText);
					var html = '<select name="auto_model">';
					html += '<option value="0">---</option>';
					for (var i=0;i<json.Models.length;i++) 
					{
						var selected = (modelId == json.Models[i].Id) ? "selected" : "";
						html += '<option value="'+json.Models[i].Id+'" '+ selected +'>'+json.Models[i].Name+'</option>';
					}
					html += "</select>";
					if (!custom_name) custom_name = "";
					if (!Auto.hide_custom_name)
						html += '<br> <div style="float: right;">или введите своё название <input class="textbox" type="text" name="custom_name" value="'+custom_name+'"></div><br>';
					document.getElementById("autoModels").innerHTML = html;
				}
			);
		}
		
	}
