var jsFlexy =
{
    setActive: function( link )
    {
        var body = document.getElementsByTagName("body")[0]; 
        var tagA = body.getElementsByTagName("a"); 
        for (var i = 0; i < tagA.length; i++) { 
            clss = tagA[i].getAttribute("class");
            if ( clss == "active" ) {
                tagA[i].removeAttribute("class");
            }
            href = tagA[i].getAttribute("href");
            if ( href == link) {
                tagA[i].setAttribute("class","active");
            }
        }
    },
    
    findParentEl: function(srcEl, destTag)
    {
    	while (srcEl!=null) {
    		if (srcEl.tagName == destTag) return srcEl;
    		srcEl = srcEl.parentNode;
    	}
    	return null;
    },

	nextRow: function(el,url,firstTD)
	{
		if (!el.tagName) {
			return false;
		}
    	if (rowID = jsFlexy.insertRow(el,url,firstTD)) {
    	    document.getElementById('td_add_'+rowID).setAttribute('innerLink',url);
    	    jsFlexy.divShow( url, 1, 'td_add_'+rowID );
    	}
		return true;
    },
    
	refreshRow: function(el)
	{
    	while (el!=null) {
    		if (el.tagName=='TD' && el.id.substring(0,7)=='td_add_') {
        	    jsFlexy.divShow( el.getAttribute('innerLink'), 0, el.id );
    		    //return el;
    		}
    		el = el.parentNode;
    	}
    },
    
    insertRow: function(el, url, firstTD)
    {
    	var tbl = jsFlexy.findParentEl(el,'TABLE');
    	if (tbl==null) return false;
    	
    	var tbl_tr = jsFlexy.findParentEl(el,'TR');
    	var insRow = null;
    	var id = '';
    
    	for (var i=0; i<tbl.tBodies[0].rows.length; i++) {
    		id = tbl.tBodies[0].rows[i].getAttribute('id');
    		if (i==insRow && id && id.substring(0,7)=='tr_add_') {
    		    if (tbl.tBodies[0].rows[i].getAttribute('innerLink')==url) {
        			tbl.deleteRow(i);
        			return false;
    		    } else {
    		        tbl.tBodies[0].rows[i].setAttribute('innerLink',url);
    		        return id.substring(7);
    		    }
    		}
    		if (tbl.tBodies[0].rows[i]==tbl_tr) { insRow = i+1; }
    	}
    	
    	if (!insRow) { return false; }
    
    	rowID = Math.random();
    	while (document.getElementById('tr_add_'+rowID)) {
        	rowID = Math.random();
    	}
    	
    	var row = tbl.tBodies[0].insertRow(insRow);
    	row.className = tbl.tBodies[0].rows[insRow-1].getAttribute('class');
    	row.setAttribute('id', 'tr_add_'+rowID );
    	row.setAttribute('innerLink', url );
    
    	var addColSpan = tbl.tBodies[0].rows[0].cells.length;
    	
    	if (firstTD) {
        	var cell0 = row.insertCell(0);
        	var textNode = document.createTextNode(firstTD);
        	cell0.appendChild(textNode);
        	addColSpan = addColSpan - 1;
        	var cell1 = row.insertCell(1);
    	} else {
        	var cell1 = row.insertCell(0);
    	}
    	
    	cell1.setAttribute('id', 'td_add_'+rowID );
    	cell1.colSpan = addColSpan;
    	
    	document.getElementById('td_add_'+rowID).innerHTML = phpVars.msgLoading + "loading...";
    	return rowID;
    },
    
    deleteRowThis: function(el)
    {
    	var tr = jsFlexy.findParentEl(el,'TR');
    	var tbl = jsFlexy.findParentEl(el,'TABLE');
    	tbl.deleteRow(tr.rowIndex);
    },
    
    disp: function (id, el_a)
    {
    	var div = document.getElementById(id);
    	if (div.style.display=='none') {
    		div.style.display = 'block';
    		if (el_a) {
    		    if (! el_a.getAttribute('title_on')) {
    		        el_a.setAttribute('title_on', el_a.innerHTML);
    		    }
    		    if (! el_a.getAttribute('title_off')) {
    		        el_a.setAttribute('title_off', 'Закрыть');
    		    }
    		    el_a.innerHTML = el_a.getAttribute('title_off');
    		}
    	} else {
    		div.style.display = 'none';
    		if (el_a) {
    		    if (! el_a.getAttribute('title_on')) {
    		        el_a.setAttribute('title_on', 'Отобразить' );
    		    }
    		    if (! el_a.getAttribute('title_off')) {
    		        el_a.setAttribute('title_off', el_a.innerHTML );
    		    }
    		    el_a.innerHTML = el_a.getAttribute('title_on');
    		    document.getElementById(div.getAttribute('title_id')).innerHTML = div.getAttribute('title_on');
    		}
    	}
    },
    
    divShow: function ( url, notClose, id_div ) {
        id_div = this.findLinkId( url, id_div );
		if (id_div == 'mainBody') {
			location.hash = url.substring(1);
			currentLocation = location.hash;
		}
        if (! url.split('/').inArray('target')) {
            url = url + 'target/' + id_div + '/';
        }
        if (id_div!=='mainBody' && (!notClose)
                && document.getElementById(id_div).getAttribute('ajax') == url) {
        	document.getElementById(id_div).style.display = 'none';
    		document.getElementById(id_div).setAttribute('ajax','');
    		document.getElementById(id_div).innerHTML = '';
        } else {
            if (id_div == 'mainBody') {
                ShowWaitWindow();
            }
        	document.getElementById(id_div).innerHTML = phpVars.msgLoading;
        	document.getElementById(id_div).setAttribute('ajax',url);
        	if (document.getElementById(id_div).style.display == 'none') {
              	document.getElementById(id_div).style.display = 'block';
        	}
        	HTML_AJAX.replace( id_div, '/inner' + url );
        	if (id_div == 'mainBody') {
        	    el = document.getElementById('scrollIntoView');
        	    if (el) {
                    el.scrollIntoView(true);
        	    }
        	}
        }
		return true;
    },
    
    findLinkId: function ( url, id_div ) {
        if (!id_div) {
            var a = url.split('/');
        	for (i = 0; i < a.length; i++) {
        	    if (id_div=='*') id_div=a[i];
        	    if (a[i]=='target') id_div='*';
        	}
        }
        if (!id_div) {
            id_div = 'mainBody';
        }
        return id_div;
    },
    
    divClose: function (id_div) {
    	document.getElementById(id_div).innerHTML = '';
      	document.getElementById(id_div).style.display = 'none';
    },
    
    Get_Cookie: function (name) {
       var start = document.cookie.indexOf(name+"=");
       var len = start+name.length+1;
       if ((!start) && (name != document.cookie.substring(0,name.length))) return null;
       if (start == -1) return null;
       var end = document.cookie.indexOf(";",len);
       if (end == -1) end = document.cookie.length; 
       return unescape(document.cookie.substring(len,end)); 
    },
    
    Set_Cookie: function (name,value,expires,path,domain,secure) { 
    	expires = expires * 60*60*24*1000;
    	var today = new Date();
    	var expires_date = new Date( today.getTime() + (expires) );
        var cookieString = name + "=" +escape(value) + 
           ( (expires) ? ";expires=" + expires_date.toGMTString() : "") + 
           ( (path) ? ";path=" + path : "") + 
           ( (domain) ? ";domain=" + domain : "") + 
           ( (secure) ? ";secure" : ""); 
        document.cookie = cookieString; 
    },
    
    Delete_Cookie: function (name,path,domain) { 
       if (jsFlexy.Get_Cookie(name)) document.cookie = name + "=" + 
          ( (path) ? ";path=" + path : "") + 
          ( (domain) ? ";domain=" + domain : "") + 
          ";expires=Thu, 01-Jan-70 00:00:01 GMT"; 
    },
    
    slideStart: function(id, slide) {
        var el = document.getElementById(id);
        var el_content = document.getElementById(id+'_Content');
        
        el_content.style.position = 'relative';
        
        if (slide <= 0) {
            el.setAttribute('slide','-1');
            if (! el.style.height) {
                el.style.height = el_content.offsetHeight + 'px';
            }
            this.slideClose(id);
        } else {
            el.setAttribute('slide','1');
            el.style.visibility = 'visible';
            this.slideShow(id);
        }
    },
    
    slideShow: function(id) {
        var el = document.getElementById(id);
        if (el.getAttribute('slide') != '1') {
            return;
        }
        var el_content = document.getElementById(id+'_Content');
        var el_height = (el.style.height.substring(0,el.style.height.length-2) * 1);
        if (el_height + phpVars.slidedownSpeed > el_content.offsetHeight) {
            el.style.height = '';
            el_content.style.top = '0px';
            return;
        }
        el_content.style.top = '-' + (el_content.offsetHeight - (el_height + phpVars.slidedownSpeed)) + 'px';
        el.style.height = (el_height + phpVars.slidedownSpeed + 10) + 'px';
        setTimeout('jsFlexy.slideShow("'+id+'")', phpVars.slidedownTimer);
    },
    
    slideClose: function(id) {
        var el = document.getElementById(id);
        if (el.getAttribute('slide') != '-1') {
            return;
        }
        var el_content = document.getElementById(id+'_Content');
        var el_height = el.style.height.substring(0,el.style.height.length-2) * 1;
        if (el_height <= phpVars.slidedownSpeed) {
            el.style.height = '1px';
            el.style.visibility = 'hidden';
            return;
        }
        el_content.style.top = '-' + (10 + el_content.offsetHeight - (el_height - phpVars.slidedownSpeed)) + 'px';
        el.style.height = (el_height - phpVars.slidedownSpeed) + 'px';
        setTimeout('jsFlexy.slideClose("'+id+'")', phpVars.slidedownTimer);
    },
    
	Redirect: function(url) {
		window.location = url;
	},
	
	hideId: function(id, time) {
	    if (time) {
	        setTimeout('jsFlexy.hideId("'+id+'")', time);
	    } else {
	        var el = document.getElementById(id);
	        if (el) {
	            el.style.display = 'none';
	        }
	    }
	}
}

Array.prototype.inArray = function (value)
{
    var i;
    for (i=0; i < this.length; i++) {
        if (this[i] === value) {
            return true;
        }
    }
    return false;
};

