function updateSpecializationSelect(indId, val)
{
    var spec = specializations[indId];
    var sel = $("select[name=specialization]");
    $("option", sel).remove();
    sel.append($("<option>-- выберите --</option>"));
    if ( indId != "" )
    {
        sel.removeAttr("disabled");
        for ( var s in spec )
        {
            var o = $("<option></option>");
            o.text(spec[s]);
            o.val(s);
            sel.append(o);
        }
    }
    else
    {
        sel.attr("disabled", "disabled");
    }
    if ( val )
    {
        sel.val(val);
    }
}

function appendClonedBlock(id, className)
{
    var wp = $("#" + id).clone();
    wp.removeAttr("id");
    $(":input", wp).each( function () {
            $(this).val("");
        }
    );
    var remove = $("<a href='#' class='remove_" + className + "'>удалить</a>");
    remove.data(className, wp);
    remove.click( function () {
            $(this).data(className).remove();
            return false;
        }
    );
    wp.append(remove);
    $("." + className + ":last").after(wp);
}

function addWorkplace()
{
    appendClonedBlock("workplace_sample", "workplace");
    $(".workplace:last .responsibilities:first").remove();
    $(".workplace:last .responsibilities")
        .attr("style", "")
        .autoResize({
            extraSpace : 20,
            limit : 400
        })
        .change();
}

function addInstitute()
{
    appendClonedBlock("institute_sample", "institute");
    $(".institute:last .comment:first").remove();
    $(".institute:last .comment")
        .attr("style", "")
        .autoResize({
            extraSpace : 20,
            limit : 400
        })
        .change();
}

function initBlocks(data, className, createFunc)
{
    for ( var i = 0; i < data.length; i++ )
    {
        var w = data[i];
        var wp = $("." + className + ":last");
        for ( n in w )
        {
            $(":input[name='" + className + "_" + n + "[]']", wp).val(w[n]);
        }
        if ( i != data.length - 1 )
        {
            createFunc();
        }
    }
}

function initWorkplaces()
{
    initBlocks(workplaces, "workplace", addWorkplace);
}

function initInstitutes()
{
    initBlocks(institutes, "institute", addInstitute);
}

function init()
{
		initDeleteResume();	
	
    initDateControl($('#dobYear'), $('#dobMonth'), $('#dobDay'), $('#dob'));
        
    $("select[name=industry]").change( function () {
            updateSpecializationSelect($(this).val());
        }
    );
    updateSpecializationSelect($("select[name=industry]").val(),$("input[name=specialization_value]").val());
    
    $("a.add_workplace").click( function () {
            addWorkplace();
            return false;
        }
    );
    $("a.add_institute").click( function () {
            addInstitute();
            return false;
        }
    );

    initWorkplaces();
    initInstitutes();

    if ( typeof(wrongFields) != "undefined" )
    {
        for ( var i = 0; i < wrongFields.length; i++ )
        {
            $(".f_" + wrongFields[i]).addClass("wrong");
        }
    }
    
    /** init submit button */
    $("#submit.confirm").click( function () {
            $isOk = confirm("После сохраниния изменений ваше резюме будет временно отсутствовать на сайте,\nтак как оно должно пройти проверку.\nПродолжить?");
            return $isOk;
        }
    );
    
    $('.institute .comment, .workplace .responsibilities').autoResize({
        extraSpace : 20,
        limit : 400
    }).change();
    $('textarea[name=skills],textarea[name=qualities]').autoResize({
        extraSpace : 20,
        limit : 400
    }).change();
}

function initDeleteResume()
{
	$('#delete-resume').click(onClickDelete);
	
	function onClickDelete()
	{
		if(confirm('Вы действительно хотите удалить Ваше резюме?'))
		{
			$('#form-delete-resume').submit();
		}
	}
	return false;
}

$(document).ready(init);
