$(function() {
	
	$('#popup').fancybox({
		width: 600,
		height: 571,
		margin: 20,
		type: 'iframe',
		centerOnScroll: true,
		overlayColor: '#000',
		overlayOpacity: 0.5
	});
	$('#popup').click();
	
	content_height_fix();
	
	// menu hover
	$('#menu li').hover(function() {
		$(this)
			.children('ul')
			.stop(true, true)
			.slideDown(800, 'easeOutElastic');
	}, function() {
		$(this)
			.children('ul')
			.stop(true, true)
			.slideUp(600, 'easeOutBounce');
	});
	
	// index slider
	$('#index_slider').bxSlider({
		prevText: 'előző',
		nextText: 'következő',
		auto: true,
		pager: true,
		pause: 6000
	});
	
	// error vagy succes üzenet alert (globálisan)
	if( $('.error').height() > 0 ) {
		alert( $('.error').html().replace(/<br>/g, '') );
	}
	else if( $('.success').height() > 0 ) {
		alert( $('.success').html().replace(/<br>/g, '') );
	}
	
	// hasznos tanácsok slide
	$('.blog .blogpost span.more').click(function() {
		
		// ha rejtve van
		if( $(this).html() == 'Részletek »' ) {
			// becsukom a többi nyitottat
			$('.blog .blogpost span.more').html( 'Részletek »' );
			$('.blog .blogpost .hidden').hide();
			
			$(this).html( '« Részletek' );
			$(this).parent('.blogpost').children('.hidden').show();
		}
		// ha látszik
		else if( $(this).html() == '« Részletek' ) {
			$(this).html( 'Részletek »' );
			$(this).parent('.blogpost').children('.hidden').hide();
		}
		content_height_fix();
		
	});
	
	// tanfolyamok jelentkezés
	$('#cancel').click(function() {
		$('.course').show();
		$('form').hide();
		content_height_fix();
		return false;
	});
	$('.course').click(function() {
		$('.course').show();
		$(this).hide();
		$('form').show();
		$('html, body').animate({ scrollTop: $('#appereance').offset().top }, 1000);
		
		// beírjuk a form-ba
		$('form .dij').html( $(this).parent('td').parent('tr').children('.hidden').children('.dij').html() );
		$('form .idotartam').html( $(this).parent('td').parent('tr').children('.hidden').children('.idotartam').html() );
		$('form .idopont').html( $(this).parent('td').parent('tr').children('.second').html() );
		$('form .varos').html( $(this).parent('td').parent('tr').children('.first').html() );
		
		// beírjuk a hidden form-ba
		$('form input[name=dij]').val( $(this).parent('td').parent('tr').children('.hidden').children('.dij').html() );
		$('form input[name=idotartam]').val( $(this).parent('td').parent('tr').children('.hidden').children('.idotartam').html() );
		$('form input[name=idopont]').val( $(this).parent('td').parent('tr').children('.second').html().replace(/\./g, '').replace(/ /g, '-') );
		$('form input[name=varos]').val( $(this).parent('td').parent('tr').children('.first').html() );
		
		content_height_fix();
		$('#name').focus();
		return false;
	});
	
	// infield label
	$('label.infield').infieldLabel();
	
	// fancybox
	$('a.fancybox').fancybox({
		loop: false,
		closeBtn: false,
		helpers: {
			title: { type: 'inside' },
			buttons: {}
		}
	});
	$('a.youtube').fancybox({
		width: 640,
		height: 360
	});
	
	// change change
	$('.index #tanfolyamok').click(function() {
		if( $('.index .tanfolyamok').css('display') != 'block' ) {
			
			$('.index .change').stop().fadeOut(250);
			$('.index .tanfolyamok').stop().fadeIn(250);
			
		}
		return false;
	});
	
	$('.index #kulfoldi_munkak').click(function() {
		if( $('.index .kulfoldi_munkak').css('display') != 'block' ) {
			
			$('.index .change').stop().fadeOut(250);
			$('.index .kulfoldi_munkak').stop().fadeIn(250);
			
		}
		return false;
	});
	
	// index spot click
	$('.cities li .spot').click(function() {
		if( !$(this).parent('li').hasClass('noclick') ) {
			fadeOut( $('.cities li .tooltip') );
			if( $(this).parent('li').children('.tooltip').css('display') == 'none' ) {
				fadeIn( $(this).parent('li').children('.tooltip') );
			}
		}
		return false;
	});
	$('.cities li .tooltip .close').click(function() {
		fadeOut( $('.cities li .tooltip') );
	});
	
	$('.cities li .spot').hover(function() {
		$(this).parent().children('.city').addClass('hover');
	}, function() {
		$(this).parent().children('.city').removeClass('hover');
	});
	
	// index tanfolyamok checkbox
	$(window).ready(function() {
		$('.index .cities li').each(function() {
			// ha az oldal betöltött, aszinkron módon betöltöm a tanfolyam listákat a boxokba
			var base = $(this);
			var this_class = $(this).attr('class');
			$.ajax({
				url: '/cd-ajax-load/kulfoldi-munkaajanlatok_init',
				data: 'varos=' + this_class,
				type: 'POST',
				
				success: function(result) {
					// az eredményt simán bemásolom ebbe a tanfolyamok_listába (ha nincs egy tanfolyam se, akkor noclick class hozzáadása az li-hez)
					if( result == '' )
						base.addClass('noclick');
					else
						base.children('.tooltip').children('.tooltip_content').children('.tanfolyamok_lista').html( result );
				}
			});
		});
	});
	
	// ajax-al betöltöm a kattintott tanfolyam tartalmát
	$('.index .cities li .tooltip .tanfolyamok_lista a').live('click', function() {
		var links = $(this).parent();
		var base = $(this).parent().parent().children('.tanfolyam_adatok');
		var this_id = $(this).attr('id').substr(4);
		var this_varos = $(this).parent().parent().parent().parent().attr('class');
		var this_job_name = $(this).html();
		
		base.children('h2').html( this_job_name );
		
		$.ajax({
			url: '/cd-ajax-load/kulfoldi-munkaajanlatok',
			data: 'id=' + this_id + '&varos=' + this_varos,
			dataType: 'JSON',
			type: 'POST',
			
			success: function(result) {
				
				base.children('.price').children('span').html( result.dij );
				base.children('.duration').children('span').html( result.idotartam );
				base.children('a.tematics').attr( 'href', result.menu_path );
				base.children('a.date').attr( 'href', result.menu_path + '#idopontok' );
				base.children('div.date').children('span').html( result.idopont );
				base.children('a.regist').attr( 'href', result.menu_path + '#idopontok' );
				
				links.slideUp(250);
				base.slideDown(250);
				
			}
		});
		
		return false;
	});
	
	$('.index .cities li .tooltip .tanfolyam_adatok a.back').click(function() {
		$(this).parent().parent().slideUp(250);
		$(this).parent().parent().parent().children('.tanfolyamok_lista').slideDown(250);
		return false;
	});
	/*$('.index .selector input').each(function() {
		// átszínezem a hozzá tartozó szöveget
		$(this).change(function() {
			if( $(this).is(':checked') ) {
				$('.selector label').removeClass('checked');
				$('.selector label[for=' + $(this).attr('id') + ']').addClass('checked');
			}
		});
	});
	$('.index .selector input[type=radio]').change(function() {
		
		$('.cities li')
			.addClass('noclick')
			.animate({
				opacity: 0.5
			}, 500);
		
		var this_id = $(this).attr('id');
		var job_name = $('label[for=' + this_id + ']').html();
		
		// lekérdezem, hogy abban a városban hol van tanfolyam
		$('.cities li h2, .cities li .spot').html( job_name );
		
		$.ajax({
			url: '/cd-ajax-load/kulfoldi-munkaajanlatok',
			data: 'id=' + this_id.replace(/selector_/g, ''),
			dataType: 'JSON',
			type: 'POST',
			
			success: function(result) {
				
				$('.cities li a.tematics').attr( 'href', result.menu_path );
				$('.cities li a.regist').attr( 'href', result.menu_path + '#idopontok' );
				$('.cities li a.date').attr( 'href', result.menu_path + '#idopontok' );
				
				if( result.budapest != undefined )
					show_this( 'budapest', result.budapest )
				if( result.bekescsaba != undefined )
					show_this( 'bekescsaba', result.bekescsaba )
				if( result.szeged != undefined )
					show_this( 'szeged', result.szeged )
				if( result.kecskemet != undefined )
					show_this( 'kecskemet', result.kecskemet )
				if( result.szolnok != undefined )
					show_this( 'szolnok', result.szolnok )
				if( result.debrecen != undefined )
					show_this( 'debrecen', result.debrecen )
				if( result.nyiregyhaza != undefined )
					show_this( 'nyiregyhaza', result.nyiregyhaza )
				if( result.miskolc != undefined )
					show_this( 'miskolc', result.miskolc )
				if( result.eger != undefined )
					show_this( 'eger', result.eger )
				if( result.tatabanya != undefined )
					show_this( 'tatabanya', result.tatabanya )
				if( result.szekesfehervar != undefined )
					show_this( 'szekesfehervar', result.szekesfehervar )
				if( result.veszprem != undefined )
					show_this( 'veszprem', result.veszprem )
				if( result.szekszard != undefined )
					show_this( 'szekszard', result.szekszard )
				if( result.pecs != undefined )
					show_this( 'pecs', result.pecs )
				if( result.kaposvar != undefined )
					show_this( 'kaposvar', result.kaposvar )
				if( result.nagykanizsa != undefined )
					show_this( 'nagykanizsa', result.nagykanizsa )
				if( result.zalaegerszeg != undefined )
					show_this( 'zalaegerszeg', result.zalaegerszeg )
				if( result.szombathely != undefined )
					show_this( 'szombathely', result.szombathely )
				if( result.gyor != undefined )
					show_this( 'gyor', result.gyor )
				if( result.salgotarjan != undefined )
					show_this( 'salgotarjan', result.salgotarjan )
				
			}
		});
		
	});*/
	
	// form validation
	index_newsletter();
	
	// tanfoylamok jelentkezés
	courses();
	
	// munkalehetőség jelentkezés
	jobs();
	
});

function content_height_fix() {
	$('.content_container').css( 'min-height', 0 );
	$('.subpage .sidebar').css( 'min-height', 0 );
	// content height fix
	if( $('.subpage .sidebar').outerHeight(true) > $('.content_container').outerHeight(true) ) {
		$('.content_container').css( 'min-height', $('.subpage .sidebar').outerHeight(true) - 50 );
	}
	// subpage sidebar fix
	else if( $('.subpage .sidebar').outerHeight(true) < $('.content_container').outerHeight(true) ) {
		$('.subpage .sidebar').css( 'min-height', $('.content_container').outerHeight(true) );
	}
}

/*function show_this( city, result ) {
	
	$('.cities li.' + city + ' .price span').html( result.dij );
	$('.cities li.' + city + ' .duration span').html( result.idotartam );
	$('.cities li.' + city + ' div.date span').html( result.idopont );
	
	$('.cities li.' + city)
		.removeClass('noclick')
		.animate({
			opacity: 1
		}, 500);
	
}*/

function index_newsletter() {
	
	$('.index .newsletter input[type=submit]').click(function() {
		
		var error = false;
		var email = $('#email').val();
		
		// email
		if( !checkEmailAddress(email) ) {
			$('.newsletter #email').colorHighlight({ 'animateColor': false, 'animateBackgroundColor': true });
			error = true;
		}
		
		if( error )
			return false;
		return true;
		
	});
	
}

function courses() {
	
	$('.text_page #appereance input[type=submit]').click(function() {
		
		var error = false;
		var name = $('#name').val();
		var city = $('#city').val();
		var address = $('#address').val();
		var phone = $('#phone').val();
		var email = $('#email').val();
		
		// name
		if( !validateFullName(name) ) {
			$('#name').colorHighlight({ 'animateColor': false, 'animateBackgroundColor': true });
			error = true;
		}
		
		// város
		if( empty(city) ) {
			$('#city').colorHighlight({ 'animateColor': false, 'animateBackgroundColor': true });
			error = true;
		}
		
		// cím
		if( empty(address) ) {
			$('#address').colorHighlight({ 'animateColor': false, 'animateBackgroundColor': true });
			error = true;
		}
		
		// mobil
		if( empty(phone) || phone.length < 7 || phone.length > 15 ) {
			$('#phone').colorHighlight({ 'animateColor': false, 'animateBackgroundColor': true });
			$('p.phone label.infield').colorHighlight({ 'color': '#fff' });
			error = true;
		}
		
		// email
		if( !checkEmailAddress(email) ) {
			$('#email').colorHighlight({ 'animateColor': false, 'animateBackgroundColor': true });
			error = true;
		}
		
		if( error )
			return false;
		return true;
		
	});
	
}

function jobs() {
	
	$('.blog_subpage #appereance input[type=submit]').click(function() {
		
		var error = false;
		var name = $('#name').val();
		var email = $('#email').val();
		var year = $('#year').val();
		var city = $('#city').val();
		var phone = $('#phone').val();
		//var cv = $('#cv').val();
		
		// name
		if( !validateFullName(name) ) {
			$('#name').colorHighlight({ 'animateColor': false, 'animateBackgroundColor': true });
			error = true;
		}
		
		// email
		if( !checkEmailAddress(email) ) {
			$('#email').colorHighlight({ 'animateColor': false, 'animateBackgroundColor': true });
			error = true;
		}
		
		// születési év
		if( year < 1900 || year > 2000 ) {
			$('#year').colorHighlight({ 'animateColor': false, 'animateBackgroundColor': true });
			error = true;
		}
		
		// város
		if( empty(city) ) {
			$('#city').colorHighlight({ 'animateColor': false, 'animateBackgroundColor': true });
			error = true;
		}
		
		// mobil
		if( empty(phone) || phone.length < 7 || phone.length > 15 ) {
			$('#phone').colorHighlight({ 'animateColor': false, 'animateBackgroundColor': true });
			$('p.phone label.infield').colorHighlight({ 'color': '#fff' });
			error = true;
		}
		
		if( error )
			return false;
		return true;
		
	});
	
}

function fadeIn( base ) {
	
	var default_bottom_pos = 38;
	var animated_bottom_pos = 48;
	var animation_time = 250;
	
	base
		.css({
			display: 'block',
			bottom: default_bottom_pos,
			opacity: 0
		})
		.stop()
		.animate({
			bottom: animated_bottom_pos,
			opacity: 1
		}, animation_time);
	
}

function fadeOut( base ) {
	
	var animated_bottom_pos = 48;
	var animation_time = 250;
	
	base
		.animate({
			bottom: animated_bottom_pos + 10,
			opacity: 0
		}, animation_time, function() {
			$(this).css('display', 'none');
		});
	
}

