$(function () {
    
    if (window.location.hash == '#setPublicProfile') { 
        editPublicProfileClick(); 
    }
    
    var pcw = $('#show_pcw_form').size();
    
    if (typeof(pcw) != 'undefined' && pcw != '') {
        var lang = $('#profileCurrentLang').val();
        pcw_dialog(0, lang);
    }
    
    //nastavenie verejneho profilu
    
    $('#editPublicProfile').click(function () {
        editPublicProfileClick();
    });
    
    $('.profile_icon').click(function () {
        profileIconClick($(this).attr('id'));
    });
    
    //uprava zakladneho profilu
    $('#editPhoto').click(function () {
        editBaseProfileClick();
    });
    $('#editBaseProfile').click(function () {
        editBaseProfileClick();
    });
    $('#editBranch').click(function () {
        editBaseProfileClick();
    });
    
    //uprava doplnujucich udajov
    $('#editWebPages').click(function () {
        editUserProfileClick();
    });
    $('#editSpecialKnowledges').click(function () {
        editUserProfileClick();
    });
    $('#editAditionalInfo').click(function () {
        editUserProfileClick();
    });
    
    //vzdelanie
    $('#addSchool').click(function () {
        addSchoolClick();
    });
    $('.editSchool', '#panelContent').click(function () {
        editSchoolClick($(this).attr('rel'));
    });
    $('.deleteSchool', '#panelContent').click(function () {
        deleteSchoolClick($(this).attr('rel'));
    });
    
    //zamestnane
    $('#addJob').click(function () {
        addJobClick();
    });
    $('#addJob1').click(function () {
        addJobClick();
    });
    $('.editJob', '#panelContent').click(function () {
        editJobClick($(this).attr('rel'));
    });
    $('.deleteJob', '#panelContent').click(function () {
        deleteJobClick($(this).attr('rel'));
    });
    
    // odstranenie profilu
    $('#deleteProfile').click(function () {
        var self = $(this);
        Confirm(__('JS_deleteProfile'), function () {
            var lang_id = self.attr('rel');
            Router.route('joblife/myjoblife/delete-profile/lang/' + lang_id + '/');
        }, function () {
            return false;
        });
    });
    
    $('#profileCompletitionWizzard').click(function () {
        var lang = $('#profileCurrentLang').val();
        pcw_dialog(0, lang);
    });
    
});

function profileIconClick(icon_id) {
    var profileCurrentLang = $('#profileCurrentLang').val();
    var route = 'joblife/myjoblife/public-profile-icon/lang/' + profileCurrentLang + '/?id=' + icon_id;
    showPopupWindow('profile', Router(route), function () {
            
    });
}

function editPublicProfileClick() {
    var profileCurrentLang = $('#profileCurrentLang').val();
    var route = 'joblife/myjoblife/edit-public-profile/lang/' + profileCurrentLang + '/';
    showPopupWindow('profile', Router(route), function () {
        $.getScript('/public/js/myjoblife/publicprofile.js');
    });
}

function editBaseProfileClick() {
    var profileCurrentLang = $('#profileCurrentLang').val();
    var route = 'joblife/myjoblife/edit-base-profile/lang/' + profileCurrentLang + '/';
    showPopupWindow('profile', Router(route), function () {
        $.getScript('/public/js/myjoblife/baseprofile.js');
    });
}

function editUserProfileClick() {
    var profileCurrentLang = $('#profileCurrentLang').val();
    var route = 'joblife/myjoblife/edit-user-profile/lang/' + profileCurrentLang + '/';
    showPopupWindow('profile', Router(route), function () {
        $.getScript('/public/js/myjoblife/profile.js');
    });
}

function addSchoolClick() {
    var profileCurrentLang = $('#profileCurrentLang').val();
    var route = 'joblife/myjoblife/edit-school/lang/' + profileCurrentLang + '/';
    showPopupWindow('profile', Router(route), function () {
        $.getScript('/public/js/myjoblife/education.js');
    });
}

function editSchoolClick(edu_id) {
    var profileCurrentLang = $('#profileCurrentLang').val();
    var route = 'joblife/myjoblife/edit-school/lang/' + profileCurrentLang + '/education_id/' + edu_id + '/';
    showPopupWindow('profile', Router(route), function () {
        $.getScript('/public/js/myjoblife/education.js');
    });
}

function deleteSchoolClick(edu_id) {
    var profileCurrentLang = $('#profileCurrentLang').val();
    var route = 'joblife/myjoblife/delete-school/education_id/' + edu_id + '/lang/' + profileCurrentLang + '/';
        
    Confirm(__('JS_deleteEducation'), function () {
        // window.location.href = Router(route);
        // window.event.returnValue = false;
        Router.route(route);
        return false;
    }, function () {
        return false;
    });
}

function addJobClick() {
    var profileCurrentLang = $('#profileCurrentLang').val();
    var route = 'joblife/myjoblife/edit-job/lang/' + profileCurrentLang + '/';
    // showPopupWindow('jobform', Router(route), function () {
    //     // $.getScript('/public/js/myjoblife/job.js');
    // });
    jobPopupWindow(Router(route));
}

function editJobClick(params) {
    var params = params.split('|');
    var profileCurrentLang = $('#profileCurrentLang').val();
    var route = 'joblife/myjoblife/edit-job/firm_id/' + params[0] + '/date_from/' + params[1] + '/lang_id/' + params[2] + '/lang/' + profileCurrentLang + '/';
    // showPopupWindow('jobform', Router(route), function () {
    //     // $.getScript('/public/js/myjoblife/job.js');
    // });
    jobPopupWindow(Router(route));
}

function deleteJobClick(params) {
    var params = params.split('|');
    var profileCurrentLang = $('#profileCurrentLang').val();
    var route = 'joblife/myjoblife/delete-job/firm_id/' + params[0] + '/date_from/' + params[1] + '/lang_id/' + params[2] + '/lang/' + profileCurrentLang + '/';
        
    Confirm(__('JS_deleteJob'), function () {
        Router.route(route);
        return false;
    }, function () {
        return false;
    });
}

function pcw_dialog(page, lang, go_back) {
    if (typeof(page) != 'undefined' && page != '') {
        var dialog_content = new Dialog.content({
            url: Router('joblife/myjoblife/profile-completition-wizzard/page/' + page + '/lang/' + lang + '/' + (typeof(go_back) != 'undefined' && go_back != '' ? 'go_back/' + go_back + '/' : ''))
        });
    }
    else {
        var dialog_content = new Dialog.content({
            url: Router('joblife/myjoblife/profile-completition-wizzard/lang/' + lang + '/' + (typeof(go_back) != 'undefined' && go_back != '' ? 'go_back/' + go_back + '/' : ''))
        });
    }
    var is_education = false;
    if (page == 'highschool' || page == 'university') {
        is_education = true;
    }
    
    var dialog = new Dialog('dialog_pcw', {
        /*size: is_education ? 'medium' : 'small',*/
        size: 'small',
        onShow: function (d) {
            d.center();
        },
        onContentLoad: function (d) {
            d.center();
            
            //if ($('#empty_hidden').val() == 'highschool' || $('#empty_hidden').val() == 'university') {
            //if ($('#empty_hidden').val() == 'university') {
            //    $('#dialog_pcw').attr('class', 'popupDialog popupDialog-medium');
            //    d.center();
            //}
            
            // preskocenie
            $('#pcw_jump').click(function () {
                var pg = $(this).attr('rel');
                d.close();
                pcw_dialog(pg, lang, go_back);
            });
            
            // vyhladavac firiem
            $('#q_firm_name').attr("autocomplete", "off");
            $('#q_firm_name').similarFirmSearch({
                onSelect: function (holder,firma) {
                    $('#q_firm_name').val(firma.name);
                    $('#firm_id').val(firma.id);
                }
            });
            
            var form = $('#pcw_form')[0];
            
            $('#specialFormSubmit').click(function () {
                submitSpecialForm(form, d);
            });
            
            $('#jobFormSubmit').click(function () {
                submitJobForm(form, d);
            });
            
            $('#schoolFormSubmit').click(function () {
                $('#schoolFormSubmit').attr('disabled', 'disabled');
                submitSchoolForm(form, d);
            });
        },
        content: dialog_content
    });
    dialog.show();
}

function clearCities() {
    var citySelect = $('#city_id')[0];
    if (citySelect) {
        
        while (citySelect.childNodes.length > 0) {
            citySelect.removeChild(citySelect.childNodes[0])
        }
    }
}

function clearSchools() {
    var schoolSelect = $('#school_id')[0];
    while (schoolSelect.childNodes.length > 0) {
        schoolSelect.removeChild(schoolSelect.childNodes[0]);
    }
}

function clearFaculties() {
    var facultySelect = $('#faculty_id')[0];
    while (facultySelect.childNodes.length > 0) {
        facultySelect.removeChild(facultySelect.childNodes[0]);
    }
}

function countryOnchange(country_id) {
    if (country_id != '') {
        if ($('#school_type').val() != 'UNIVERSITY') {
            $.getJSON(Router('joblife/portal/load-cities-json'), {
                country_id: country_id
            }, function (json) {
                // if (json.length > 0) {
                    clearCities();
                    
                    var citySelect = $('#city_id')[0];
                    citySelect.options[0] = new Option('   ', '');
                    for (var i=0; i<json.length; i++) {
                        citySelect.options[i + 1] = new Option(json[i].city, json[i].city_id);
                    }
                    // citySelect.options[i + 1] = new Option(__('JS_ohterItemInSelect'), 'OTHER');
                    $('#city_id').show();
                    $('#city_id_txt').hide();
                // }
                // else {
                //     $('#city_id').hide();
                //     $('#city_id_txt').val('');
                //     $('#city_id_txt').show();
                // }
            });
        }
        
        clearCities();
        clearSchools();
        
        if ($('#school_type').val() == 'UNIVERSITY') {
            clearFaculties();
            loadUniversities(country_id);
            $('#city_id').hide();
        }
    }
}

function cityOnchange(value) {
    var school_type = $('#school_type').val();
    var country = $('#country_id').val();
    var city    = value;
    
    if (school_type != '' && country != '' & city != '') {
        $.getJSON(Router('joblife/portal/load-schools-json'), {
            country_id: country,
            city_id: city,
            school_type: school_type
        }, function (json) {
            var citySelect = $('#school_id')[0];
            while (citySelect.childNodes.length > 0) {
                citySelect.removeChild(citySelect.childNodes[0])
            }
            citySelect.options[0] = new Option('   ', '');
            for (var i=0; i<json.length; i++) {
                citySelect.options[i + 1] = new Option(json[i].school_name, json[i].school_id);
            }
        });
        
        if (school_type == 'UNIVERSITY') {
            // pri nacitani skol sa musia odstranit fakulty
            var facultySelect = $('#faculty_id')[0];
            while (facultySelect.childNodes.length > 0) {
                facultySelect.removeChild(facultySelect.childNodes[0]);
            }
            facultySelect.options[0] = new Option('   ', '');
        }
    }
}

function schoolOnchange(school_id) {
    if ($('#school_type').val() == 'UNIVERSITY') {
        $.getJSON(Router('joblife/portal/load-school-faculties-json'), {
            school_id: school_id
        }, function (json) {
            var facultySelect = $('#faculty_id')[0];
            while (facultySelect.childNodes.length > 0) {
                facultySelect.removeChild(facultySelect.childNodes[0]);
            }
            facultySelect.options[0] = new Option('   ', '');
            for (var i=0; i<json.length; i++) {
                facultySelect.options[i + 1] = new Option(json[i].faculty_name, json[i].faculty_id);
            }
        });
    }
}

function loadUniversities(country_id) {
    //if (country_id == 'SK') {
        $.getJSON(Router('joblife/portal/load-schools-json'), {
            country_id: country_id,
            school_type: 'UNIVERSITY'
        }, function (json) {
            var schoolSelect = $('#school_id')[0];
            while (schoolSelect.childNodes.length > 0) {
                schoolSelect.removeChild(schoolSelect.childNodes[0]);
            }
            schoolSelect.options[0] = new Option('   ', '');
            for (var i=0; i<json.length; i++) {
                schoolSelect.options[i + 1] = new Option(json[i].school_name, json[i].school_id);
            }
        });
    //}
}

function submitSpecialForm(form, d) {
    var special_knowledge = $('#special_knowledge').val();
    // var aditional_info    = $('#aditional_info').val();
    
    var valid = true;
    
    if (typeof(special_knowledge) == 'undefined' || special_knowledge == '') {
        valid = false;
    }
    
    // if (typeof(aditional_info) == 'undefined' || aditional_info == '') {
    //     valid = false;
    // }
    
    if (valid) {
        // $.post(Router('joblife/myjoblife/profile-completition-wizzard-save/'), {
        //     special_knowledge: special_knowledge,
        //     aditional_info: aditional_info,
        //     pcw_special_sent: 1
        // }, function (data) {
        //     if (data == 'OK') {
        //         Alert(__('JS_specialFormSaved'));
        //         var pg = $('#pcw_jump').attr('rel');
        //         d.close();
        //         pcw_dialog(pg, lang);
        //     }
        //     else {
        //         Alert(data);
        //     }
        // });
        form.submit();
        return false;
    }
    else {
        Alert(__('JS_emptyFields'));
    }
}


function submitJobForm(form, d) {
    var firm_name    = $('#q_firm_name').val();
    var job_position = $('#job_position').val();
    var month_from   = $('#month_from').val();
    var year_from    = $('#year_from').val();
    var month_to     = $('#month_to').val();
    var year_to      = $('#year_to').val();
    if($('#till_now').length){
        var till_now     = $('#till_now')[0].checked == true ? 1 : 0;
    }
    var description  = $('#description').val();
    
    var valid = true;
    var years_valid = true;
    
    if (typeof(firm_name) == 'undefined' || firm_name == '') {
        valid = false;
        $('#firm_name_label').addClass('required');
    }
    else {
        $('#firm_name_label').removeClass('required');
    }
    
    if (typeof(job_position) == 'undefined' || job_position == '') {
        valid = false;
        $('#job_position_label').addClass('required');
    }
    else {
        $('#job_position_label').removeClass('required');
    }
    
    if ((typeof(month_from) == 'undefined' || month_from == '') || (typeof(year_from) == 'undefined' || year_from == '')) {
        valid = false;
        $('#date_from_label').addClass('required');
    }
    else {
        $('#date_from_label').removeClass('required');
    }
    
    if (((typeof(month_to) == 'undefined' || month_to == '') || (typeof(year_to) == 'undefined' || year_to == '')) && till_now == 0) {
        valid = false;
        $('#date_to_label').addClass('required');
    }
    else {
        $('#date_to_label').removeClass('required');
    }
    
    if (!till_now) {
        var currentDate = new Date();
        
        if (parseInt(year_to,10) > parseInt(currentDate.getFullYear(),10)) {
            years_valid = false;
            Alert(__('JS_badJobYears'));
        } else if (parseInt(year_to,10) == parseInt(currentDate.getFullYear(),10) && parseInt(month_to,10) > (parseInt(currentDate.getMonth(),10) + parseInt(1,10))) {
            years_valid = false;
            Alert(__('JS_badJobYears'));
        } else {
            if (parseInt(year_from,10) > parseInt(year_to,10)) {
                years_valid = false;
                Alert(__('JS_badJobYears'));
            }
            if (parseInt(year_from,10) == parseInt(year_to,10)) {
                if (parseInt(month_from,10) > parseInt(month_to,10)) {
                    years_valid = false;
                    Alert(__('JS_badJobYears'));
                }
            }
        }
    }
    
    if (!valid) {
        // $('#formEmptyFields').show();
        Alert(__('JS_emptyFields'));
    }
    else {
        // $('#formEmptyFields').hide();
        if (years_valid) {
            form.submit();
            return false;
        }
    }
}
function submitSchoolForm(form, d) {
    var school_type    = $('#school_type').val();
    var country_id     = $('#country_id').val();
    var city_id        = $('#city_id').val();
    var city_id_txt    = $('#city_id_txt').val();
    var school_id      = $('#school_id').val();
    var faculty_id     = $('#faculty_id').val();
    var year_from      = $('#year_from').val();
    var year_to        = $('#year_to').val();
    
    var valid = true;
    var years_valid = true;
    
    if (typeof(school_type) == 'undefined' || school_type == '') {
        valid = false;
        $('#school_type_label').addClass('required');
    }
    else {
        $('#school_type_label').removeClass('required');
    }
    
    if (typeof(country_id) == 'undefined' || country_id == '') {
        valid = false;
        $('#country_id_label').addClass('required');
    }
    else {
        $('#country_id_label').removeClass('required');
    }
    
    if (school_type == 'HIGHSCHOOL') {
        if (!city_id || typeof(city_id) == 'undefined' || city_id == '' || city_id == 'null' || typeof(city_id) == 'null') {
            if (typeof(city_id_txt) != 'undefined' && city_id_txt != '') {
                $('#city_id_label').removeClass('required');
            }
            else {
                valid = false;
                $('#city_id_label').addClass('required');
            }
        }
    }
    else {
        $('#city_id_label').removeClass('required');
    }
    
    if (typeof(school_id) == 'undefined' || school_id == '') {
        valid = false;
        $('#school_id_label').addClass('required');
    }
    else {
       $('#school_id_label').removeClass('required');
    }
    
    // if (school_type == 'UNIVERSITY') {
    //     if (typeof(faculty_id) == 'undefined' || faculty_id == '') {
    //         valid = false;
    //         $('#faculty_id_label').addClass('required');
    //     }
    // }
    // else {
    //     $('#faculty_id_label').removeClass('required');
    // }
    
    if (typeof(year_from) != 'undefined' && year_from != '' && typeof(year_to) != 'undefined' && year_to != '') {
        if (parseInt(year_from,10) > parseInt(year_to,10)) {
            years_valid = false;
            Alert(__('JS_badEducationYears'));
        }
    }
    
    // if ((typeof(year_from) == 'undefined' || year_from == '') && (typeof(year_to) == 'undefined' || year_to == '')) {
    //     $('#years_label').addClass('required');
    //     valid = false;
    // }
    // else {
    //     $('#years_label').removeClass('required');
    // }
    
    if (!valid) {
        $('#formEmptyFields').show();
        $('#schoolFormSubmit').removeAttr('disabled');
    }
    else {
        $('#formEmptyFields').hide();
        if (years_valid) {
            form.submit();
        }
    }
}


