﻿/* ------------------------------------------------------------------------ 
 * VersionViewer represents a client side control that allows versions to be viewed, approved, declined etc
 * ------------------------------------------------------------------------ */         
var VersionViewer = Class.create();

Object.extend(VersionViewer.prototype, {

    initialize: function(userCtl, dbid, editStatus, isLive, opsContainer,
                notesContainer, opsMenuBtn, opsVisible, roles, domainObject) {
        this.mWebContent = new VDS.VWeb.ClientSideUI.WebContentDTO();
        this.mWebContent.DatabaseIdentifier = dbid;
        this.mWebContent.EditStatus = editStatus;
        this.mWebContent.IsLive = isLive;
        this.mWebContent.Roles = roles;
        this.mWebContent.DomainObjectType = domainObject;

        if (userCtl.startsWith('dnn$'))
            this.mUserCtl = userCtl;
        else if (VDS$CSPage.DomainObject() == 101)
            this.mUserCtl = VDS$CSPage.CtlPrefix() + 'PageContentEdit$mVersionInfoViewer';
        else if (VDS$CSPage.DomainObject() == 104)
            this.mUserCtl = VDS$CSPage.CtlPrefix() + 'NewsItemEdit$mVersionInfoViewer';
        else if (VDS$CSPage.DomainObject() == 100)
            this.mUserCtl = window.VDS$VersionViewerName + '$mVersionInfoViewer';

        this.mOpsCon = opsContainer;
        this.mNotesCon = notesContainer;
        this.mApprove = 'Approve';
        this.mDecline = 'Decline';
        this.mRequest = 'Request';
        this.mEditMode = 'EditMode';
        this.mLiveMode = 'LiveMode';
        this.mOpsMenuBtn = opsMenuBtn;

        this.BindHandlers(opsVisible);
        this.DisplayOperations(this.mWebContent);
    },

    BindHandlers: function(opsVisible) {
        if (!opsVisible)
            return;

        var menuHandler = this["HandlerForDisplayMenu"].bind(this);
        $(this.mOpsMenuBtn).observe('click', menuHandler);

        //$(this.mOpsCon).observe('mouseover', this["HandlerForMouseOver"].bind(this));
        //$(this.mOpsCon).observe('mouseout', this["HandlerForMouseOut"].bind(this));

        var container = $(this.mOpsCon);

        this.BindOperation(container.down('a.' + this.mLiveMode), null, this["HandlerForLiveModeClicked"].bind(this));
        this.BindOperation(container.down('a.' + this.mEditMode), null, this["HandlerForEditModeClicked"].bind(this));
        this.BindOperation(container.down('a.' + this.mApprove), null, this["HandlerForApproveClicked"].bind(this));
        this.BindOperation(container.down('a.' + this.mRequest), null, this["HandlerForRequestClicked"].bind(this));
        this.BindOperation(container.down('a.' + this.mDecline), null, this["HandlerForEnterNotesClicked"].bind(this));

        this.mConfirmLnk = $(this.mNotesCon).down('a.commitLnk');
        if (this.mConfirmLnk != null) {
            var handler = this["HandlerForDeclineClicked"].bind(this);
            this.mConfirmLnk.observe('click', handler);
        }

        this.mCancelLnk = $(this.mNotesCon).down('a.cancelLnk');
        if (this.mCancelLnk != null) {
            var handler = this["HandlerForCancelClicked"].bind(this);
            this.mCancelLnk.observe('click', handler);
        }
    },

    BindOperation: function(anchor, image, handler) {
        if (anchor != null)
            anchor.observe('click', handler);

        if (image != null)
            image.observe('click', handler);
    },

    DisplayNotes: function(display) {
        if (display) {
            $(this.mNotesCon).show();
            $(this.mOpsCon).hide();
            $(this.mNotesCon).down('input.notes').focus();
        }
        else {
            $(this.mNotesCon).hide();
            $(this.mOpsCon).show();
            $(this.mNotesCon).down('input.notes').value = "";
        }
    },

    DisplayOperations: function(wContent) {
        var container = $(this.mOpsCon);

        var isManager = false;
        if (wContent.EditStatus == 2) {
            if (this.mWebContent.DomainObjectType == 101 || this.mWebContent.DomainObjectType == 100) {
                if (this.mWebContent.Roles.search(/Content Manager;/) != -1)
                    isManager = true;
            }
            else if (this.mWebContent.DomainObjectType == 104) {
                if (this.mWebContent.Roles.search(/News Manager;/) != -1)
                    isManager = true;
            }
        }

        this.DisplayOperation(container.down('a.' + this.mApprove), null, isManager);
        this.DisplayOperation(container.down('a.' + this.mRequest), null, wContent.EditStatus == 1);
        this.DisplayOperation(container.down('a.' + this.mDecline), null, isManager);
        this.DisplayOperation(container.down('a.' + this.mLiveMode), null, !this.mWebContent.IsLive);
        this.DisplayOperation(container.down('a.' + this.mEditMode), null, true);
    },

    DisplayOperation: function(anchor, image, visible) {
        if (visible) {
            anchor.show();

            if (image != null)
                image.show();
        }
        else {
            anchor.hide();

            if (image != null)
                image.hide();
        }
    },

    HandlerForApproveClicked: function(event) {
        if (window.VDS$CSPage.PromptForConfirm('Are you sure you want to Approve this content?')) {
            window.VDS$CSPage.DisplayServerOperation(true);

            WebForm_DoCallback(this.mUserCtl,
                    this.mApprove + ":" + window.VDS$CSPage.mDBID,
                    this.HandlerForUpdateComplete, null,
                    this.HandlerForException, true);
        }
    },

    HandlerForCancelClicked: function(event) {
        this.DisplayNotes(false);
	window.VDS$CSPage.DisplayServerOperation(false);
    },

    HandlerForDeclineClicked: function(event) {
        if (window.VDS$CSPage.PromptForConfirm('Are you sure you want to Decline this content?')) {
            window.VDS$CSPage.DisplayServerOperation(true);

            WebForm_DoCallback(this.mUserCtl,
                    this.mDecline + ":" + window.VDS$CSPage.mDBID
                        + ":" + $(this.mNotesCon).down('input.notes').value,
                    this.HandlerForUpdateComplete, null,
                    this.HandlerForException, true);
        }

        this.DisplayNotes(false);
    },

    HandlerForDisplayMenu: function(event) {
        this.PositionOpsMenu();
        $(this.mOpsCon).toggle();
    },

    HandlerForEditModeClicked: function(event) {
        WebForm_DoCallback(this.mUserCtl,
                    this.mEditMode + ":" + window.VDS$CSPage.mDBID,
                    this.HandlerForUpdateComplete, null,
                    this.HandlerForException, true);
    },

    HandlerForEnterNotesClicked: function(event) {
        this.DisplayNotes(true);
    },

    HandlerForLiveModeClicked: function(event) {
        WebForm_DoCallback(this.mUserCtl,
                this.mLiveMode + ":" + window.VDS$CSPage.mDBID,
                this.HandlerForUpdateComplete, null,
                this.HandlerForException, true);
    },

    HandlerForRequestClicked: function(event) {
        if (window.VDS$CSPage.PromptForConfirm('Are you sure you want to Request Approval for this content?')) {
            window.VDS$CSPage.DisplayServerOperation(true);

            WebForm_DoCallback(this.mUserCtl,
                    this.mRequest + ":" + window.VDS$CSPage.mDBID,
                    this.HandlerForUpdateComplete, null,
                    this.HandlerForException, true);
        }
    },

    HandlerForMouseOver: function(event) {
        if (event.target.hasClassName('optionsItem'))
            event.target.addClassName('sel');
    },

    HandlerForMouseOut: function(event) {
        if (event.target.hasClassName('optionsItem'))
            event.target.removeClassName('sel');
    },

    HandlerForUpdateComplete: function(result, userContext) {
        //result = result.replace("0", window.VDS$CSPage.mDBID);
        eval(result);
        //            window.VDS$VV.DisplayOperations(result);   
        window.VDS$CSPage.DisplayServerOperation(false);      
    },

    HandlerForException: function(result, userContext) {
        if (typeof (result) == 'string')
            alert(result);
        else
            alert(result._message);
        window.VDS$CSPage.DisplayServerOperation(false);
    },

    PositionOpsMenu: function() {
        $(this.mOpsCon).clonePosition($(this.mOpsMenuBtn),
            { setHeight: false, setWidth: false, offsetLeft: -85, offsetTop: 13 });
    }
});		  		
		  
/* ------------------------------------------------------------------------ 
 * Toggler represents a collapsible div
 * ------------------------------------------------------------------------ */         
var Toggler = Class.create();

Object.extend(Toggler.prototype, {  

    initialize: function(container, target, header, expandImg, collapseImg, placementContainer, draggable) 
		{
		    this.mContainer = container;
		    this.mTarget = target;
		    this.mHeader = header;
		    var handle = $(this.mHeader).up().down('div.top');
		    
		    if (draggable)		    		        
		        this.mDrag = new Draggable($(this.mHeader).up(), {handle:handle});		    		    
            else
                handle.setStyle({ cursor: 'default'});
            
		    this.mExpandImg = expandImg;	    
		    this.mCollapseImg = collapseImg;
		    this.BindHandlers();
		    
		    if ($(this.mTarget).visible())
		        this.Collapse();		        
        },
        
    BindHandlers: function()
        {
            var handler = this["HandlerForToggle"].bind(this);
            $(this.mHeader).down('img.toggle').observe('click', handler);
        },
        
    Collapse: function()
        {
            $(this.mTarget).toggle(); 
            var img = $(this.mHeader).down('img.toggle');
            
            if (img != null)
            {
                img.src = img.src.replace(this.mCollapseImg, this.mExpandImg)
                img.alt = "Maximise";
            }
        },

    Expand: function()
        {
            $(this.mTarget).toggle(); 
            var img = $(this.mHeader).down('img.toggle');
            
            if (img != null)
            {
                img.src = img.src.replace(this.mExpandImg, this.mCollapseImg)
                img.alt = "Minimise";
            }
        },
                        
    HandlerForToggle: function(event)
        {
            if ($(this.mTarget).visible())
                this.Collapse();
            else
                this.Expand();
            
            event.stop();
        }        
});		  		
		  		

/* ------------------------------------------------------------------------ 
 * Watermark represents a label within a input area
 * ------------------------------------------------------------------------ */         
var Watermark = Class.create();

Object.extend(Watermark.prototype, {  

    initialize: function(target, unselectedValue, message)  
		{
            this.mTarget = target;		
            this.mMessage = unselectedValue;
            if (message == undefined)
		        this.mDisplayMessage = unselectedValue;    		    
		    else
		        this.mDisplayMessage = message;    		    
		    this.mEmptyClassName = 'italic'		    		    
		    this.BindHandlers();
		    
		    if (this.IsEmpty())		    
		        this.DisplayInitMessage();
        },
        
    BindHandlers: function()
        {
            this.BindOperation($(this.mTarget), 'click', this["HandlerForClick"].bind(this));            
            this.BindOperation($(this.mTarget), 'blur', this["HandlerForLeave"].bind(this));            
        },
  
    BindOperation: function(element, eventName, handler)
        {
            if (element != null)
                element.observe(eventName, handler);            
        },
        
    DisplayInitMessage: function()
        {            
            $(this.mTarget).addClassName('italic'); 
            
            if (this.IsSelectTag())            
                $(this.mTarget)[0].text == this.mDisplayMessage;
            else
                $(this.mTarget).value = this.mDisplayMessage;                     
        },
                    
    DisplayMessage: function()
        {
            $(this.mTarget).addClassName('italic');      
            
            if (this.IsSelectTag())            
                $(this.mTarget)[0].text == this.mMessage;
            else
                $(this.mTarget).value = this.mMessage;                     
        },

    IsSelectTag: function()
        {
            return ($(this.mTarget).type == "select-one");
        },
    
    HandlerForClick: function(event)
        {
            if (this.IsEmpty())
            {
                event.target.value = '';
                event.target.removeClassName('italic');              		    
            }
        },
                
    HandlerForLeave: function(event)
        {
            if (this.IsEmpty())
                this.DisplayMessage();
        },
    
    IsEmpty: function()
        {
            if ($(this.mTarget).value == '')
                return true;
                
            if (this.IsSelectTag())            
                return $(this.mTarget)[$(this.mTarget).selectedIndex].text == this.mMessage;
            else
                return $(this.mTarget).value == this.mMessage;            
        }
});	  		

/* ------------------------------------------------------------------------ 
 * DropdownListWatermark represents an inherited Watermark
 * ------------------------------------------------------------------------ */        
var DropdownListWatermark = Class.create(Watermark,
{
        initialize: function(target, unselectedValue, message) 
		{
            this.mTarget = $(target);
		    this.mMessage = unselectedValue;
		    this.mDisplayMessage = message;
		    this.mEmptyClassName = 'italic'		    		    
		    this.BindHandlers();
		    this.mFontSize = $(this.mTarget).getStyle('fontSize');
		    this.mFontStyle = $(this.mTarget).getStyle('fontStyle');
		    this.mColor = $(this.mTarget).getStyle('color');
		    
		    if (this.IsEmpty())		    
		        this.DisplayInitMessage();

        }, 
        
        BindHandlers: function()
        {
            this.BindOperation($(this.mTarget), 'click', this["HandlerForClick"].bind(this));            
            this.BindOperation($(this.mTarget), 'change', this["HandlerForTextChanged"].bind(this));            
            this.BindOperation($(this.mTarget), 'blur', this["HandlerForLeave"].bind(this));            
        },

        DisplayInitMessage: function()
        {            
            $(this.mTarget).addClassName('italic');    
            $(this.mTarget)[0].text == this.mDisplayMessage;                         
        },
                
        DisplayMessage: function()
        {            
            $(this.mTarget).addClassName('italic');   
            $(this.mTarget)[0].text == this.mMessage;                      
        },
        
        HandlerForClick: function(event)
        {
        /*
            if (this.IsEmpty())
            {
                event.target.value = '';         
                event.target.removeClassName('italic');                             
            }
            */
        },
        
        HandlerForLeave: function(event)
        {
            //if (this.IsEmpty())
              //  this.DisplayMessage();
        },
        
        HandlerForTextChanged: function(event)
        {
        /*
            if (this.IsEmpty())
                this.DisplayMessage();
            else
                event.target.selectedIndex = event.target.selectedIndex;
                */
        }
});   

/* ------------------------------------------------------------------------ 
 * DnnWatermark represents an inherited Watermark
 * ------------------------------------------------------------------------ */        
var DnnWatermark = Class.create(Watermark,
{
        initialize: function(target, unselectedValue, message) 
		{
            this.mTarget = $(target).down('input.rcbInput').id;		    
		    this.mMessage = unselectedValue;
		    this.mDisplayMessage = message;
		    this.mEmptyClassName = 'italic'		    		    
		    this.BindHandlers();
		    this.mFontSize = $(this.mTarget).getStyle('fontSize');
		    this.mFontStyle = $(this.mTarget).getStyle('fontStyle');
		    this.mColor = $(this.mTarget).getStyle('color');
		    
		    if (this.IsEmpty())		    
		        this.DisplayInitMessage();

        }, 
        
        BindHandlers: function()
        {
            this.BindOperation($(this.mTarget), 'click', this["HandlerForClick"].bind(this));            
            this.BindOperation($(this.mTarget), 'change', this["HandlerForTextChanged"].bind(this));            
            this.BindOperation($(this.mTarget), 'blur', this["HandlerForLeave"].bind(this));            
        },

        DisplayInitMessage: function()
        {            
            $(this.mTarget).value = this.mDisplayMessage;             
            $(this.mTarget).setStyle( { fontStyle: 'italic',
                                    fontSize: '11px',
	                                color: '#BEBEBE' });              
        },
                
        DisplayMessage: function()
        {            
            $(this.mTarget).value = this.mMessage;             
            $(this.mTarget).setStyle( { fontStyle: 'italic',
                                    fontSize: '11px',
	                                color: '#BEBEBE' });              
        },
        
        HandlerForClick: function(event)
        {
            if (this.IsEmpty())
            {
                event.target.value = '';                
                event.target.setStyle( { fontStyle: this.mFontStyle,
                                    fontSize: this.mFontSize,
	                                color: this.mColor });                 		    
            }
        },
        
        HandlerForLeave: function(event)
        {
            if (this.IsEmpty())
                this.DisplayMessage();
        },
        
        HandlerForTextChanged: function(event)
        {
            if (this.IsEmpty())
                this.DisplayMessage();
            else
                event.target.value = event.target.value.strip();
        }
});        

/*
http://www.telerik.com/community/forums/thread/b311D-mbata.aspx
*/

/* ------------------------------------------------------------------------
 * ReturnHandler
 * ------------------------------------------------------------------------ */
  
var ReturnHandler = Class.create();

Object.extend(ReturnHandler.prototype, {  
  initialize: function(elementName, buttonName) 
		{           
    		this.mElement = $(elementName);
			this.mButton = $(buttonName);	
			this.BindHandlers();
  		}, 
  		
    BindHandlers: function()
    {
        var handler = this["HandlerForKeyDown"].bind(this);
        this.mElement.observe('keydown', handler);
    },
      		
    HandlerForKeyDown: function(event) 
    {
        if (event.keyCode != 13)
        {
            event.cancel = false;
            return;
        }	
        
        if (event.keyCode == 13)
        {
            event.returnValue=false;
            event.cancel = true;
            this.mButton.click();
        }	
	}
});