$(document).ready(function(){
						   
	
	$('.sub_menu').hide(0);
	$('#country').hide();
	$('#town').hide();
	$('#ajaxLoading').hide();
	$('#submitFilter').hide();

	$(function() {
		$("#school_tab").tabs();
	});
	$(function(){
		$('.school_info').hover(function(){
			$(this).addClass('schoolOver');
		},function(){
			$(this).removeClass('schoolOver');
		});
	});	
	$(function(){
		$('.school_image').hover(function(){
			$(this).addClass('imageOver');
		},function(){
			$(this).removeClass('imageOver');
		});
	});	
	$(function(){
		$('.main_menu').mouseover(function(){
			id = $(this).attr('id');
			$('.sub_menu').hide(0);
			$('#sub_'+id).show('drop');
		});
	});
	$('.clickable').click(function(){
		if($(this).text() == 'Zavřít'){
			$(this).text('Více informací');
		}else{
			$(this).text('Zavřít');
		}
		var id = $(this).attr('id');
		$('#toggl'+id).toggle('slow');
	});
	$('.togglable').hide();
	$("table.price_table tr:even").addClass("even");
	$("table.price_table tr:odd").addClass("odd");
	
	$("#language").change(function(){
		var id = $(this).val();
		changeLanguage(id);
		
	});
	$("#country").change(function(){
		var id = $(this).val();
		changeCountry(id);
	});
	$("#town").change(function(){	   	
		var town = $(this).val();
		changeTown(town);
	});
	
	if(language){	
		changeLanguage(language);	
		
		if(country){	
			changeCountry(country);				
		}
	}
	$("#login").css({color: "#cccccc"}).focus(function(){
		$(this).css({color: "#000"});
		if($(this).val() == "Přihlašovací jméno"){
			$(this).val("");	
		}
		$(this).blur(function(){
			if($(this).val()==""){
				$(this).val("Přihlašovací jméno").css({color: "#cccccc"});
			}
		});
	});
	$("#password").css({color: "#cccccc"}).focus(function(){
		$(this).css({color: "#000"});
		if($(this).val() == "Heslo"){
			$(this).val("");	
		}
		$(this).blur(function(){
			if($(this).val()==""){
				$(this).val("Heslo").css({color: "#cccccc"});
			}
		});
	});
	$("#loginButton").click(function(){
		$("#container").append("<div class='login_overlay'></div><div class='login_dialog'>Přihlášení</div>");

		var pos = $(".login_dialog").position();
		var left = parseInt(pos.left - ($(".login_dialog").width())/2);
		var top = parseInt(pos.top - ($(".login_dialog").height())/2);
		$(".login_dialog").css({top : top, left : left});
	});
	
	$("#ajaxLoading").ajaxStart(function(){
		$(this).show();
	}).ajaxStop(function(){
		$(this).hide();
	});	
	
});
var language;
var country;
var town;
function changeLanguage(id){
	$('#country').fadeOut();
	$('#town').fadeOut();
	$.getJSON("/application/changeSchool.php",{id : id, action : "country"},function(msg){
			if(msg.country){
				$("#language option[value='0']").remove();
				$("#country option").remove();
				$("#country").append($("<option></option>").val("0").html("--- Země ---"));
				var array = msg.country.split("/*/");
				for(var i = 0;i<array.length;i++){
					var info = array[i].split("<;>");
					$("#country").append($("<option></option>").val(info[0]).html(info[1]));
				}
				$("#country").fadeIn();
				if(language == id){
					$("#language").val(language);
				}
			}
	});
}
function changeCountry(id){
	$('#town').fadeOut();
	$.getJSON("/application/changeSchool.php",{id : id, action : "town"},function(msg){
			if(msg.town){
				$("#country option[value='0']").remove();																	  
				$("#town option").remove();
				$("#town").append($("<option></option>").val("0").html("--- Město ---"));
				var array = msg.town.split("/*/");
				for(var i = 0;i<array.length;i++){
					var info = array[i].split("<;>");
					$("#town").append($("<option></option>").val(info[0]).html(info[1]));
				}
				$("#town").fadeIn();
				if(country == id){
					$("#country").val(country);
				}
				if(town){
					$("#town").val(town);	
				}
			}
	});
}
function changeTown(town){
	var lang = $("#language").val();
	var country = $("#country").val();
	$.getJSON("/application/changeSchool.php",{lang : lang, country : country, town : town, action : "url"},function(msg){
			window.location.href = msg.url;
	});
}


	

