/**
 * SELFAdDirectory extension for eZ Publish 4.0
 * Written by Paweł Graczyk, Copyright (C) SELF s.c.
 * http://www.mediaself.pl, http://ez.ryba.eu
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
    //Plugin ukrywający klasy ze względu na id dla pól typu checkbox
    jQuery.fn.hideClassEqualId = function(options){
    	
    	var options = jQuery.extend(
        {
            action: 'show'
        }, options);
    	
        var id = this.attr('id');
        var name = this.attr('name');
        var element = $('.' + id);

        if($("input[id='"+ id +"']:checked").length == 0){
        	if(options['action'] == 'hide'){	
        		element.show();
        	}	
        	else{
        		element.hide();
        	}
        }
        else{
        	if(options['action'] == 'hide'){	
        		element.hide();
        	}	
        	else{
        		element.show();
        	}
        }

        $("input[id='"+ id +"']").click(function(){

        	if(element.css('display') != 'none'){
			    element.hide();
			}
			else{
				element.show();
			}
        });
   };
   
   
   //Plugin ukrywający klasy ze względu na id dla pól typu select
   jQuery.fn.hideClassEqualIdSelect = function(){
       var id = this.attr('id');
       $('#'+id+' option').each(function(){
           $('#'+$(this).attr('class')).hide();
       });
       
       var idForAction = $('#'+id+' option:selected').attr('class');
       //alert($('#'+id+' option:selected').attr('class'))
       $('#'+idForAction).show();
       //$('#'+$(this).attr('class')).show();
       
       
       $(this).change(function(){
           var idForAction = $('#'+id+' option:selected').attr('class');
           $('#'+id+' option').each(function(){
               if($(this).attr('class') == idForAction){
                   $('#'+$(this).attr('class')).show(); 	
               }
               else{
                   $('#'+$(this).attr('class')).hide();
               }
           });
       });
   };

    //Podyplomowe aktywne jedynie przy zaznaczonych checkbox dla wykształcenia wyższego   
	function studyChecked(){
		
        if( $('#EduLevel7').is(":checked")){
            $('#EduLevel8').removeAttr("disabled");
            $('#doktoranckie').removeAttr("disabled");
            $('#mba').removeAttr("disabled");
        }
        else{
        	$('.EduLevel8').hide();
        	$('#EduLevel8').removeAttr("checked", "checked");
            $('#EduLevel8').attr("disabled", "disabled");
            
        	$('#doktoranckie').removeAttr("checked", "checked");
            $('#doktoranckie').attr("disabled", "disabled");
            
        	$('#mba').removeAttr("checked", "checked");            
            $('#mba').attr("disabled", "disabled");            
        }
    };

    function skillsChecked(){
    	$('.startpeople_skills_automat').each(
    	function(){
    		var skillId = $(this).attr('id');
    		$('#'+skillId).hideClassEqualId();
    	});
    	
    }

$(document).ready(
    function(){
        $('.language_id').each(function(){
    	    if( $(this).attr('id') ){
    		    var language_id = $(this).attr('id').substr(12);
        		//Ukrywanie elementu z danym id	
    		    $('#Language > option').each(function(){
                    var current_language_id =  $('#current_language_id').html();
			        var lang = $(this).attr('class').substr(8);
			        if( lang == language_id &&  lang !== current_language_id ){
				        $(this).remove();
			        }	
    		    });
    	    };
        });

	var skill_others =[ 'Skill-cad_software-13',
	                    'Skill-operating_systems-18',
						'Skill-databases-17',
						'Skill-internet_tools-25',
						'Skill-business_software-5',
						'Skill-accounting_software-6',
						'Skill-dtp_graphic_software-21',
						'Skill-stat_software-5', 
						'Skill-groupware-5',
						'Skill-programming_langs-39',
						'Skill-office_software-11',
						'Skill-server_software-7'
						];

	$.each(skill_others, function(){
		$('#' + this).hideClassEqualId();
	});
	
	$('#EduLevel7').bind('click', studyChecked);
	
	studyChecked();
	$('#EduLevelAdditionalInfo').hideClassEqualId();
	
	$('#ExperienceResponsibilites').hideClassEqualId();
	$('#ExperienceAchievements').hideClassEqualId();
	$('#ExperienceAdditionalInfo').hideClassEqualId();

	$('#LanguageExtraInfo').hideClassEqualId();
	$('#LanguageCertificates').hideClassEqualId();
	
	//Kwalifikacje
	$("#DriverLicense").hideClassEqualId();
	$("#PartTime").hideClassEqualId();
	$("#AdditionalInfo").hideClassEqualId();
	
	//Preferencje zatrudnienia
	$('#PreferencesWorkLocation').hideClassEqualIdSelect();
	$("#PreferencesAvailabilityShift").hideClassEqualId();

	//Informacje uzupełniające
	$("#StatusStudent26").hideClassEqualId();
	
	//Umiejętności
	skillsChecked();
	
	//Znikające województwo i kod pocztowy dla krajów innych niż Polska
	
	$("#LocationCountry").change(function(){
		hideCountyAndPostCode();
	});
	
	$("#ContactCountry").change(function(){
		hideCountyAndPostCodeForContactAddress();
	});
	//Ukrywanie kodu pocztowego i województwa w przypadku państw innych niż Polska
	hideCountyAndPostCode();
	hideCountyAndPostCodeForContactAddress();
	
	
	function hideCountyAndPostCode(){
		if( $("#LocationCountry option:selected").val() === 'PL' )
		{
			$("tr#location_state").show();
			$("tr#location_postcode").show();
			
			$("#location_address").removeClass('not_need');
			$("#location_building_number").removeClass('not_need');
			
			var labelWithoutStar = $("#location_building_number_label").html();
			$("#location_building_number_label").html(labelWithoutStar + '*');

			var labelWithoutStar = $("#location_address_label").html();
			$("#location_address_label").html(labelWithoutStar + '*');

		}
		else
		{
			$("tr#location_state").hide();
			$("tr#location_postcode").hide();
			
			$("#location_address").attr('class', 'not_need');
			$("#location_building_number").attr('class', 'not_need');
			
			var labelWithStar = $("#location_building_number_label").html();

			if(typeof(labelWithStar) === 'string')
			{
				var label = $("#location_building_number_label").html();
				if(label.substr( (label.length - 1), label.length) === '*')
				{
					label = label.substr(0, (label.length - 1) );
				}
				$("#location_building_number_label").html(label);
			}

			var labelWithStar = $("#location_address_label").html();
			if(typeof(labelWithStar) === 'string')
			{
				var label = $("#location_address_label").html();
				if(label.substr( (label.length - 1), label.length) === '*')
				{
					label = label.substr(0, (label.length - 1) );
				}
				$("#location_address_label").html(label);

			}
		}	
	}
	
	$("#EducationLevel").bind('change', changeEducationLevelRequired);
	
	changeEducationLevelRequired();
	function changeEducationLevelRequired(){
		if( $("#EducationLevel option:selected").val() === '1' ||
			$("#EducationLevel option:selected").val() === '2' ||
			$("#EducationLevel option:selected").val() === '9' ||				
			$("#EducationLevel option:selected").val() === '10')
		{
			
			$('#occupation_other').show();
			$('#occupation_shool').hide();
			
			$("tr.okres_nauki").addClass('not_need');
			
			if(typeof($("#okres_nauki_label").html()) === 'string')
			{
				var label = $("#okres_nauki_label").html();
				if(label.substr( (label.length - 1), label.length) === '*')
				{
					label = label.substr(0, (label.length - 1) );
				}
				$("#okres_nauki_label").html(label);
			}
		}
		//Zasadnicze zawodowe
		else if( $("#EducationLevel option:selected").val() === '3' ){
			
			$('#occupation_other').hide();
			$('#occupation_shool').show();
			
			$("tr.okres_nauki").removeClass('not_need');
			var label = $("#okres_nauki_label").html();

			if(label != null && label.substr( (label.length - 1), label.length) === '*')
			{
				label = label.substr(0, (label.length - 1) );
			}
			$("#okres_nauki_label").html(label + '*');
		}
		else{

			$('#occupation_other').show();
			$('#occupation_shool').hide();
			
			$("tr.okres_nauki").removeClass('not_need');
			var label = $("#okres_nauki_label").html();

			if(label != null && label.substr( (label.length - 1), label.length) === '*')
			{
				label = label.substr(0, (label.length - 1) );
			}

			$("#okres_nauki_label").html(label + '*');
		}

		//Sortowanie doświadczeń zawodowych
		(function(){ 
			//Sortowanie liczb całkowitych
	        function sortJobsByDate(a,b) {	
	        	return b.time - a.time;
	        }
	        function display(){
	            for(var i=0; i < job_elements.length; i++ ){
	            	$('#jobs_list').append( job_elements[i].element );
	            };	
	        }	
			job_elements = new Array()
	    	$('.job_element').each(function(){
	    		var id = $(this).attr('id');
	    		var time = parseInt(id.substr(14));
	            var element = $(this).html();
	            var job = {
	            	time: time,
	            	element: element
	            };
	            job_elements.push(job);
	            $(this).remove();
			});		            
	        job_elements.sort(sortJobsByDate);
	        display();
		})();
		
		
		(function(){
			//Dezaktywacja checkboxów w info
			
			$('#status_4').bind('change', status4checkedChange )
			$('#status_2').bind('change', status2checkedChange )
			$('#status_3').bind('change', status3checkedChange )
			
			var status1 = $('#status_4:checked');
			var status2 = $('#status_2:checked');
			var status3 = $('#status_3:checked');
			
			
			function status4checked(){
				if($('#status_4:checked').length){
					$('#status_2').attr("disabled", "disabled");
					$('#status_3').attr("disabled", "disabled");
				}
			}
			
			function status4checkedChange(){
				if($('#status_4:checked').length){
					$('#status_2').attr("disabled", "disabled");
					$('#status_3').attr("disabled", "disabled");
				}
				else{
					$('#status_2').attr("disabled", "");
					$('#status_3').attr("disabled", "");
				}
			}
			
			
			function status2checked(){
				if($('#status_2:checked').length){
					$('#status_4').attr("disabled", "disabled");
					$('#status_3').attr("disabled", "disabled");
				}
			}

			function status2checkedChange(){
				if($('#status_2:checked').length){
					$('#status_4').attr("disabled", "disabled");
					$('#status_3').attr("disabled", "disabled");
				}
				else{
					$('#status_4').attr("disabled", "");
					$('#status_3').attr("disabled", "");
				}
			}	
			

			function status3checked(){
				if($('#status_3:checked').length){
					$('#status_4').attr("disabled", "disabled");
					$('#status_2').attr("disabled", "disabled");
				}
			}				

			function status3checkedChange(){
				if($('#status_3:checked').length){
					$('#status_4').attr("disabled", "disabled");
					$('#status_2').attr("disabled", "disabled");
				}
				else{
					$('#status_4').attr("disabled", "");
					$('#status_2').attr("disabled", "");
				}
			}				
			
			status2checked();
			status3checked();
			status4checked();
			
		})();
		
	}
	
	$("#EducationLevel").bind('change', changeEducationLevelSchoolNameRequired);
	changeEducationLevelSchoolNameRequired();
	function changeEducationLevelSchoolNameRequired(){
		
		$("tr.nazwa_szkoly").addClass('not_need');
		if( $("#EducationLevel option:selected").val() === '3' || $("#EducationLevel option:selected").val() === '4' ||
			$("#EducationLevel option:selected").val() === '5' || $("#EducationLevel option:selected").val() === '6' ||
			$("#EducationLevel option:selected").val() === '7' || $("#EducationLevel option:selected").val() === '8')
		{
			if(typeof($("#nazwa_szkoly_label").html()) === 'string')
			{
				$("tr.nazwa_szkoly").removeClass('not_need');				
				var label = $("#nazwa_szkoly_label").html();
				if(label.substr( (label.length - 1), label.length) === '*')
				{
					label = label.substr(0, (label.length - 1) );
				}
				$("#nazwa_szkoly_label").html(label + '*');
			}
		}
		else
		{	//Usuwamy gwiazdke 
			var label = $("#nazwa_szkoly_label").html()
			if(label != null && label.substr( (label.length - 1), label.length) === '*')
			{
				label = label.substr(0, (label.length - 1) );
			}
			$("#nazwa_szkoly_label").html(label);
		}	
	}
	
	$("#EducationLevel").bind('change', changeEducationLevelFacultyRequired);
	changeEducationLevelFacultyRequired();
	function changeEducationLevelFacultyRequired(){
		
		$("tr.wydzial").addClass('not_need');
		if(  $("#EducationLevel option:selected").val() === '7')
		{
			if(typeof($("#wydzial_label").html()) === 'string')
			{
				$("tr.wydzial").removeClass('not_need');				
				var label = $("#wydzial_label").html();
				if(label.substr( (label.length - 1), label.length) === '*')
				{
					label = label.substr(0, (label.length - 1) );
				}
				$("#wydzial_label").html(label + '*');
			}
		}
		else
		{	//Usuwamy gwiazdke 
			var label = $("#wydzial_label").html()
			if(label != null && label.substr( (label.length - 1), label.length) === '*')
			{
				label = label.substr(0, (label.length - 1) );
			}
			$("#wydzial_label").html(label);
		}	
	}
	
	function hideCountyAndPostCodeForContactAddress(){
		if( $("#ContactCountry option:selected").val() === 'PL' )
		{
			$("tr#contact_state").show();			
			$("tr#contact_postcode").show();
		}
		else
		{
			$("tr#contact_state").hide();
			$("tr#contact_postcode").hide();
		}	
	}
	
	manageEducation();

	$("#EducationLevel").bind('change', manageEducation);
	$("#EducationLevel").bind('change', clearErrors);

	// Odpowiada za czyszczenie komunikatów błędów przy przełączaniu poziomów wykształcenia
	function clearErrors(){
		$("span.error_message").empty();
		
		$(".message-error").remove();
		$(".message-feedback").remove();
		
		
	}
	
	function manageEducation(){
		var educationLevel = $("#EducationLevel option:selected").val();
		
        // BRAK OPCJI
		if(educationLevel === ''){
			$('tr.poziom_wyksztalcenia').show();
			$('tr.okres_nauki').hide();
			$('tr.nazwa_szkoly').hide();
			$('tr.wydzial').hide();
			$('tr.kierunek').hide();
			$('tr.specjalizacja').hide();
			$('tr.zawod_wyuczony').hide();
			$('tr.ukonczone').hide();
			$('tr.dodatkowe_informacje').hide();
		}
		
        // Podstawowe, Gimnazjalne, Doktoranckie, MBA
		if(educationLevel === '1' || educationLevel === '2'){
			$('tr.poziom_wyksztalcenia').show();
			$('tr.okres_nauki').show();
			$('tr.nazwa_szkoly').hide();
			$('tr.wydzial').hide();
			$('tr.kierunek').hide();
			$('tr.specjalizacja').hide();
			$('tr.zawod_wyuczony').hide();
			$('tr.ukonczone').hide();
			$('tr.dodatkowe_informacje').show();
		}
		// Doktoranckie
		 if(educationLevel === '9')
		 {
			$('tr.poziom_wyksztalcenia').show();
			$('tr.okres_nauki').show();
			$('tr.nazwa_szkoly').show();
			$('tr.wydzial').show();
			$('tr.kierunek').hide();
			$('tr.specjalizacja').hide();
			$('tr.zawod_wyuczony').hide();
			$('tr.ukonczone').hide();
			$('tr.dodatkowe_informacje').show();
		 }	 
		
		// MBA 
		if(educationLevel === '10')
		{
			$('tr.poziom_wyksztalcenia').show();
			$('tr.okres_nauki').show();
			$('tr.nazwa_szkoly').show();
			$('tr.wydzial').hide();
			$('tr.kierunek').hide();
			$('tr.specjalizacja').hide();
			$('tr.zawod_wyuczony').hide();
			$('tr.ukonczone').hide();
			$('tr.dodatkowe_informacje').show();
		}
		
        // Zasadnicze zawodowe i Policalne i średnie zawodowe		
		if(educationLevel === '3' || educationLevel === '4' ){
			$('tr.poziom_wyksztalcenia').show();
			$('tr.okres_nauki').show();
			$('tr.nazwa_szkoly').show();
			$('tr.wydzial').hide();
			$('tr.kierunek').hide();
			$('tr.specjalizacja').hide();
			$('tr.zawod_wyuczony').show();
			$('tr.ukonczone').show();
			$('tr.dodatkowe_informacje').show();
		}
        // Ogólnokształcące		
		if(educationLevel === '5'){
			$('tr.poziom_wyksztalcenia').show();
			$('tr.okres_nauki').show();
			$('tr.nazwa_szkoly').show();
			$('tr.wydzial').hide();
			$('tr.kierunek').hide();
			$('tr.specjalizacja').hide();
			$('tr.zawod_wyuczony').hide();
			$('tr.ukonczone').show();
			$('tr.dodatkowe_informacje').show();
		}
		/*
		-Okres nauki*
		- Nazwa szkoły / uczelni*
		- Wydział*
		- Kierunek*
		- Specjalizacja
		- Uzyskany tytuł / zawód* 
		*/

        // Wyższe zawodowe (licencjat)		
		if(educationLevel === '6'){
			$('tr.poziom_wyksztalcenia').show();
			$('tr.okres_nauki').show();
			$('tr.nazwa_szkoly').show();
			$('tr.wydzial').show();
			$('tr.kierunek').show();
			$('tr.specjalizacja').show();
			$('tr.zawod_wyuczony').show();
			$('tr.ukonczone').hide();
			$('tr.dodatkowe_informacje').show();
		}
        // Wyższe (magister)		
		if(educationLevel === '7'){
			$('tr.poziom_wyksztalcenia').show();
			$('tr.okres_nauki').show();
			$('tr.nazwa_szkoly').show();
			$('tr.wydzial').show();
			$('tr.kierunek').show();
			$('tr.specjalizacja').show();
			$('tr.zawod_wyuczony').show();
			$('tr.ukonczone').hide();
			$('tr.dodatkowe_informacje').show();
		}
        // Podyplomowe
		if(educationLevel === '8'){
			$('tr.poziom_wyksztalcenia').show();
			$('tr.okres_nauki').show();
			$('tr.nazwa_szkoly').show();
			$('tr.wydzial').hide();
			$('tr.kierunek').hide();
			$('tr.specjalizacja').hide();
			$('tr.zawod_wyuczony').hide();
			$('tr.ukonczone').hide();
			$('tr.dodatkowe_informacje').show();
		}
	};
	
});