//****************************************** METODY *****************************************************************

//*************************************** J Q U E R Y ****************************************************************
$(document).ready(function() {
	  
	setSliderPosition(sliderPosition);
	
	$("body").click(function(e){
		  var target = $(e.target);
	
		  //you can then check what has been clicked
		  if(!target.is("#userName")){			   
			   if ( $("#userName") && $("#userName").val()=="" ){
			   
				    $("#userName").val('Uživatelské jméno');
			   }
		   }
	});
	
	quickSearchInit('searchInput','searchContent');
	quickSearchInit('searchInput2','searchContent2');
	
	//nastaví šířku meny
	setMenuWidth();
	
});

function setMenuWidth(){
	var size = $("div#header ul li.main").length;
	var elmSize = Math.floor((1000-44-size)/size);
	
	$("div#header ul li.main a.main").css("width",elmSize);
	$("div#header ul li.main div.new").css("width",elmSize);
}

function quickSearchInit(searchInput,searchContent){

	if(document.getElementById(searchInput)){
 	
 		$('input#'+searchInput).quicksearch('ul.'+searchContent+' li');
		
		$('input#'+searchInput).focus(function () {
			$('ul.'+searchContent).show();
		 	$('ul.'+searchContent+' li').show();
		});
		
 		$("body").click(function(e){
		  //you can then check what has been clicked
		   var target = $(e.target);
  
		   if(!target.is("input#"+searchInput)){
			   if($('ul.'+searchContent).is(':visible')){
		 			$('ul.'+searchContent).hide();
		 			$('ul.'+searchContent+' li').hide();
		 	   }
	 	   }
		});
		 		
 	}
}

function addSchool(element){
    
    var title    = $(element).find("input[name='schoolTitle']").val();
    var schoolID = $(element).find("input[name='schoolID']").val();
    
    var cont = true;
    $('div.selected-content ul.selected-list li').each(function(index) {
	    var alredyVal = $(this).find('input').val();	    	
	    if(alredyVal == schoolID){
	    		cont = false;
	    }
	});
    if(cont){
    	$("input[name=sID]").val(schoolID);
    	$("input#searchInput").val(title);
    }else{
    	jAlert('Nemůžete zvolit školu stejnou jako je název nejbližší školy!','Upozornění');
    }
}

function setSliderPosition(position){
	if(position != null){
		var animateTo = 0;
		switch(position)
		{
		case 1:
		 	break;
		case 2:
			animateTo = 149;
		  	break;
		case 3:
			animateTo = 298;
		  	break;
		case 4:
			animateTo = 447;
		  	break;
		case 5:
			animateTo = 596;
		  	break;
		}
		
		$('div.slider-div').animate({ left: animateTo },{
		     duration:1500,
		     step: function(now, fx){
		     	removeSlideClasses();
		     	if(now < 75)$('li.school-sign').addClass("selected");
		     	if(now > 75 && now < 224)$('li.school-round').addClass("selected");
		     	if(now > 224 && now < 373)$('li.region-round').addClass("selected");
		     	if(now > 373 && now < 522)$('li.pre-final-round').addClass("selected");
		     	if(now > 522)$('li.final-round').addClass("selected");
		     	
		      }
		  });
	}
}

function removeSlideClasses(){
	$('li.school-sign').removeClass("selected");	
	$('li.school-round').removeClass("selected");	
	$('li.region-round').removeClass("selected");	
	$('li.pre-final-round').removeClass("selected");	
	$('li.final-round').removeClass("selected");
}
function nameInputFocused(element){

	if( $(element).val() == "Uživatelské jméno") $(element).val('');
	
}
function passwordInputFocused() {
    $('#fakepassword').hide();
    $('#password').show();
    $('#password').focus();
}

function passwordInputBlurred() {
    if ($('#password').attr('value') == '') {
        $('#password').hide();
        $('#fakepassword').show();
    }
}

function addNearestSchool(thisClick){
	
	var schoolID 	= $(thisClick).find("input[name='schoolID']").val();
	var schoolTitle = $(thisClick).find("input[name='schoolTitle']").val();
	
	var cont = true;
	
	
		$('div.selected-content ul.selected-list li').each(function(index) {
	    	var alredyVal = $(this).find('input').val();	    	
	    	if(alredyVal == schoolID){
	    		cont = false;
	    	}
	    });
	    
	    if(cont){
	    	
	    	if($("input[name=sID]").val() != schoolID){
				var count = $("div.selected-content ul.selected-list > li").size();
				if( (count%2) == 0 ) color = "c2ddeb";
				else				 color = "ffffff";		
				
				$('div.selected-content  ul.selected-list').append('<li style="background-color:#'+color+';"><a class="del" onclick="removeNearestSchool(this);return false;" title="Odebrat" href=""></a><span>'+schoolTitle+'</span><input type="hidden" value="'+schoolID+'" name="nearestSchools[]"/><div class="clear-left" ></div></li>');
			}else{
				jAlert('Nemůžete zvolit nejbližší školu stejnou jako je název přihlašované školy!','Upozornění');
			}
		}else{
			jAlert('Tato škola je již vybraná!','Upozornění');
		}
		$('input#school').val('');
	
}
function removeNearestSchool(thisClick){
	$(thisClick).parent().remove();
}


function getSchools(){

	var region = $("select[name=region]").val();
	var city   = $("select[name=city]").val();

	$.ajax({
	    type: "POST",
		url: "/helper/get-school",
		data: "region="+region+"&city="+city,
		success: function(data){  			
		    $('ul.searchContent').html(data);		
		}
		
	}); 
}

function getCity(){

	var region = $("select[name=region]").val();
	
	$.ajax({
	    type: "POST",
		url: "/helper/get-city",
		data: "region="+region,
		success: function(data){  			
		    $('div.city').html(data);		
		}
		
	});
}
function showTeam(elm){
	
	$("div."+elm).animate({
	    height: 'toggle'
	  	}, 1000, function() {
	    // Animation complete.
  });
	
	
}

function refresh(){

	setInterval(function() {
        refreshOnlineReport();
        
        
   	}, 300000);
}
function refreshOnlineReport(){

	$.ajax({
	    type: "POST",
		url: "/helper/refresh-online-report",
		success: function(data){  			
		    $('div.overflow-div').html(data);		
		}
		
	}); 
	
}
