$.ec = {};
$.ec.forms = {};
$.ec.forms.inquiry = {};
$.ec.forms.inquiry.rules = {};
$.ec.forms.inquiry.messages = {};
$.ec.forms.apply_offer = {};
$.ec.forms.apply_offer.rules = {};
$.ec.forms.apply_offer.messages = {};

$.ec.forms.invalidHandler = function(form, validator) {
  	$('#error-notification').removeClass('hidden');
};

$.ec.forms.errorPlacement = function(error,element){
	element.parents('.field-wrapper').addClass('error');	
};

$.ec.updateStepState = function(step){
	$('.step-state li').removeClass('active');
	$('.step-state .'+step).addClass('active');
};


$.datepicker.regional['pl'] = {clearText: 'Wyczyść', clearStatus: 'Wyczyść obecną datę',
		closeText: 'Zamknij', closeStatus: 'Anuluj',
		prevText: '<poprzedni', prevStatus: 'Przejdź do poprzedniego miesiąca',
		nextText: 'następny>', nextStatus: 'Przejdź do następnego miesiąca',
		currentText: 'Teraz', currentStatus: 'Przejdź do obecnego miesiąca',
		monthNames: ['Styczeń','Luty','Marzec','Kwiecień','Maj','Czerwiec',
		'Lipiec','Sierpień','Wrzesień','Paźdzernik','Listopad','Grudzień'],
		monthNamesShort: ['Sty','Lut','Mar','Kwi','Maj','Cze','Lip','Sie','Wrz','Paź','Lis','Gru'],
		monthStatus: 'Pokaż inny miesiąc', yearStatus: 'Pokaż inny rok',
		weekHeader: 'Tyg', weekStatus: 'Tydzień w roku',
		dayNames: ['Poniedziałek','Wtorek','Środa','Czwartek','Piątek','Sobota','Niedziela'],
		dayNamesShort: ['Pon.','Wt.','Śr.','Czw.','Pt.','Sob.','Nd.'],
		dayNamesMin: ['Pon.','Wt.','Śr.','Czw.','Pt.','Sob.','Nd.'],
		dayStatus: 'Ustaw DD jako pierwszy dzień tygodnia', dateStatus: 'Ustaw DD, MM d',
		dateFormat: 'dd/mm/yy', firstDay: 0, 
		initStatus: 'Wybierz datę', isRTL: false};



$(function(){


	$('body').addClass('delay-load');

	if ($('#page').is('.pl'))
		$('.calendar input').datepicker($.datepicker.regional['pl']);
	
	
	$('.calendar input').datepicker();
	
	$('#tab-facets li').click(function(){
		
		$('#tab-facets li').removeClass('active');
		$(this).addClass('active');
		
		
		var section_id = $(this).find('a').attr('href');
		
		$('#tab-items-wrapper .tab-item').each(function(index,element){
		if ('#'+$(element).attr('id') == section_id){
				$(element).parents('.masked').animate({'marginLeft' : (index*(-960)) });
			}
		});
		
		return false;
		
		
		
		});
		
		
	$('#top-menu li').mouseenter(function(){
		$(this).find('.sub-menu').fadeIn(100);
	}).mouseleave(function(){
		$(this).find('.sub-menu').fadeOut(100);
	});
	
	
	$('#clients-slider').slider({
			min:1,
			max:12,
			animate:true
		});
	
	$('#job-apply-form').parents('.wpcf7').each(function(){
		
		
		var job_id_number = $('#job-offer-id-wrapper .info-value',window.parent.document).text();
		if ( job_id_number == ''){
			 job_id_number = 'Wysłane z działu rekrutracja';
		}
		
		$(this).find('#job-id-number').text(job_id_number);	
		$(this).find('#job-id-number').attr('value',job_id_number);	
		$(this).find('form').validate({
		
			errorContainer: '#error-notification',
			onFocusOut : false,
			rules: {
				'candidate-name' : 'required',
				'candidate-email' : { 'required' : true, 'email':true},
				'candidate-confirmation[]' : 'required',
				'candidate-message' : {},
				'candidate-cv' : {}
			},
			messages : {
				'candidate-name': { 'required' : ''},
				'candidate-email': { 'required' : '', 'email' : ''},
				'candidate-confirmation[]': { 'required' : ''}					
			},
			errorPlacement : $.ec.forms.errorPlacement,
			submitHandler : function(form){
						
				$(form).find('.field-wrapper').removeClass('error');	
				$(form).ajaxSubmit({
					beforeSubmit : function(){
						$('#preloader').removeClass('hidden');
					},
					success : function(){
						$('#job-offer-apply',window.parent.document).hide();
						$('#job-offer-confirmation',window.parent.document).removeClass('hidden');
						parent.$.fancybox.close();
					}
				});
			
				},
			invalidHandler:  $.ec.forms.invalidHandler
			  
		});	
	});
	
	
	
	$('#recruitment-path').each(function(){
		var steps = $(this).find('#recruitment-path-graph li');
		var pointer = $(this).find('#recruitment-path-description .pointer');
		var descriptions = $(this).find('#recruitment-path-description ol');
		
		steps.each(function(index,element){
			$(element).click(function(){
				cur_index = (parseInt(pointer.css('left'))-40)/100;
				steps.filter('.active').removeClass('active');
				$(this).addClass('active');
				pointer.animate({left : 40+(index*100)},(Math.abs(cur_index - index)+1)*200);
				descriptions.animate({marginLeft:-(index*600)},(Math.abs(cur_index - index)+1)*200);
			});	
		});
				
			
	});



	/*ONLY FOR OFFER INQUIRY*/
	$('.offer-inquiry').each(function(){
	
		var next_height = parseInt($('.offer-inquiry form').find('.field-group:first').css('height'));
		var offer_inquiry_form = $(this).find('form');
		$('.offer-inquiry form').css({'height':next_height+50});
		
		
		var offset = $('.offer-inquiry form').offset()
		
	
		$(this).find('.next').click(function(){
			
			
			window.scrollTo(0,offset.top);
			
			$(this).parents(".field-group").find(".field-wrapper").removeClass('error');
			var fields = $(this).parents(".field-group").find('input, textarea, select');
			
			if( fields.valid()){
			
				
				$(this).parents(".field-group").find('.error-notification').addClass('hidden');
				var cur_margin = parseInt(offer_inquiry_form.css('margin-left'));
				var next_height = parseInt($(this).parents('.field-group').next().css('height'));
				offer_inquiry_form.css({'height' : next_height + 50,'marginLeft' :cur_margin-550});
				$.ec.updateStepState($(this).parents('.field-group').next().attr('id'));		
			
			}else{
				$(this).parents(".field-group").find('.error-notification').removeClass('hidden');
				
			}
		
		});
	
		$(this).find('.back').click(function(){
			window.scrollTo(0,offset.top);
			var cur_margin = parseInt(offer_inquiry_form.css('margin-left'));	
			var prev_height = parseInt($(this).parents('.field-group').prev().css('height'));
			offer_inquiry_form.css({'marginLeft' :cur_margin+550,'height' : prev_height+ 50});
			$.ec.updateStepState($(this).parents('.field-group').prev().attr('id'));			
		});
		
		
		$(this).find('form').validate({
			errorContainer: '#error-notification',
			onFocusOut : false,
			rules: {
				'offer-inquiry-country' : 'required',
				'offer-inquiry-city' : 'required',
				'offer-inquiry-project-start' : 'required',
				'offer-inquiry-project-end' : 'required',
				'offer-inquiry-client-name' : 'required',
				'offer-inquiry-client-phone' : 'required',
				'offer-inquiry-client-email' : { 'required' : true, 'email':true} 
								
			},
			messages : {
				'offer-inquiry-country' : {'required':''},
				'offer-inquiry-city' : {'required':''},
				'offer-inquiry-project-start' : {'required':''},
				'offer-inquiry-project-end' : {'required':''},
				'offer-inquiry-client-phone' : {'required':''},
				'offer-inquiry-client-email' : {'required':'', 'email':''}
			},
			
			errorPlacement :$.ec.forms.errorPlacement,
			
			submitHandler : function(form){
			
				$(form).find('.error-notification').addClass('hidden');
				$(form).find('.field-wrapper').removeClass('error');	
				
				$(form).ajaxSubmit({
					beforeSubmit : function(){
						$('#preloader').removeClass('hidden');
					},
			
					success : function(){
						$('#preloader').addClass('hidden');
						$('.masked>p').addClass('hidden');
						$('#offer-inquiry-confirmation').removeClass('hidden');
						window.scrollTo(0,250);
					}
				});
			
			},
			
			invalidHandler:  function(form, validator) {
				window.scrollTo(0,250);
				$('#form-submit').parents('.field-group').find('.error-notification').removeClass('hidden');
			}

			  
		});	
		
	
	});
	
	
	$("#team-member-grid").each(function(){
		$(this).find('.photo-container').hover(
			function(){
				
				
				$('.base-picture').not('.active').addClass('active');
				
				$(this).find('.base-picture').removeClass('active');
				
				var personal_info = $(this).parent();
				
				var name = personal_info.find('.name').html();
				var occupation = personal_info.find('.occupation').html();
				var description = personal_info.find('.description').html();
				
				var wordarr = name.split(" ");
					wordarr[0] = wordarr[0] + "</br>";
				name = wordarr.join("");
				
				
				$('#person-description .name').html(name);
				$('#person-description .occupation').html(occupation);
				$('#person-description .description').html(description);
				
				
				
				Cufon.replace($('#person-description .name, #person-description .occupation'));
				
			},
			function(){
				$(this).find('.base-picture').addClass('active');
			}
		);
	});


	//fancybox handling
	$('#content #job-offer-apply, #content #recruitment-application-button').fancybox({
		'autoDimensions' : true,
		'autoScale' : false,
		'width' : 550,
		'height' : 700
	});
		
});


