var req;
var block;

var hist = {
	position : 0,
	block : 'content',
	Hash : new Array(),
	cookies: new Array(),

	init : function() {
	},

	saveCookies: function() {
		this.cookies.splice(this.position, (this.cookies.length-this.position), document.cookie);
	},

	Save : function() {
		this.Hash.splice(this.position, (this.Hash.length-this.position), gebi(this.block).innerHTML);
		this.saveCookies();
		this.position++;
	},

	Back : function() {
		if (this.position>0) {
			this.Hash.splice(this.position, 1, gebi(this.block).innerHTML);
			this.saveCookies();
			this.position--;
			gebi(this.block).innerHTML = this.Hash[this.position];
			this.activateButtons();
			this.setMenu(this.position, this.position-1);
			activateFB();
      setImage();
		}
	},

	Forward : function() {
		this.position++;
		gebi(this.block).innerHTML = this.Hash[this.position];
		activateFB();
    setImage();
		this.setMenu(this.position-2, this.position-1);
	},

	first : function() {
		return (this.position == 0);
	},

	last : function() {
		return (this.position == this.Hash.length);
	},

	activateButtons : function() {
		if (!this.first()) {
			gebi('link_back').style.display="inline";
		}
		if (!this.last()) {
			gebi('link_fw').style.display="inline";
		}
	},

	setMenu: function(before, after) {
		closeAll(this.cookies[before]);
		expandAll(this.cookies[after]);
	}
};

function blockLoad(id, ref) {
	if (ref.substr(0,4) == 'http') {
		arr = ref.split("/").slice(3);
		ref = "/"+arr.join("/");
	}
	var url = "http://"+window.location.hostname+"/ajax"+ref;
	hist.Save();
	block = gebi(id);
	block.innerHTML = '<span id="waitLoad">&nbsp</span>';
	gebi('waitLoad').style.height = block.clientHeight;
	loadXMLDoc(url);
}

function blockPost(id, ref, form){
	var params = gatherForm(form);
	var url = "http://"+window.location.hostname+ref;
	block = gebi(id);
	block.innerHTML += '<span id="waitLoad">&nbsp</span>';
	gebi('waitLoad').style.height = block.clientHeight;
	loadXMLDoc(url, 'POST', params);
}

function gatherForm(form){
	var params = new Array();
	oForm = document.forms[form];
	var k =0;
	for ( var i = 0; i<oForm.elements.length;i++){
		var fieldNameVal = encodeURIComponent(oForm.elements[i].name)+'=';
		if ((oForm.elements[i].type) == "checkbox") {
			if (oForm.elements[i].checked == true) fieldNameVal +=	encodeURIComponent(oForm.elements[i].checked);
			else fieldNameVal="";
		}	else {
			if ((oForm.elements[i].value != 'off') && (oForm.elements[i].value != '')) fieldNameVal +=	encodeURIComponent(oForm.elements[i].value);
			else fieldNameVal="";
		}
		if (fieldNameVal != "") {
			params[k] = fieldNameVal;
			k += 1;
		}
  }
	return params.join('&');
}


function loadXMLDoc(url, method, params) {
		if (!method) method="GET";
    // для "родного" XMLHttpRequest
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChange;
        req.open(method, url, true);
				if (method=='POST')	req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        req.send(params);
        
    // для версии с ActiveX
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChange;
            req.open(method, url, true);
						if (method=='POST')	req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            req.send(params);
        }
    }
}

function processReqChange() {
    // только при состоянии "complete"
    if (req.readyState == 4) {
        // для статуса "OK"
        if (req.status == 200) {
            // здесь идут всякие штуки с полученным ответом
					block.innerHTML = req.responseText;
					replaceLinks();
					activateFB();
					hist.activateButtons();
					setImage();
					setTitle();
        } else {
            alert(req.status + " Не удалось получить данные:\n" +
                req.statusText);
        }
    }
}
