
function getel(el){
  return document.getElementById(el);
}

function text_null()
{
  var text_value=5;
  if (getel('post_title').value.length>text_value){
    getel('post_err').value="Требуется название";
    getel('post_title').style.borderColor="green";
  }
  else
    getel('post_title').style.borderColor="red";
}

function add_cat(h,cat_id){
	var listBox = $("#tab_v");
  var len = $("#tab_v > *").length;
	for(var i=0; i<len; i++) {
  	if ($("#tab_v > *:eq("+ i +")").text() == h) {
    	alert('"'+h+'" уже в списке');
     	return;
   	}
  }
  var inp = $("<input type='hidden' name='category[]'>").attr({
		id : "input_category_" + cat_id,
		value : cat_id
	});

  var scat = $("<div class='sel_cat' title= 'Убрать из списка отмеченных'>").text(h).click(function(){
		$(this).remove();
		$("#input_category_"+cat_id).remove();
	});
  listBox.append(scat);
  listBox.append(inp);
} 

function comment_form(pid){
  var comment_form = $("#comment_form");
  if (pid!=0){
    $("#comment_"+pid).append(comment_form);
    $("#comment_pid").attr("value" , pid);
  }
  else{
    $("#comment_main").append(comment_form);
		$("#comment_pid").attr("value" , pid);
	}
		
  comment_form.css("display" , "block");
  return false;
}

function type_select(value){/*
  var ident;
  //$("#type_foto" , "#type_video").css("display" , "none"); don`t work!
 // getel('type_video').style.display = 'none';
  if (value==3){
		
    //collection = getel("form_add").elements;
    var collection = $("#form_add > *");
		alert(collection);
    for (a in collection){
      if (collection[a].name == 'foto[]' || collection[a].name == 'foto_title[]') collection[a].disabled=false;
    }
    getel('type_foto').style.display = 'block';
  }
  else{
    collection = getel('form_add').elements;
    for (a in collection){
      if (collection[a].name == 'foto[]' || collection[a].name == 'foto_title[]') collection[a].disabled=true;
    }
    if (value==2) 
      $('#type_video').css("display", "block");
    
  }*/
  return false;
}

function foto_form_create(){
  var el = document.createElement('input');
  el.name='foto[]';
  el.type='file';
  el.enctype='multipart/form-data';
  if (el.addEventListener) el.addEventListener('change', foto_form_create, false);
  else el.attachEvent('onclick', foto_form_create);
  var el2 = document.createElement('span');
  el2.innerHTML = '&nbsp;подпись к фото - ';
  var el3 = document.createElement('input');
  el3.name='foto_title[]';
  el3.type='text';
  el3.className='fotoz';
  var el4 = document.createElement('<br>');
  getel('type_foto').appendChild(el);
  getel('type_foto').appendChild(el2);
  getel('type_foto').appendChild(el3);
  getel('type_foto').appendChild(el4);
}



function Tree(objName) {
	this.obj = objName;
	this.aNodes = [];
	this.treeView = [];
};

Tree.prototype.add = function(id, pid, text,datetime,user,reply,avatar,foto_in_comment,foto_in_comment_s) {
	this.aNodes[id] = new Node(id, pid, text,datetime,user,reply,avatar,foto_in_comment,foto_in_comment_s);
};

function Node(id, pid, text, datetime, user, reply,avatar,foto_in_comment,foto_in_comment_s) {
	this.id = id;
	this.pid = pid;
	this.text = text;
	this.datetime = datetime;
	this.user = user;
	this.reply = reply;
	this.indent = false;
	this.avatar = avatar;
	this.foto_in_comment = foto_in_comment;
	this.foto_in_comment_s = foto_in_comment_s;
};

Tree.prototype.sort = function(){
	for(element in this.aNodes){
		if (this.aNodes[element].pid==0){
			this.aNodes[element].indent = 0;
			this.treeView.push(this.aNodes[element].id);
			this.ChildGet(this.aNodes[element].id, this.aNodes[element].indent+1);
		}
	}
}

Tree.prototype.ChildGet = function(id, indent){
	for (element in this.aNodes) {
		if (this.aNodes[element].pid==id){
			this.aNodes[element].indent = indent;
			this.treeView.push(this.aNodes[element].id);
			this.ChildGet(this.aNodes[element].id, this.aNodes[element].indent+1);
		}
	}
}

Tree.prototype.draw = function(){
	var output = '';
	
	for (element in this.treeView) {
		var id = this.treeView[element];
		output+= "<div class='in_comment' id='comment_"+id+"'";
		if (this.aNodes[id].indent!=0) output+="style='margin-left: "+(this.aNodes[id].indent*15)+"px;'";
		output+=">"+ this.aNodes[id].text;
		if (this.aNodes[id].foto_in_comment != '' && this.aNodes[id].foto_in_comment != undefined){
      output+="<div class='foto_in_comment'><a rel='prettyPhoto'' href="+this.aNodes[id].foto_in_comment+"><img src=" + this.aNodes[id].foto_in_comment_s + " /></a></div>";  
    }
		
		output+="<div class='service_line_full'>";
		output+="<div class='datetime'>"+ this.aNodes[id].datetime +"</div>";
		if (this.aNodes[id].avatar == '' || this.aNodes[id].avatar == undefined){
      output+="<div class='user'>"+ this.aNodes[id].user +"</div>";
    }
    else{
      output+="<div class='user2'><img src="+this.aNodes[id].avatar+" height=40 width=40 alt='' />"+ this.aNodes[id].user +"</div>";
    }
		
		if (this.aNodes[id].reply){
			output+="<div class='comment'><a href='#' onClick='comment_form("+this.aNodes[id].id+"); return false;'>ответить (+)</a></div>";			
		}
		output+="</div>";
        output+="</div>";
	}
	//alert(output);
	getel('comment_all').innerHTML = output;
}
function _dotest(value) {
    var req = new JsHttpRequest();
    req.onreadystatechange = function() {
        if (req.readyState == 4) {
            alert(req.responseJS.q);
            //document.getElementById('debug').innerHTML = req.responseText;
        }
    }
    req.open("GET", '/ajax_file.php', true);
    req.send( { q: value } );
}

var el_block;
function show_litle_user(user_id , el_id){
	$.ajax({
		url:'http://360.kg/user_viewer.php',
		success:getUserLitle,
		error:function(){alert('error')},
		type:'GET',
		data: 'uid=' + user_id
	});
	el_block = el_id;
}

function getElementPosition(elemId){
    var elem = document.getElementById(elemId);
    var w = elem.offsetWidth;
    var h = elem.offsetHeight;
    var l = 0;
    var t = 0;
	
    while (elem){
        l += elem.offsetLeft;
        t += elem.offsetTop;
        elem = elem.offsetParent;
    }
    return {"left":l, "top":t, "width": w, "height":h};
}


function getUserLitle(data){
	$("div.user_litle_info").remove();
	if (el_block==undefined)
	 var pos = getElementPosition($('user', data).attr('id'));
	else
  	var pos = getElementPosition(el_block);
	
	$('.copy').append($('<div class="user_litle_info"></div>').html("<div id='block_avatar' style='width:100px; height:100px; text-align:center; float:left;'><img alt='' src=" + $('avatar', data).text() + " ></div><div style='margin-left:105px;'>Ник : <span style='color:#F3E35B;'>" + $('nick', data).text() + "</span><br />Количество записей : " + $('posts', data).text() +"<br />Количество комментариев : " +$('comments', data).text()+"<br /><a href="+$('full_url', data).text()+" >Профиль</a></div>"));
	
	if($('avatar', data).text() == '')
		$("#block_avatar").attr("class","no_avatar");

	
	$('.user_litle_info').css({
	  display: 'none',
  	position: 'absolute',
		backgroundColor:'#A6A6A6',
		border:'2px solid #fff',
		padding: '5px',
		top:pos.top + 10,
		left:pos.left + 20
  }).show("slow").cornerz();	
}

function markFotoForDel(fotoid){
	$("div#"+fotoid).hide(2000);
	$("#fotodel").append("<input type='checkbox' name=fotodel[] value="+fotoid+">").css("display", "none");
}

function viewAvatar(file){
	//создаем большую картинку по позиции
}

function make_edit_post(el_id,action_type){
	$.ajax({
		url:'/edit_post.php',
		success:function(){},
		error:function(){alert('error')},
		type:'GET',
		//data: 'el_id=' + el_id, 'action_type=' + action_type
		data: {el_id: el_id, action_type: action_type}
	});
}
function showBlock(el){
  $("#" + el).show("normal");
}
