/////////////////////////////////////////
//                                     //
// http://glu.agarty.ru # Агарти М.С.  //
//                                     //
//                                     //
//        "GLUпые Новости"             //
//                                     //
/////////////////////////////////////////

// Показ\скрытыие выпадающего меню или элемента
	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) {
		id.className="select";
		}

	function flushout(id) {
		id.className="normal";
		}

	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;
	}

    var older;
    var nowid;
    function change(id) {
        var theObj = getObject(id);
		if (older && older != theObj) hide(older);

    	if (theObj.visibility == "visible") { hide(theObj); nowid=null; }
    	else { show(theObj); nowid=id; }

    	older=theObj;
    }

    var hide_t=true;
    function change_time(obj) {        var theObj2 = getObject(obj);
        //alert(id);
        if (hide_t==true) {        	hide_t=false;
            theObj2.visibility = "hidden";
        }

        else {        	hide_t=true;
            theObj2.visibility = "visible";
        }
    }
//Элементы объекта
   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) {
	if (menu == true) var theObj = getObject(obj);
	else var theObj = obj;

	if (typeof theObj.visibility != "undefined") theObj.visibility = "visible";
	if (typeof theObj.display != "undefined") theObj.display = "block";
   }

  function hide(obj, menu)
	{
		if (menu == true) var theObj = getObject(obj);
		else var theObj = obj;
		if (typeof theObj.visibility != "undefined") theObj.visibility = "hidden";
		if (typeof theObj.display != "undefined") theObj.display = "none";
    }

//Непомню, гавно какое-то
 function checked(id) {

   document.getElementById(id).checked = true;

 }

//Проверка
 function check(action, query) {  if (confirm('Будет произведено ' + action + ' ' + query + ".\nВы уверены?")) return true;
  else return false;

 }

 // Добавление тегов BB-code в поле ввода
function bbcode(tag, param, frm){
var forma = frm;

		if (param == null || param == false) {
			doInsert("[" + tag + "]", "[/" + tag + "]", 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 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 = "";
		obj_ta.focus();
		alert('Вначале выделите текст');
		}
		rng.text = ibTag;
	}

}

//Добавление кода (смайлик и вся херня)
function add(ad,promt, forma) {
	text = ' ' + ad + ' ';
	var obj_ta = forma;

	if (obj_ta.createTextRange && obj_ta.caretPos) {

		 if (promt = true) {	 		query=prompt(ad+" - Введите ссылку");
	 		if (query != '' && typeof query != "undefined" && query != null) {
            	var caretPos = obj_ta.caretPos;
	        	caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? '' : ' ['+ad+']' + query + '[/'+ad+'] ';
				obj_ta.focus();
			}
		 }

		 else{		 	var caretPos = obj_ta.caretPos;
	        caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text + ' ' : text;
			obj_ta.focus();
		 }

	}

	else {
	     if (promt = true) {		 	query=prompt(ad+' - Введите ссылку');
		 	if (query != '' && typeof query != "undefined" && query != null) {
            	obj_ta.value  += " ["+ad+"]" + query + "[/"+ad+"] " ;
				obj_ta.focus();
			}
		 }

		 else{
			obj_ta.value  += text;
			obj_ta.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 change_menu(id, id2, total) {
	change(id);
	var d;
	var theObj;
    var a;
	for (var i=0; i < total; i++) {       d = "id_" + i;
       a = "id" + i;       theObj = getObject(d);

       if (id == a) hide(d, true);
       else show(d, true);
	}

}

function wopen(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 );

}
