

function branch(catid) {
		var mis = $('div#mis_'+catid);
		if(mis.length !=0) {
			if(mis[0].className == 'mis_opened') {
				mis.removeClass();
				mis.addClass('mis_closed');} 
			else {
				mis.removeClass();
				mis.addClass('mis_opened');
				checkObjects(catid);}		
		}
		return false;
	}

function showObjects(grkey) {
	var obj = $('tr#obj_'+grkey);
	var allopenedobj = $('.obj_opened');
	if (allopenedobj.length != 0) {
		allopenedobj.removeClass();
		allopenedobj.addClass('obj_closed');
	}
	if (obj.length != 0) {
		obj.removeClass();
		obj.addClass('obj_opened');
	}
	return false;
}

function loadObjects(grid) {
		$.ajax({
		type: "POST",
		url: "catalog.php",
		data: "action=getobjs&id="+grid+"",
		datatype: "html",
		beforeSend: function() {
			$('body')[0].style.cursor = 'wait';
			},
		complete: function(resp) {
			$('body')[0].style.cursor = 'default';
			},
		success: function(resp) {
			$('table#objtbl')[0].innerHTML = resp;
			},
		error: function(err) {
			$('body')[0].style.cursor = 'default';
			alert('Ошибка! Невозможно загрузить обьекты!');
			}
		});
	}

function loadObject(id) {
	$.ajax({
		type: "POST",
		url: "catalog.php",
		data: "id="+id+"&action=getobj&id="+id+"",
		datatype: "html",
		beforeSend: function() {
			$('body')[0].style.cursor = 'wait';
			},
		complete: function(resp) {
			$('body')[0].style.cursor = 'default';
			},
		success: function(resp) {
			$('div#show_obj')[0].innerHTML = resp;
			
			var $galimg = $('div.galimg');
			if($galimg.length != 0) {
				$galimg.hide();
				if($galimg.length > 3) {
					for(i=0;i<3;i++) {
						$('div#'+$galimg[i].id).show();
					}
				} else {
					$galimg.show();
					}
			}
			var $oimg = $('div.obj_img');
			if($oimg.length != 0) {
				$oimg[0].style.display = 'block';
			}
			
			showPage(2);
			
			},
		error: function(err) {
			$('body')[0].style.cursor = 'default';
			alert('Ошибка! Невозможно загрузить обьект!');
			}
		});
	}


function showImg(id) {
	$('div.obj_img').hide();
	$('div#img_'+id)[0].style.display = 'block';
	}

function ReadFile() {
	$.ajax({
		type: "POST",
		url: "catalog.php",
		data: "action=readfile",
		datatype: "html",
		beforeSend: function() {
			$('body')[0].style.cursor = 'wait';
			},
		complete: function(resp) {
			$('body')[0].style.cursor = 'default';
			},
		success: function(resp) {
			alert(resp);
			},
		error: function(err) {
			$('body')[0].style.cursor = 'default';
			alert('Ошибка! Невозможно добавить комментарий!');
			}
		});
	}
/**/








function getBranch(id, catid) {

	$.ajax({
		type: "POST",
		url: "catalog.php",
		data: "id="+id+"&action=category&catid="+catid+"",
		datatype: "html",
		beforeSend: function() {
			$('body')[0].style.cursor = 'wait';
			},
		complete: function(resp) {
			$('body')[0].style.cursor = 'default';
			},
		success: function(resp) {
			var mi = $('div#mi_'+catid);
			mi[0].innerHTML = resp;
			},
		error: function(err) {
			$('body')[0].style.cursor = 'default';
			alert('Ошибка! Невозможно добавить комментарий!');
			}
		});
		
		checkObjects(catid);			

		return false;
}

	
function checkObjects(catid) {
		var result = 0;
		$.ajax({
		type: "POST",
		url: "catalog.php",
		data: "action=objcheck&catid="+catid+"",
		datatype: "html",
		beforeSend: function() {
			$('body')[0].style.cursor = 'wait';
			},
		complete: function(resp) {
			$('body')[0].style.cursor = 'default';
			},
		success: function(resp) {
			
			if(resp != 0) {
				var tbl = $('table#objecttable');
				if(tbl.length != 0) {
					loadObjects(catid);
				} 
			} 
			return resp;
			},
		error: function(err) {
			$('body')[0].style.cursor = 'default';
			alert('Ошибка! Невозможно добавить комментарий!');
			}
		});
	}
/*	
function loadObjects(catid) {
		var result = 0;
		$.ajax({
		type: "POST",
		url: "catalog.php",
		data: "action=loadobjects&catid="+catid+"",
		datatype: "html",
		beforeSend: function() {
			$('body')[0].style.cursor = 'wait';
			},
		complete: function(resp) {
			$('body')[0].style.cursor = 'default';
			},
		success: function(resp) {
			
		if(resp != '') {
			var tbl = $('table#objecttable');
			if(tbl.length != 0) {
				tbl[0].innerHTML = resp;
			} 
		} 
			return resp;
			},
		error: function(err) {
			$('body')[0].style.cursor = 'default';
			alert('Ошибка! Невозможно добавить комментарий!');
			}
		});
	}
*/

function loadTree() {
		var result = 0;
		$.ajax({
		type: "POST",
		url: "catalog.php",
		data: "action=gettree",
		datatype: "json",
		beforeSend: function() {
			$('body')[0].style.cursor = 'wait';
			},
		complete: function(resp) {
			$('body')[0].style.cursor = 'default';
			},
		success: function(resp) {
		
			var tree = $('div#cat_menu_');
			var html = JSON.parse(resp);
			alert(html[0]['name']);
			tree[0].innerHTML = resp;
			},
		error: function(err) {
			$('body')[0].style.cursor = 'default';
			alert('Ошибка! Невозможно добавить комментарий!');
			}
		});
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	function prevImg() {
		var $galimg = $('div.galimg');					
		var o = document.createElement("array");
		var via = $.makeArray(0);		
		var j = 0;
		for(i=0;i<$galimg.length;i++) {
			if($galimg[i].style.display == 'block') {
				via[j] = i;
				j++;
			}
		}
		if(($galimg.length > 3) && (via[0] != 0)) {
			$galimg.hide();
			$galimg[via[0]-1].style.display = 'block';
			$galimg[via[0]].style.display = 'block';
			$galimg[via[1]].style.display = 'block';
		}
	}
	
	function nextImg() {
		var $galimg = $('div.galimg');					
		var o = document.createElement("array");
		var via = $.makeArray(0);		
		var j = 0;
		for(i=0;i<$galimg.length;i++) {
			if($galimg[i].style.display == 'block') {
				via[j] = i;
				j++;
			}
		}
		if(($galimg.length > 3) && (via[2] != $galimg.length-1)) {
			$galimg.hide();
			$galimg[via[1]].style.display = 'block';
			$galimg[via[2]].style.display = 'block';
			$galimg[via[2]+1].style.display = 'block';
		}
		
	}
	$(window).load(function() {
		var $galimg = $('div.galimg');
		if($galimg.length != 0) {
			$galimg.hide();
			if($galimg.length > 3) {
				for(i=0;i<3;i++) {
					$('div#'+$galimg[i].id).show();
				}
			}
		}
	});	
	
	
function showPage(pk) {
	var pg1 = $('div#pg1');
	var pg2 = $('div#pg2');
	if (pk == 1) {
		pg2.hide();
		pg1.fadeIn(600);
		}
	else if(pk == 2) {
		pg1.hide();
		pg2.fadeIn(600);
		} 
	}	
	
	
function sendComment($id,$text) {
	$.ajax({
		type: "POST",
		url: "catalog.php",
		data: "action=postcoment&txt="+$text+"&object="+$id,
		datatype: "json",
		beforeSend: function() {
			$('body')[0].style.cursor = 'wait';
			},
		complete: function(resp) {
			$('body')[0].style.cursor = 'default';
			},
		success: function(resp) {		
			loadObject($id);
			},
		error: function(err) {
			$('body')[0].style.cursor = 'default';
			alert('Ошибка! Невозможно добавить комментарий!');
			}
		});
	}	
	

function vote($id) {
	$value = $('.urei');
	$val = null;
	for(i=0;i<$value.length;i++) {
		if ($value[i].checked) {
			$val = $value[i].value;
			}
		}
	if($val!== null) {
		$.ajax({
			type: "POST",
			url: "catalog.php",
			data: "action=vote&object="+$id+"&val="+$val,
			datatype: "json",
			beforeSend: function() {
				$('body')[0].style.cursor = 'wait';
				},
			complete: function(resp) {
				$('body')[0].style.cursor = 'default';
				},
			success: function(resp) {
				$('#vote')[0].innerHTML = "Спасибо! Ваш голос принят.";
				//loadObject($id);
				},
			error: function(err) {
				$('body')[0].style.cursor = 'default';
				alert('Ошибка! Невозможно проголосовать!');
				}
			});
	} else {
		alert('Необходимо выбрать оценку!');
		}
}
	
	
	
	
	
	
	
