var sections = {
    /**
    * @function: select, move-left, move-right, up, down section;
    * @location: changesection.php;
    * @status: in progress about 80%;
    * @problem: when insert new section, a details in old section is delete;
    */
    
    id:'',
    lang:'',
    
    loadReady:function(){
            $("[name=copy_selected]").click(sections.demo2_copy_selected);
            $("[name=left_button]").click(sections.demo2_move_selected);
            $("[name=remove_selected]").click(sections.demo2_remove_selected);
            $("[name=right_button]").click(sections.demo2_moveright_selected);
            $("[name=up_button]").click(sections.demo2_up_selected);
            $("[name=down_button]").click(sections.demo2_down_selected);
            $("[name=savesection_button]").click(sections.saveEdit_section);
            sections.optionAppend();
            $("select[name=available_select]").dblclick(sections.demo2_moveright_selected);
            $("select[name=assign_select]").dblclick(sections.demo2_move_selected);
            $("[name=addheading_button]").click(sections.addcustomer_section); 
        },
        demo2_copy_selected:function()
        {
            sections.get_selected("select[name=available_select]").clone().appendTo("select[name=assign_select]");
        },
        demo2_move_selected:function()
        {
            var indexOpion = sections.get_selected("select[name=assign_select]").val();
            var barIndexOption = $("option.assign_option").index($("#option"+indexOpion));
            //console.log($("option.assign_option").index($("#option"+indexOpion)));
            if($("option.assign_option").eq(barIndexOption).is("option.assign_option:first-child")){
                var nextOption = barIndexOption+1;                          
            }else{
                var nextOption = barIndexOption;
            }
            $("select[name=available_select] option").removeAttr("selected");
            sections.get_selected("select[name=assign_select]").appendTo("select[name=available_select]");
            $("option.assign_option").eq(nextOption).attr("selected","selected");
            
        },
        demo2_moveright_selected:function()
        {
            var index = sections.get_selected("select[name=available_select]").val();
            var barIndex = $("option.available_option").index($("#option"+index));         
            $("select[name=assign_select] option").removeAttr("selected");               
            sections.get_selected("select[name=available_select]").appendTo("select[name=assign_select]");
            $("option.available_option").eq(barIndex).attr("selected","selected");                             
        },
        demo2_remove_selected:function()
        {
            sections.remove_selected("select[name=available_select]");
        },

        
        get_selected:function (path)
        {
            return $(path + " option:selected");
        },

         remove_selected:function(path)
        {
            $(path + " option:selected").remove();
        },

         append_item:function(path, value, text)
        {
            $(path).append("<option value='" + value + "'>" + text + "</option>");
        },
         select_first:function(path)
        {
            $(path + " option:first-child").attr("selected","selected");
        },
         select_last:function(path)
        {
            $(path + " option:last-child").attr("selected","selected");
        },
        remove_all:function (path)
        {
            $(path).empty();
        },
         select_by_value:function(path, value)
        {
            $(path + " option[value=" + value + "]").attr("selected","selected");
        },
         remove_by_value:function(path, value)
        {
            $(path + " option[value=" + value + "]").remove();
        },
        demo2_up_selected:function (){
            var path = "select[name=assign_select]";
            var value = sections.get_selected(path).prev().val();
            var text = sections.get_selected(path).prev().text();           
            var pos = sections.get_selected(path);
            var stop = $("select[name=assign_select] option:first-child").attr("value");
            var option = pos.attr("value");
            if(stop != option ){
                sections.get_selected("select[name=assign_select]").prev().remove();
                pos.after("<option value='" + value + "'>" + text + "</option>");
            }
        },
         demo2_down_selected:function(){           
            var path = "select[name=assign_select]";
            var value = sections.get_selected(path).next().val();
            var text = sections.get_selected(path).next().text();            
            var pos = sections.get_selected(path);            
            var stop = $("select[name=assign_select] option:last-child").attr("value");            
            var option = pos.attr("value");
            if(stop != option){           
                sections.get_selected("select[name=assign_select]").next().remove();
                pos.before("<option value='" + value + "'>" + text + "</option>");
            }
        },
        saveEdit_section:function(){
            var path = "select[name=assign_select]";
            var sectionId = '';
            var arraySection = new Array();
            var i = 1;
            $(path +" option").each(function(){
                sectionId = $(this).attr("value");
                arraySection.push(sectionId);
                i++;
            });        
            $.ajax({
                url:'modules/section/editSectionSub.php',
                cache: false,
                data:{'section':arraySection},
                type:'POST',
                success:function(data){
                   if(data != 0){
                      $.ajax({
                            url:'modules/resume/openResume.php',
                            cache: false,
                            data:{'resumeId':data},
                            type:'POST',
                            success:function(data){
                               $("#main").empty().append(data);
                               button.control_button();
                               button.buttonEducation();
                               $("#education_section [name=close_button]").click(button.cancel_education);
                            }
                      });  
                   }                 
                }

            });          
            $("#fancy_content").click($.fn.fancybox.close);                                             
        },
        optionAppend:function(){
            var path = "select[name=assign_select]";
            var pathAvailable = "select[name=available_select]";           
            var optionAssign = '';
            var optionAvailable = '';           
            $(path +" option").each(function(){
                optionAssign = $(this).attr("value");
                $(pathAvailable +" option").each(function(){
                    optionAvailable = $(this).attr("value");
                    if(optionAssign == optionAvailable){
                        $(this).remove();
                    }
                });
            });           
        },
        addcustomer_section:function(){
          var text = $('#custom_text').val();
          var data = '';
          var head = '';
           if(text != ""){
                $.ajax({
                                url:'modules/section/customSectionSub.php',
                                cache: false,
                                data:{'name':text},
                                type:'POST',
                                success:function(data){ //data is customer section id
                                    $('#custom_text').val("");                               
                                    var value = data;
                                    var path = "select[name=assign_select]";
                                    $(path).append("<option value='" + value + "'>" + text + "</option>");
                                }
                });
            }else{
                switch(sections.lang){
                    case'th':
                            head = 'คำเตือน';
                            data = 'กรุณากรอกชื่อหมวดหมู่ที่ต้องการคะ';
                    break;
                    
                     case'en':
                            head = 'Warning';
                            data = 'Please enter your name section';
                    break;
                }
                $('#dialog').empty().attr('title', head).append('<p>'+data+'</p>');
                $('#dialog').dialog({
                                        bgiframe: true,
                                        resizable: false,
                                        width:300,
                                        height:300,
                                        modal: true,
                                        overlay: {
                                            backgroundColor: '#000',
                                            opacity: 0.5
                                        },
                                        close: function(ev, ui) { $(this).dialog( 'destroy' ); $('#selectTools').val(" ");},
                                        buttons: {                                       
                                        'ตกลง': function() {
                                            $(this).dialog( 'destroy' );
                                        }
                                    }
                });
            }
        }
       
}
