
$(document).ready(function(){

    if(window.location.hash)
    {
        var hash = window.location.hash.substring(1); //Puts hash in variable, and removes the # character
//        hash = hash.replace(/[a-zA-Z0-9\-\_\.\,]+\/[a-zA-Z0-9\-\_\.\,]+\/[a-zA-Z0-9\-\_\.\,]+\/[a-zA-Z0-9\-\_\.\,]+\//gi, '');
//        var hash = parseInt(hash);
        hash = ''+hash;
        if(hash)
        {
            insertOfferPopup(hash);
            centerPopup();
            loadPopup();
        }        
    }
    $('#fromToDate').change(function(){
        var value = $('#fromToDate option:selected').val();
        if(value == 3)
        {
            $('#from').val('');
            $('#to').val('');
            return;
        }
        var from = new Date();
        var to = new Date();
        switch(value)
        {
            case '1': // ostatnie 2 dni
                from.setDate(from.getDate()-2);
                break;
            case '2': // ostatnie 7 dni
                from.setDate(from.getDate()-7);
                break;
        }
        
        var fromMonth = from.getMonth() + 1;
        fromMonth = (fromMonth < 10)?'0'+fromMonth:fromMonth;
        var fromDate = from.getDate();
        fromDate = (fromDate < 10)?'0'+fromDate:fromDate;
        var toMonth = to.getMonth() + 1;
        toMonth = (toMonth < 10)?'0'+toMonth:toMonth;
        var toDate = to.getDate();
        toDate = (toDate < 10)?'0'+toDate:toDate;

        $('#from').removeClass('example');
        $('#to').removeClass('example');
        $('#from').val(from.getFullYear()+'-'+fromMonth+'-'+fromDate);
        $('#to').val(to.getFullYear()+'-'+toMonth+'-'+toDate);

    });
    // popup init end

    $('#frequency_contact_date').datepicker({minDate: '+1d', dateFormat: 'yy-mm-dd'});
    $('#actual_date').datepicker({minDate: '+1d', maxDate: '+1m', dateFormat: 'yy-mm-dd'});

    $('#from').datepicker({minDate: '-1y', maxDate: '0', dateFormat: 'yy-mm-dd'});
    $('#to').datepicker({minDate: '-1y', maxDate: '0', dateFormat: 'yy-mm-dd'});

    $('#job_start_date').datepicker({minDate: '+1d', dateFormat: 'yy-mm-dd'});
    if($('#job_start_now').is(':checked'))
    {
        $('#job_start_date').datepicker('disable');
        $('#job_start_date').val('');
        $('#job_start_date').attr('readonly', 'readonly');
    }
    
    $('#job_start_now').change(function(){
        if($('#job_start_now').is(':checked'))
        {
            $('#job_start_date').val('');
            $('#job_start_date').attr('readonly', 'readonly');
            $('#job_start_date').datepicker('disable');
        }
        else
        {
            $('#job_start_date').removeAttr('readonly');
            $('#job_start_date').datepicker('enable');
        }
    });

    $('.selectAction').change(function(){
        var type = $("#"+$(this).attr("id")+" option:selected").attr('class');
        if (type == 'preview') {
            window.open($(this).val());
            return false;
        }
        if (type == 'confirm') {
            if (confirm('Czy na pewno chcesz usunąć wybraną ofertę?')) {
                document.location = $(this).val();
            } else {
                $(this).val('Wybierz');
            }
            return false;
        } else if (type != 'select') {
            document.location = $(this).val();
            return false;
        }

    });
    $('#emitButton').click(function() {
        $('#emit').val(1);
        $('#addAdminOfferForm').submit();
    });

    $('#handicap_vacats').keyup(function(){
        if ($(this).val() > 0) {
            $('#handicap_level').removeAttr('disabled');
        } else {
            $('#handicap_level').val(0);
            $('#handicap_level').attr('disabled', 'disabled');
        }
    });

    $('#handicap_vacats').each(function(){
        if ($(this).val() > 0) {
            $('#handicap_level').removeAttr('disabled');
        } else {
            $('#handicap_level').val(0);
            $('#handicap_level').attr('disabled', 'disabled');
        }
    });

    $('#agreement_type').change(function(){
        if ($(this).val() == 38 || $(this).val() == 40 || $(this).val() == 41) {
            $('#period').removeAttr('disabled');
        } else {
            $('#period').val('');
            $('#period').attr('disabled', 'disabled');
        }
        if ($(this).val() == 42 || $(this).val() == 43 || $(this).val() == 69) {
            $('#time_dimension').val('');
            $('#time_dimension').attr('disabled', 'disabled');
            $('#altering').val('');
            $('#altering').attr('disabled', 'disabled');
            if ($(this).val() == 42 || $(this).val() == 43 || $(this).val() == 69) {
                $('#alter_one_from').attr('disabled', 'disabled');
                $('#alter_one_to').attr('disabled', 'disabled');
                $('#alter_two_from').attr('disabled', 'disabled');
                $('#alter_two_to').attr('disabled', 'disabled');
                $('#alter_three_from').attr('disabled', 'disabled');
                $('#alter_three_to').attr('disabled', 'disabled');
            } else {
                $('#alter_one_from').removeAttr('disabled');
                $('#alter_one_to').removeAttr('disabled');
                $('#alter_two_from').removeAttr('disabled');
                $('#alter_two_to').removeAttr('disabled');
                $('#alter_three_from').removeAttr('disabled');
                $('#alter_three_to').removeAttr('disabled');
            }
        } else {
            $('#time_dimension').removeAttr('disabled');
            $('#altering').removeAttr('disabled');
            $('#alter_one_from').removeAttr('disabled');
            $('#alter_one_to').removeAttr('disabled');
            $('#alter_two_from').removeAttr('disabled');
            $('#alter_two_to').removeAttr('disabled');
            $('#alter_three_from').removeAttr('disabled');
            $('#alter_three_to').removeAttr('disabled');
        }
    });
    $('#agreement_type').each(function(){
        if ($(this).val() == 38 || $(this).val() == 40 || $(this).val() == 41) {
            $('#period').removeAttr('disabled');
        } else {
            $('#period').val('');
            $('#period').attr('disabled', 'disabled');
        }
        if ($(this).val() == 42 || $(this).val() == 43 || $(this).val() == 69) {
            $('#time_dimension').val('');
            $('#time_dimension').attr('disabled', 'disabled');
            $('#altering').val('');
            $('#altering').attr('disabled', 'disabled');
            if ($(this).val() == 42 || $(this).val() == 43) {
                $('#alter_one_from').attr('disabled', 'disabled');
                $('#alter_one_to').attr('disabled', 'disabled');
                $('#alter_two_from').attr('disabled', 'disabled');
                $('#alter_two_to').attr('disabled', 'disabled');
                $('#alter_three_from').attr('disabled', 'disabled');
                $('#alter_three_to').attr('disabled', 'disabled');
            } else {
                $('#alter_one_from').removeAttr('disabled');
                $('#alter_one_to').removeAttr('disabled');
                $('#alter_two_from').removeAttr('disabled');
                $('#alter_two_to').removeAttr('disabled');
                $('#alter_three_from').removeAttr('disabled');
                $('#alter_three_to').removeAttr('disabled');
            }
        } else {
            $('#time_dimension').removeAttr('disabled');
            $('#altering').removeAttr('disabled');
            $('#alter_one_from').removeAttr('disabled');
            $('#alter_one_to').removeAttr('disabled');
            $('#alter_two_from').removeAttr('disabled');
            $('#alter_two_to').removeAttr('disabled');
            $('#alter_three_from').removeAttr('disabled');
            $('#alter_three_to').removeAttr('disabled');
        }
    });
//
//    $('#education').change(function(){
//        if ($(this).val() == 56 || $(this).val() == 58 || $(this).val() == 59) {
//            $('#education_type').removeAttr('disabled');
//
//        } else {
//            $('#education_type').attr('disabled', 'disabled');
//            $('#education_type').val('');
//        }
//    });
//    $('#education').each(function(){
//        if ($(this).val() == 56 || $(this).val() == 58 || $(this).val() == 59) {
//            $('#education_type').removeAttr('disabled');
//
//        } else {
//            $('#education_type').attr('disabled', 'disabled');
//            $('#education_type').val('');
//        }
//    });
    $('#frequency_contact').change(function(){
        if ($(this).val() == 68) {
            $('#frequency_contact_date').removeAttr('disabled');
        } else {
            $('#frequency_contact_date').attr('disabled', 'disabled');
        }
    });
    $('#frequency_contact').each(function(){
        if ($(this).val() == 68) {
            $('#frequency_contact_date').removeAttr('disabled');
        } else {
            $('#frequency_contact_date').attr('disabled', 'disabled');
        }
    });
    $('#another_institute').change(function(){
        if ($(this).val() == 1) {
            $('#another_institute_name').removeAttr('disabled');
        } else {
            $('#another_institute_name').attr('disabled', 'disabled');
        }
    });
    $('#another_institute').each(function(){
        if ($(this).val() == 1) {
            $('#another_institute_name').removeAttr('disabled');
        } else {
            $('#another_institute_name').attr('disabled', 'disabled');
        }
    });

    $('#institute_share').change(function(){
        if ($(this).val() == 1) {
            $('#institute_share_name').removeAttr('disabled');
        } else {
            $('#institute_share_name').attr('disabled', 'disabled');
        }
    });
    $('#institute_share').each(function(){
        if ($(this).val() == 1) {
            $('#institute_share_name').removeAttr('disabled');
        } else {
            $('#institute_share_name').attr('disabled', 'disabled');
        }
    });

    
    $('#eog_offer').change(function(){
        if ($(this).val() == 1) {
            $("#eogOfferDiv input[name!='eog_offer']'").removeAttr('disabled');
            $("#eogOfferDiv select[name!='eog_offer']'").removeAttr('disabled');
            $("#eogOfferDiv textarea[name!='eog_offer']'").removeAttr('disabled');
        } else {
            $("#eogOfferDiv input[name!='eog_offer']'").attr('disabled', 'disabled');
            $("#eogOfferDiv select[name!='eog_offer']'").attr('disabled', 'disabled').val(3);
            $("#eogOfferDiv textarea[name!='eog_offer']'").attr('disabled', 'disabled');
        }
    });

    $('#eog_offer').each(function(){
        if ($(this).val() == 1) {
            $("#eogOfferDiv input[name!='eog_offer']'").removeAttr('disabled');
            $("#eogOfferDiv select[name!='eog_offer']'").removeAttr('disabled');
            $("#eogOfferDiv textarea[name!='eog_offer']'").removeAttr('disabled');
        } else {
            $("#eogOfferDiv input[name!='eog_offer']'").attr('disabled', 'disabled');
            $("#eogOfferDiv select[name!='eog_offer']'").attr('disabled', 'disabled').val(3);
            $("#eogOfferDiv textarea[name!='eog_offer']'").attr('disabled', 'disabled');
        }
    });

    $('#experience').change(function(){
        if ($(this).val() == 61) {
            $('#experience_desc').removeAttr('disabled');
        } else {
            $('#experience_desc').attr('disabled', 'disabled');
        }
    });
    $('#experience').each(function(){
        if ($(this).val() == 61) {
            $('#experience_desc').removeAttr('disabled');
        } else {
            $('#experience_desc').attr('disabled', 'disabled');
        }
    });

    $.timepicker.regional['pl'] = {
            timeOnlyTitle: 'Wybierz czas',
            timeText: 'Czas',
            hourText: 'godzina',
            minuteText: 'minuta',
            secondText: 'sekunda',
            currentText: 'teraz',
            closeText: 'wybierz',
            ampm: false
    };
    $.timepicker.setDefaults($.timepicker.regional['pl']);


    $('.timepic').timepicker({
	hourGrid: 4,
	minuteGrid: 15
    });
    var data = "Core Selectors Attributes Traversing Manipulation CSS Events Effects Ajax Utilities".split(" ");
//    $("#craft").autocomplete(data);

    $("#craft").autocomplete('/pobierz-zawody/', {
	        matchContains: true,
	        minChars: 1,
                max: 10,
	        autoFill: true,
                cacheLength:0,
	        extraParams: {
	    		state_id: function() {return $("#seller_state_idId").val()}
	    	}
	    }).result(function(event, data, formatted){
            if (data) {
                $("#craft_code").val(data[1]);
            } else {
                $("#craft_code").val('');
            }


        });
    
});

function showOfferPopup(offerId) {
    $('.offerDetailsPopWindow').each(function(){
        if ($(this).attr('id') != offerId) {
            $(this).css('display', 'none');
        }
    })
    $('#'+offerId).css('display','block');
}
