$(function(){
    var userid=$('input#current_user_id').val();
    
    //  If the user chooses themselves as assignee, hide and disable the notify checkbox
    function onAssigneeChange(){
        if(userid==$(this).val()){
            $('span#iNotifyAssignee').hide()
            $('span#iNotifyAssignee input').attr('disabled','disabled');
        }else{
            $('span#iNotifyAssignee input').attr('disabled','');
            $('span#iNotifyAssignee').show();

        }
    }
    
    $('select#frmCreateIssue_frmAssignedTo_id').bind('change',onAssigneeChange);

    //  Trigger onload
    $('select#frmCreateIssue_frmAssignedTo_id').trigger('change');

    var oDescriptionOptions={partDescription:$('div#iBasicDescription')[0],
                             fullDescription:$('div#iFullDescription')[0]
                            };
    var oDescriptionController=new DescriptionController(oDescriptionOptions);

    $('a#lnkHelpWithDesc').bind('click',function(){
        $('li#liDescriptionHelpContainer').hide();
        oDescriptionController.showFullDescription();
    });

    //  Action to close attachment panel
    function closeAttachmentPanel(){
        $('a#lnkAddAnAttachment').addClass('show');
        $('div#iAttachmentContainer').hide();
        _oCloneableTemplate.onCancel();
        return false;
    }

    $('a#lnkCancelAddAttachment').bind('click',closeAttachmentPanel);

    var attachmentPanelOptions={
            rootNode:$('div#iFileUploadContainer')[0],
            removeClass:'a._actRemove',
            onAdded:function(oNode){
                enableFieldsInDiv($('div#iAttachmentContainer')[0]);
            },
            onAllRemoved:function(){
                $('a#lnkAddAnAttachment').show();
                $('div#iAttachmentContainer').hide();
                $('div.protoMenuBar').removeClass('hide');
                //}
                disableFieldsInDiv($('div#iAttachmentContainer')[0]);
            }
        }

    var _oCloneableTemplate=new CloneableTemplateDecorator(attachmentPanelOptions);

    $('a#lnkAddAnAttachment').bind('click',function(){
        $('a#lnkAddAnAttachment').addClass('hide');
        $('div#iAttachmentContainer').show();

        _oCloneableTemplate.begin();
        return false;
    });

    
    $('a#lnkAddAnotherFile').bind('click',function(){
            _oCloneableTemplate.clone();
            return false;
        });


    $('select#frmCreateIssue_frmProject_id').bind('change',function(){
        $('select#subProject_id').attr('disabled','true');
        $('select#frmCreateIssue_frmIssueType_id').attr('disabled','true');
        $('select#frmCreateIssue_frmAssignedTo_id').attr('disabled','true');
         
        
        var projectId=this.options[this.selectedIndex].value;

        new JsonCall('/api/projects/'+projectId+'/areas',{},function(oData){
            var oSelect=$('select#subProject_id')[0];
            if(oSelect != null){
                $(oSelect).html("");
                try {
                    oData = arrayizeJSON($.parseJSON(oData).area);
                } catch (e){
                    oData = null;
                }
                if(oData != null){
                    $.each(oData,function(i,n){
                        var oOption=document.createElement('option');
                        $(oOption).html(n.name);
                        $(oOption).val(n.id);
                        oSelect.appendChild(oOption);
                    });
                }
                $('select#subProject_id').attr('disabled','');
            }
        });

        //  Issue Types
        new JsonCall('/api/projects/'+projectId+'/issue-types',{},function(oData){
            var oSelect=$('select#frmCreateIssue_frmIssueType_id')[0];
            if(oSelect != null){
                $(oSelect).html("");
                var oSelectOneOption = document.createElement('option');
                $(oSelectOneOption).html(globalSelectOne);
                $(oSelectOneOption).val(-1);
                oSelect.appendChild(oSelectOneOption);
                try {
                    oData = arrayizeJSON($.parseJSON(oData).issueType);
                } catch (e){
                    oData = null;
                }                if(oData != null){
                    $.each(oData,function(i,n){
                        var oOption=document.createElement('option');
                        $(oOption).html(n.name);
                        $(oOption).val(n.id);
                        oSelect.appendChild(oOption);
                    });
                }
                $('select#frmCreateIssue_frmIssueType_id').attr('disabled','');
            }
        });

        //  Affected version
        new JsonCall('/api/projects/'+projectId+'/versions',{},function(oData){
            var oSelect=$('select#frmCreateIssue_frmFixIn_id')[0];
            if(oSelect != null){
                $(oSelect).html("");
                var oSelectOneOption = document.createElement('option');
                $(oSelectOneOption).html(globalSelectOne);
                $(oSelectOneOption).val(-1);
                oSelect.appendChild(oSelectOneOption);
                try {
                    oData = arrayizeJSON($.parseJSON(oData).version);
                } catch (e){
                    oData = null;
                }
                if(oData != null){
                    $.each(oData,function(i,n){
                        var oOption=document.createElement('option');
                        $(oOption).html(n.name);
                        $(oOption).val(n.id);
                        oSelect.appendChild(oOption);
                    });
                }
                $('select#frmCreateIssue_frmFixIn_id').attr('disabled','');
            }
        });

        //  Fix for version
        new JsonCall('/api/projects/'+projectId+'/versions?type=fixfor',{},function(oData){
            var oSelect=$('select#frmCreateIssue_frmFixFor_id')[0];
            if(oSelect != null){
                $(oSelect).html("");
                var oSelectOneOption = document.createElement('option');
                $(oSelectOneOption).html(globalSelectOne);
                $(oSelectOneOption).val(-1);
                oSelect.appendChild(oSelectOneOption);
                try {
                    oData = arrayizeJSON($.parseJSON(oData).version);
                } catch (e){
                    oData = null;
                }
                if(oData != null){
                    $.each(oData,function(i,n){
                        var oOption=document.createElement('option');
                        $(oOption).html(n.name);
                        $(oOption).val(n.id);
                        oSelect.appendChild(oOption);
                    });
                }
                $('select#frmCreateIssue_frmFixFor_id').attr('disabled','');
            }
        });

        //  Users
        new JsonCall('/api/projects/'+projectId+'/users',{},function(oData){
            var oSelect=$('select#frmCreateIssue_frmAssignedTo_id')[0];
            if($('select#frmCreateIssue_frmAssignedTo_id').length > 0){
               $(oSelect).html("");
                var oSelectOneOption = document.createElement('option');
                $(oSelectOneOption).html(globalSelectOne);
                $(oSelectOneOption).val(-1);
                oSelect.appendChild(oSelectOneOption);
                try {
                    oData = arrayizeJSON($.parseJSON(oData).user);
                } catch (e){
                    oData = null;
                }
                if(oData != null){
                    $.each(oData,function(i,n){
                        var oOption=document.createElement('option');
                        $(oOption).html(n.firstName + " " + n.lastName);
                        $(oOption).val(n.id);
                        oSelect.appendChild(oOption);
                    });
                }
                $('select#frmCreateIssue_frmAssignedTo_id').attr('disabled','');
            }
        });

    })

});

//  Class's go down here
function DescriptionController(options){
    var _options=options;

    this.showFullDescription=function(){
        $(_options.partDescription).hide();
        $(_options.fullDescription).show();
    }

    this.showPartDescription=function(){
        $(_options.fullDescription).hide();
        $(_options.partDescription).show();
    }
}

function AttachmentController(){
    this.showAttachmentsPanel
}











