var checkboxHeight = "25";
var radioHeight = "25";
var selectWidth = "118";
var form_string = "";

/* No need to change anything after this */

document.write('<style type="text/css">input.styled { display: none; } select.styled { position: relative; width: ' + selectWidth + 'px; filter: alpha(opacity=0);	-moz-opacity: 0; opacity: 0; z-index: 5; }</style>');

var Custom = {
	init: function() {
		var inputs = document.getElementsByTagName("input"), span = Array(), textnode, option, active;
		for(a = 0; a < inputs.length; a++) {
			if((inputs[a].type == "checkbox" || inputs[a].type == "radio") && inputs[a].className == "styled") {
				span[a] = document.createElement("span");
				span[a].className = inputs[a].type;

				if(inputs[a].checked == true) {
					if(inputs[a].type == "checkbox") {
						position = "0 -" + (checkboxHeight*2) + "px";
						span[a].style.backgroundPosition = position;
					} else {
						position = "0 -" + (radioHeight*2) + "px";
						span[a].style.backgroundPosition = position;
					}
				}
				inputs[a].parentNode.insertBefore(span[a], inputs[a]);
				inputs[a].onchange = Custom.clear;
				span[a].onmousedown = Custom.pushed;
				span[a].onmouseup = Custom.check;
				document.onmouseup = Custom.clear;
			}
		}
		inputs = document.getElementsByTagName("select");
		for(a = 0; a < inputs.length; a++) {
			if(inputs[a].className == "styled") {
				option = inputs[a].getElementsByTagName("option");
				active = option[0].childNodes[0].nodeValue;
				textnode = document.createTextNode(active);
				for(b = 0; b < option.length; b++) {
					if(option[b].selected == true) {
						textnode = document.createTextNode(option[b].childNodes[0].nodeValue);
					}
				}
				span[a] = document.createElement("span");
				span[a].className = "select";
				span[a].id = "select" + inputs[a].name;
				span[a].appendChild(textnode);
				inputs[a].parentNode.insertBefore(span[a], inputs[a]);
				inputs[a].onchange = Custom.choose;
			}
		}
	},
	pushed: function() {
		element = this.nextSibling;
		if(element.checked == true && element.type == "checkbox") {
			this.style.backgroundPosition = "0 -" + checkboxHeight*3 + "px";
		} else if(element.checked == true && element.type == "radio") {
			this.style.backgroundPosition = "0 -" + radioHeight*3 + "px";
		} else if(element.checked != true && element.type == "checkbox") {
			this.style.backgroundPosition = "0 -" + checkboxHeight + "px";
		} else {
			this.style.backgroundPosition = "0 -" + radioHeight + "px";
		}
	},
	check: function() {
		element = this.nextSibling;
		if(element.checked == true && element.type == "checkbox") {
			this.style.backgroundPosition = "0 0";
			element.checked = false;
		} else {
			if(element.type == "checkbox") {
				this.style.backgroundPosition = "0 -" + checkboxHeight*2 + "px";
			} else {
				this.style.backgroundPosition = "0 -" + radioHeight*2 + "px";
				group = this.nextSibling.name;
				inputs = document.getElementsByTagName("input");
				for(a = 0; a < inputs.length; a++) {
					if(inputs[a].name == group && inputs[a] != this.nextSibling) {
						inputs[a].previousSibling.style.backgroundPosition = "0 0";
					}
				}
			}
			element.checked = true;
		}
	},
	clear: function() {
		inputs = document.getElementsByTagName("input");
		for(var b = 0; b < inputs.length; b++) {
			if(inputs[b].type == "checkbox" && inputs[b].checked == true && inputs[b].className == "styled") {
				inputs[b].previousSibling.style.backgroundPosition = "0 -" + checkboxHeight*2 + "px";
			} else if(inputs[b].type == "checkbox" && inputs[b].className == "styled") {
				inputs[b].previousSibling.style.backgroundPosition = "0 0";
			} else if(inputs[b].type == "radio" && inputs[b].checked == true && inputs[b].className == "styled") {
				inputs[b].previousSibling.style.backgroundPosition = "0 -" + radioHeight*2 + "px";
			} else if(inputs[b].type == "radio" && inputs[b].className == "styled") {
				inputs[b].previousSibling.style.backgroundPosition = "0 0";
			}
		}
	},
	choose: function() {
		option = this.getElementsByTagName("option");
		for(d = 0; d < option.length; d++) {
			if(option[d].selected == true) {
				document.getElementById("select" + this.name).childNodes[0].nodeValue = option[d].childNodes[0].nodeValue;
			}
		}
	}
}

function ShowLanguages(nyelv) {
	
	text='';
	if (!(nyelv=='hun')) text='<a href="http://www.balatoniingatlan.com/" title="Balatoni ingatlanok" lang="hu">MAGYAR</a> | ';
	if (!(nyelv=='eng')) text=text+'<a href="http://www.balatoninvestments.net" title="Balaton properties" lang="en">ENGLISH</a>';
	if (nyelv=='hun') text=text+' | ';
	if (!(nyelv=='ger')) text=text+'<a href="http://www.balatonimmobilien.net" title="Balaton immobilien" lang="de">DEUTSCH</a>';
	
	document.write(text);
	
}

function change_tab(aktiv_tab) {
	$('#adatlap_erdeklodes').hide();
	$('#adatlap_adatok').hide();
	$('#adatlap_ajanlas').hide();
	$('#adatlap_projekt').hide();
	
	$('#adatlap_'+aktiv_tab).show();

	$('#erdeklodes_gomb').removeClass('aktiv');
	$('#adatok_gomb').removeClass('aktiv');
	$('#ajanlas_gomb').removeClass('aktiv');
	if ($('#projekt_gomb').size()>0) $('#projekt_gomb').removeClass('aktiv');
	
	$('#'+aktiv_tab+'_gomb').addClass('aktiv');
}
/*
function show_telep() {
	$('#telepulesek').removeClass('rejt');
	Element.clonePosition('telep_fedo', 'telepulesek', {setWidth: true, setHeight: true});
	$('#telep_fedo').hide();
}

function hide_telep() {
	$('#telepulesek').addClass('rejt');
	Element.clonePosition('telep_fedo', 'telepulesek', {setWidth: true, setHeight: true});
	$('#telep_fedo').show();
}
*/

top_win_closed_height = "25px";
top_win_loading = false;
top_win_mouse_on = false;
hiding_time = 0;

function top_win_hide_it() {
	if (hiding_time>0) {
		hiding_time = hiding_time-200;
		window.setTimeout('top_win_hide_it()', 100);
	}
	else if (hiding_time==0) $('.top_win_ll').stop().animate({ height: top_win_closed_height}, 1000);
}

function start_script() {
	
	Custom.init();
	if ($('.gallery').size()>0) make_gallery();
	
	//window.setTimeout('top_win_hide_it()', 500);
	/*$('#top_win').hover(
		function() {
			hiding_time = -1;
			$('.top_win_ll').stop().css("height", "auto");
			top_win_mouse_on = true;
		},
		function() {
			if (!top_win_loading) {
				hiding_time = 2000;
				top_win_hide_it();
			}
			top_win_mouse_on = false;
		}
	);*/
	
	if ($('#inkiri').size()>0) ajax_link('/form/kapcsolat');
	
	if ($('table.bi_form').size()>0) make_bi_form();
	
}

function make_bi_form() {
	
	
	$('table.bi_form td a').bind("click", function(e) {
												   	$(this).parent().children('a.selected').removeClass('selected');
													$(this).addClass('selected');
													return false;
												   })
	
}

function search_close() {
	$('.bf1').slideUp();
	$('.bf2').fadeIn();
	return false;
}

function search_open() {
	$('.bf2').fadeOut();
	$('.bf1').slideDown();
	return false;
}

function submit_form() {
	form_string="";
	$('table.bi_form a.selected').each(function() {
												if (form_string.length > 0) form_string+='&';
												form_string+=$(this).parent().attr('name')+'='+$(this).attr('name');
											})
	//window.alert(form_string);
	location.href = "/kereses/szures?"+form_string;
}

function switch_on_loader() {
	
	if (!$('#top_win').is(':visible')) {
		$('.top_win_content').hide();
		$('.top_win_loader').show();
		$('#top_win').show();
	} else {
		$('.top_win_loader').slideDown('fast');
		$('.top_win_content').slideUp('fast');
	}	
	top_win_loader = true;
	hiding_time = -1;
}

function switch_off_loader() {
	
	$('.top_win_loader').slideUp('fast');
	$('.top_win_content').slideDown('fast');
	top_win_loader = false;
/*	if (!top_win_mouse_on) {
		hiding_time = 5000;
//		top_win_hide_it();
	}*/
	
}

function switch_off_top_win(utasitas) {
	if (utasitas) $('#top_win').fadeOut('normal', window.location.reload());
	else $('#top_win').fadeOut('normal');
}

function ajax_process(address, variables) {
	
	if (variables != 0) variables += '&rnd=' + Math.floor(Math.random()*1000000) + '&m=' + active_menu;
	else variables = 'rnd=' + Math.floor(Math.random()*1000000) + '&m=' + active_menu;
	$.ajax({
		type: "POST",
		url: address,
		data: variables, 
		beforeSend: switch_on_loader()
   })
	
}

function validateForm(form_id) {

	var error = FALSE;
	
	//console.log('validate NOT_NULL')
	// NOT_NULL validator, input mezo nem lehet üres - késobb checkbox stb.
	$('#'+form_id+' input.NOT_NULL').each(function() {
		$(this).toggleClass( 'error', !validateNotNull($(this).val()));
		error = TRUE;
	});
	
	//console.log('validate VALID_EMAIL')
	// NOT_NULL validator, input mezo nem lehet üres - késobb checkbox stb.
	$('#'+form_id+' input.VALID_EMAIL').each(function() {
		$(this).toggleClass( 'error', !validateEmail($(this).val()));
		error = TRUE;
	});
	
	return !error;
	
}

function ajax_submit(form_id, url) {
	
	//console.log('submit');
	/*if (validateForm(form_id))*/ ajax_process(url, $('#'+form_id).serialize());
	/*
	$(this).toggleClass( 'error', !validateEmail($(this).val()));

	form = $('#'+id+' input').add('#'+id+' textarea').add('#'+id+' textarea');ajax_process('/ajax/login', jQuery.param(form));
	return false;*/
}

function validateEmail(elementValue){      
	var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
	return emailPattern.test(elementValue); 
}

function validateNotNull(elementValue) {
	return elementValue;
}

function ajax_link(address) {
	
	ajax_process(address, 0);
	
}

function make_gallery() {
	
		$("a.gallery").fancybox({
			'zoomOpacity'			: true,
			'overlayShow'			: true,
			'overlayOpacity'		: 0.4,
			'zoomSpeedIn'			: 500,
			'zoomSpeedOut'			: 500,
			'hideOnContentClick'	: false
		});
	
}

function toggleCheckbox(a) {
//	console.log("toggle");
	a.toggleClass("chb_sel");
	if (a.children().val() == '0') a.children().val('1');
	else a.children().val('0');
}


$(document).ready(function () {start_script();});