
$(function(){
    //  JTip on hover

    // Removes default tinymce formatting that breaks the edit input box
    setTimeout(function(){
       $('tr.mceLast td').css({ backgroundImage: 'none' });
        $('iframe#description_ifr').css({ height: '100px' });
    }, 500);

    //  Move Issue
    $('select#frmMoveIssue_project_id').bind('change',function(){
        $('select#frmMoveIssue_subproject_id').attr('disabled','true');


        var projectId=this.options[this.selectedIndex].value;
        //  @todo Need to be able to access this ajax method without being an admin, need to refactor servierside code into a reachable class
        new JsonCall('/api/projects/'+projectId+'/areas',{},function(oData){
            var oSelect=$('select#frmMoveIssue_subproject_id')[0];
            $(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#frmMoveIssue_subproject_id').attr('disabled','');
        });
    });



    //  Tab Panes
    new TabPane({
        element:'div#divIssuePropertiesTabPane',
        tabs:'div.Tabs ul li',
        pane:'div.Pane',
        current:'current',
        hide:'hide'
    });

    //  Actions for watching
    $('a#lnkStopWatching,a#lnkStartWatching').bind('click',function(){
        showLoading();
    });

    if($('div#iFileUploadContainer')[0]!=null){
        var attachmentPanelOptions={
          rootNode:$('div#iFileUploadContainer')[0],
          removeClass:'a._actRemove',
          onAdded:function(oNode){},
          onAllRemoved:function(){
              mc.doMagic($('div#iAttachmentContainer')[0]);
          }
        };

        var ct=new CloneableTemplateDecorator(attachmentPanelOptions);
        ct.begin();
    }

    //  Create magic hide show component
    var mc=new MagicDoer();

    //  Edit issue
    $('a#lnkEditIssue').bind('click',function(){
        toggleCurrentLink(this);
        mc.doMagic($('div#iIssueDetails')[0]);
        return false;
     });

    $('a#lnkEditIssueCancel').bind('click',function(){
        mc.undoMagic($('div#iIssueDetails')[0]);
        return false;
    });
    //
    //
    //
    //hax by reyhan
    $('input#lnkEditIssueCancel').bind('click',function(){
        mc.undoMagic($('div#iIssueDetails')[0]);
        return false;
    });

    $('input#lnkAddAnAttachmentCancel').bind('click',function(){
        mc.undoMagic($('div#iAttachmentContainer')[0]);
        return false;
    });

    $('input#lnkResolveIssueCancel').bind('click',function(){
        mc.undoMagic($('div#iResolutionContainer')[0]);
        return false;
    });

    $('input#lnkCloseIssue').bind('click',function(){
        mc.doMagic($('div#iCloseContainer')[0]);
        return false;
    });

    $('input#lnkCloseIssueCancel').bind('click',function(){
        mc.undoMagic($('div#iCloseContainer')[0]);
        return false;
    });


    $('input#lnkReestimateCancel').bind('click',function(){
        mc.undoMagic($('div#iCloseContainer')[0]);
        return false;
    });

    $('input#lnkReassignCancel').bind('click',function(){
        mc.undoMagic($('div#iReassignContainer')[0]);
        return false;
    });

    $('input#lnkLogTimeCancel').bind('click',function(){
        mc.undoMagic($('div#iLogTimeContainer')[0]);
        return false;
    });

    $('input#lnkMove').bind('click',function(){
        mc.doMagic($('div#iMoveContainer')[0]);
        return false;
    });

    $('input#lnkMoveCancel').bind('click',function(){
        mc.undoMagic($('div#iMoveContainer')[0]);
        return false;
    });


    //end hax lol
    //
    //
    //
    $('a#lnkCloseIssue').bind('click',function(){
        mc.doMagic($('div#iCloseContainer')[0]);
        return false;
    });

    $('a#lnkCloseIssueCancel').bind('click',function(){
        mc.undoMagic($('div#iCloseContainer')[0]);
        return false;
    });

    //  Actions for resolution
    $('a#lnkResolveIssue').bind('click',function(){
        toggleCurrentLink(this);
        mc.doMagic($('div#iResolutionContainer')[0]);
        new ResolveLogTimeRequiredValidator();
        return false;
    });

    $('a#lnkResolveIssueCancel').bind('click',function(){
        mc.undoMagic($('div#iResolutionContainer')[0]);
        return false;
    });

    $('a#lnkReassign').bind('click',function(){
        toggleCurrentLink(this);
        mc.doMagic($('div#iReassignContainer')[0]);
        checkForCurrentUser($('select#ddlAssignedTo').val());
        return false;
    });

    $('a#lnkReassignCancel').bind('click',function(){
        mc.undoMagic($('div#iReassignContainer')[0]);
        return false;
    });
    /**
     *  Radio buttons on re-assign
     */

    //  check user id against chosen radio value to hide show "notify by email" field
    function checkForCurrentUser(id){
        if(parseInt($('input#currentUserId').val())!=parseInt(id)){
            $('div#divNotifyAssigneeOnReassign').show();
        }else{
            $('div#divNotifyAssigneeOnReassign').hide();
        }
    }

    //  If any of the key role radios are checked hide the user dropdown
    $('input#frmAssignedToMe,input#frmAssignedToCreator,input#frmAssignedToLead').bind('click',function(){
        $('input#frmAssignedTo').val($(this).val());
        $('span#spanddlAssignedTo').addClass('hide');
        checkForCurrentUser($(this).val());
    });

    //  If the assigned to other radio is checked show the dropdown
    $('input#frmAssignedToOther').bind('click',function(){
        $('span#spanddlAssignedTo').removeClass('hide');
        $('input#frmAssignedTo').val($('select#ddlAssignedTo').val());
        checkForCurrentUser($('input#frmAssignedTo').val());
    });

    //  When the value of the dropdown changes update the assignedto field
    $('select#ddlAssignedTo').bind('change',function(){
        $('input#frmAssignedTo').val($(this).val());
        checkForCurrentUser($('input#frmAssignedTo').val());
    });

    //  Show the notify on startup if other is checked
    if($('input#frmAssignedToOther').attr('checked')){
        $('span#divNotifyAssigneeOnReassign').show();
        $('span#spanddlAssignedTo').removeClass('hide');
    }
    //  END!!!!!!

    //  check fixfor id against chosen select value to hide show "move due date to version release date" field
    function checkForCurrentFixFor(id){
        if(parseInt($('input#currentFixFor').val())!=parseInt(id) && parseInt(id) != -1){
            $('span#spanMoveDueOnToFixFor').show();
        }else{
            $('span#spanMoveDueOnToFixFor').hide();
        }
    }

    //  When the value of the dropdown changes update the move due date field
    $('select#ddlfrmFixFor').bind('change',function(){
        checkForCurrentFixFor($(this).val());
    });

    //  If any of the key role radios are checked hide the user dropdown
    $('input#frmAssignedToMe,input#frmAssignedToCreator,input#frmAssignedToLead').bind('click',function(){
        $('input#frmAssignedTo').val($(this).val());
        $('span#spanddlAssignedTo').addClass('hide');
        checkForCurrentUser($(this).val());
    });

    $('a#lnkReestimate').bind('click',function(){
        toggleCurrentLink(this);
        mc.doMagic($('div#iReestimateContainer')[0]);
        return false;
    });

    $('a#lnkReestimateCancel').bind('click',function(){
        mc.undoMagic($('div#iCloseContainer')[0]);
        return false;
    });

    $('a#lnkMoveIssue').bind('click',function(){
        toggleCurrentLink(this);
        mc.doMagic($('div#iMoveContainer')[0]);
        return false;
    });

    //  Actions to manage comment panel
    $('a#lnkLogTime').bind('click',function(){
        toggleCurrentLink(this);
        mc.doMagic($('div#iLogTimeContainer')[0]);
        return false;
    });

    $('a#lnkLogTimeCancel').bind('click',function(){
        mc.undoMagic($('div#iLogTimeContainer')[0]);
        return false;
    });

    $('a#lnkAddAnAttachment').bind('click',function(){
        toggleCurrentLink(this);

        mc.doMagic($('div#iAttachmentContainer')[0],function(state){

            ct.end();
        });
        ct.begin();
        return false;
    });

    $.hotkeys.add('return', {type: 'keydown', propagate: true},$.hotkeys.delegate(
            function(e){
               $('a#lnkAddAnAttachment').trigger('click');
            }
            ));

    $('a#lnkAddAnAttachmentCancel').bind('click',function(){
        mc.undoMagic($('div#iAttachmentContainer')[0]);
        return false;
    });

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

    $('a#lnkDeleteIssue').bind('click',function(){
        $.prompt(i18nConfirmDelete,{ submit: doDeleteIssue,buttons: { Ok: true, Cancel: false }, focus: 1 });
    });

    $('a#lnkCommentTags').bind('click',function(){
        $('div#helpCommentTags').slideDown();
        return false;
    });

    $('a#lnkCommentTagsClose').bind('click',function(){
    	$('div#helpCommentTags').slideUp();
        return false;
    });

    function doDeleteIssue(v,m){
    		if(v){
    			window.location.href = lnkDelete;
    		}
     		return true;
    }


function MagicDoer(oComponent,fCallback){
    var _oComponent=oComponent;
    var _fCallback=fCallback;

    this.doMagic=function(oComponent,fCallback){

        if($(oComponent).attr('display')=='none' && _oComponent==oComponent){

            return false;
        }

        enableFieldsInDiv(oComponent);

        if(_oComponent){
                $(_oComponent).slideUp(function(){
                    disableFieldsInDiv(_oComponent);
                    $(oComponent).slideDown();
                    enableFieldsInDiv(oComponent);
                })
        }else{
            $(oComponent).slideDown();
        }

        if(_fCallback) {_fCallback(false);}

        if(fCallback) fCallback(true);
        _fCallback = fCallback;
        _oComponent=oComponent;
    };

    this.undoMagic=function(oComponent,fCallback){
        $(_oComponent).slideUp();
        disableFieldsInDiv(oComponent);
        _oComponent=null;
    };
}

function ResolveLogTimeRequiredValidator(){
	var _oTimeField = $('#frmResolveLogTimeEffort');
	var _oNoTimeToLog = $('#chkNoTimeToLog');
	var _oiResolveLogTimeContainer = $('#iResolveLogTimeContainer');
	var _oResolveButton = $('#btnResolve');
	var _oTxtForceLogTime = $('#txtForceLogTime');

	checkValid=function(){
        if(_oTimeField.length > 0){
            if(_oTimeField.val() && _oTimeField.val().length > 0 && _oTimeField.val() > 0){
                _oResolveButton.removeAttr("disabled");
                _oTxtForceLogTime.addClass('hide');
            } else {
                _oResolveButton.attr("disabled","disabled");
                _oTxtForceLogTime.removeClass('hide');
            }
        }
    };

	checkOverride=function(obj){
		if($(obj).attr("checked") == true){
			$(_oiResolveLogTimeContainer).addClass('hide');
			disableFieldsInDiv(_oiResolveLogTimeContainer);
			_oResolveButton.removeAttr("disabled");
			_oTxtForceLogTime.addClass('hide');
		} else {
			$(_oiResolveLogTimeContainer).removeClass('hide');
			enableFieldsInDiv(_oiResolveLogTimeContainer);
			_oResolveButton.attr("disabled","disabled");
			_oTxtForceLogTime.removeClass('hide');
		}
	};

	function ResolveLogTimeRequiredValidator(){
		if(_oTimeField.length > 0){
			_oTimeField.bind('keyup',checkValid);
			if($(_oNoTimeToLog)){
				_oNoTimeToLog.bind('change',function(){
					checkOverride(this);
				});
			}
			checkValid();
			checkOverride(_oNoTimeToLog);
		}
	}

	ResolveLogTimeRequiredValidator();
}


});