//Opsti event handleri

function TestArrayIndexOf () {
	if (!Array.prototype.indexOf)
	{Array.prototype.indexOf = function(elt /*, from*/)
	  {
		var len = this.length;
		var from = Number(arguments[1]) || 0;
		from = (from < 0) ? Math.ceil(from) : Math.floor(from);
		if (from < 0) from += len;
		for (; from < len; from++)
		{
		  if (from in this &&
			  this[from] === elt)
			return from;
		}
		return -1;
	  };
	}
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

document.getElementsByClassName = function(clsName){
    var retVal = new Array();
    var elements = document.getElementsByTagName("*");
    for(var i = 0;i < elements.length;i++){
        if(elements[i].className.indexOf(" ") >= 0){
            var classes = elements[i].className.split(" ");
            for(var j = 0;j < classes.length;j++){
                if(classes[j] == clsName)
                    retVal.push(elements[i]);
            }
        }
        else if(elements[i].className == clsName)
            retVal.push(elements[i]);
    }
    return retVal;
}

// nizovi collapse/expand panela, njihovih visina u px i odgovarajucih komandi-ikonica
var panes = new Array, icons = new Array, heights = new Array, zaglavlja = new Array, dirs = new Array, disps = new Array;

var n = navigator.userAgent;
if(/Opera/.test(n)) bypixels = 2;
else if(/Firefox/.test(n)) bypixels = 3;
else if(/MSIE/.test(n)) bypixels = 4;

function setIdsByName() {    
    var paneli = document.getElementsByName("PaneColExp");
    var komande = document.getElementsByName("IconColExp");
    var trt;
    for (i=0;i<komande.length;i++) {
        trt = "IconColExp" + (i+2).toString();
        komande[i].setAttribute('id',trt);
        trt = "PaneColExp" + (i+2).toString();
        paneli[i].setAttribute('id',trt);
        }    
    }

function ColExpInit() {
	
	TestArrayIndexOf();
	
	var elms = 1; 
// pokupi sve collapse/expand panele, njihove visine u px i odgovarajuce komande-ikonice u nizove
// na strani mora da budu paneli sa id-jevima tipa "PaneColExp(redni broj od 1 nadalje)"
// komande sa id-jevima tipa "IconColExp(redni broj od 1 nadalje)"
// ako panel ima jos i "H" na kraju imena razvlaci se i skuplja horizontalno (dir="width",disp="inline"), 
// u protivnom vertikalno (dir="height",disp="block")
 
    if (document.getElementsByName("PaneColExp") != null || typeof document.getElementById("PaneColExp") != "undefined") {
	    setIdsByName();
	    }
 
    for (var i=1;i<document.getElementsByTagName("div").length;i++) {
		var pane="PaneColExp" + i.toString();
		if (typeof document.getElementById(pane) == "undefined"
			|| document.getElementById(pane) == null) pane="PaneColExp"+i.toString()+"H";
		var icon="IconColExp" + i.toString();
		if (typeof document.getElementById(pane) != "undefined"
			&& document.getElementById(pane) != null) {
			panes[elms-1]=document.getElementById(pane);
			if (panes[elms-1].id.indexOf("H") != -1) {
				dirs[elms-1]="width";
				disps[elms-1]="inline";
				if (!panes[elms-1].style.width || typeof panes[elms-1].style.width == "undefined" || panes[elms-1].style.width == 0) {
					if (panes[elms-1].style.display=="none") {
						document.getElementById(pane).style.display="inline";
						heights[elms-1] = document.getElementById(pane).offsetwidth;
						document.getElementById(pane).style.display="none";
						}
					else heights[elms-1] = document.getElementById(pane).offsetWidth;
					}
				else heights[elms-1]=document.getElementById(pane).style.width; 
				}
			else {
				dirs[elms-1]="height";
				disps[elms-1]="block";
				if (!panes[elms-1].style.height || typeof panes[elms-1].style.height == "undefined" || panes[elms-1].style.height == 0) {
					if (panes[elms-1].style.display=="none") {
						document.getElementById(pane).style.display="block";
						heights[elms-1] = document.getElementById(pane).offsetHeight;
						document.getElementById(pane).style.display="none";
						}
					else heights[elms-1] = document.getElementById(pane).offsetHeight;
					}
				else heights[elms-1]=document.getElementById(pane).style.height;
				};
			
			icons[elms-1]=document.getElementById(icon);
			icons[elms-1].onclick = ShowHidePane;
			elms+=1;
			}
	}

// dodeli ponasanje zaglavlju svakog panela
	zaglavlja=document.getElementsByClassName("ColExpHeaderRow");
    for (var i=0;i<zaglavlja.length;i++) {
    	zaglavlja[i].ondblclick = function () {this.childNodes[1].getElementsByTagName("IMG")[0].click();};
    	}
} 

function ShowHidePane() {
	var IcIndex = icons.indexOf(this);
	if (panes[IcIndex].style.display == "none") {
		ShowElm(panes[IcIndex].id);
		if (dirs[IcIndex] == "height") this.src = this.src.replace("ExpandIcon","CollapseIcon");
		}
	else {
		HideElm(panes[IcIndex].id);
		if (dirs[IcIndex] == "height") this.src = this.src.replace("CollapseIcon","ExpandIcon");
		}
} 

function HideElm(elmId) {
	var elm = document.getElementById(elmId);
	var IcIndex = panes.indexOf(elm);	
	
	if (dirs[IcIndex] == "height") {
		var nr = (parseInt(elm.style.height)/bypixels/2);
		if (!elm.style.height) elm.style.height = "0px"; 
	    else elm.style.height = (parseInt(elm.style.height)-nr)+"px";
	    var to = setTimeout("HideElm('"+elmId+"')",30);
	    if (parseInt(elm.style.height) <= nr) {
	        clearTimeout(to);
	        elm.style.display = "none";
	        elm.style.height = "0px";
	    }
	   }
	else	{
		var nr = (parseInt(elm.style.width)/bypixels);
		if (!elm.style.width) elm.style.width = elm.offsetWidth.toString() + "px";
		else elm.style.width = (parseInt(elm.style.width)-nr)+"px";
		var heightV = elm.offsetHeight;
		//elm.style.height = heightV.toString() + "px";
		elm.style.overflow = "hidden";
	    var to = setTimeout("HideElm('"+elmId+"')",30);
	    if (parseInt(elm.style.width) <= nr) {
	        clearTimeout(to);
	        elm.style.display = "none";
	        elm.style.width = "0px";
	    //    elm.style.height = heightV.toString() + "px";
	    	}
	   }
}

function ShowElm(elmId) {
    var elm = document.getElementById(elmId);
	var IcIndex = panes.indexOf(elm);
    var nr = (parseInt(heights[IcIndex])/bypixels/2);
    if (dirs[IcIndex] == "height") {
	    elm.style.display = "block";
		if (!elm.style.height) elm.style.height = "0px";	 
	    else elm.style.height = (parseInt(elm.style.height)+nr)+"px";
	    var to = setTimeout("ShowElm('"+elmId+"')", 30);
	    if(parseInt(elm.style.height) > (parseInt(heights[IcIndex])-nr)) {
	        clearTimeout(to);
	        elm.style.height = parseInt(heights[IcIndex])+"px";
	    	}
	    }
	 else  {
	    elm.style.display = "inline";
	    var heightV = elm.offsetHeight;
		if (!elm.style.width) elm.style.width = "0px"; 
	    else elm.style.width = (parseInt(elm.style.width)+nr)+"px";
	    var to = setTimeout("ShowElm('"+elmId+"')", 30);
	    if(parseInt(elm.style.width) > (parseInt(heights[IcIndex])-nr)) {
	        clearTimeout(to);
	        elm.style.width = parseInt(heights[IcIndex])+"px";
	    	//elm.style.height = heightV+"px";
	    	}
	    }
}
 