var comments_cache = new Array();
var display_type;

if(navigator.appName == "Microsoft Internet Explorer") display_type = "block";
else display_type = "table-cell";	

function checkLogin(){
    getUrlAsync("/ajax/check_login.php", execOnSuccess(updateLoginData));
}

function updateLoginData(X){
    var user_xml = X.responseXML.getElementsByTagName("user");
	user_id = user_xml.getAttribute("id");
	if(user_id != "0")
        user_nick = user_xml.firstChild.nodeValue;
	else user_nick = "";
}

function loadCached(comment_id){
    var popup_inner_block = document.getElementById("popup_content");
    try{
        popup_inner_block.innerHTML = "<div style=\"margin: 10px;\"><div><strong>" + comments_cache[comment_id][0] + "&nbsp;&nbsp;&nbsp;" + comments_cache[comment_id][1] + "</strong></div><div style=\"font-family: Verdana; font-size: 11px;\">" + comments_cache[comment_id][2] + "</div><div>";
	}catch(e){
	}
}

function commentPopUp(elem, block_type, comm_id){
    var win_width = 300;
    var win_height = 150;
	var content = document.createElement("div");
	content.setAttribute("id", "popup_content");
	elem_coords = findPos(elem);
    animatePopUp(elem_coords[0], elem_coords[1], win_width, win_height, content, block_type);
	//alert(comments_cache[comm_id]);
	if(comments_cache[comm_id] != undefined){
	    loadCached(comm_id);
	    return false;
	}
	getUrlAsync("/ajax/get_asks_comments.php?cmnt_id=" + comm_id, execOnSuccess(fillCommentPopUp));
}

function fillCommentPopUp(X){
    var popup_inner_block = document.getElementById("popup_content");
    var xml_cmnt_node = X.responseXML.getElementsByTagName("cmnt")[0];

    try{
    	comments_cache[xml_cmnt_node.getAttribute("id")] = [xml_cmnt_node.childNodes[0].firstChild.nodeValue, xml_cmnt_node.getAttribute("date"), xml_cmnt_node.childNodes[1].firstChild.nodeValue];
        popup_inner_block.innerHTML = "<div style=\"margin: 10px;\"><div><strong>" + xml_cmnt_node.childNodes[0].firstChild.nodeValue + "&nbsp;&nbsp;&nbsp;" + xml_cmnt_node.getAttribute("date") + "</strong></div><div style=\"font-family: Verdana; font-size: 11px;\">" + xml_cmnt_node.childNodes[1].firstChild.nodeValue + "</div><div>";
    }catch(e){
    }
   
}

function commentFormPopUp(elem, block_type, ask_id){
    var win_width = 450;
    var win_height = 220;
	var content_elem = document.createElement("div");
	var elem_coords = findPos(elem);
	
	if(user_id == 0){
	    content_elem.innerHTML = "<iframe src=\"/mini_login.php?feedback=" + window.location + "\" height=\"100%\" width=\"100%\" frameborder=\"0\" style=\"margin-top: 10px;\"></iframe>";
	}else{
		content_elem.innerHTML = "<iframe src=\"/conf_comment.php?ask_id=" + ask_id + "\" frameborder=\"0\" width=\"100%\" height=\"100%\" style=\"margin-top: 10px;\"></iframe>";
	}
	animatePopUp(elem_coords[0], elem_coords[1], win_width, win_height, content_elem, block_type);
}

function loadThread(ask_id){
    var thread_row = document.getElementById("thread_" + ask_id);
    if(thread_row){
	    var thread_block = thread_row.getElementsByTagName("div")[0];
		
		if(thread_block.parentNode.style.display == display_type || thread_block.parentNode.style.display == "")
	    thread_block.parentNode.style.display = "none";
		else{
		    thread_block.parentNode.style.height = 1;
		    thread_block.parentNode.style.display = display_type;
		}

    	return false;
	}
    getUrlAsync("/ajax/get_asks_comments.php?conf_id=" + conf_id + "&ask_id=" + ask_id, execOnSuccess(insertThread));
}

function insertThread(X){
	var ask_node = X.responseXML.getElementsByTagName("ask")[0];
	var ask_id = ask_node.getAttribute("id");
	var xml_comments = X.responseXML.getElementsByTagName("cmnt");
	var next_row = document.getElementById("row_" + ask_id).nextSibling;
	if(next_row == null){
	    next_row = document.createElement("tr");
		next_row_cell = document.createElement("td");
		next_row_cell.setAttribute("colspan", "3");
		next_row.appendChild(next_row_cell, true);
		document.getElementById("asks_tab").appendChild(next_row, true);
	}

	var new_row = document.createElement("tr");
	var new_cell = document.createElement("td");
	var comments_block = document.createElement("div");

	new_cell.style.display = display_type;
	comments_block.style.marginTop = "10px";
	comments_block.style.marginBottom = "10px";
	comments_block.style.marginLeft = "80px";
	comments_block.style.borderLeft = "1px solid silver";
	comments_block.style.borderBottom = "1px solid silver";

	for(var i=0; i<xml_comments.length; i++){
	    var comment_text = "";
		var pos = limit = 1000;
		var chunks_cnt = Math.floor(xml_comments[i].childNodes[1].firstChild.nodeValue.length / limit); //alert(chunks_cnt);
		if(chunks_cnt > 0){
			for(k = 0; k<chunks_cnt; k++){
			    if(xml_comments[i].childNodes[1].firstChild.nodeValue.substring(pos, 0) != "\n") 
				comment_text += xml_comments[i].childNodes[1].firstChild.nodeValue.substring(pos, 0);
			}
		} else comment_text = xml_comments[i].childNodes[1].firstChild.nodeValue;
	    comments_block.innerHTML += "<div class=\"cm_item\"><strong>" + xml_comments[i].childNodes[0].firstChild.nodeValue + "&nbsp;&nbsp;&nbsp;" + xml_comments[i].getAttribute("date") + "</strong><pre>" + comment_text + "</pre></div>";
	}

	new_cell.setAttribute("id", "thread_" + ask_id);
	new_cell.setAttribute("colSpan", "3");
	new_cell.appendChild(comments_block, true);
	
	new_row.appendChild(new_cell);
	next_row.parentNode.insertBefore(new_row, next_row);
}

function applyRowsHoverProp(){
    var rows_arr = document.getElementById("asks_tab").getElementsByTagName("tr");
	for(var i=0; i<rows_arr.length; i++){
	    var row_id = rows_arr[i].getAttribute("id");
		var is_thread = row_id.match(/thread_[0-9]+/);
		if(is_thread != null) continue;
	    rows_arr[i].prevColor = rows_arr[i].style.backgroundColor;
	    rows_arr[i].onmouseover = rowHover;
	    rows_arr[i].onmouseout = rowHoverOut;
		
	}
}

function rowHover(){
    this.style.backgroundColor = "#FDF5E6";
}

function rowHoverOut(){
    this.style.backgroundColor = this.prevColor;
}

function loadCommentsChunks(){

    getUrlAsync("/ajax/get_asks_comments.php?conf_id=" + conf_id + "&chunk=1", execOnSuccess(fillCommentsBlocks));
}

function fillCommentsBlocks(X){
    var asks_comments_arr = X.responseXML.getElementsByTagName("ask");
	for(var i = 0; i<asks_comments_arr.length; i++){
	    var ask_id = asks_comments_arr[i].getAttribute("id")
	    var comment_block = document.getElementById("ask_" + ask_id);
		comment_block.style.width = comment_block.parentNode.parentNode.offsetWidth - 10;
		comment_block.style.height = comment_block.parentNode.parentNode.offsetHeight - 20;
		var cmnts_cnt = Math.floor((parseInt(comment_block.style.height) / 13) / 2.5);
		var comments_arr = Array(cmnts_cnt);
		var comments = asks_comments_arr[i].getElementsByTagName("cmnt");
		if(comments.length>0){
		    for(var j=0; j<comments_arr.length; j++){
                if(comments[j])
				    comment_block.innerHTML += "<div style=\"margin-bottom: 6px;\" onmouseover=\"commentPopUp(this, 'perm', " + comments[j].getAttribute("id") + ")\"><span class=\"cm_chunk\">" + comments[j].childNodes[1].firstChild.nodeValue + "</span></div>";
		    }
		}
	}
}

// Ñþäà âñå ô-öèè íà îíëîàä ñòðàíèöû
function bodyOnload(){
    applyRowsHoverProp();
	loadCommentsChunks();
}


