var bookcaseform_loaded = 0
var imgloading = "<img src='/images/loadingmini.gif' width=20 height=20 border=0 alt='&infin;'>";
var imgloading_bookcase = "<img src='/images/loadmini_bookcase.gif' width=14 height=14 border=0 alt='&infin;'>";



function LoadBookCaseForm(work_edition_id,type)
{
  if (!work_edition_id) { return }
  if (bookcaseform_loaded == 0)
  {
    var random_num = (Math.round((Math.random()*Math.random()*10001)+1));
    var req = getXmlHttp();
    req.open("GET", "/bookcaseset"+type+work_edition_id+"form?"+random_num, false); 
    req.send(null);
//alert("/bookcaseset"+type+work_ediion_id+"form?"+random_num);
    if (req.readyState == 4) 
    {
      if(req.status == 200) { document.getElementById("bookcaseframe").innerHTML=req.responseText }

    }
    bookcaseform_loaded = 1;
  }
}

function LoadBookCaseForm_1(work_edition_id,type)
{
  if (!work_edition_id) { return }
  if (bookcaseform_loaded == 0)
  {
    var random_num = (Math.round((Math.random()*Math.random()*10001)+1));
    var req = getXmlHttp();
    req.open("GET", "/bookcaseset"+type+work_edition_id+"form?"+random_num, false); 
    req.send(null);
//alert("/bookcaseset"+type+work_ediion_id+"form?"+random_num);
    if (req.readyState == 4) 
    {
      if(req.status == 200) { document.getElementById("bookcaseframe").innerHTML=req.responseText }
    }
    bookcaseform_loaded = 1;
  }
}


function BookCaseCheckClick(work_edition_id,bc_id,vote)
{
  if (!work_edition_id) { return }

    vote = document.getElementById("bc"+bc_id).checked; 
    document.getElementById("bcload"+bc_id).innerHTML = imgloading;
    var random_num = (Math.round((Math.random()*Math.random()*10001)+1));
    var req = getXmlHttp();
    req.open("GET", "/bookcaseclick"+work_edition_id+"bc"+bc_id+"change"+vote, false); 
    req.send(null);
    if (req.readyState == 4) 
    {
      if(req.status == 200) { 
              document.getElementById("bcload"+bc_id).innerHTML=""; 
      }
    }
    
}
//неоткуда брать Offset.
//1.забить в каждый элемент???
//2.брать из ссылок??? 
function BookCaseWorkDelete(work_id){
	if (!work_id) {
		return;
	}
	document.getElementById("del_td" + work_id).innerHTML = imgloading_bookcase;
	
	var list = document.getElementById("bcase_sel");
	var option = list.options[list.selectedIndex];
	var bcase_id = option.value;
	var bcase_type = option.parentNode.title;
		
	var req = getXmlHttp();
	req.open("GET", "/bookcaseworkdelete" + work_id, false);
	req.send(null);
	
	if (req.readyState == 4) {
		if (req.status == 200) {
			var leftLink = document.getElementById("left_link");
			var bcase_content = document.getElementById("bcase_cont"); //?
			var offset = 0;
			if (leftLink != null) {
				offset = leftLink.getAttribute("rel");				
				/*---------------*/
				if (bcase_content.getElementsByTagName("tr").length == 1) {					
					offset -= 10;
				}
				/*---------------*/
			}
			var req_2 = getXmlHttp();
			req_2.open("GET", "/bookcasechange"+bcase_id+"offset"+offset+"type="+bcase_type, false);
			req_2.send(null);
			if (req_2.readyState == 4) {
				if (req_2.status == 200) {
					 document.getElementById("bcase_cont").innerHTML = req_2.responseText;
				}
			}
		}
	}
}
	


function BookCaseChange(offset)
{
	var list = document.getElementById("bcase_sel");
	var option = list.options[list.selectedIndex];
	var bcase_id = option.value;
	var bcase_type = option.parentNode.title;
	
	/*-----------------*/
	var parent_panel = document.getElementById("bcase_cont").parentNode;
	var load_img = document.getElementById("ch_load");
	load_img.style.display = "block";
	load_img.style.top = parent_panel.offsetHeight / 2 - load_img.offsetHeight / 2;
	load_img.style.left = parent_panel.offsetWidth / 2 - load_img.offsetWidth / 2;
	
	
	/*-----------------*/
	var req = getXmlHttp();
	req.open("GET", "/bookcasechange"+bcase_id+"offset"+offset+"type="+bcase_type, false);
	
	req.send(null);
	if (req.readyState == 4)
	{
		if (req.status == 200)
		{
			load_img.style.display = "none";
			document.getElementById("bcase_cont").innerHTML = req.responseText;
			save_bcase_id(bcase_id,bcase_type);
		}
	}
}

function save_bcase_id(bcase_id, bcase_type) {
	var expires = new Date();
 	expires.setTime(expires.getTime() + (1000 * 86400 * 365)); 
	document.cookie = "bookcase=" + escape(bcase_id) + "_" + escape(bcase_type) + "; expires=" + expires.toGMTString() + "; path=/; domain= fantlab.ru;";
}

function BookCaseCheckClick(work_edition_id, bc_id){

	var galka_elem = document.getElementById("galka_" + work_edition_id + "_" + bc_id);
	var memory_input = document.getElementById("memoriz_" + work_edition_id).value;
	
	if (galka_elem.rel == "true") {
		vote = "false;"
		galka_elem.rel = "false";
		galka_elem.innerHTML = '&#9633;';
		galka_elem.style.color = '#aaa';
		
		document.getElementById("bccommblock_" + work_edition_id + "_" + bc_id).style.display = 'none';
		document.getElementById("bccomm_" + work_edition_id + "_" + bc_id).innerHTML = '';
		document.getElementById("row_" + work_edition_id + "_" + bc_id).style.display = "none";
		//удаление позиции из списка
		if (memory_input < 2) {
			document.getElementById("bcases_" + work_edition_id).style.display = "none";
			document.getElementById("bc_message_" + work_edition_id).style.display = "block";
		}
		memory_input--;
		document.getElementById("memoriz_" + work_edition_id).value = memory_input;
	}
	else {
		galka_elem.rel = "true";
		vote = "true";
		galka_elem.innerHTML = '&#9632;';
		galka_elem.style.color = '#3277A7';
		document.getElementById("bccommblock_" + work_edition_id + "_" + bc_id).style.display = '';
		if (memory_input == 0) {
			document.getElementById("bc_message_" + work_edition_id).style.display = "none";
			document.getElementById("bcases_" + work_edition_id).style.display = "";
		}
		memory_input++;
		document.getElementById("memoriz_" + work_edition_id).value = memory_input;
		document.getElementById("row_" + work_edition_id + "_" + bc_id).style.display = "";
		//document.getElementById("row_" + work_edition_id + "_" + bc_id).style.display = "block";
	}
	//var IE = (navigator.appName == "Microsoft Internet Explorer"?true:false);	
	var prev_state;	
	/*if (IE) {
		prev_state = document.getElementById("ie_img_" + work_edition_id + "_" + bc_id).innerHTML;
		document.getElementById("ie_img_" + work_edition_id + "_" + bc_id).innerHTML = imgloading;
	}*/	
	if (!work_edition_id) {
		return;
	}
	//var random_num = (Math.round((Math.random()*Math.random()*10001)+1));
	var req = getXmlHttp();
	
	req.open("GET", "/bookcaseclick" + work_edition_id + "bc" + bc_id + "change" + vote, true);
	/*if (IE) {
		req.onreadystatechange = function(){
			if (req.readyState == 4) {
				if (req.status == 200) {
					document.getElementById("ie_img_" + work_edition_id + "_" + bc_id).innerHTML = prev_state;
				}
			}
		};
	}*/
	req.send(null);
}




function SetItemComment(work_edition_id,bc_id,titl)
{
  titl = titl || 'заметка к позиции';
  var c = prompt(titl, document.getElementById("bccomm_"+work_edition_id+"_"+bc_id).innerHTML );
  if (c != null) {
    var random_num = (Math.round((Math.random()*Math.random()*10001)+1));
    var req = getXmlHttp();
    req.open("GET", "/bookcasecomm"+bc_id+"item"+work_edition_id+"comm?txt="+escapeEx(c)+"&"+random_num, false); 
    req.send(null);
    document.getElementById("bccomm_"+work_edition_id+"_"+bc_id).innerHTML=c;
    document.getElementById("bccommblock_"+work_edition_id+"_"+bc_id).style.display='';
	var p_bccomm = document.getElementById("p_bccomm_"+work_edition_id+"_"+bc_id);
	if (c == "") {
		p_bccomm.style.display = 'none';
	}
	else {
		p_bccomm.style.display = 'inline';
		p_bccomm.setAttribute("title", c);
	}
  }
}



function BookCaseItemDel(work_edition_id,bc_id)
{
  if (confirm("Убрать позицию с полки?")) {
    var random_num = (Math.round((Math.random()*Math.random()*10001)+1));
    var req = getXmlHttp();
    req.open("GET", "/bookcaseclick"+work_edition_id+"bc"+bc_id+"change"+"false", false); 
    req.send(null);
    document.getElementById("e"+work_edition_id).style.backgroundColor='#dddddd';
    document.getElementById("e"+work_edition_id).style.color='#aaaaaa';
    document.getElementById("e"+work_edition_id).style.opacity ='0.6';
  }
}



function BookCaseCreate(work_edition_id, bookcasetype, c)
{
    document.getElementById("bcnew").innerHTML = imgloading;
    var random_num = (Math.round((Math.random()*Math.random()*10001)+1));
    var req = getXmlHttp();
    req.open("GET", "/bookcasecreate"+bookcasetype+"?name="+escapeEx(c)+"&type="+bookcasetype+"&shared=on&"+random_num, false); 
    req.send(null);
    if (req.readyState == 4) 
    {
      if(req.status == 200) { 
        if(req.responseText.indexOf("Ok")>-1) {
	   			bookcaseform_loaded=0;
           LoadBookCaseForm(work_edition_id,bookcasetype);
 		}
      }
    }
}

function BookCaseCreateNew(work_edition_id, bookcasetype, c)
{
	var bcnew_elem = document.getElementById("bcnew");
	var prev_state =  bcnew_elem.innerHTML;
    bcnew_elem.innerHTML = imgloading;
    var random_num = (Math.round((Math.random()*Math.random()*10001)+1));
    var req = getXmlHttp();	
    req.open("GET", "/bookcasecreate"+bookcasetype+"?name="+escapeEx(c)+"&type="+bookcasetype+"&shared=on&"+random_num, false); 
    req.send(null);
    if (req.readyState == 4) 
    {
      if(req.status == 200) { 	
	
	  		var bc_id = parseInt(req.responseText);	
					
			bookcaseform_loaded = 0;
			bcnew_elem.style.display = "none";
			bcnew_elem.innerHTML = prev_state;
			document.getElementById("bcstr").style.display = "block";
			var bc_case_row = document.createElement("tr");
			bc_case_row.onmouseover = function() {this.style.backgroundColor='#E1E6EC';}
			bc_case_row.onmouseout = function() {this.style.backgroundColor='';}			
			//текст скрипта стал монструозным. Из-за бага firefox 3.6
			var bc_case_row_td1 = document.createElement("td");
			//bc_case_row_td1.setAttribute("nowrap","");
			//bc_case_row_td1.setAttribute("class","bctd"); //hover в ie всё равно не работает! Х))))
			bc_case_row_td1.height = "23";
			bc_case_row_td1.style.cursor = "pointer";
			bc_case_row_td1.vAlign = "middle";
			bc_case_row_td1.style.borderTop = "1px solid #3377A8";
			bc_case_row_td1.innerHTML = "<div style=\"float:right;padding:5px;display:inline-block;\"><span id=\"bccommblock_"+work_edition_id + "_" + bc_id+"\"  \
								title='оставить заметку к позиции' class=\"agray2\" style=\"cursor:pointer;width:auto;display:none;font-style:italic;\" \
								onclick=\"SetItemComment("+work_edition_id+","+bc_id+")\"># <span id='bccomm_"+work_edition_id+"_"+bc_id+"'></span></span></div>	\
								<div href=\"javascript://\" class=\"bctxt\" id=\"bctext_"+bc_id+"\" onclick=\"BookCaseCheckClick("+work_edition_id+","+bc_id+")\" \
								style=\"display:block;cursor:pointer;text-align:left;padding:5px\"> \
								<a id=\"galka_"+work_edition_id+"_"+bc_id+"\" rel=\"false\" style=\"width:20px;height:20px;text-align:center;font-size:15px;color:#aaa;margin-top:-5px; float:left\">&#9633;</a> \
								<span id=bcname_"+work_edition_id+"_"+bc_id+"\">"+c+"</span></div>";
			var bc_case_row_td2 = document.createElement("td");
			bc_case_row_td2.width = "20";
			bc_case_row_td2.style.cursor = "pointer";
			bc_case_row_td2.style.borderTop = "1px solid #3377A8";
			bc_case_row_td2.bgColor = "#DFE8EF";
			bc_case_row_td2.align = "center";
			bc_case_row_td2.onmouseover = function() {this.style.backgroundColor='#ffe6ba';}
			bc_case_row_td2.onmouseout = function() {this.style.backgroundColor='#dfe8ef';}
			bc_case_row_td2.innerHTML = "<a style=\"display:block;color:#343233;text-decoration:none;\" href=\"/bookcase"+bc_id+"\">&nbsp;&raquo;&nbsp;</a>";
			bc_case_row.appendChild(bc_case_row_td1);
			bc_case_row.appendChild(bc_case_row_td2);
		    var footer_row = bcnew_elem.parentNode.parentNode; //div->td->tr...... *facepalm*
		    var bccase_parent = footer_row.parentNode; //не в таблицу надо вставлять, а в tbody!!!!
			bccase_parent.insertBefore(bc_case_row, footer_row);
			//вставляем в постоянную панель
			var bc_panel_row = document.createElement("tr");
			bc_panel_row.id = "row_"+work_edition_id+"_"+bc_id;
			bc_panel_row.style.display = "none";
			bc_panel_row.onmouseover = function() {this.style.backgroundColor='#E1E6EC';}
			bc_panel_row.onmouseout = function() {this.style.backgroundColor='';}
			var bc_panel_row_td1 = document.createElement("td");
			bc_panel_row_td1.height = "23";
			bc_panel_row_td1.style.cursor = "pointer";
			//bc_panel_row_td1.setAttribute("class","bctd");
			if (navigator.appName == "Microsoft Internet Explorer")
				bc_panel_row_td1.setAttribute("onclick", function() { bclick(); });
			else
				bc_panel_row_td1.setAttribute("onclick","bclick();");
			bc_panel_row_td1.innerHTML = "<span style=\"cursor:pointer; width: 20px; height: 20px; text-align: center; font-size: 15px; margin-top: 0; margin-left:4; float: left; text-decoration:none;color: #3277A7\"><b>&radic;</b></span> \
					<span id=\"p_bccomm_"+work_edition_id+"_"+bc_id+"\" title=\"$bookcase_item_comment{$bcid}\" class=\"agray2\" style='display:none;;cursor:pointer;float:right;font-style:italic;padding-top:3;padding-right:5;'>#</span> \
					<span style='cursor:pointer;width:auto;padding-top:3;float:left;'>"+c+"</span>";
			var bc_panel_row_td2 = document.createElement("td");
			bc_panel_row_td2.width = "20";
			bc_panel_row_td2.style.cursor = "pointer";
			bc_panel_row_td2.bgColor = "#DFE8EF";
			bc_panel_row_td2.align = "center";
			bc_panel_row_td2.onmouseover = function() {this.style.backgroundColor='#ffe6ba';}
			bc_panel_row_td2.onmouseout = function() {this.style.backgroundColor='#dfe8ef';}
			bc_panel_row_td2.innerHTML = "<a style='display:block;color:#343233;text-decoration:none;' href=/bookcase"+bc_id+">&nbsp;&raquo;&nbsp;</a>\n";
			bc_panel_row.appendChild(bc_panel_row_td1);
			bc_panel_row.appendChild(bc_panel_row_td2);
			//фикс для ie
			var bcase_panel = document.getElementById("bcases_"+work_edition_id);
			bcase_panel.getElementsByTagName("tbody")[0].appendChild(bc_panel_row);			
								
			document.getElementById("bcnot").style.display="none";
			document.getElementById("bcnewform").style.display="none";
      }
    }
}

function Hide(element)
{
	
}





