/* diamondCMS.scripts (c) Ivan Denisov 2010, d.ivan.krsk@gmail.com */

var loader = new Image(); loader.src = "http://"+window.location.hostname+"/face/loader.gif"
var nowimg = new Image();
function $(id) { return document.getElementById(id); }

function addHandler(object, event, handler, useCapture) {
    if (object.addEventListener)
        object.addEventListener(event, handler, useCapture);
    else if (object.attachEvent)
        object.attachEvent('on' + event, handler);
    else object['on' + event] = handler;
}

function removeHandler(object, event, handler) {
    if (object.removeEventListener)
        object.removeEventListener(event, handler, false);
    else if (object.detachEvent)
        object.detachEvent('on' + event, handler);
}

function defPosition(event) {
            var x = y = 0;
            if (document.attachEvent != null) { // Internet Explorer & Opera
            x = window.event.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
            y = window.event.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
      } else if (!document.attachEvent && document.addEventListener) { // Gecko
            x = event.clientX + window.scrollX;
            y = event.clientY + window.scrollY;
      } else {
            // Do nothing
      }
      return {x:x, y:y};
}

function absPosition(obj) {
      var x = y = 0;
      while(obj) {
            x += obj.offsetLeft;
            y += obj.offsetTop;
            obj = obj.offsetParent;
      }
      return {x:x, y:y};
} 

// IMAGE SYSTEM
function showBig(path){
    winW = window.innerWidth;
    winH = window.innerHeight;
    if (navigator.appName.indexOf("Microsoft")!=-1) {
        winW = document.body.offsetWidth;
        winH = document.body.offsetHeight;
    }
    var newdiv = document.createElement("div");
    newdiv.innerHTML = "<div id='over'></div>";
    var myimg = new Image();
    myimg.src = "http://"+window.location.hostname+"/img.php?img="+path+"&w="+(winW-100)+"&h="+(winH-50);
    $('shaded').appendChild(newdiv);
    $('overall').innerHTML = "<div id='imgbox' style='background: #DDF; padding: 7px'></div>";
    $('imgbox').appendChild(loader);
    $('imgbox').style.marginLeft = "-10px";
    $('imgbox').style.marginTop  = Math.ceil(winH/2)+"px";
    if (myimg.complete){
        nowimg = myimg; showIt();
    } else {
        myimg.onload = function(){ nowimg = myimg; setTimeout("showIt()", 50); };
    }
}

function showIt(){
    $('imgbox').innerHTML = '';
    $('imgbox').appendChild(nowimg);
    $('imgbox').style.marginLeft = "-"+Math.ceil(nowimg.width/2+7)+"px"; 
    $('imgbox').style.marginTop  = Math.ceil((winH - nowimg.height)/2-7)+"px";
    // document.body.className = 'sel';
    addHandler(nowimg, "click", cla);
}  

function cla(){
    $('shaded').innerHTML = ' ';
    $('overall').innerHTML = ' ';
    document.body.style.opacity = '0.9';
    // document.body.className = '';
    setTimeout("document.body.style.opacity = '1';", 100);
    removeHandler(document, "click", cla);
}

function eggPro(egg){
    var obj = $(egg);
    var head = $('h'+egg);
    if (obj.style.display == 'none'){
        obj.style.display = 'block';
        head.style.background = '#ef7d00';
    } else {
        obj.style.display = 'none';
        head.style.background = '#AAAAAA';
    }
}

/* Плавное изменение размера формы */
function adjustHeight(id){
	var dif = 5;
	var textarea = $(id);
	var start = textarea.clientHeight;
	var end = textarea.scrollHeight;
	if(start < end){
		if(end - start > 10) dif = (end - start)/2 ;
		if (isNaN(parseInt(textarea.style.height))){
			start = start + dif;
			textarea.style.height = start + "px";
		} else {
			start = start + dif;
			textarea.style.height = parseInt(textarea.style.height) + dif + "px";
		}
	}
}
function refresh_form(textarea){
	adjustHeight(textarea);
	setTimeout("refresh_form('" + textarea + "')",50);
}

/* Вопрос перед переходом по ссылке */
function ask(url, message) {
	if(confirm(message)){
		document.location.replace(url);
	}
}

/* AJAX */

var xmlHttp = false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
try {
  xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
  try {
    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  } catch (e2) {
    xmlHttp = false;
  }
}
@end @*/

if (!xmlHttp && typeof XMLHttpRequest != "undefined") {
  xmlHttp = new XMLHttpRequest();
}

function askByAjaxCare(url,egg,message) {
	if(confirm(message)) askByAjax(url, egg);
}

function askByAjax(url, egg) {
    var workurl = "http://"+window.location.hostname+"/?" + url + "&rnd=" + Math.round(Math.random()*100000);
	xmlHttp.open('GET', workurl, true);
	xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlHttp.onreadystatechange = function () { insertSmw(egg); }
	$(egg).innerHTML = '';
	$(egg).appendChild(loader);
	xmlHttp.send(null);
}
function insertSmw(egg) {
	if (xmlHttp.readyState == 4) { var response = xmlHttp.responseText; $(egg).innerHTML = response; }
}

/* SEARCH */
function search(){
	var query = document.getElementById('sform').value;
	askByAjax('ajax=1&c=Search&f=find&q='+query,'pubZone');
	return false;
}

/* MENU */
var menu_shunt = new Array ();

function menu_tools(id, name){
	menu_shunt[id] = 0;
	if($('t' + id).innerHTML == '[]') {
		a = "'" + id + "', '" + name + "', 'me" + id + "'";
		$('t' + id).innerHTML = "<span onMouseOut='menu_tools_hide(" + id + ")'>[<a href='javascript:void(0)' onClick=\"askByAjax('ajax=1&c=Menu&f=movetree&page=" + page + "&id=" + id + "','pubZone')\">T</a><a href='javascript:void(0)' onClick=\"menu_edit(" + a + ")\">E</a><a href='?c=Menu&f=sort&where=up&id=" + id + "&page=" + page + "'>\/\\\</a><a href='?c=Menu&f=sort&where=down&id=" + id + "&page=" + page + "' class='menu_ed_tools'>\\\/</a><a href='/?c=Menu&f=del&id=" + id + "&page=" + page + "'>D</a>]</span>";
	}
}
function menu_edit(id, conttoedit, edid){
	$(edid).innerHTML = "<form action='?c=menu&f=upd&id=" + id + "&page=" + page + "' method='post' name='editone' style='display: inline;'><input name='name' type='text' value='" + conttoedit + "' id='editfield"+edid+"' style='width: 100px; font-size: 8pt;'><input style='width: 25px; font-size: 8pt;' name='add' type='submit' value='ok'></form>";
	$("editfield"+edid).focus();
}
function menu_tools_hide(id){
	if(menu_shunt[id] == 0)
		if($('t' + id).innerHTML != '[]') {
			menu_shunt[id] = 1;
			setTimeout("document.getElementById('t" + id +"').innerHTML = '[]';", 4000);
		}
}

var aopen = 0;
function swicha() {
	if (aopen == 0){
		$("arrows").innerHTML = "<<<"; aopen = 1;
		$("authbox").style.display = 'block';
		$("login").focus();
	} else {
		$("arrows").innerHTML = ">>>"; aopen = 0;
		$("authbox").style.display = 'none';
	}
}

