

//////////////////////////////////////////////////////////////////////////////////////

// LAYERS

/////////////

function id_showhide ( e_idclass , e_type , e_action ) {

var status = '';

    if (e_type == 'class') {

    elems = document.getElementsByClassName( e_idclass );
    
        for ( i=0 ; i<elems.length ; i++ ) {

            if (e_action == 'hide') {
            
            elems[i].style.display = 'none';
    
            }
            
            if (e_action == 'show') {
    
            elems[i].style.display = '';
            status = 'shown';
            
            }

        }
    
    }
    
    if (e_type == 'id') {
    
    target = document.getElementById( e_idclass );

        if (e_action == 'hide') {
        
        target.style.display = 'none';
        
        }
        
        if (e_action == 'show') {
        
        target.style.display = '';
        
        }
    
    }    
  
}



function  mouseout(t,e) {
    if(e.relatedTarget)
        return(e.relatedTarget != t && e.relatedTarget.parentNode  != t);

    return(e.toElement != t && e.toElement.parentNode  != t);
}

// -------------------------------------------------------------------------------------

function id_hide ( targetId ) {

    if ( target.style.display == 'none' ) {
    
    } else {

    Effect.Fade( target );
    
    }
  
}

// -------------------------------------------------------------------------------------

function id_show ( targetId , effect ) {

target = document.getElementById( targetId );

    if ( target.style.display == 'none' ) {

    Effect.Appear( target );

    } else {
    
    }
        
}

// -------------------------------------------------------------------------------------

function id_toggle ( targetId ) {

    if ( document.getElementById ) {
    
    target = document.getElementById( targetId );
    
        if ( target.style.display == 'none' ) {
        
        //target.style.display = '';
        Effect.Appear( target );
        } else {
        
        //target.style.display = 'none';
        Effect.Fade( target );
        }
        
    }
    
}

// -------------------------------------------------------------------------------------

function hd_layer_toggle_next_with_class ( classname ) {

elems = document.getElementsByClassName( classname );

    for ( i=0 ; i<elems.length ; i++ ) {
    
        if ( elems[i].style.display == 'none' ) {
        
        id_toggle(elems[i].id);
        break;
        
        }
    
    }
    
}

// -------------------------------------------------------------------------------------

function hd_layer_toggle_prev_with_class ( classname ) {

elems = document.getElementsByClassName( classname );

    for ( i=0 ; i<elems.length ; i++ ) {
    
        if ( elems[i].style.display != 'none' ) {
        
        var elem = elems[i];

        }
    
    }

id_toggle(elem.id);

}

// -------------------------------------------------------------------------------------

function id_do_childs ( action , containerId , targetTag, exceptionId ) {

var container = document.getElementById( containerId );
var elem_num = container.getElementsByTagName( targetTag );
var elem_tot = elem_num.length;
    
    for ( i=0 ; i<elem_tot ; i++ ) {
        
        if (elem_num[i].id != exceptionId) {

            if (action == 'hide') {

            id_hide(elem_num[i].id);
            
            }
        
        }
    
    }

}

// -------------------------------------------------------------------------------------

function id_blink ( _tag , _class ) {

var el = document.body.getElementsByTagName( _tag );

    for (var i = 0; i < el.length; i++) {
    
        if (el[i].className == _class){
        
        el[i].style.visibility = el[i].style.visibility == 'hidden' ? 'visible' : 'hidden';
        
        }
    
    }

}

// -------------------------------------------------------------------------------------

function id_class_swap ( _tag , _class1 , _class2 ) {

var el = document.body.getElementsByTagName( _tag );

    for (var i = 0; i < el.length; i++) {

        if (el[i].className == _class1) {
        
        el[i].className = _class2;
        
        } else if (el[i].className == _class2) {
        
        el[i].className = _class1;
        
        }

    }

}

// -------------------------------------------------------------------------------------

function id_autoscroll(id, sx, sy) {

ns = (navigator.appName.indexOf("Netscape") != -1);
d = document;

var el = d.getElementById?d.getElementById(id):d.all?d.all[id]:d.layers[id];
var px = document.layers ? "" : "px";

window[id.replace(/\W/g,'') + "_obj"] = el;

var offset_y_first = el.offsetTop;
var offset_y_temp = 0;

    if (d.layers) el.style = el;
    
el.cx = el.sx = sx;
el.cy = el.sy = sy;

el.sP = function(x,y) {

        this.style.left = x + px;
        this.style.top = y + px;

        };

    el.id_scroll = function() {
    
    var pX, pY;
    
    pX = (this.sx >= 0) ? 0 : ns ? innerWidth :
    document.documentElement && document.documentElement.clientWidth ?
    document.documentElement.clientWidth : document.body.clientWidth;
    
    pY = ns ? pageYOffset : document.documentElement && document.documentElement.scrollTop ?
    document.documentElement.scrollTop : document.body.scrollTop;
    
        if (this.sy < 0) {
        
        pY += ns ? innerHeight : document.documentElement && document.documentElement.clientHeight ?
        document.documentElement.clientHeight : document.body.clientHeight;
        
        }
     
    this.cx += (pX + this.sx - this.cx) / 2;
    
        if(offset_y_first < 1) offset_y_first = this.offsetTop;
        if(pY > offset_y_first) offset_y_temp = offset_y_first - sy;
        
        if(pY < offset_y_first) {
        
        offset_y_temp = 0 + sy;    

        this.cy =  offset_y_first - offset_y_first;
        
        } else {
        
        this.cy += (pY + this.sy - this.cy - offset_y_temp) / 2;
        
        }
    
    this.sP(this.cx, this.cy);
        
    setTimeout(this.id.replace(/\W/g,'') + "_obj.id_scroll()", 10);

    }
   
return el;

}

// -------------------------------------------------------------------------------------
// tabs management
function id_toggle_tabs ( tabname , zonename , targetid , idnum ) {
    
    // fix possible targetid errors
    if ( targetid >= idnum || targetid == 0 ) targetid = 1;
    
    // loop through zones
    for ( var j=1; j <= idnum; j++ ) {
        
        if ( document.getElementById( zonename+j ) ) {

        // hide zones linked to unclicked tabs
        document.getElementById( zonename+j ).style.display = 'none';
        
            // fix look of unclicked tabs
            if ( j != targetid ) document.getElementById( tabname+j ).className = tabname + 'off';
        
        }
        
    }

    if ( document.getElementById( zonename + targetid ) ) {
    
    // change look of clicked tab
    document.getElementById( tabname + targetid ).className = tabname+'on';
    
    // unhide zone linked to specified tab
    //document.getElementById( zonename + targetid ).style.display = '';
    Effect.Appear( zonename + targetid );

    // save clicked tab number in cookie
    cookie_make( 'admin_tab_num' , targetid , 2 );
   
    }

}

// -------------------------------------------------------------------------------------

function elem_div_add ( top_elem_name , sub_elem_name , method , parent_num , cat_parent_ID , pixel_num , content_html ) {

parent_id = sub_elem_name + parent_num;

    if ( !row_num ) var row_num = document.getElementById( top_elem_name ).getElementsByTagName( 'div' ).length - 1;
    if ( method == 'before' ) var next_elem = document.getElementById( parent_id );
    if ( method == 'after' ) var next_elem = document.getElementById( parent_id ).nextSibling;

row_num++;

var new_id = sub_elem_name + row_num;

    if ( pixel_num > 0 ) {
    
    var prefix = content_html[0];
    
    } else {
    
    var prefix = '';
    
    }

var div_content_new = '';
div_content_new = prefix + content_html[1];

div_content_new = div_content_new.replace( /{cat_parent_ID}/g , cat_parent_ID );
div_content_new = div_content_new.replace( /{row_num}/g , row_num );
div_content_new = div_content_new.replace( /{pixel_num}/g , pixel_num );
div_content_new = div_content_new.replace( /{parent_id}/g , parent_id );


var div = document.createElement( 'div' );
div.setAttribute( 'id' , new_id );

    if ( method == 'inside' ) {
    
    var parent_node = document.getElementById( parent_id );
    parent_node.appendChild( div );
        
    } else {
    
    var parent_node = document.getElementById( parent_id ).parentNode;
    parent_node.insertBefore( div , next_elem );

    }

document.getElementById( new_id ).style.padding =  '0px 0px 0px ' + pixel_num + 'px';
document.getElementById( new_id ).innerHTML = div_content_new;

}

// -------------------------------------------------------------------------------------

function elem_div_page_add ( method , root_num, row_prefix , row_num , row_parent_ID , row_order ) {

var tab = cookie_read('admin_tab_num');
    
    if (!tab) tab = 0;

row_num_last[tab]++;
row_ID[tab]++;

var pixel_num = 80;

var prefix = pages_html_temp[tab][root_num][0];

id_parent = row_prefix + row_num;

    if ( method == 'before' ) {
    
    var next_elem = document.getElementById( id_parent );
    row_order--;
    
    }
    
    if ( method == 'after' ) {
    
    var next_elem = document.getElementById( id_parent ).nextSibling;
    row_order++;
    
    }
    
    if ( method == 'inside' ) row_order++;

var id_new = row_prefix + row_num_last[tab];

var div = document.createElement( 'div' );
div.setAttribute( 'id' , id_new );

    if ( method == 'inside' ) {
    
    var parent_node = document.getElementById( id_parent );
    parent_node.appendChild( div );
    
        if ( parent_node.style.padding == '' ) pixel_num /= 2;
    
    var div_padding = '0px 0px 0px ' + pixel_num + 'px';
    
    } else {

    var parent_node = document.getElementById( id_parent ).parentNode;
    var div_padding = document.getElementById( id_parent ).style.padding;

        if ( div_padding == '' ) prefix = '';

    parent_node.insertBefore( div , next_elem );

    }

var div_content_new = '';

    if ( max_pixel[tab] > 0 && row_num_parent != 'NULL' && pixel_num >= max_pixel[tab] ) {

    div_content_new = prefix + pages_html_temp[tab][root_num][1] + pages_html_temp[tab][root_num][3] + pages_html_temp[tab][root_num][4];

    } else {
    
    div_content_new = prefix + pages_html_temp[tab][root_num][1] + pages_html_temp[tab][root_num][2] + pages_html_temp[tab][root_num][3] + pages_html_temp[tab][root_num][4];
        
    }

    if ( row_parent_ID == 'NULL' ) {
    
    row_parent_ID = '\'NULL\'';

    }

div_content_new = div_content_new.replace( /{row_ID}/g , row_ID[tab] );
div_content_new = div_content_new.replace( /{row_parent_ID}/g , row_parent_ID );
div_content_new = div_content_new.replace( /{row_num_last}/g , row_num_last[tab] );
div_content_new = div_content_new.replace( /{row_order}/g , (row_order) );

    if (set_path == 'yes') {
    
    var row_path = '';
    
        if (row_parent_ID == '\'NULL\'') {
        
        row_path = '';
    
        } else {
        
            if ( method == 'inside' ) {
            
            row_path = document.getElementById( 'path_' + row_num ).value + '' + row_parent_ID + '.';
            
            } else {
            
            row_path = document.getElementById( 'path_' + row_num ).value;
            
            }
        
        }
     
    div_content_new = div_content_new.replace( /{row_path}/g , row_path );
    
    document.getElementById( id_new ).style.padding = div_padding;
    document.getElementById( id_new ).innerHTML = div_content_new;
    
        if (document.getElementById( 'path_' + row_num_last[tab] )) {
        
        document.getElementById( 'path_' + row_num_last[tab] ).value = row_path;
        
        }
    
    } else {
    
    document.getElementById( id_new ).style.padding = div_padding;
    document.getElementById( id_new ).innerHTML = div_content_new;    

    }

}


// -------------------------------------------------------------------------------------

function elem_div_html_multiply ( div_id , content_html , num ) {

var div_content_new = '';

    for ( i=0 ; i<num ; i++ ) {
    
    div_content_new += content_html;
    
    }

document.getElementById( div_id ).innerHTML = div_content_new;

}

// -------------------------------------------------------------------------------------

function elem_del ( this_id ) {
   
var el = document.getElementById( this_id );
el.parentNode.removeChild( el );

}

// -------------------------------------------------------------------------------------

function elem_move(this_id , nstep) {

    if (nstep == null) {
    
    var el = document.getElementById(this_id);
    el.parentNode.appendChild(el);    
    
    } else {
    
        //nstep=1 move down 1, nstep=-1 move up 1
        var el = document.getElementById( this_id );
        var idx;
        
        if (el) {
        
            var cel=el.parentNode.getElementsByTagName(el.tagName);
            for (var i=0;i<cel.length;i++) {
            
                if (el==cel[i]) {
                
                idx=i;
                break;
                
                }
            }
            
            if (idx==null) {
                //alert("element not found");
            } else {
            
                if ((idx+nstep)>-1 && (idx+nstep)<cel.length) {
    
                    el.parentNode.insertBefore(el, cel[idx+nstep]);
                    if (nstep>0) {
                    
                    el.parentNode.insertBefore(cel[idx+nstep],el);
                    
                    }
                    
                } else {
                    //alert("move steps out of range")
                }
                
            }
            
        }
        
    }
    
}

// -------------------------------------------------------------------------------------

function documentOverlay() {
 
    // Shortcut to current instance of object:
    var instance = this,
 
    // Cached body height:
    bodyHeight = (function(){
        return getDocDim('Height','min');    
    })();
 
    // CSS helper function:
    function css(el,o) {
        for (var i in o) { el.style[i] = o[i]; }
        return el;
    };
 
    // Document height/width getter:
    function getDocDim(prop,m){
        m = m || 'max';
        return Math[m](
            Math[m](document.body["scroll" + prop], document.documentElement["scroll" + prop]),
            Math[m](document.body["offset" + prop], document.documentElement["offset" + prop]),
            Math[m](document.body["client" + prop], document.documentElement["client" + prop])
	);
    }
 
    // get window height: (viewport):
    function getWinHeight() {
        return window.innerHeight ||
                (document.compatMode == "CSS1Compat" && document.documentElement.clientHeight || document.body.clientHeight);
    }
 
    // Public properties:
 
    // Expose CSS helper, for public usage:
    this.css = function(o){
        css(instance.element, o);
        return instance;
    };
 
    // The default duration is infinity:
    this.duration = null;
 
    // Creates and styles new div element:
    this.element = (function(){
        return css(document.createElement('div'),{
            width: '100%',
            height: getDocDim('Height') + 'px',
            position: 'absolute', zIndex: 999,
            left: 0, top: 0,
            cursor: 'wait'
        });
    })();
 
    // Resize cover when window is resized:
    window.onresize = function(){
 
        // No need to do anything if document['body'] is taller than viewport
        if(bodyHeight>getWinHeight()) { return; }
 
        // We need to hide it before showing
        // it again, due to scrollbar issue.
        instance.css({display: 'none'});
        setTimeout(function(){
            instance.css({
                height: getDocDim('Height') + 'px',
                display: 'block'
            });
        }, 10);
 
    };
 
    // Remove the element:
    this.remove = function(){
        this.element.parentNode && this.element.parentNode.removeChild(instance.element);
    };
 
    // Show element:
    this.show = function(){};
 
    // Event handling helper:
    this.on = function(what,handler){
        what.toLowerCase() === 'show' ? (instance.show = handler)
        : instance.element['on'+what] = handler;
        return instance;
    };
 
    // Begin:
    this.init = function(duration){
 
        // Overwrite duration if parameter is supplied:
        instance.duration = duration || instance.duration;
 
        // Inject overlay element into DOM:
        document.getElementsByTagName('body')[0].appendChild(instance.element);
 
        // Run show() (by default, an empty function):
        instance.show.call(instance.element,instance);
 
        // If a duration is supplied then remove element after
        // the specified amount of time:
        instance.duration && setTimeout(function(){instance.remove();}, instance.duration);
 
        // Return instance, for reference:
        return instance;
 
    };
 
}



