//////////////////////////////////////////////
// NEGAR CMS: Content Management System  //
// ===========================              //
// Copyright (c)2007-2008 by Igor Ognichenko//
// http://www.kasseler-cms.net/             //
//////////////////////////////////////////////
var timeout;

if(typeof HTMLElement!="undefined" && !HTMLElement.prototype.insertAdjacentElement){
    HTMLElement.prototype.insertAdjacentElement = function(where,parsedNode){
        switch (where){
            case 'beforeBegin': this.parentNode.insertBefore(parsedNode,this); break;
            case 'afterBegin': this.insertBefore(parsedNode,this.firstChild); break;
            case 'beforeEnd': this.appendChild(parsedNode); break;
            case 'afterEnd':
	        if (this.nextSibling)
                    this.parentNode.insertBefore(parsedNode,this.nextSibling);
                else this.parentNode.appendChild(parsedNode);
                break;
        }
    }

    HTMLElement.prototype.insertAdjacentHTML = function(where,htmlStr){
        var r = this.ownerDocument.createRange();
        r.setStartBefore(this);
        var parsedHTML = r.createContextualFragment(htmlStr);
        this.insertAdjacentElement(where,parsedHTML)
    }

    HTMLElement.prototype.insertAdjacentText = function(where,txtStr){
        var parsedText = document.createTextNode(txtStr)
        this.insertAdjacentElement(where,parsedText)
    }
};

function allselect(){
    var chekeds = document.getElementsByTagName('input');
    for(i=0; i<chekeds.length; i++){
        if(chekeds[i].type == 'checkbox'){
            chekeds[i].checked = true;
        }
    }
}

function voted(id, langparam){
    var frm = document.vote;
    var varvote = 0;
    var elmnt;
    for (var i=0; i<frm.elements.length; i++) {
        elmnt = frm.elements[i];
        if (elmnt.type=='radio') {
            if(elmnt.checked == true){ varvote = elmnt.value; break;}
        }
    }
    if (varvote!=0){
        KR_AJAX.options.idElm = 'voting';
        KR_AJAX.options.requestFile = document.getElementById('voted-form').action;
        KR_AJAX.set_var('var', varvote);
        KR_AJAX.set_var('ajax', 'true');
        KR_AJAX.processResponse();
    } else alert(langparam);
}                    

function showhide(){
    var Elm = document.getElementById('blockquickreply');
    if (Elm.style.display == 'block') Elm.style.display = 'none';
    else Elm.style.display = 'block';
}

function set_smile(num){
    var textarea = document.getElementById('message');
    textarea.value+=' :'+num+': ';
}

function ckeck_uncheck_all() {
    var chekeds = document.getElementsByTagName('input');
    for(i=0; i<chekeds.length; i++){
        if(chekeds[i].type == 'checkbox' && chekeds[i].id!="checkbox_sel"){
            chekeds[i].checked = (chekeds[i].checked) ? false : true;
        }
    }
}

function ajax(url, idElm){
    if (KR_AJAX.isReady){
        if(idElm) KR_AJAX.options.idElm = idElm;
        KR_AJAX.options.requestFile = url;
        KR_AJAX.processResponse();
    } else alert('AJAX not ready');
}

function ajax2(url, idElm){
    if (KR_AJAX.isReady){
        if(idElm) KR_AJAX.options.idElm = idElm;
        KR_AJAX.options.requestFile = url;
        KR_AJAX.options.animation = false;
        KR_AJAX.processResponse();
    } else alert('AJAX not ready');
}

//minichat function
function load_text_chat(){
    KR_AJAX.options.idElm = 'text_chat';
    KR_AJAX.options.requestFile = 'index.php?blockfile=block-mini_chat.php&block=text_chat';
    KR_AJAX.options.animation = false;
    KR_AJAX.onendload = function(){
        document.getElementById('col_text_chat').style.verticalAlign = 'top';
        document.getElementById('col_text_chat').style.textAlign = 'left';
    }
    KR_AJAX.processResponse();
}

//minichat function
function add_message(){
    if (document.getElementById('name_user').value=='Guest') {alert('"._MSGGUEST."'); return;}
    if (document.getElementById('name_user').value!=''){
        if (document.getElementById('text_user').value!=''){
             KR_AJAX.options.idElm = 'text_chat';
             KR_AJAX.options.requestFile = 'index.php?blockfile=block-mini_chat.php&block=set_text_in_chat';
             KR_AJAX.options.animation = false;
             KR_AJAX.options.add = true;
             KR_AJAX.options.addType = 'DESC';
             KR_AJAX.set_var('name_user', document.getElementById('name_user').value);
             KR_AJAX.set_var('text_user', document.getElementById('text_user').value);
             KR_AJAX.processResponse();
            document.getElementById('text_user').value = '';
        } else alert('The name is not entered!');
    } else alert('The message is not entered!');
}

//minichat function
function delete_message_chat(id, langparam){
    if (confirm(langparam)){
        document.getElementById('text_chat_id'+id).style.display = 'none';
        KR_AJAX.options.requestFile = 'index.php?blockfile=block-mini_chat.php&block=delete_text_chat';
        KR_AJAX.options.animation = false;
        KR_AJAX.set_var('cid', id);
        KR_AJAX.processResponse();
    } else return false;
}

function windowresize(id, resizetype){
	if (resizetype=="+") document.getElementById(id).rows+=5;
	if (resizetype=="-") document.getElementById(id).rows-=5;
}

var agent = navigator.userAgent.toLowerCase();
var browser = {
    safari: agent.indexOf("safari") != -1,
    opera: agent.indexOf("opera") != -1,
    msie: (agent.indexOf("msie") != -1) && (agent.indexOf("opera") == -1),
    mozilla: (agent.indexOf("mozilla") != -1) && ((agent.indexOf("webkit") == -1) && (agent.indexOf("compatible") == -1))
}

function center_screen(w, h, typexy){
    var browserName = navigator.appName
    var ie=document.all && browserName!='Opera';
    var domclientWidth=document.documentElement && parseInt(document.documentElement.clientWidth) || 100000;
    var standardbody=(document.compatMode=="CSS1Compat")? document.documentElement : document.body;
    var docwidth=(ie)? standardbody.clientWidth : (/Safari/i.test(navigator.userAgent))? window.innerWidth : Math.min(domclientWidth, window.innerWidth-16);
    var docheight=(ie)? standardbody.clientHeight: window.innerHeight;
    if (typexy == 'top') return ((docheight/2 - h/2) - 50);
    if (typexy == 'left') return (docwidth/2 - w/2);
};

function size_bb(type_resize, n){
    if (type_resize=="plus") document.getElementById(n).style.height= (parseInt(document.getElementById(n).style.height)+75)+'px';
    if (type_resize=="minus" && bbEditorHeight<parseInt(document.getElementById(n).style.height)) document.getElementById(n).style.height=(parseInt(document.getElementById(n).style.height)-75)+'px';
    hidden_selectbb(n);
};

function set_rating(id, module, star, user, rating, voted){
    KR_AJAX.options.requestFile = 'index.php?ajaxed=rating';
    KR_AJAX.set_var('modul', module);
    KR_AJAX.set_var('star', star);
    KR_AJAX.set_var('id', id);
    KR_AJAX.set_var('rating', rating);
    KR_AJAX.set_var('voted', voted);
    KR_AJAX.processResponse();
    var ratingtmp = ((rating*voted)+star)/(voted+1);
    rating = (ratingtmp<1) ? star : ratingtmp;
    var stars = '<ul class=\"unit-rating\"><li class="current-rating" style="width:'+(rating*17).toFixed(2)+'px;">'+(rating*17).toFixed(2)+'</li></ul>';
    document.getElementById('rating'+id).innerHTML = stars;    
}

function UpdateTable(result, url, langparam){
    if (langparam!=""){
        if (confirm(langparam)){
            ajax(url, result);
        }
    } else ajax(url, result);
}

function DeleteRow(obj, url, langparam, level){
    if (confirm(langparam)){
        var par;
        for (var i=1; i<=level; i++) obj=obj.parentNode;
        par=obj.parentNode.removeChild(obj);
        ajax(url, '');
    } else return false;
}

function DeleteTable(id, url, langparam){
    if (confirm(langparam)){
       var obj=document.getElementById("table"+id);
       obj.style.display = 'none';
       ajax(url, '');
    }
}

function EditPost(id, element, url, EditType, SetText){
    KR_AJAX.options.idElm = element;
    KR_AJAX.options.requestFile = url;
    KR_AJAX.set_var('nid', id);
    KR_AJAX.set_var('type', EditType);
    KR_AJAX.set_var('text', SetText);
    KR_AJAX.processResponse();
}

function onoff(id, url){
    ajax(url, 'onoff'+id);
}


function he(){
    vc = document.getElementsByTagName((document.compatMode && document.compatMode=="CSS1Compat") ? "HTML":"BODY")[0];
    document.getElementById('dd').value=vc.scrollHeight;
}

function scrollTo(_var){
     if (_var=='head') scroll(0,0);
}   

userinfo = Array();  
DivObjsUsers = Array();


function user_info(UserID, Obj, Width, Height){         
    var animLoaded = '<table width="100%" height="100%"><tr><td align="center" valign="center"><img src="includes/images/mini_loading.gif" border="0" alt="Loading..."></td></tr></table>';
    if (!DivObjsUsers[UserID]){
        DivObjsUsers[UserID] = document.createElement("DIV");
        DivObjsUsers[UserID].setAttribute('id','UserInfoDiv'+UserID);
        document.body.appendChild(DivObjsUsers[UserID]);
        DivObjsUsers[UserID].className = 'userinfo';
        DivObjsUsers[UserID].innerHTML = animLoaded;
    } else DivObjsUsers[UserID].innerHTML = userinfo[UserID];
    
    Obj.onmouseout = function(){DivObjsUsers[UserID].style.display = 'none'; document.onmousemove = function(){};};                           
    document.onmousemove = function(e){
        var vc = document.getElementsByTagName((document.compatMode && document.compatMode=="CSS1Compat") ? "HTML":"BODY")[0];
        var x = window.event ? event.clientX + vc.scrollLeft : e.pageX;
        var y = window.event ? event.clientY + vc.scrollTop : e.pageY;
        var vcwidth = vc.clientWidth ? vc.clientWidth + vc.scrollLeft : window.innerWidth + window.pageXOffset;
        var vcheight = vc.innerHeight ? window.innerHeight + window.pageYOffset : vc.clientHeight + vc.scrollTop;
        if((x + DivObjsUsers[UserID].offsetWidth+20) > vcwidth) DivObjsUsers[UserID].style.left = (x - DivObjsUsers[UserID].offsetWidth - 4)+'px'; else DivObjsUsers[UserID].style.left = (x + 15)+'px';
        if((y + DivObjsUsers[UserID].offsetHeight) > vcheight) DivObjsUsers[UserID].style.top = (y - DivObjsUsers[UserID].offsetHeight)+'px'; else DivObjsUsers[UserID].style.top = y+'px';
    }   
    DivObjsUsers[UserID].style.display = 'block';
    if (window.openhinst && window.closehints) DivObjsUsers[UserID].innerHTML = openhinst+animLoaded+closehints;
    else DivObjsUsers[UserID].innerHTML = animLoaded;
    DivObjsUsers[UserID].style.width = Width+'px';
    DivObjsUsers[UserID].style.height = Height+'px';
    if (!userinfo[UserID] || userinfo[UserID]=='undefined'){  
        KR_AJAX.options.idElm = DivObjsUsers[UserID];
        KR_AJAX.options.requestFile = 'index.php?ajaxed=usersinfo';
        KR_AJAX.options.animation = false;
        KR_AJAX.set_var('UserID', UserID);
        KR_AJAX.onendload = function(){
            if (openhinst && closehints) DivObjsUsers[UserID].innerHTML = openhinst+DivObjsUsers[UserID].innerHTML+closehints;
            userinfo[UserID] = DivObjsUsers[UserID].innerHTML;
            DivObjsUsers[UserID].style.width = '160px';
            DivObjsUsers[UserID].style.height = 'auto';
        }
        KR_AJAX.processResponse();
    } if (userinfo[UserID]){ 
        DivObjsUsers[UserID].innerHTML = userinfo[UserID];
        DivObjsUsers[UserID].style.width = '160px';
        DivObjsUsers[UserID].style.height = 'auto';
    }    
}