addEvent(window, 'load', user_init, false);
addEvent(window, 'resize', set_form_max_width, false);


function user_init()
{
    glossary_init();
    left_col_height_init();
    set_form_max_width();
    popups_init()
}

//
// Resize left column if needed (short content)
//
function left_col_height_init()
{
    if (!document.getElementById("section-side-navig")) {
        return true;
    }

    var left_col = document.getElementById("section-side-navig");
    var main_block = document.getElementById("section-main");
    
    if (left_col.offsetHeight>main_block.offsetHeight) {
        main_block.style.height = left_col.offsetHeight + 'px';
    }
}


//
// Init popup windows for images
//
function popups_init()
{
    var ancs = document.getElementsByTagName("a");
    for (i=0; i<ancs.length; i++) {
        if ((ancs.item(i).className).indexOf("popup")!=-1) {
            // popup-width-height
            parts = ancs.item(i).className.split("-");
            addEvent(ancs.item(i), 'click', create_show_image_func(ancs.item(i).href, parts[1], parts[2]), false);
        }
    }
}

function create_show_image_func(href, width, height)
{
    return function(e) { 
        if (e.preventDefault) {
            e.preventDefault();
        }
        show_image(href, width, height); 
        return false;
    };
}

function show_image(href, width, height)
{
        var left = (screen.width-width)/2;
        var top  = (screen.height-height)/2;
        
        height = 30 + parseInt(height);
        features = 'width='+width+', height='+height+', location=no, menubar=no, personalbar=no, resizable=yes, scrollbars=no, status=no, titlebar=yes, toolbar=no, left='+left+', top='+top;
		openedWindow = window.open('/pic.php?p='+href, 'mywind', features);
		openedWindow.focus();
        return false;
}

//
// Form resizer
//
// IE doesn't support max/min-width. 
// If browser window is opened wide we have to limit our form width to 400px.
// If browser window is narrow and parent's element width is less then 450px
// we force our form to have 93% width (otherwise it will slide down)
//
var form_max_width = 400;
var save_form_width = new Array;
var container_width_limit = 450;

function set_form_max_width()
{
    if (!document.getElementById("section-meat")) {
        return true;
    }

    if (window.getComputedStyle) {
        // browser (probably) able to understand max-width and min-width;
        return false;
    }
    
    var forms = document.getElementById("section-meat").getElementsByTagName("form");
    if (forms) {
        for (i=0; i<forms.length; i++) {
            if (forms.item(i).offsetWidth) {
                if (forms.item(i).offsetWidth > form_max_width) {
                    forms.item(i).style.width = form_max_width+'px';
                }
                
                if (forms.item(i).parentNode.offsetWidth < container_width_limit) {
                    forms.item(i).style.width = '93%';
                }
            }
        }
    }
}

//
// Glossary functions
//
function glossary_show_term(e)
{
  	    if (e.preventDefault) {
            e.preventDefault();
		}
		
		var anchor_node = (e.target) ? e.target : window.event.srcElement;
		var tmp = anchor_node.href.match(/(.*)=(\d+)/);
		var id = RegExp.$2;

        var width = 400;
        var height = 300;
        var left = (screen.width-width)/2;
        var top  = (screen.height-height)/2;
        
        features = 'width='+width+', height='+height+', location=no, menubar=no, personalbar=no, resizable=yes, scrollbars=no, status=no, titlebar=yes, toolbar=no, left='+left+', top='+top;
		openedWindow = window.open('/glossary.php?id='+id, 'term', features);
		openedWindow.focus();
        return false;
}

function glossary_init()
{
    if (document.getElementById("glossary")) {
	    var ancs = document.getElementById("glossary").getElementsByTagName("a");
    	for (i=0; i<ancs.length; i++) {
       	    addEvent(ancs.item(i), 'click', glossary_show_term, false);
	    }
    }
    else if (document.getElementById("section-main")) {
        var ancs = document.getElementById("section-main").getElementsByTagName("a");
    	for (i=0; i<ancs.length; i++) {
	        if (ancs.item(i).href.indexOf("glossary.php")!=-1) {
           	    addEvent(ancs.item(i), 'click', glossary_show_term, false);
		}
	}
    }
}


