$(function () {
    function onStopSorting(){
        var weight=$('div.PriorityList div.PriorityItem input.weight');
        var nums=$('div.PriorityList div.PriorityItem span.Weight');
        var arr=[];
        var count=0;
        for(var iInex=weight.length-2;iInex>=0;iInex--){
            arr.push($(weight[iInex]).val());
        }

        function tmp(obj,cnt){
            $(obj).fadeOut(function(){
                    $(obj).html(''+(cnt+1));
                    $(obj).fadeIn();
                });
        }
        function animate(){
            if(count<arr.length){
                new tmp(nums[count],arr.length-1-count);
                setTimeout(animate,10*count);
                count++;
            }
        }

        animate();
        
        $('input#new_order').val(arr);

        function onResponse(){
            setTimeout(function(){
                $('div.Pane h2#prioritiesH').removeClass('loading');
                $('div.Pane h2#prioritiesH span.doing').hide();
            },500);
        }
        var options = {
            success:onResponse,  // post-submit callback
             type:'post'
         }
        
        $('div.Pane h2#prioritiesH').addClass('loading');
        $('div.Pane h2#prioritiesH span.doing').show();
        $('form#sortOrder').ajaxSubmit(options);

    }
    
    $('div.PriorityList').sortable(
        {handle:'ul li.actMove a'
        ,placeholder:'sortHelper'
        ,update:onStopSorting,
            zIndex:998,
            placeholder:'placeHolder'
        }
    );

    function onDoneComplete(){
    }
    var options={onDone:onDoneComplete};
    function onEachPriorityItem(i,n){
        new PriorityItem(n,options);
    }

    $('div.PriorityItem').each(onEachPriorityItem);
});

function PriorityItem(oElement,options){
    var _oElement=oElement;
    var _options=options;
    
    var _oForm=$('form',_oElement)[0];
    
    function onEditClicked(){
        $('span.EditView',_oElement).slideDown('hide');
        $('li.actDone',_oElement).removeClass('hide');
        $('li.actCancel',_oElement).removeClass('hide');

        copyLabelToField();
        $('li.actEdit',_oElement).addClass('hide');
        $('li.actMove',_oElement).addClass('hide');

        return false;
    }

    function onCancelClicked(){
        $('span.EditView',_oElement).slideUp('hide');
        $('li.actDone',_oElement).addClass('hide');
        $('li.actCancel',_oElement).addClass('hide');

        $('li.actEdit',_oElement).removeClass('hide');
        $('li.actMove',_oElement).removeClass('hide');
    }
    
    function onDoneClicked(){
        function showRequest(formData, jqForm, options) {
            // formData is an array; here we use $.param to convert it to a string to display it
            // but the form plugin does this for you automatically when it submits the data
            var queryString = $.param(formData);

            // here we could return false to prevent the form from being submitted;
            // returning anything other than false will allow the form submit to continue

            $('li.actEdit',_oElement).addClass('hide');
            $('li.actMove',_oElement).addClass('hide');

            
            //$(_oElement).addClass('ajaxLoading');

            return true;
        }

        // post-submit callback
        function showResponse(responseText, statusText)  {

            $(_oElement).removeClass('ajaxLoading');

            setTimeout(function(){
                copyFieldToLabel();
                $('span.EditView',_oElement).slideUp('hide',function(){


                $('li.actDone',_oElement).addClass('hide');
                $('li.actCancel',_oElement).addClass('hide');

                $('li.actEdit',_oElement).removeClass('hide');
                $('li.actMove',_oElement).removeClass('hide');
                });

                $('div.Pane h2#prioritiesH').removeClass('loading');
                $('div.Pane h2#prioritiesH span.doing').hide();
            },500);

            _options.onDone();
        }

         var options = { 
            beforeSubmit:  showRequest,  // pre-submit callback
            success:showResponse,  // post-submit callback
             type:'post'
         }


        
        $('div.Pane h2#prioritiesH').addClass('loading');
        $('div.Pane h2#prioritiesH span.doing').show();
        $(_oForm).ajaxSubmit(options);
        return false;
    }

    function copyFieldToLabel(){
        $('span.InfoView span.Name',_oElement).html($('span.EditView input.Name',_oElement).val());

        $('span.InfoView span.Description',_oElement).html($('span.EditView input.Description',_oElement).val());

    }

    function copyLabelToField(){
        $('span.EditView input.Name',_oElement).val($('span.InfoView span.Name',_oElement).html());
        $('span.EditView input.Description',_oElement).val($('span.InfoView span.Description',_oElement).html());
    }

    $('ul li.actEdit a',_oElement).bind('click',onEditClicked);
    $('ul li.actDone a',_oElement).bind('click',onDoneClicked);
    $('ul li.actCancel a',_oElement).bind('click',onCancelClicked);
}