var errors= new Array();
function loadModul(target,modul,action,params){
	var load = document.getElementById('loader');
	load.style.display="block";
	DMT_loadModul(target,modul,action,params);
}
function submit_form(target,modul,action,form){
	var error = 0;
	var params = xajax.getFormValues(form).split("<q>");
   	var info = params[1].split("</q>");
    params = info[0];
    info = params.split("&");
    for(var i=0; i<info.length; i++)
    {
        var temp = info[i].split("=");
        if(temp[0] != "id" && temp[0] != "submit"){
		//alert(temp[1]);
            if(temp[1] == "" || temp[1] == " "){
			 errors[i] = 1;
		}else{
			errors[i] = 0;
		}
        }
    }
    for(var j =0; j<errors.length; j++){
    		if(errors[j] == 1) error = 1;
    }  
    if(error != 0){
        alert("Molim unesite podatke u sva polja!");
        return;
    }
    loadModul(target,modul,action,xajax.getFormValues(form)); 
	return false;
}
function submit_pristupnica(target,modul,action){		
	loadModul(target,modul,action);
	//zatvoriPopup();
	return false;
}
function check(args){
    if(args.value=="" || args.value==" "){
        args.className="required";
    }else{
        args.className = "input";
    }
}
function start(){
	loadModul('container', 'pocetna', 'viewAll','');
}
function loadSlika(slika){
	var sadrzaj = '<a href="#" onclick="zatvoriPopup()"><img class=\"slika\" src="slike/' + slika + '"></a>';
	var div = document.getElementById('popup');
	div.innerHTML = sadrzaj;
	div.style.display="block";
}
function loadSlikaGalerija(slika){
    var sadrzaj = '<a href="#" onclick="zatvoriPopup()"><img class=\"slika\" src="galerija/' + slika + '"></a>';
    var div = document.getElementById('popup');
    div.innerHTML = sadrzaj;
    div.style.display="block";
}
function otvoriDokument(dokument){
	window.open('dokumenti/' + dokument,'','height=800,width=800,menubar=no,scrollbars=no,status=no,toolbar=no,resize=yes')

}
function startDrag(e){
	// determine event object
	if(!e){var e=window.event};
	// determine target element
	var targ=e.target?e.target:e.srcElement;
	
	if(targ.className!='drag'){return};
	// calculate event X,Y coordinates
    offsetX=e.clientX;
    offsetY=e.clientY;
    var dnd = document.getElementById('popup');
	// assign default values for top and left properties
	if(!dnd.style.left){dnd.style.left='350px'};
	if(!dnd.style.top){dnd.style.top='90px'};
	// calculate integer values for top and left properties
    coordX=parseInt(dnd.style.left);
    coordY=parseInt(dnd.style.top);
    drag=true;
	// move div element
    document.onmousemove=dragDiv;
}
// continue dragging
function dragDiv(e){
	if(!drag){return};
	if(!e){var e=window.event};
	var dnd = document.getElementById('popup');
	// move div element
  	dnd.style.left=coordX+e.clientX-offsetX+'px';
  	dnd.style.top=coordY+e.clientY-offsetY+'px';
  	return false;
}
// stop dragging
function stopDrag(){
	drag=false;
}
function zatvoriPopup(){
	var popup = document.getElementById('popup');
	popup.style.display="none";
	popup.innerHTML="";
	
}

//DHTML 
    var contentHeight = 0;     // The total height of the content
    var visibleContentHeight = 0;    
    var scrollActive = false;
    
    var scrollHandleObj = false; // reference to the scroll handle
    var scrollHandleHeight = false;
    var scrollbarTop = false;
    var eventYPos = false;

    var scrollbuttonActive = false;
    var scrollbuttonDirection = false;
    var scrollbuttonSpeed = 2; // How fast the content scrolls when you click the scroll buttons(Up and down arrows)
    var scrollTimer = 10;    // Also how fast the content scrolls. By decreasing this value, the content will move faster    
    
    var scrollMoveToActive = false;
    var scrollMoveToYPosition = false;
    function scrollDiv_startScroll(e)
    {
        if(document.all)e = event;
        scrollbarTop = document.getElementById('scrolldiv_theScroll').offsetTop;
        eventYPos = e.clientY;
        scrollActive = true;
    }
    
    function scrollDiv_stopScroll()
    {
        scrollActive = false;
        scrollbuttonActive = false;
        scrollMoveToActive = false;
    }
    function scrollDiv_scroll(e)
    {
        if(!scrollActive)return;
        if(document.all)e = event;
        if(e.button!=1 && document.all)return;
        var topPos = scrollbarTop + e.clientY - eventYPos; 
        if(topPos<0)topPos=0;
        if(topPos/1>visibleContentHeight-(scrollHandleHeight+4)/1)topPos = visibleContentHeight-(scrollHandleHeight+4);
        document.getElementById('scrolldiv_theScroll').style.top = topPos + 'px';
        document.getElementById('scrolldiv_content').style.top = 0 - Math.floor((contentHeight) * ((topPos)/(visibleContentHeight-scrollHandleHeight)))+'px' 
    }
    
    /*
    Click on the slider
    Move the content to the this point
    */
    function scrolldiv_scrollMoveToInit(e)
    {        
        if(document.all)e = event;
        scrollMoveToActive = true;
        scrollMoveToYPosition = e.clientY - document.getElementById('scrolldiv_scrollbar').offsetTop;
        if(document.getElementById('scrolldiv_theScroll').offsetTop/1 > scrollMoveToYPosition) scrollbuttonDirection = scrollbuttonSpeed*-2; else  scrollbuttonDirection = scrollbuttonSpeed*2;
        scrolldiv_scrollMoveTo();    
    }
    
    function scrolldiv_scrollMoveTo()
    {
        if(!scrollMoveToActive || scrollActive)return;
        var topPos = document.getElementById('scrolldiv_theScroll').style.top.replace('px','');
        topPos = topPos/1 + scrollbuttonDirection;
        if(topPos<0){
            topPos=0;
            scrollMoveToActive=false;
        }
        if(topPos/1>visibleContentHeight-(scrollHandleHeight+4)/1){
            topPos = visibleContentHeight-(scrollHandleHeight+4);    
            scrollMoveToActive=false;
        }
        if(scrollbuttonDirection<0 && topPos<scrollMoveToYPosition-scrollHandleHeight/2)return;    
        if(scrollbuttonDirection>0 && topPos>scrollMoveToYPosition-scrollHandleHeight/2)return;            
        document.getElementById('scrolldiv_theScroll').style.top = topPos + 'px';
        document.getElementById('scrolldiv_content').style.top = 0 - Math.floor((contentHeight) * ((topPos)/(visibleContentHeight-scrollHandleHeight)))+'px'         
        setTimeout('scrolldiv_scrollMoveTo()',scrollTimer);        
    }
    
    function cancelEvent()
    {
        return false;            
    }

    function scrolldiv_scrollButton()
    {
        if(this.id=='scrolldiv_scrollDown')scrollbuttonDirection = scrollbuttonSpeed; else scrollbuttonDirection = scrollbuttonSpeed*-1;
        scrollbuttonActive=true;
        scrolldiv_scrollButtonScroll();
    }
    function scrolldiv_scrollButtonScroll()
    {
        if(!scrollbuttonActive)return;
        var topPos = document.getElementById('scrolldiv_theScroll').style.top.replace('px','');
        //if(!topPos) topPos = 0;
        topPos = topPos/1 + scrollbuttonDirection;
        if(topPos<0){
            topPos=0;
            scrollbuttonActive=false;
        }
        if(topPos/1>visibleContentHeight-(scrollHandleHeight+4)/1){
            topPos = visibleContentHeight-(scrollHandleHeight+4);    
            scrollbuttonActive=false;
        }    
        document.getElementById('scrolldiv_theScroll').style.top = topPos + 'px';
        document.getElementById('scrolldiv_content').style.top = 0 - Math.floor((contentHeight) * ((topPos)/(visibleContentHeight-scrollHandleHeight)))+'px'             
        setTimeout('scrolldiv_scrollButtonScroll()',scrollTimer);
    }
    function scrolldiv_scrollButtonStop()
    {
        scrollbuttonActive = false;
    }
    
    
    function scrolldiv_initScroll()
    {
        var ch = document.getElementById('scrolldiv_content').offsetHeight;
        var ph = document.getElementById('scrolldiv_parentContainer').offsetHeight;
        if(ch>ph) document.getElementById('scrolldiv_slider').style.display='block';  
        visibleContentHeight = document.getElementById('scrolldiv_scrollbar').offsetHeight ;
        contentHeight = document.getElementById('scrolldiv_content').offsetHeight - visibleContentHeight;        
        scrollHandleObj = document.getElementById('scrolldiv_theScroll');
        scrollHandleHeight = scrollHandleObj.offsetHeight;
        scrollbarTop = document.getElementById('scrolldiv_scrollbar').offsetTop;        
        document.getElementById('scrolldiv_theScroll').onmousedown = scrollDiv_startScroll;
        document.body.onmousemove = scrollDiv_scroll;
        document.getElementById('scrolldiv_scrollbar').onselectstart = cancelEvent;
        document.getElementById('scrolldiv_theScroll').onmouseup = scrollDiv_stopScroll;
        if(document.all)document.body.onmouseup = scrollDiv_stopScroll; else document.documentElement.onmouseup = scrollDiv_stopScroll;
        document.getElementById('scrolldiv_scrollDown').onmousedown = scrolldiv_scrollButton;
        document.getElementById('scrolldiv_scrollUp').onmousedown = scrolldiv_scrollButton;
        document.getElementById('scrolldiv_scrollDown').onmouseup = scrolldiv_scrollButtonStop;
        document.getElementById('scrolldiv_scrollUp').onmouseup = scrolldiv_scrollButtonStop;
        document.getElementById('scrolldiv_scrollUp').onselectstart = cancelEvent;
        document.getElementById('scrolldiv_scrollDown').onselectstart = cancelEvent;
        document.getElementById('scrolldiv_scrollbar').onmousedown = scrolldiv_scrollMoveToInit;
         
    }
    /*
    Change from the default color
    */    
    function scrolldiv_setColor(rgbColor)
    {
        document.getElementById('scrolldiv_scrollbar').style.borderColor = rgbColor;
        document.getElementById('scrolldiv_theScroll').style.backgroundColor = '#F4A6D7';
        document.getElementById('scrolldiv_scrollUp').style.borderColor = rgbColor;
        document.getElementById('scrolldiv_scrollDown').style.borderColor = rgbColor;
        document.getElementById('scrolldiv_scrollUp').style.color = rgbColor;
        document.getElementById('scrolldiv_scrollDown').style.color = rgbColor;
        document.getElementById('scrolldiv_parentContainer').style.borderColor = rgbColor;
    }
    /*
    Setting total width of scrolling div
    */
    function scrolldiv_setWidth(newWidth)
    {
        document.getElementById('dhtmlgoodies_scrolldiv').style.width = newWidth + 'px';
        document.getElementById('scrolldiv_parentContainer').style.width = newWidth-30 + 'px';        
    }
    
    /*
    Setting total height of scrolling div
    */
    function scrolldiv_setHeight(newHeight)
    {
        document.getElementById('dhtmlgoodies_scrolldiv').style.height = newHeight + 'px';
        document.getElementById('scrolldiv_parentContainer').style.height = newHeight + 'px';
        document.getElementById('scrolldiv_slider').style.height = newHeight + 'px';
        document.getElementById('scrolldiv_scrollbar').style.height = newHeight-40 + 'px';        
    }
    /*
    Setting new background color to the slider 
    */
    function setSliderBgColor(rgbColor)
    {
        document.getElementById('scrolldiv_scrollbar').style.backgroundColor = rgbColor;
        document.getElementById('scrolldiv_scrollUp').style.backgroundColor = rgbColor;
        document.getElementById('scrolldiv_scrollDown').style.backgroundColor = rgbColor;
    }
    /*
    Setting new content background color
    */
    function setContentBgColor(rgbColor)
    {
        document.getElementById('scrolldiv_parentContainer').style.backgroundColor = rgbColor;
    }
    
    /*
    Setting scroll button speed
    */
    function setScrollButtonSpeed(newScrollButtonSpeed)
    {
        scrollbuttonSpeed = newScrollButtonSpeed;
    }
    /*
    Setting interval of the scroll
    */
    function setScrollTimer(newInterval)
    {
        scrollTimer = newInterval;
    }
    function loadScroll(){
        scrolldiv_setColor('#FFFFFF');    // Setting border color of the scrolling content
        setSliderBgColor('#FFFFFF');    // Setting color of the slider div
        setContentBgColor('#FFFFFF');    // Setting color of the scrolling content
        setScrollButtonSpeed(1);    // Setting speed of scrolling when someone clicks on the arrow or the slider
        setScrollTimer(5);    // speed of 1 and timer of 5 is the same as speed of 2 and timer on 10 - what's the difference? 1 and 5 will make the scroll move a little smoother.
        scrolldiv_setWidth(530);    // Setting total width of scrolling div
        scrolldiv_setHeight(350);    // Setting total height of scrolling div
        scrolldiv_initScroll();    // Initialize javascript functions
    }
