
var nev_kereses_min = 3;
var auto_kereses_min = 2;
var kereses_delay = 800;


jQuery.noConflict();

jQuery('.hiba').live('focus', function(){ jQuery(this).removeClass('hiba'); });
jQuery('.ui-draggable-dragging').live('mouseup', function(){ jQuery(this).remove(); });

var $box = null;
var $current_name = null;
var $current_auto = null;
var utolso_nev_kereses = null;
var utolso_auto_kereses = null;

var $nevKereses = null;
var $autoKereses = null;
	

var utolso_kereses = 0;
var engedelyezett_kereses = 0;

function close_eub_box()
{
	$box.fadeOut('slow', function(){
		$box.remove();
	});
	return true;
}

function eub_box(box_id)
{
	var $ = jQuery;
	
	$box = $('<div id="eub_box" style="display: none"><a href="#" id="close_eub_box" onclick="close_eub_box(); return true;">bezárás</a><div id="eub_box_content">'+$('#'+box_id).html()+'</div></div>');
	$('body').prepend($box);
	$box.fadeIn('slow');
}

function datepicker_loaded()
{
	var $ = jQuery;
	$('#tol, #ig').datepicker('destroy');
	$('#tol, #ig').datepicker({ dateFormat: 'yy-mm-dd', lang: 'hu' });
	
	$('#uttol, #utig, #foglalas, #foglalas_basic').datepicker('destroy');
	$('#uttol, #utig, #foglalas, #foglalas_basic').datepicker({ dateFormat: 'yy-mm-dd', lang: 'hu' });
	
}

function nev_beszuras(biztositott, cel_szuldatum)
{
	var $ = jQuery;

	$('input.id', cel_szuldatum).attr('value', $('input[name=id_bizt]', biztositott).attr('value'));
	$('input.nev', cel_szuldatum).attr('value', $('input[name=nev_bizt]', biztositott).attr('value'));

	$('input.ev', cel_szuldatum).attr('value', $('input[name=szuldatum_bizt]', biztositott).attr('value').substr(0,4));
	$('input.honap', cel_szuldatum).attr('value', $('input[name=szuldatum_bizt]', biztositott).attr('value').substr(5,2));
	$('input.nap', cel_szuldatum).attr('value', $('input[name=szuldatum_bizt]', biztositott).attr('value').substr(8,2));

	$('input.tel', cel_szuldatum).attr('value', $('input[name=telefon]', biztositott).attr('value'));
	$('input.email', cel_szuldatum).attr('value', $('input[name=email]', biztositott).attr('value'));
	
	$('input.cim', cel_szuldatum).attr('value', $('input[name=cim]', biztositott).attr('value'));
	$('input.iszam', cel_szuldatum).attr('value', $('input[name=iszam]', biztositott).attr('value'));
	$('input.varos', cel_szuldatum).attr('value', $('input[name=varos]', biztositott).attr('value'));
	$('input.utca', cel_szuldatum).attr('value', $('input[name=utca]', biztositott).attr('value'));
	
	$('input.passport', cel_szuldatum).attr('value', $('input[name=passport]', biztositott).attr('value'));
	$('input.kulfcim', cel_szuldatum).attr('value', $('input[name=kulfcim]', biztositott).attr('value'));
	$('input.huncim', cel_szuldatum).attr('value', $('input[name=huncim]', biztositott).attr('value'));
	
	$('input.user_id', cel_szuldatum).attr('value', $('input[name=user_id]', biztositott).attr('value'));
	$('input.szerz', cel_szuldatum).attr('value', $('input[name=szerz]', biztositott).attr('value'));
	
	$('textarea.cim', cel_szuldatum).html($('input[name=cim_bizt]', biztositott).attr('value'));
	$('input.kufoldicim', cel_szuldatum).attr('value', $('input[name=kufoldicim_bizt]', biztositott).attr('value'));
	$('textarea.kufoldicim', cel_szuldatum).html($('input[name=kufoldicim_bizt]', biztositott).attr('value'));
			
	$('input.allampolgarsag', cel_szuldatum).attr('value', $('input[name=allampolgarsag_bizt]', biztositott).attr('value'));

	if ($('input[name=orszag_id]', biztositott).attr('value') != '0')
		$('#orszag_szerzodo', cel_szuldatum).val($('input[name=orszag_id]', biztositott).attr('value'))
	
	$('input', cel_szuldatum).removeClass('hiba');
	
	$('input.nap', cel_szuldatum).keyup();
}

function auto_beszuras(auto, cel_auto)
{
	var $ = jQuery;
	$('input.rendsz_auto', cel_auto).attr('value', $('input[name=rendszam_auto]', auto).attr('value'));
	$('input.id_auto', cel_auto).attr('value', $('input[name=id_auto]', auto).attr('value'));
	$('input.gyev_auto', cel_auto).attr('value', $('input[name=evjarat_auto]', auto).attr('value'));
	
	$('input', cel_auto).removeClass('hiba');
	
	$('input.rendsz_auto', cel_auto).keyup();
}

function ajax_load_nevek(searchString, kid)
{
	if (kid != engedelyezett_kereses) return true;
	var $ = jQuery;
	$('.nevek .biztositottak').html('<i>Keresés...</i>');
	$.post('ajax_nevek.php', { search: searchString }, function(data){
		$('.nevek .biztositottak').html(data);
		if ($('.nevek .biztositott').length > 0)
		{
			$('.nevek .biztositott').draggable('destroy');
			$('.nevek .biztositott').draggable({
				scope: 'szemely',
				appendTo: 'body',
				helper: 'clone',
				scroll: false,
				revert: true,
				start: function(){ $current_name = $(this); }
			});
			
			$('.nevek .biztositott').each(function(j){
				$(this).unbind('dblclick');
				$(this).dblclick(function(){
					if ($('.szuldatum').length > 0)
					{
						szuldat = null;
						$('input.ev, input.honap, input.nap').each(function(i){
							if (!szuldat && $(this).attr('value') == '')
							{
								szuldat = $(this).parents('.szuldatum').get(0);
							}
						});
						if (szuldat)
						{
							if ($(szuldat).css('display') != 'none')
							{
								nev_beszuras(this, szuldat);
								$(this).remove();
							}
							else
							{
								alert('Nincs több szabad hely!');
								return false;
							}
						}
						else
						{
							alert('Nincs több szabad hely!');
							return false;
						}
					}
					else
					{
						szuldat = null;
						$('input.ev, input.honap, input.nap, input.nev').each(function(i){
							if (!szuldat && $(this).attr('value') == '')
							{
								szuldat = $(this).parents('.szemely').get(0);
							}
						});
						if (szuldat)
						{
							var szuldatId = $(szuldat).attr('id');
							if (szuldatId != 'szerzod' && szuldatId != 'dijfizet')
							{
								var idExists = false;
								var szemelyId = $('input[name=id_bizt]', $(this)).attr('value'); 
								$('input.id', $(szuldat).parent()).each(function(i){
									if (!idExists && szemelyId == $(this).attr('value'))
									{
										idExists = true;
									}		
								});
								if(idExists)
								{
									alert('Ezt a személyt már hozzáadta a biztosítottakhoz!');
									return;
								}					
							}	
		
							nev_beszuras(this, szuldat);
						}
						else
						{
							alert('Nincs több szabad hely!');
							return false;
						}
					}	
				});
			});
			
		}
	});
}

function ajax_load_autok(searchString, kid)
{
	if (kid != engedelyezett_kereses) return true;
	var $ = jQuery;
	$('.nevek .autok').html('<i>Keresés...</i>');
	$.post('ajax_autok.php', { search: searchString }, function(data){
		$('.nevek .autok').html(data);
		if ($('.nevek .auto').length > 0)
		{
			$('.nevek .auto').draggable('destroy');
			$('.nevek .auto').draggable({
				scope: 'jarmu',
				appendTo: 'body',
				helper: 'clone',
				zIndex: 300,
				scroll: false,
				revert: true,
				start: function(){ $current_auto = $(this); }
			});
			
			$('.nevek .auto').each(function(j){
				$(this).unbind('dblclick');
				$(this).dblclick(function(){
					auto = null;
					$('input.rendsz_auto').each(function(i){
						if (!auto && $(this).attr('value') == '')
						{
							auto = $(this).parents('.bizt_auto').get(0);
						}
					});
					if (auto)
					{
						auto_beszuras(this, auto);
						$(this).remove();
					}
					else
					{
						alert('Nincs több szabad hely!')
					}	
				});
			});
			
			
		}
	});
}


function droppable_unload()
{
	var $ = jQuery;
	$(".szuldatum").droppable('destroy');
	$(".szemely").droppable('destroy');
	$(".bizt_auto").droppable('destroy');
	$("#auto_dummy").droppable('destroy');
}


function adatok_loaded()
{
	var $ = jQuery;
	/* Nevek */
	if ($('.nevek .biztositottak').length > 0)
	{
				
		$current_name = null;
		$current_auto = null;
		utolso_nev_kereses = null;
		utolso_auto_kereses = null;
		
		utolso_kereses = 0;
		engedelyezett_kereses = 0;
		
		if ($nevKereses) $nevKereses.remove();
		if ($autoKereses) $autoKereses.remove();
		$nevKereses = jQuery('<form onsubmit="return false;"><label class="qs_label" for="qs_input_nev">Keresés:</label><input type="text" name="qs_input_nev" id="qs_input_nev" class="qs_input" /></form>');
		$autoKereses = jQuery('<form onsubmit="return false;"><label class="qs_label" for="qs_input_auto">Keresés:</label><input type="text" name="qs_input_auto" id="qs_input_auto" class="qs_input" /></form>');

		$('.nevek .biztositottak').before($nevKereses);
		$('.nevek .autok').before($autoKereses);
		
		$('#qs_input_nev').unbind('keyup');
		$('#qs_input_nev').keyup(function(e, ui){
			if (e.keyCode == 13) return;
			utolso_kereses++;
			engedelyezett_kereses = utolso_kereses;
			var search = $(this).attr('value');
			if (search != utolso_nev_kereses && (search.length == 0 || search.length >= nev_kereses_min))
			{
				utolso_nev_kereses = search;
				setTimeout('ajax_load_nevek("'+search+'",'+utolso_kereses+');', kereses_delay);
			}
		});
		
		$('#qs_input_auto').unbind('keyup');
		$('#qs_input_auto').keyup(function(e, ui){
			if (e.keyCode == 13) return;
			utolso_kereses++;
			engedelyezett_kereses = utolso_kereses;
			var search = $(this).attr('value');
			if (search != utolso_auto_kereses && (search.length == 0 || search.length >= auto_kereses_min))
			{
				utolso_auto_kereses = search;
				setTimeout('ajax_load_autok("'+search+'",'+utolso_kereses+');', kereses_delay);
			}
		});
		
		ajax_load_nevek('', engedelyezett_kereses);
		ajax_load_autok('', engedelyezett_kereses);
		
		$(".szuldatum").droppable({
			scope: 'szemely',
			activeClass: 'szuldatum-highlight',
			hoverClass: 'szuldatum-aktiv',
			tolerance: 'pointer',
			drop: function(event, ui) {
				nev_beszuras($current_name, this);
				//$current_name.remove();
				$current_name.hide();
				$current_name = null;
			}
		});
		
		$(".szemely").droppable({
			scope: 'szemely',
			activeClass: 'szuldatum-highlight',
			hoverClass: 'szuldatum-aktiv',
			tolerance: 'pointer',
			drop: function(event, ui) {
				var thisId = $(this).attr('id');
				if (thisId != 'szerzod' && thisId != 'dijfizet')
				{
					var idExists = false;
					var szemelyId = $('input[name=id_bizt]', $current_name).attr('value'); 
					$('input.id', $(this).parent()).each(function(i){
						if (!idExists && szemelyId == $(this).attr('value'))
						{
							idExists = true;
						}		
					});
					if(idExists)
					{
						alert('Ezt a személyt már hozzáadta a biztosítottakhoz!');
						return;
					}					
				}		
				nev_beszuras($current_name, this);
				// $current_name.remove(); 
			}
		});
		
		$(".bizt_auto").droppable({
			scope: 'jarmu',
			activeClass: 'szuldatum-highlight',
			hoverClass: 'szuldatum-aktiv',
			tolerance: 'pointer',
			drop: function(event, ui) {
				auto_beszuras($current_auto, this);
				//$current_auto.remove();
				$current_auto.hide();
				$current_auto = null;
			}
		});
		
		$("#auto_dummy").droppable({
			scope: 'jarmu',
			tolerance: 'pointer',
			drop: function(event, ui) {
				$current_auto = null;
			}
		});

		$('.nevek .autok').parent().hide();
	}
}

function dijszamitas_loaded()
{
	var $ = jQuery;

	var dij_offset = 0;
	$('#dijnext').click(function(){
		var dij_count = $('.dijszamitas').length;
		if (dij_offset < dij_count - 3)
		{
			if (dij_offset != dij_count - 3)
				dij_offset++;
			$('#dijszamitasok_belso').animate({'left' : '-' + (220 * dij_offset) + 'px'}, 1000);
			if (dij_offset == dij_count - 3)
				$('#dijnext').fadeTo(500, 0.1);
			$('#dijprev').fadeTo(500, 1);
		}
	});
	$('#dijprev').click(function(){
		var dij_count = $('.dijszamitas').length;
		if (dij_offset > 0)
		{
			dij_offset--;
			$('#dijszamitasok_belso').animate({'left' : '-' + (220 * dij_offset) + 'px'}, 1000);
			if (dij_offset == 0)
				$('#dijprev').fadeTo(500, 0.1);
			$('#dijnext').fadeTo(500, 1);
		}
	});
	$('#dijprev').fadeTo(500, 0.1);
	if ($('.dijszamitas').length < 4)
		$('#dijnext').fadeTo(500, 0.1);
	$('.reszletek').hide();
	$('.reszletek-link').click(function(){ $(this).next().slideToggle(); });
}


(function($){

	$.datepicker.regional['hu'] = {
		closeText: 'bezárás',
		prevText: '&laquo;&nbsp;vissza',
		nextText: 'előre&nbsp;&raquo;',
		currentText: 'ma',
		monthNames: ['Január', 'Február', 'Március', 'Április', 'Május', 'Június',
		'Július', 'Augusztus', 'Szeptember', 'Október', 'November', 'December'],
		monthNamesShort: ['Jan', 'Feb', 'Már', 'Ápr', 'Máj', 'Jún',
		'Júl', 'Aug', 'Szep', 'Okt', 'Nov', 'Dec'],
		dayNames: ['Vasámap', 'Hétfö', 'Kedd', 'Szerda', 'Csütörtök', 'Péntek', 'Szombat'],
		dayNamesShort: ['Vas', 'Hét', 'Ked', 'Sze', 'Csü', 'Pén', 'Szo'],
		dayNamesMin: ['V', 'H', 'K', 'Sze', 'Cs', 'P', 'Szo'],
		dateFormat: 'yy-mm-dd', firstDay: 1,
		isRTL: false};
	$.datepicker.setDefaults($.datepicker.regional['hu']);


	$(document).ready(function(){
	
		adatok_loaded();
		dijszamitas_loaded();
		
		/* Jobb box tabok */
		
		$('.belso .tartalom').hide();
		$('.belso').each(function(i){ $($('.tartalom', this).get(0)).show(); });
		
		$('.right-tab').live('click', function(){ 
			$(this).prev().attr('class', 'left-tab'); 
			$(this).attr('class', 'active-right-tab'); 
			$($('.tartalom', $(this).parent()).get(0)).hide();
			$($('.tartalom', $(this).parent()).get(1)).show();
		});
		$('.left-tab').live('click', function(){ 
			$(this).next().attr('class', 'right-tab'); 
			$(this).attr('class', 'active-left-tab'); 
			$($('.tartalom', $(this).parent()).get(1)).hide();
			$($('.tartalom', $(this).parent()).get(0)).show();
		});
		
		$('.login-field').focus(function(){ $(this).attr('value', ''); });

		
	});



})(jQuery);

