$(document).ready(function() {
	$("#fm-calendar_tour").css({
		"width"		: 	"250px"
	});
	$("#fm-calendar_web")
	.focus(function() {
		if ($(this).val() == "") $(this).val("http://");
	})
	.blur(function() {
		if ($(this).val() == "http://") $(this).val("");
	});


	
	$("#fm-calendar_county").change(function() {
		var selected = $("#fm-calendar_county option:selected");
		if (selected.val() == "20") {
			$("#fm-calendar_city").html("<option value='392'>Budapest</option>");
		}
		else {
			$("#div-calendar_district").hide();
			$.ajax({
				type: "GET",
				url: "/ajax.php?mod=registration&cla=registration&fun=access&ajax=getCityFromMegyeID&pid="+selected.val(),
				success: function(answer){
					$("#fm-calendar_city").html(answer);	
				}
			});			
		}
	});	
	
	
	$('#fm-calendar_text').keyup(function() {
		var max = parseInt($(this).attr('maxlength'));
		if ($(this).val().length > max){
			$(this).val($(this).val().substr(0, $(this).attr('maxlength')));
		}
		$('.fm-calendar_text_charsRemaining').html('még ' + (max - $(this).val().length) + ' karaktert írhat');
	});

});



var news_calendar_monthcache = new Array();
var news_calendar_monthcache_page = new Array();
var news_calendar_city = Array();
var news_calendar_location = Array();
var news_calendar_actual_city = '';
var news_calendar_actual_location = '';

function news_calendar_load_month(year,month) {
	var cache_id = year + ':' + month;
	if (!news_calendar_monthcache[cache_id]) news_calendar_makeRequest("/index.php?mod=news_calendar&cla=news_calendar&fun=access&ajax=getcalendar:"+year+":"+month,'loadcalendar',cache_id);
	else document.getElementById('news_calendar').innerHTML = news_calendar_monthcache[cache_id];
}

function news_calendar_load_month_page(year,month) {
	var cache_id = year + ':' + month;
	if (!news_calendar_monthcache_page[cache_id]) news_calendar_makeRequest("/index.php?mod=news_calendar&cla=news_calendar&fun=access&ajax=getcalendar:"+year+":"+month+"&page",'loadcalendar_page',cache_id);
	else document.getElementById('news_calendar_page').innerHTML = news_calendar_monthcache_page[cache_id];
	document.getElementById('news_calendar_results').innerHTML = '';
}

function news_calendar_turn_eventtype(type) {
	var tmp_class = "event_topic_" + type;
	var elements = getElementsByClass(tmp_class);

	if (elements.length > 0) {
		var set = (elements[0].style.display == 'inline' ? 'none' : 'inline')

		for( var i = 0; i < elements.length; i++) {
			elements[i].style.display = set;
		} // for
	}
	else {
		var set = (document.getElementById('turn_status_'+type).innerHTML == "[on]" ? "none" : "inline");
	}

	news_calendar_makeRequest("/index.php?mod=news_calendar&cla=news_calendar&fun=access&ajax=turn_eventtype&type="+type+"&set="+set,'turn_eventtype','');
	document.getElementById('turn_status_'+type).innerHTML = (set == 'inline' ? "[on]" : "[off]");
}

function news_calendar_search(type,value) {
	if (type == 'country') {
		news_calendar_actual_city = '';
		news_calendar_actual_location = '';
		if (value == '0') {
			document.getElementById('search_city').style.display = 'none';
			document.getElementById('search_location').style.display = 'none';
		}
		else {
			document.getElementById('search_city').style.display = 'inline';
			document.getElementById('search_location').style.display = 'inline';
		}
	}
	if (type == 'city') {
		news_calendar_actual_city = value;
		news_calendar_actual_location = '';
	}
	if (type == 'location') news_calendar_actual_location = value;
	news_calendar_makeRequest("/index.php?mod=news_calendar&cla=news_calendar&fun=access&ajax=search&type="+type+"&value="+value,'search','');
}

/**/
function appendOptionLast(select_id,value,text) {
	var elOptNew = document.createElement('option');
	elOptNew.value = value;
	elOptNew.text = text;
	var elSel = document.getElementById(select_id);

	try {
		elSel.add(elOptNew, null); // standards compliant; doesn't work in IE
	}
	catch(ex) {
		elSel.add(elOptNew); // IE only
	}
}

function removeOptions(select_id) {
	var elSel = document.getElementById(select_id);
	var i;
	for (i = elSel.length - 1; i>=0; i--) {
		elSel.remove(i);
	}
}
function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if (node == null) node = document;
	if (tag == null) tag = '*';
	var elems = node.getElementsByTagName(tag);
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	var i = 0;
	for (var j = 0; j < elems.length; j++) {
		if ( pattern.test(elems[j].className) ) {
			classElements[i++] = elems[j];
		}
	}
	return classElements;
}

/**/
var news_calendar_ajax_answer;
function news_calendar_makeRequest(url,event,id) { // kérelem
 var http_request = false;
  if (window.XMLHttpRequest) { // Mozilla, Safari,...
   http_request = new XMLHttpRequest();
   if (http_request.overrideMimeType) {
    http_request.overrideMimeType('text/xml'); // ha nem xml alapú lapot töltünk be, és hibát ír, akkor ezt kell kikommentezni
   }
  } else if (window.ActiveXObject) { // IE
   try {
    http_request = new ActiveXObject("Msxml2.XMLHTTP");
   } catch (e) {
      try {
       http_request = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {}
     }
   }

   if (!http_request) {
    alert('Feladtam :( Nem tudok XMLHTTP példányt létrehozni');
            return false;
   }
    http_request.onreadystatechange = function() { news_calendar_alertContents(http_request,event,id); };
    http_request.open('GET', url, true);
    http_request.send(null);
}

/* feldolgozás */
function news_calendar_alertContents(http_request,event,id) {
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			news_calendar_ajax_answer=http_request.responseText;
			if (event == "loadcalendar") {
				document.getElementById('news_calendar').innerHTML = news_calendar_ajax_answer;
				news_calendar_monthcache[id] = news_calendar_ajax_answer;
			}
			else if (event == "loadcalendar_page") {
				document.getElementById('news_calendar_page').innerHTML = news_calendar_ajax_answer;
				//news_calendar_monthcache_page[id] = news_calendar_ajax_answer;
			}
			else if (event == "search") {
				//alert(news_calendar_ajax_answer);
				eval(news_calendar_ajax_answer);

				var i;
				removeOptions('search_city');
				appendOptionLast('search_city',0,'- Város -');
				for( i = 0; i < news_calendar_city.length ; i++) {
					appendOptionLast('search_city',news_calendar_city[i],news_calendar_city[i]);
					if (news_calendar_city[i] == news_calendar_actual_city) document.search.search_city[i + 1].selected = '1';
				} // for i

				removeOptions('search_location');
				appendOptionLast('search_location',0,'- Helyszín -');
				for( i = 0; i < news_calendar_location.length ; i++) {
					appendOptionLast('search_location',news_calendar_location[i],news_calendar_location[i]);
					if (news_calendar_location[i] == news_calendar_actual_location) document.search.search_location[i + 1].selected = '1';
				} // for i

				//window.location.href = window.location.href;
			}
		}
	}
}

