
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 ($('q')) {
        $('q').addEvents ({
            "click": function(e) {
                hideAllFloats(e);  
            },
            "mouseenter": function(e) {
                hideAllFloats(e);  
            }
        })
    }
	
	if ($('tip-chat-txt')) {
		Ajax('http://www.hgspot.hr/livezilla-virtual/image.php?id=01&txt=1', '', 'getChatStatus', '');
	}
	
	if ($$('.tip-starter')) {
        $$('.tip-starter').each(function(el) {
        	
            el.addEvents({
            	
                "mouseenter" : function (e) {
                    if ($('tip-' + el.id)) $('tip-' + el.id).setStyle("display", "block");    
                },
                "mouseleave" : function (e) {
                    if ($('tip-' + el.id)) $('tip-' + el.id).setStyle("display", "none");    
                }
            })
        })
    }


    // fiksaj visinu .fixed-home klase na temelju najviseg stupca na naslovnici:
    
    
    col2 = $("e-akcija") ? $("e-akcija").getSize().y : 0;
    
    maxH = 0;

    if (col2>maxH) maxH = col2;

    

    
    if ($("b-list-news")) {
        $("b-list-news").setAttribute("style", "height: " + maxH + "px;");
        $("e-cjenik").setAttribute("style", "height: " + maxH + "px;");
        $("d-list-info").setAttribute("style", "height: " + maxH + "px;");
        //$("d-poll").setAttribute("style", "height: " + maxH + "px;");
    }

    

    if ($$('.bla')) {
        $$('.bla').each(function(el){
            
            if (el.getAttribute("name") == "rbAnketa") {
                el.addEvents({
                    "click": function() {
                        //alert(el);
                        $('PollChoiceId').value = el.getAttribute("value");
                    }
                })
            }
        })
    }
    
    $$("p.d1").each(function(el) {
        el.addEvents({
            "click": function () {
                hideAllByClassName("d0", "d1");
                $("d-"+el.get("id")).setStyle("display", "block");
                el.setAttribute("class", el.className.replace("tab ", "tab-active "));
            }
        })
    })  
    
    
    $$("p.e1").each(function(el) {
        el.addEvents({
            "click": function () {
                hideAllByClassName("e0", "e1");
                $("e-"+el.get("id")).setStyle("display", "block");
                
                if ($("bottom-"+el.get("id"))) {
                	// sakrij sve bottomsa, ako ih ima:
					hideAll("bottoms");
					//pokazi pripadajuci bottom:
					$("bottom-"+el.get("id")).setStyle("display", "block");
				     $$(".tab-inverse-wrapper").each(function(el2) {
				     	el2.setStyle("display", "block");
					 })
				} else {
				     $$(".tab-inverse-wrapper").each(function(el2) {
				     	el2.setStyle("display", "none");
					 })
				}
                
                
                el.setAttribute("class", el.className.replace("tab ", "tab-active "));
                el.setAttribute("class", el.className.replace("tab-p ", "tab-active-p "));
            }
        })
    })
    
    
    $$("p.f1").each(function(el) {
        el.addEvents({
            "click": function () {
                hideAllByClassName("f0", "f1");
                $("f-"+el.get("id")).setStyle("display", "block");
                el.setAttribute("class", el.className.replace("tab ", "tab-active "));
                el.setAttribute("class", el.className.replace("tab-p ", "tab-active-p "));
            }
        })
    })  
    
    function hideAll(cls) {
	    $$("."+cls).each(function(el){
	        el.setStyle("display", "none");
	    });
	}
    
    
    function hideAllByClassName(cls, tabs_class) {
        $$("."+cls).each(function(el){
            el.setStyle("display", "none");
        });
        
        $$("."+tabs_class).each(function(el){
            el.setAttribute("class", el.className.replace("tab-active ", "tab "));
            el.setAttribute("class", el.className.replace("tab-active-p ", "tab-p "));
        }); 
    }

    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");
            }
        })
    })
    

    
    /*
    $("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(){
            	if ((Browser.Engine.name == "webkit") && (el.id == "prijavaxxx")) {
					// chrome, nemoj sakrivati, krivo detektira prelazak preko input boxova kao moueseleave sa box-a
				} else {
					if (el.id == "prijava") {
						$('b-prijava').fade("out");
					}else{
						$(el).fade("out");
					}
					
				}
            }
        })
     })
     
    // sakrij box kad mis pomaknemo sa istog:
    $$("div.box1").each(function(el){
        el.addEvents({
            "mouseleave" : function(){
            	if ((Browser.Engine.name == "webkit") && (el.id == "b-prijavaxxx")) {
					// chrome, nemoj sakrivati, krivo detektira prelazak preko input boxova kao moueseleave sa box-a
				} else {
					$(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;
	
	
	
	if (canMove) {
	    canMove = false;
	    inercijaFx.start(xPos);
	} 
}

var canMove = true;


    


//sakrij sav homepage content, osim nuzne sredine:
function killThemAll() {
    $("home-news-wrapper").set("opacity", "0");
    $("nav-wrapper").set("opacity", "0");
    $("footer").set("opacity", "0");
    $$("div.box1").each(function(el){
        el.set("opacity", "0");
    });
}


// sakrij sve float prozore:
function hideAllFloats(elem) {
    var elem = elem;
    $$("div.box1").each(function(el){
        if(el!=elem){
            el.fade("out");
        }
    }) 
}


