/////////////////////////////////////////
//                                     //
// http://glu.agarty.ru # Агарти М.С.  //
//                                     //
//                                     //
//        "GLUпые Новости"             //
//                                     //
/////////////////////////////////////////


    var IE;
    var browserd = navigator.appName;

    if(browserd == 'Microsoft Internet Explorer'){
        IE = true;
    }
    else{
        IE = false;
    }


// Показ\скрытыие выпадающего меню или элемента
	var oldsubmenu;
	var timeOnMenu;
	function showmenu(obj) {

		if (timeOnMenu) {
			clearTimeout(timeOnMenu);
		}

		if (oldsubmenu) hide(oldsubmenu, true);
		oldsubmenu=obj;
		show(obj, true);
    }

   function hidemenu() {
   		if (oldsubmenu) timeOnMenu=setTimeout("hide(oldsubmenu, true)", 500);

   }

//Подсветка при наведении, клике
	function flushover(id, nam) {		id.className=nam;
		}

	function flushout(id, nam) {
		id.className=nam;
		}

	var oldid;
	function flushing(id) {     //alert(id);
		var theObj = getObject(id);

	  if (oldid)	oldid.background = "none";
      oldid=theObj;
      theObj.background = "#0f0";


	}

//Показ\скрытие по нажатию

	var now;
	function hideimg(body) {
	  if (nowid && nowid == now) change(nowid);
	  if (event.keyCode==27) change(nowid);
	  now = nowid;
	}

	function change_row(ids, row, total) {

	 for (a=1; a <= total; a++) {	 	id=document.getElementById(ids + a);

        //if (older && older != id) hide(older, false, true);

		if (id.style.display != "none") {
			row.style.backgroundColor="#597B97";
			hide(id, false, true);
			nowid=null;
		}
		else {			row.style.backgroundColor="#800";
			show(id, false, true);
			nowid=id;
		}
		//older2=older=id;
     }

    }

    var older;
    var nowid;
    var nowtime;
    var timeOnMen;
    function change(id, timer) {    	 if (document.getElementById(id)) {
	    	var theObj = getObject(id);

	       if (timeOnMen) {
				clearTimeout(timeOnMen);
			}

			if (older && older != theObj) hide(older);

	    	if (theObj.visibility == "visible") { hide(theObj); nowid=null; }
	    	else { show(theObj); nowid=id; }

	    	older2=older=theObj;
	        nowtime = timer * 1000;

	    	if (timer) timeOnMen=setTimeout("hide(older2)", nowtime);
	  	}
    }

    function change_r(id, cat) {	     if (document.getElementById(id)) {
		    	var theObj = getObject(id);

		    	if (theObj.visibility == "visible") {
		    		hide(theObj);
		    		if (cat) document.getElementById(cat).innerHTML='[+]';
		    	}
		    	else {		    		 show(theObj);
		    	     if (cat) document.getElementById(cat).innerHTML='[-]';
		    	}
	     }
    }

    function change_r(id, cat) {
	     if (document.getElementById(id)) {
		    	var theObj = getObject(id);

		    	if (theObj.visibility == "visible") {
		    		hide(theObj);
		    		if (cat) document.getElementById(cat).innerHTML='[+]';
		    	}
		    	else {
		    		 show(theObj);
		    	     if (cat) document.getElementById(cat).innerHTML='[-]';
		    	}
	     }
    }

//Элементы объекта
   function getObject(obj)
   	{
	var theObj;
   		if (document.layers) {
   			if (typeof obj == "string") return document.layers(obj);
   			else return obj;
   			}

   		if (document.all) {
   			if (typeof obj == "string")
   				{
   					if (typeof document.all(obj) != null)  return document.all(obj).style;
   					else return null;
   				}
   			else return obj.style;
   		}

   		if (document.getElementById) {
   			if (typeof obj == "string") return document.getElementById(obj).style;
   			else return obj.style;
      	}
      	return null;
    }

// Функции показа\скрытие элемента по событию
   function show(obj, menu, table_tr) {
	if (menu == true) var theObj = getObject(obj);
	else var theObj = obj;

	  if (!table_tr) {
		if (typeof theObj.visibility != "undefined") theObj.visibility = "visible";
		if (typeof theObj.display != "undefined") theObj.display = "block";
	  }
	  else  obj.style.display = IE ? "block" : "table-row";
   }

  function hide(obj, menu, table_tr) {
  		table_tr = table_tr ? true : false;

		if (menu == true) var theObj = getObject(obj);
		else var theObj = obj;

	  if (!table_tr) {
		if (typeof theObj.visibility != "undefined" && !table_tr) theObj.visibility = "hidden";
		if (typeof theObj.display != "undefined") theObj.display = "none";
	  }
	  else obj.style.display = "none";
    }

//Непомню, гавно какое-то
 function checked(id) {

   document.getElementById(id).checked = true;

 }

//Проверка
 function conf(query) {  if (query == "del_anime") query = "Вы действительно хотите удалить элемент из списка?";
  if (confirm(query)) return true;
  else return false;

 }

//Считает количество символов в поле
function strlen(inp) {    total=inp.value.length;
	document.getElementById("strlen_count").innerHTML=total;
}

 // Добавление тегов BB-code в поле ввода
function bbcode(tag, param, frm){
var forma = frm;

		if (param == null || param == false) {
			doInsert("[" + tag + "]", "[/" + tag + "]", forma);
		}

		else if (param == 2) doInsert("[" + tag + "]", false, forma);

		else doInsert("[" + tag + "=" + param + "]", "[/" + tag + "]", forma);
}

function doInsert(ibTag, ibClsTag, forma)
{
	var obj_ta = forma;

  if (document.getSelection) {    var sel = document.selection;
    var rng = sel.createRange();
  }
  else if (document.selection && document.selection.createRange && obj_ta.caretPos) {

    var sel = document.selection;
    //var rng =  document.post_besit.u_message.createTextRange;
    var rng = obj_ta.caretPos;
  }

  else if (!ibClsTag) {    var rng = obj_ta.caretPos;
  }

  else alert('Вначале выделите текст');

	//var sel = document.selection;
	//var rng = sel.createRange();
	rng.colapse;
	if((sel.type == "Text" || sel.type == "None") && rng != null)
	{
		if(ibClsTag != "" && rng.text.length > 0){
		ibTag += rng.text + ibClsTag;
		obj_ta.focus();
	    }
		else {
		ibTag += rng.text;
		obj_ta.focus();
		}
		rng.text = ibTag;
	}

}

//Добавление кода (смайлик и вся херня)
function add(ad, promt, field) {
	text = ' ' + ad + ' ';
	var obj_ta = document.getElementById(field);

	if (obj_ta.createTextRange && obj_ta.caretPos) {

		 if (promt == true) {	 		query=prompt(ad+" - Введите ссылку");
	 		if (query != '' && typeof query != "undefined" && query != null) {
            	var caretPos = document.getElementById(field).caretPos;
	        	caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? '' : ' ['+ad+']' + query + '[/'+ad+'] ';
				document.getElementById(field).focus();
			}
		 }

		 else{		 	var caretPos = document.getElementById(field).caretPos;
	        caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text + ' ' : text;
			document.getElementById(field).focus();
		 }

	}

	else {
	     if (promt == true) {		 	query=prompt(ad+' - Введите ссылку');
		 	if (query != '' && typeof query != "undefined" && query != null) {
            	document.getElementById(field).value  += " ["+ad+"]" + query + "[/"+ad+"] " ;
				document.getElementById(field).focus();
			}
		 }

		 else{
			document.getElementById(field).value  += text;
			document.getElementById(field).focus();
		}
	}
}

//Карету мне, карету!
function storeCaret(textEl) {
	if (textEl.createTextRange) textEl.caretPos = document.selection.createRange().duplicate();
}

//Фотогалерея

 function img(i, names, attr) { 	var sh = document.getElementById('sh');
    if (sh.checked) return false;

 	var newImg = new Image();
 	var lin;

 	if (names=="adm") lin="adm";
 	else lin="thumb";
 	s= "/img.php?"+lin+"=" + i;
 	var path = document.getElementById(i);
    newImg.src = "/img.php?"+lin+"=" + i;
    myimg=document.getElementById('imgload');
    //alert(myimg.src);
    myimg.src = newImg.src;

    nams=document.getElementById('nam');
    //alert(nam.firstChild.nodeValue);
    nams.firstChild.nodeValue = names;

    atr=document.getElementById('atrib');
    //alert(atr.firstChild.nodeValue);
    atr.firstChild.nodeValue= attr;

    dow=document.getElementById('down');
    dow.href = "/img.php?link=" + i;

 }

function loc(url, val) {   if (val !== "false") location.href="/index.php?mode=" + url + val;}

function user(action, uid, w, h) { if (w) var width = w; else var width = 600;

 if (h) var height = h;
 else var height =500;

 var top= (screen.availHeight/2) - (height/2) - 30;
 var left= (screen.availWidth/2) - (width/2);

 if (action == "edit") {
 	window.open("/share/user_control.php?edit=" + uid, "edit_user", "scrollbars, width="+ width + ", height="+ height + ", top="+ top + ", left=" + left);

 }

 else if (action == "add") {
  window.open("/share/user_control.php?add=" + uid, "add_user", "scrollbars, width="+ width + ", height="+ height + ", top="+ top + ", left=" + left );


 }

 else if (action == "pass") {
 	  window.open("/share/pass_recovery.php", "pass_rec", "scrollbars, width="+ width + ", height="+ height + ", top="+ top + ", left=" + left );

 }
}

function screens(screend, w, h){
 var width = 450;
 var height =350;

 var top= (screen.availHeight/2) - (height/2) - 30;
 var left= (screen.availWidth/2) - (width/2);

 window.open("/share/catalog/"+screend, "screenf", "scrollbars, width="+ width + ", height="+ height + ", top="+ top + ", left=" + left );
}

function win(loc, w, h, wname, mods) { if (w) var width = w;
 else var width = 600;

 if (h) var height = h;
 else var height =500;
 var top= (screen.availHeight/2) - (height/2) - 30;
 var left= (screen.availWidth/2) - (width/2); window.open(loc,"screenf", mods + wname + ", width="+ width + ", height="+ height + ", top="+ top + ", left=" + left );

}

function alerts(query) {	if (confirm(query)) return true;
	else return false;
}

function addsubcat(category) {
	var subcatname=prompt("Введите название подкатегории:", "");

	if (subcatname) {      document.getElementById("subcat").value=subcatname;
      document.getElementById("cat").value=category;
      document.getElementById("subcat_form").submit();
	}

}

function getsubcats(cat) {
	if (cat) {       var cats=sndReq(cat);
	}

}

function openw(loc, w, h, wname, mods) {
 if (w) var width = w;
 else var width = 600;

 if (h) var height = h;
 else var height =500;

 var top= (screen.availHeight/2) - (height/2) - 30;
 var left= (screen.availWidth/2) - (width/2);
 window.open(loc,wname , mods + ", width="+ width + ", height="+ height + ", top="+ top + ", left=" + left );

}

function is_object(obj) {	if (typeof obj == "object") return true;
	else return false;
}

function is_string(str) {	if (typeof obj == "string") return true;
	else return false;
}


function change_img(obj, img) {
	if (is_object(obj)) obj.src=img;
	else document.getElementById(obj).src=img;
}

function check_box(id, erss) {	var chk=document.getElementById(id);
	if (chk.checked) {		chk.checked=false;
	}
	else chk.checked=true;

	if (erss) enables_rss();
}

function enables_rss() {	var blog_chk=document.getElementById("blog").checked;
    var news_chk=document.getElementById("news").checked;
    var events_chk=document.getElementById("events").checked;
    var photo_chk=document.getElementById("photo").checked;
    var disabled_rss=true;

    if (blog_chk || news_chk || events_chk || photo_chk) disabled_rss=false;

    document.getElementById("feed_rss").disabled=disabled_rss;
}

function clear_value(field) {	document.getElementById(field).value="";
}

function del(id, confr, addt) {	if (conf(confr)) {		document.location.href="/mail/?del="+id+addt;
	}
}

