var cnt = 0;
var index = 0;
var top5fx = [];
var bot5fx = [];
var per;

window.addEvent("domready", function(){	
	
	
	var ieORwebkit = (window.ie || window.webkit420 | window.webkit419);
	
	if ($('mjestood')) {
    	var inputWord = $('mjestood');
    	new Autocompleter.Request.HTML(inputWord, 'ajax.aspx?action=getMjestaod', {
    		'indicatorClass': 'autocompleter-loading', // class added to the input during request
    		'multiple': false,
            'selectFirst': true,
            'selectMode': false,
            'minLength': 3
    	});
	} 
	
	$$("input.keyupclass").each(function(el) {
		el.addEvent(ieORwebkit ? 'keydown' : 'keypress', function (e) {
			var e = new Event(e);
			if(e.key && !e.shift && !e.control) {
				switch(e.key){
					case "tab":
						return;
						break;
					case "enter":
						e.stop();
						return;
						break;
				}
				$(el.id + "_id").value = "-1";
			}			
		})
	})
	
	
	if ($("bigImageRotator")){
	
	    $$("div.rotatorSlide").each(function(el, index){
	        if(el.getStyle("display")=="none"){
	            el.setStyles({"display": "block", "opacity": "0"});
	        }
	        if(index==0) el.setStyles({"display": "block", "opacity": "1"});
	        top5fx.push(new Fx.Tween(el, {duration:750, wait:false}));
	    })
	    
	    
	    $$("a.clickable").each(function(el, index){
	    
	        bot5fx.push(new Fx.Tween(el, {duration:750, wait:false}));
	        el.setStyle("cursor","pointer");
	        el.setProperty("res", index);
	         
	        el.addEvents({
	            "click":function(){
	                $clear(per);
	              
	               $$("a.active").each(function(ele){
	                 ele.removeClass("active");
	               });
	                
	                el.addClass("active");
	                $$("div.rotatorSlide").each(function(el, index){
	                    if(el.getStyle("opacity")!=0) {
	                        top5fx[index].start('opacity', 0);
	                    }
	                });		
	                			
	                top5fx[el.getProperty("res")].start('opacity', 1);	
	                
	                cnt=index;
	                per = fx.periodical(5000);
	            }
	        })
	    })
	    
	    per = fx.periodical(5000);
	}
	
	if ($("formaprijava")) {
		new FormCheck('formaprijava', {
		display : {
			errorsLocation : 1,
			indicateErrors : 2,
			showErrors : 0
			}
		});
	}
	
	if ($("snijegforma")) {
		new FormCheck('snijegforma', {
		display : {
			errorsLocation : 1,
			indicateErrors : 1,
			showErrors : 0
			},
	    	submit : false,
	    	onValidateSuccess : snijegprikazi
		});
	}
	
})

function snijegprikazi(){
		var putanja;
		putanja='ajax.aspx?action=prikazi&ulica=' + escape($('mjestood_id').value)

		
		var myRequest = new Request({
		    url:putanja,
		    method: 'get',
		    onRequest: function(){
		       $('spinnergif').setStyle('display','block')
		    },
		    onSuccess: function(responseText){
		    	$('spinnergif').setStyle('display','none')
		        $('opisulice').innerHTML=responseText;
		        $('uliceWrapper').setStyle('display','block');
			    $('backgroundPopup').setStyles({
				    display: 'block',
				    opacity: 0.8
				})
    			new Fx.Scroll(window).toElement($('bodytop')); 
		    },
		    onFailure: function(){
		        $('spinnergif').setStyle('display','none');
		        $('opisulice').innerHTML='Došlo je do greške';
		    }
		});
		myRequest.send()
		
	}
	

function checkNaseljeod(el){
	if ($("mjestood_id").value == "-1") {
    	el.errors.push("Nepostojeća ulica.");
    	return false;
    } else {
        return true;
    }
}

function fx(){
    cnt++;
    if (cnt==top5fx.length) cnt = 0;
    top5fx[cnt].start('opacity', 1);
   
    $$("a.active").each(function (el){
        el.removeClass("active");
    })
    $$("a.clickable")[cnt].addClass("active");
    $$("div.rotatorSlide").each(function(el, indx){
        if(el.getStyle("opacity")!=0){
            top5fx[indx].start('opacity', 0);
        }
    })
}
