
var mX = 0, mY = 0;

var agent=navigator.userAgent.toLowerCase();
var is_iphone = ((agent.indexOf('iphone')!=-1)) || ((agent.indexOf('opera mini')!=-1)) || ((agent.indexOf('ipad')!=-1));


document.ondragstart =  function() {
    return false;
};


window.addEvent("domready",function(){        

    if (Custom) Custom.init();
    
    
	/* hide using opacity on page load */
	if ($('fb-modal')) {
    	$('fb-modal').setStyles({
    		opacity:0,
    		display:'block'
    	});
    	/* hiders */
    	$('fb-close').addEvent('click',function(e) { $('fb-modal').fade('out'); });
    	window.addEvent('keypress',function(e) { if(e.key == 'esc') { $('fb-modal').fade('out'); } });
    	$(document.body).addEvent('click',function(e) { 
    		if($('fb-modal').get('opacity') == 1 && !e.target.getParent('.generic_dialog')) { 
    			$('fb-modal').fade('out'); 
    		} 
    	});
    	/* click to show */
    	$('fb-trigger').addEvent('click',function() {
    		$('fb-modal').fade('in');
    	});
    }


    $$("img.swappable").each(function(el) {
        el.addEvents({
            "mouseenter": function () {
                if (el.getAttribute("src") != el.getAttribute("rel")) {
                    el.src = el.getAttribute("rel");
                }
            },
            "mouseleave": function () {
                el.src = el.getAttribute("rel").replace("2", "1");
            }
        })
    })
    
    
    if (is_iphone) {
        showThemAll();
        moveNewsInitial();
    } else {
        $("html").addEvents({
            "mousemove" : function(e){
                if (!started) {
                    var e = new Event(e);
    				if (mX != 0 && mY !=0){
    					if (mX!=e.page.x || mY!=e.page.y){
                            showThemAll();
    		                moveNewsInitial();
    		                started = true;
    					}
    				}
    				else{
    					mX = e.page.x;
    					mY = e.page.y;
    				}
                    
                }
            }
        })
    }
    
    
    $("l-button").addEvents({
        "click" : function (el) {
            inercija(1, 450);
        }
    })
    
    $("r-button").addEvents({
        "click" : function (el) {
            inercija(-1, 450);
        }
    })

    $$("span.expand", "div.expand").each(function(el){

        el.addEvents({
            "mouseenter" : function(){
                
                hideAllFloats(el);

                $("b-"+el.get("id")).fade("in");
            },
            "mouseleave" : function(){
            	$("b-"+el.get("id")).fade("out");
            }
        })
     })
    
    // sakrij box kad mis pomaknemo sa istog:
    $$("div.box1").each(function(el){
        el.addEvents({
            "mouseleave" : function(){
                $(el).fade("out");    
            }
        })
    });

    
    
    var fx = [];
    $$("div.news-item").each(function(el, indx){
        if (!fx[indx]){
            
            fx[indx] = new Fx.Tween("t-"+el.getChildren()[0].get("id"), {
            	property: 'margin-top',
            	duration: '500',
            	link: 'cancel',
            	transition: Fx.Transitions.Quart.easeOut
            });
        }
        el.addEvents({
            "mouseenter" : function(){
                fx[indx].start(0,-50);
            },
            "mouseleave" : function(){
                fx[indx].start(-50, 0); 
            }   
        })
    })

    $("knob").makeDraggable({
        onComplete: function() {
            inercija(null,null);
        },
        onDrag: function() {
        },
        modifiers:{y:false}
    }); 

    $("knob").setStyle("top", "0px");
    
    
    
    
    if ($('q')) {
    	var inputWord = $('q');
    	new Autocompleter.Request.HTML(inputWord, 'AutoComplete.aspx', {
    		'indicatorClass': 'autocompleter-loading', // class added to the input during request
    		'multiple': false,
            'selectFirst': true,
            'selectMode': false,
            'minLength': 2
    	});
	} 
	
	if ($("frmSrch")) {
    	$("btnSrch").addEvents({
            "click" : function() {
                $("frmSrch").submit();
            }
        })
	}
	
	if($("prijava3")){

		$("prijava3").addEvent("click", function(){
		    var ar = $$("input[name=email2]");
			if(ar[0].value!="" && ar[1].value=="") ar[1].value=ar[0].value;
		})
	}

})

/* config start */
var dx = 100;
var xMin = 0;
var xMax = 2020 - 960;
var current_left = xMin;
var started = false;
var inercijaFx;
var _pravac;
var _koliko;
var _xPos;
// xMax je ovdje 1600, a -500 da ne ode iz vidljivog dijela
/* config end */

        
function inercija(pravac, koliko){
	// koji smjer?
	var direction = 1;
	var xPos = 0;
	var abs_diff;
	var pomak = dx;
	
	_pravac = pravac;
	_koliko = koliko;
	
	if (!inercijaFx){
		inercijaFx = new Fx.Tween('knob', {
			        	property: 'left',
			        	transition: Fx.Transitions.Quart.easeOut,
			    		link:'cancel'
			    		
			    		,
			        	onComplete: function() {
			                canMove = true;
			               /* if (_xPos<-1300 || _xPos>300){
			                	inercija(null,null);
			                	_pravac = null; _koliko = null; 
			                	
			                }*/
	        			}
	        			
	    			});
	}
	
	abs_diff =  Math.abs(parseInt($("knob").getStyle("left").replace("px","")) - current_left);
	if (abs_diff < 100) pomak = dx / 10; // inercija je manja ako je pomak misa manji
	//if (parseInt($("knob").getStyle("left").replace("px","")) < current_left) direction = -1;
	current_left = parseInt($("knob").getStyle("left").replace("px",""));
	
	
	if (pravac!=null) direction = pravac;
	if (koliko!=null) pomak = koliko;
	
	//console.log(current_left + " " + koliko + " " + xMin + " " + xMax);
	
	xPos = current_left + (pomak * direction);
	_xPos = xPos;
	
	if (current_left > xMin) {
	    xPos = xMin; 
	    current_left = xMin;
	} else if(current_left < -xMax) {
	    xPos = -xMax; 
	    current_left = -xMax;
	} 
	
	if (xPos<-1060) xPos = -1060;
	if (xPos>0) xPos = 0;
	
	//console.log(current_left + " koliko " + koliko + " xMin " + xMin + " xMax " + xMax + " xPos " + xPos);
	
	
	if (canMove) {
	    canMove = false;
	    inercijaFx.start(xPos);
	} 
}

var canMove = true;


    


//sakrij sav homepage content, osim nuzne sredine:
function killThemAll() {
    $("nav-wrapper").set("opacity", "0");
    $("radio-home").set("opacity", "0");
    $("footer").set("opacity", "0");
    $("news-wrapper").set("opacity", "0");
    $$("div.box1").each(function(el){
        el.set("opacity", "0");
    });
    $("l-button").set("opacity", "0");
    $("r-button").set("opacity", "0");
}

function showThemAll() {  
    $("nav-wrapper").fade("in");
    $("radio-home").fade("in");
    $("footer").fade("in");
    $("news-wrapper").fade("in");
    $("q").focus();
    $("l-button").fade("in");
    $("r-button").fade("in");
}


function moveNewsInitial() {
    new Fx.Tween(("knob"), {
    	property: 'left',
    	duration: '1000',
    	transition: Fx.Transitions.Quart.easeOut
    }).start(0); 
}

// sakrij sve float prozore:
function hideAllFloats(elem) {
    var elem = elem;
    $$("div.box1").each(function(el){
        if(el!=elem){
            el.fade("out");
        }
    }) 
}

