

function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            if (oldonload) {
                oldonload();
            }
            func();
        };
    }
}



function setWULimitPopupForLink(linkElementId, popupFunction) {
    linkElement = document.getElementById(linkElementId);
    if (linkElement !== null) {
        linkElement.removeAttribute('href');
        linkElement.style.cursor = 'pointer';                
        linkElement.onclick = popupFunction;
    }
}

function setWULimitPopupForButton(buttonElementId, popupFunction) {
    var buttonElement = document.getElementById(buttonElementId);
    if (buttonElement !== null) {                   
        buttonElement.onclick = function() {
            popupFunction();
            return false;
        };
    }
}


function PopupSkin() {
    /*this._darkBgImg = darkBgImg;
    this._topBgImg = topBgImg;
    this._midBgImg = midBgImg;
    this._bottomBgImg = bottomBgImg;*/
}

PopupSkin.prototype.darkBgImg = '';
PopupSkin.prototype.topBgImg = '';
PopupSkin.prototype.midBgImg = '';
PopupSkin.prototype.bottomBgImg = '';
PopupSkin.prototype.topHeight = null;
PopupSkin.prototype.topWidth = null;
PopupSkin.prototype.midHeight = null;
PopupSkin.prototype.midWidth = null;
PopupSkin.prototype.bottomHeight = null;
PopupSkin.prototype.bottomWidth = null;
PopupSkin.prototype.midClass = null;

PopupSkin.prototype.getDarkBgStyle = function() {
    var result = 'background-image: url(' + this.darkBgImg + ');';
    return result;
};

PopupSkin.prototype.getTopBgStyle = function() {
    var result = 'background-image: url(' + this.topBgImg + ');';
    result += ' width: ' + this.topWidth + 'px;';
    result += ' height: ' + this.topHeight + 'px;';
    return result;
};

PopupSkin.prototype.getMidBgStyle = function() {
    var result = 'background-image: url(' + this.midBgImg + ');';
    result += ' width: ' + this.midWidth + 'px;';
    result += ' height: ' + this.midHeight + 'px;';
    return result;
};

PopupSkin.prototype.getBottomBgStyle = function() {
    var result = 'background-image: url(' + this.bottomBgImg + ');';
    result += ' width: ' + this.bottomWidth + 'px;';
    result += ' height: ' + this.bottomHeight + 'px;';
    return result;
};

function createSkin(skinId) {
    var skin = null;
    
    if (skinId == 1) {
        skin = new PopupSkin();
        skin.darkBgImg = '/site_images/popups/popup_grid.gif';
        
        skin.topBgImg = '/site_images/popups/popup_top.gif';
        skin.topWidth = 720;
        skin.topHeight = 100;
        
        skin.midBgImg = '/site_images/popups/popup_mid.gif';
        skin.midWidth = 720;
        skin.midHeight = 250;
        
        skin.bottomBgImg = '/site_images/popups/popup_bot.gif';
        skin.bottomWidth = 720;
        skin.bottomHeight = 40;
    } else if (skinId == 10) {
        skin = new PopupSkin();
        skin.darkBgImg = '/site_images/popups/popup_grid.gif';
        
        skin.topBgImg = '/~bjbasanes/_popupimages/top.gif';
        skin.topWidth = 720;
        skin.topHeight = 100;
        
        skin.midBgImg = '/~bjbasanes/_popupimages/mid.gif';
        skin.midWidth = 720;
        skin.midHeight = 250;
        
        skin.bottomBgImg = '/~bjbasanes/_popupimages/bottom.gif';
        skin.bottomWidth = 720;
        skin.bottomHeight = 40;
    } else if (skinId == 2) {
        skin = new PopupSkin();
        skin.darkBgImg = '/site_images/popups/popup_grid.gif';
        
        skin.topBgImg = '/site_images/popups/popup_top.gif';
        skin.topWidth = 720;
        skin.topHeight = 100;
        
        skin.midBgImg = '/site_images/popups/popup_mid.gif';
        skin.midWidth = 720;
        skin.midHeight = 650;
        skin.midClass = 'popup_mid_skin_2';
        
        skin.bottomBgImg = '/site_images/popups/popup_bot.gif';
        skin.bottomWidth = 720;
        skin.bottomHeight = 40;
    }
    
    return skin;
}


/* ALTERNATIVE BOX */

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

function addEvent(obj ,evt, fnc) {
    if (obj.addEventListener) {
        obj.addEventListener(evt, fnc, false);
    } else if (obj.attachEvent) {
        obj.attachEvent('on'+evt,fnc);
    } else {
        return false;
    }
    
    return true;
}

function removeEvent(obj ,evt, fnc) {
    if (obj.removeEventListener) {
        obj.removeEventListener(evt,fnc,false);
    } else if (obj.detachEvent) {
        obj.detachEvent('on'+evt,fnc);
    } else {
        return false;
    }
    
    return true;
}

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

function appendElement(node, tag, id, htm) {
    if(node) {
        var ne = document.createElement(tag);
        
        if(id) 
            ne.id = id;
        if(htm) 
            ne.innerHTML = htm;
        
        node.appendChild(ne);
        return true;
    } else {
        return false;
    }
}

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



function addPopup(my_id, my_class, my_html, my_x, my_y, skinId) {
    var obj = document.getElementById(my_id);
    if(!obj) {
        if( appendElement(document.getElementById('popup_master'), 'div', my_id) );
        else 
            return;
    }

    var popup = document.getElementById( my_id );
    if (my_class) {
        popup.setAttribute("class", my_class);  
        popup.className = my_class; 
    }

    if(my_x) 
        popup.style.left = my_x + 'px';
    if(my_y) 
        popup.style.top = my_y + 'px';

    if(my_html) {
        var skin = createSkin(skinId);
        
        if (skin == null) {
            popup.innerHTML = "<div class='pop_up_top'></div><div class='pop_up_mid'>" +  my_html + "</div><div class='pop_up_bot'>";
        } else {
            var popupHTML = '<div class="pop_up_top" style="' + skin.getTopBgStyle() +'"></div>';
            if (skin.midClass !== null) 
                popupHTML += '<div class="' + skin.midClass + '" style="' + skin.getMidBgStyle() + '">' +  my_html + '</div>';
            else
                popupHTML += '<div class="pop_up_mid" style="' + skin.getMidBgStyle() + '">' +  my_html + '</div>';
            popupHTML += '<div class="pop_up_bot" style="' + skin.getBottomBgStyle() + '"></div>';
            
            popup.innerHTML = popupHTML;
        }
    }
}



var depth = 2;
var gray_out = 0;
var popup_stack = new Array();
var close_gray = new Array();

function showPopup(p, htm, x, y, modal, skinId) {
    popup_stack.push(p);
    
    if (skinId === undefined) 
        skinId = 1;

    if(modal)
        greyout(true, false);
    else  
        greyout(true, true);

    var obj = document.getElementById(p);
    if(!obj) {
        if ( htm )
            addPopup(p, "pop_up", htm, x, y, skinId);
        else 
            addPopup(p, "pop_up", "no content given", x, y, skinId);
        
        obj = document.getElementById(p);
    }

    obj.style.display = 'block';
    obj.style.zIndex =  depth++;        	
}

function showModal(p) { }

function setPopup(p) { }

function hidePopup() {
    p = popup_stack.pop();
    
    if(p) {
        document.getElementById(p).style.display = 'none';
        greyout(false);
        depth--;
    }  
}

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

function greyout(d, lock) {
    var obj = document.getElementById('greyout');

    if (!obj) {
        if( appendElement(document.getElementById('popup_master') ,'div','greyout') );
        else 
            return;

        obj = document.getElementById('greyout');
        obj.setAttribute( 'class' , 'dark_background');
        obj.className = "dark_background";          
    }
    
    if (d) {
        obj.className = "dark_background";
        greyoutResize();
    
        obj.style.display = 'block';
        gray_out++;
        obj.style.zIndex = gray_out + 1;

        addEvent(window, 'resize', greyoutResize);

        close_gray.push(lock);
        autoclose = lock;
        if( autoclose )
            obj.onclick =  hidePopup;
        else 
            obj.onclick = '';

    } else {
        autoclose =	close_gray.pop();
        if( autoclose )           
            obj.onclick =  hidePopup;
        else 
            obj.onclick = '';

        gray_out--;
        obj.style.zIndex = gray_out + 1;

        //alert(gray_out);
        if ( gray_out <= 0 ) {
            gray_out = 0;
            obj.style.display = 'none';   
        }
    }
} // end function greyout(d, lock)
     
function greyoutResize() {
    var obj = document.getElementById('greyout');
  //      obj.style.height = document.body.clientHeight+'px';
  //      obj.style.width  = document.body.clientWidth+'px';
    obj.style.height = Math.max( Math.max(document.body.scrollHeight,document.body.clientHeight ) , screen.availHeight)+'px';
    obj.style.width  = Math.max( Math.max(document.body.scrollWidth,document.body.clientWidth) , screen.availWidth  )+'px';
}

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