var ajax = new JsHttpRequest();

function showloginform() {
	ajax.onreadystatechange = function() {
		$('#spinner').css({ display: "block" });
		if(ajax.readyState == 4) {
			$('#spinner').css({ display: "none" });
			$('#authpop').css({ display: "block" });
			$('#screen').css({ display: "block" });
			$('#screen').css({ height: getyScroll() + "px" });
			$('object').css({ visibility: "hidden" });
			document.getElementById("authpop").innerHTML = ajax.responseText;
		}
	}
	ajax.open(null,"/ajax.php?action=authpop", true);
	ajax.send();
}
/*

*/
function showwarningform(param, mod) {
	ajax.onreadystatechange = function() {
		$('#spinner').css({ display: "block" });
		if(ajax.readyState == 4) {
			$('#spinner').css({ display: "none" });
			$('#authpop').css({ display: "block" });
			$('#screen').css({ display: "block" });
			$('#screen').css({ height: getyScroll() + "px" });
			document.getElementById("authpop").innerHTML = ajax.responseText;
		}
	}
	ajax.open(null,"/ajax.php?action=warningpop&param=" + param + "&mod=" + mod, true);
	ajax.send();
}

function showsendmeilform(cat, firm) {
	ajax.onreadystatechange = function() {
		$('#spinner').css({ display: "block" });
		if(ajax.readyState == 4) {
			$('#spinner').css({ display: "none" });
			$('#authpop').css({ display: "block" });
			$('#screen').css({ display: "block" });
			$('#screen').css({ height: getyScroll() + "px" });
			$('object').css({ visibility: "hidden" });
			//set_options($(form).elements["title"], ajax.responseJS.title);
			document.getElementById("authpop").innerHTML = ajax.responseText;
			$('select#title').css({ display: "none" });
		}
	}
	ajax.open('get',"/firms/ajax.php", true);
	ajax.send({prop:'mailpop', cat:cat, firm:firm});
}

function showcontactform() {
	ajax.onreadystatechange = function() {
		$('#spinner').css({ display: "block" });
		if(ajax.readyState == 4) {
			$('#spinner').css({ display: "none" });
			$('#authpop').css({ display: "block" });
			$('#screen').css({ display: "block" });
			$('#screen').css({ height: getyScroll() + "px" });
			document.getElementById("authpop").innerHTML = ajax.responseText;
		}
	}
	ajax.open(null,"/doska/ajax.php?prop=contactpop", true);
	ajax.send();
}

function showrules() {
	ajax.onreadystatechange = function() {
		$('#spinner').css({ display: "block" });
		if(ajax.readyState == 4) {
			$('#spinner').css({ display: "none" });
			$('#authpop').css({ display: "block" });
			$('#screen').css({ display: "block" });
			$('#screen').css({ height: getyScroll() + "px" });
			document.getElementById("authpop").innerHTML = ajax.responseText;
		}
	}
	ajax.open(null,"/doska/ajax.php?prop=show_rules", true);
	ajax.send();
}


function set_options(select, arr) {
	select.length = 1;
	for (var i in arr) {
		select.options[select.length] = new Option(arr[i]['title'], arr[i]['id'], false, false);
	}
	select.selectedIndex = 0;
	return;
}

function show_subcat(form, cat) {
	if (cat > 0) {
		ajax.onreadystatechange = function() {
			$('#spinner').css({ display: "block" });
			$('#sub_cat').attr("disabled","disabled");
			if(ajax.readyState == 4) {
				$('#spinner').css({ display: "none" });
				$('#sub_cat').removeAttr("disabled");
				set_options(form.elements["type"], []);
				set_options(form.elements["sub_cat"], ajax.responseJS.sub_cat);
			}
		}
		ajax.open(null,"/doska/ajax.php?prop=show_subcat&cat=" + cat, true);
		ajax.send();
	} else {
		set_options(form.elements["sub_cat"], []);
		set_options(form.elements["type"], []);
	}
}

function show_type(form, cat) {
	if (cat > 0) {
		ajax.onreadystatechange = function() {
			$('#spinner').css({ display: "block" });
			$('#type').attr("disabled","disabled");
			if(ajax.readyState == 4) {
				$('#spinner').css({ display: "none" });
				$('#type').removeAttr("disabled");
				set_options(form.elements["type"], ajax.responseJS.type);
			}
		}
		ajax.open(null,"/doska/ajax.php?prop=show_type&cat=" + cat, true);
		ajax.send();
	} else {
		set_options(form.elements["type"], []);
	}
}

function hideloginform() {
	$('#authpop').css({ display: "none" });
	$('#authpop2').css({ display: "none" });
	$('#menupop').css({ display: "none" });
	$('#screen').css({ display: "none" });
	$('object').css({ visibility: "visible" });
}
function getyScroll() {
	yScroll = 0;
	if (window.innerHeight && window.scrollMaxY || window.innerWidth && window.scrollMaxX) {
		yScroll = window.innerHeight + window.scrollMaxY;
		xScroll = window.innerWidth + window.scrollMaxX;
		
		var deff = document.documentElement;
		var wff = (deff&&deff.clientWidth) || document.body.clientWidth || window.innerWidth || self.innerWidth;
		var hff = (deff&&deff.clientHeight) || document.body.clientHeight || window.innerHeight || self.innerHeight;
		
		xScroll -= (window.innerWidth - wff);
		yScroll -= (window.innerHeight - hff);
	} 
	else if (document.body.scrollHeight > document.body.offsetHeight || document.body.scrollWidth > document.body.offsetWidth) {
		yScroll = document.body.scrollHeight;
		xScroll = document.body.scrollWidth;
	} 
	else {
		yScroll = document.body.offsetHeight;
		xScroll = document.body.offsetWidth;
	}
	return yScroll;
}
function search_tab_selected(elem) {
	 $('ul.search li').removeClass();
	 elem.className = "this";
}
function search_type_selected(value){
	$('#search_type').val(value);
}
function submitf(form) {
	$('searchform').submit();
}
function toggle_footer(param) {
	ajax.onreadystatechange = function() {
		$('#spinner').css({ display: "block" });
		if(ajax.readyState == 4) {
			$('#spinner').css({ display: "none" });
			document.getElementById('cfoot').innerHTML = ajax.responseText;
		}
	}
	ajax.open(null,"/ajax.php?action=toggle_footer&param=" + param, true);
	ajax.send();
}
function toggle_filter() {
	
	$('.eye span').text('показать фильтр');
	
	$("#filter_form").toggle(
		function () {
			$(this).css("display", "none");
		},
		function () {
			$(this).css("display", "none");
		}
	);
	return false;
}

function toggle_rest_menu(cat) {
	//alert(cat);
	$('.info ul li table').css("display", "none");
	$('.info ul li a').css("font-weight", "normal");
	$('#' + cat + ' table').css("display", "block");
	$('#' + cat + ' a').css("font-weight", "bold");
	
	//.attr('style', 'background-color: #' + ajax.responseJS.butt['color']);
}

function menu(pos, ico) {
	ajax.onreadystatechange = function() {
		$('#spinner').css({ display: "block" });
		if(ajax.readyState == 4) {
			$('#spinner').css({ display: "none" });
			$('#menupop').css({ display: "none" });
			$('#screen').css({ display: "none" });
			$('object').css({ visibility: "visible" });
			document.getElementById(pos).innerHTML = ajax.responseText;
			//$("#"+ pos).html(ajax.responseText);
		}
	}
	ajax.open(null,"/ajax.php?action=check_icon&pos=" + pos + "&icon=" + ico, true);
	ajax.send();
	
	return false;
}


function check_icon(index) {
	ajax.onreadystatechange = function() {
		$('#spinner').css({ display: "block" });
		if(ajax.readyState == 4) {
			$('#spinner').css({ display: "none" });
			$('#menupop').css({ display: "block" });
			$('#screen').css({ display: "block" });
			$('#screen').css({ height: getyScroll() + "px" });
			$('#menutbl a').attr('class', index);
			$('object').css({ visibility: "hidden" });
			document.getElementById("menupop").innerHTML = ajax.responseText;
		}
	}
	ajax.open(null,"/ajax.php?action=menupop&index=" + index, true);
	ajax.send();	
	return false;
}

function add_note(id, type) {
	ajax.onreadystatechange = function() {
		$('#spinner').css({ display: "block" });
		if(ajax.readyState == 4) {
			$('#spinner').css({ display: "none" });
			if(type == 'table') {
				$('#add_note_' + id + ' img').attr('src', ajax.responseJS.butt['ico']);
				$('#add_note_tr_' + id).attr('style', 'background-color: #' + ajax.responseJS.butt['color']);
				$('#my_notes').text(ajax.responseJS.my_notes);
			} else {
				$('#add_note img').attr('src', ajax.responseJS.butt['ico']);
				$('#add_note a').text(ajax.responseJS.butt['text']);
				$('#my_notes').text(ajax.responseJS.my_notes);
			}
		}
	}
	ajax.open(null,"/doska/ajax.php?prop=add_note&id=" + id + "&type=" + type, true);
	ajax.send();
	return false;
}

$(document).ready(function() {
	$(".toggle").click( function () {
		var index = $(this).parents('li.m').attr('id');
		check_icon(index);
		return false;
	});						  
	
	
});
