﻿/* ------------------------------------------------------------------------ 
 * EditSection represents an overall section 
 * ------------------------------------------------------------------------ */        
 
var EditSection = Class.create();

Object.extend(EditSection.prototype, {  

    initialize: function(id, dbid, containerName, isNew, isNewVersion, index,
                             titleDivName) 
		{
		    this.mIndex = index;
		    this.mID = id;
		    this.mDBID = dbid;
		    this.mContainerName = containerName;
		    this.mIsNew = isNew;
		    this.mIsNewVersion = isNewVersion;		    
		    this.mSection = $(titleDivName).up('div.editSection');
		    this.mSaveBtn = this.mSection.down('a.saveSection');	
		    this.mDeleteBtn = this.mSection.down('a.deleteSection');	
		    this.mIsDirty = false;
		    this.mIsMain = false;
		    
		    var handler = this["HandlerForSaveClicked"].bind(this);
		    this.mSaveBtn.observe('click', handler);	
		    
		    if (this.mDeleteBtn != null)    		    
		    {
                this.mDeleteBtn.observe('click', this["HandlerForDeleteClicked"].bind(this));	  
                this.mDeleteBtn.observe('mouseover', this["HandlerForDeleteMouseMove"].bind(this));	  
                this.mDeleteBtn.observe('mouseout', this["HandlerForDeleteMouseLeave"].bind(this));	
		    }
		    		    
		    this["HandlerForSaveComplete"].bind(this);
		    this["HandlerForSaveException"].bind(this);
        },
    
    AssignDirtySectionClass: function(display, scroll)
        {
            this.AssignClass(display, scroll, 'dirty');
        },
        
    AssignDeleteSectionClass: function(display, scroll)        
        {
            this.AssignClass(display, scroll, 'deleteHL');
        },
        
    AssignClass: function(display, scroll, className)        
        {
            if (display)  
                this.mSection.addClassName(className);       
            else
                this.mSection.removeClassName(className);
                
            if (scroll)
                this.mSection.scrollTo();
        },
                
    AssignEditControls: function(title, subTitle, image, html)
        {
            this.mTitle = title
            this.mTitle.SetEditSection(this);            
            this.mSubTitle = subTitle;
            this.mSubTitle.SetEditSection(this);
            this.mImage = image;
            this.mImage.SetEditSection(this);
            this.mHtml = html;
            this.mHtml.SetEditSection(this);
        },

    AssignHtmlControl: function(html)
        {
            this.mHtml = html;
            this.mHtml.SetEditSection(this);
        },
        
    AssignResourceAndLinkEditAreas: function(resource, link)
        {        
            this.mResource = resource;
            this.mResource.SetEditSection(this);
            this.mLink = link;
            this.mLink.SetEditSection(this);
        },
        
    CheckForEmptyTitles: function(section)
        {
            if (section.Title.toLowerCase() == VDS$CSPage.EmptyTitleMsg)            
                section.Title = '';

            if (section.SubTitle.toLowerCase() == VDS$CSPage.EmptySubTitleMsg)
                section.SubTitle = '';
                
            return section;
        },
        
    Delete: function()
        {
            window.VDS$CSPage.DisplayServerOperation(true);
            
            var section = new VDS.VWeb.ClientSideUI.WebContentSectionDTO(); 
            
            section.ContainerName = this.mContainerName;
            section.IsNew = this.mIsNew; 
            section.IsNewVersion = this.mIsNewVersion;
            section.ID = this.mID;   
            section.DatabaseIdentifier = this.mDBID;
            section.Index = this.mIndex;
                
            VDS.VWeb.Services.WebContentService.DeleteWebContentSection(window.VDS$CSPage.PageDetails(),
                 section, this.HandlerForDeleteComplete, 
                 this.HandlerForSaveException, this.mIndex);                                                                    
        },

    GetEditSection: function()
        {
            var section = new VDS.VWeb.ClientSideUI.WebContentSectionDTO(); 
            
            section.ContainerName = this.mContainerName;
            section.IsNew = this.mIsNew; 
            section.IsNewVersion = this.mIsNewVersion;
            section.ID = this.mID;   
            section.DatabaseIdentifier = this.mDBID;
            section.Index = this.mIndex;
            section.Position = window.VDS$CSPage.RetrievePosition(this.mHtml.mContainerName);
                        
            return section;
        },
                            
    HandlerForDeleteClicked: function(event) 
		{
		    if (!window.VDS$CSPage.IsDirtyWithMessage())
		    {
		        if (this.PromptForDeletion())
		            this.Delete();
            }
		}, 

    HandlerForDeleteComplete: function(result, userContext) 
		{
		    window.VDS$CSPage.DisplayServerOperation(false);
		
		    if (result != null)
		        window.VDS$CSPage.SetPageDetails(result);  
		
		    var index = userContext - 1;
		    var editSection = window.VDS$EditSections[index];
            
            if (editSection == undefined)            
                return;
                
		    editSection.Remove();		    
		    window.VDS$CSPage.LoadVersionViewer();  
		},             
				
    HandlerForDeleteMouseMove: function(event)
        {
            if (event.target.hasClassName('deleteSection'))
                this.AssignDeleteSectionClass(true, false);
        },

    HandlerForDeleteMouseLeave: function(event)
        {
            if (event.target.hasClassName('deleteSection'))
                this.AssignDeleteSectionClass(false, false);
        },
        
    HandlerForSaveClicked: function(event) 
		{
		    if (this.mIsDirty)
		        this.Save();
		}, 
		
    HandlerForSaveComplete: function(result, userContext)
        {
            window.VDS$CSPage.SetPageDetails(result[0]);  
            
            var index = userContext - 1;
            var editSection = window.VDS$EditSections[index];
            
            if (editSection == undefined)            
            {
                window.VDS$CSPage.DisplayServerOperation(false);  
                return;
            }
                                 
            editSection.SetIsDirty(false);   
            editSection.SetSectionDetails(result[1]);
            window.VDS$CSPage.DisplayServerOperation(false);      
            window.VDS$CSPage.LoadVersionViewer();  
        },
        
    HandlerForSaveException: function(result, userContext)
        {   
            alert(result._message);
            
             var editSection = window.VDS$EditSections[userContext];
            
            if (editSection == undefined)            
                return;
                
            window.VDS$CSPage.DisplayServerOperation(false);  
        },
    
    IsDirty: function()
        {
            return this.mIsDirty;
        },               
        
    PromptForDeletion: function()
        {         
            if (confirm('Are you sure you want to delete this section?'))             
                return true;            
            else
                return false;            
        },

    PageContentSectionDeletionComplete: function(result, context) 
        {
            eval(result);
        },
            
    Remove: function()
        {
            var container = $(this.mSection).up('div.insertionPointContainer');            
            if (container != undefined)
                container.remove();
        },        
                
    Save: function()
        {
            if (this.mIsDirty)
            {
                window.VDS$CSPage.DisplayServerOperation(true);
                
                var section = this.SectionDetails();                               
                                      
                VDS.VWeb.Services.WebContentService.SaveWebContentSection(window.VDS$CSPage.PageDetails(),
                     section, this.HandlerForSaveComplete, 
                     this.HandlerForSaveException, this.mIndex);  
            }
        },
        
    Section: function()
        {
            return this.mSection;
        },
   
    SectionDetails: function()
    {
        var section = new VDS.VWeb.ClientSideUI.WebContentSectionDTO(); 

        section.ContainerName = this.mContainerName;
        section.IsNew = this.mIsNew; 
        section.IsNewVersion = this.mIsNewVersion;
        section.ID = this.mID;   
        section.DatabaseIdentifier = this.mDBID;
        section.Index = this.mIndex;
        section.Position = window.VDS$CSPage.RetrievePosition(this.mHtml.mContainerName);
        section.IsMain = this.mIsMain;
        
        if (this.mTitle != null)
        {
            section.Title = this.mTitle.mTag.innerHTML;   
            section.SubTitle = this.mSubTitle.mTag.innerHTML;
            section.Image = this.mImage.mTag.innerHTML;
        }
        
        section.Body = this.mHtml.mTag.innerHTML;
        
        return this.CheckForEmptyTitles(section);        
    },
     
    SetIsDirty: function(isDirty)
    {
            this.mIsDirty = isDirty;
            
            if (this.mIsDirty)
            {
                this.mSaveBtn.show();
                this.AssignDirtySectionClass(true, true);
            }
            else
            {
                this.mSaveBtn.hide();                
                this.AssignDirtySectionClass(false, true);
            }
    },
        
    SetSectionDetails: function(section)
    {
        this.mIsNew = section.IsNew;
        this.mIsNewVersion = section.IsNewVersion;
        this.mID = section.ID;
        this.mDBID = section.DatabaseIdentifier;
    }
}); 



/* ------------------------------------------------------------------------ 
 * ExpirySection represents an inherited EditSection
 * ------------------------------------------------------------------------ */        
var ExpirySection = Class.create(EditSection,
    {
        initialize: function(start, end, hidden) 
		{
		    this.mStartCtl = start;
		    this.mEndCtl = end;
		    this.mIsHiddenCtl = hidden;
		    this.mSection = $(this.mStartCtl).up('div.section');
		    this.mSaveBtn = this.mSection.down('a.saveSection');	
		    this.mIsDirty = false;
		    
		    this.BindHandlers();
        },
        
        BindHandlers: function()
        {           
		    this.mSaveBtn.observe('click', this["HandlerForSaveClicked"].bind(this));	
		    $(this.mStartCtl).observe('change', this["HandlerForEditStarted"].bind(this));	
		    $(this.mEndCtl).observe('change', this["HandlerForEditStarted"].bind(this));	
		    
		    var hidden = $(this.mIsHiddenCtl);
		    if (hidden)
		        hidden.observe('click', this["HandlerForEditStarted"].bind(this));	
        },
                
        HandlerForEditStarted: function(event)
        {   
            if (!window.VDS$CSPage.IsNew())
                window.VDS$ExpirySection.SetIsDirty(true);        
        },
        
        HandlerForSaveComplete: function(result, userContext)
        {                  
            window.VDS$CSPage.SetPageDetails(result);  
            window.VDS$ExpirySection.SetIsDirty(false);        
            window.VDS$CSPage.DisplayServerOperation(false);      
        },
        
        HandlerForSaveException: function(result, userContext)
        {   
            alert(result._message);                            
            window.VDS$ExpirySection.SetIsDirty(false);  
            window.VDS$CSPage.DisplayServerOperation(false);  
        },
        
        Save: function()
        {
            if (this.mIsDirty)
            {
                window.VDS$CSPage.DisplayServerOperation(true);
                
                // Retrieve selected keywords                  
                var wContent = window.VDS$CSPage.PageDetails();
                wContent.Start = $(this.mStartCtl).value;
                wContent.End = $(this.mEndCtl).value;
                
                var hidden = $(this.mIsHiddenCtl);
		        if (hidden)
		            wContent.IsHidden = hidden.checked;
		            
                VDS.VWeb.Services.WebContentService.SaveWebContentExpiryDates(wContent, 
                        this.HandlerForSaveComplete, this.HandlerForSaveException);  
            }
        }
});

        

        
        

        

