﻿/*The place where this script was placed is important.
  It should be after divZoneClient and before phControls*/
function CMSBox() {
    this.id = '';
    this.canMoveAndResize = false;
    this.hddPosition = '';
    this.heightTop = 0;
}

function CMSContainer(_divContainerClientID){
    this.divContainerClientID = _divContainerClientID;
    this.CMSBoxList = new Array();
    
    this.GetCMSBox = function(divClientID){
        for(x in this.CMSBoxList){
            if (this.CMSBoxList[x].id == divClientID){
                //if the div id exists, the object is returned
                return this.CMSBoxList[x];
            }
        }
        //Else created a new object
        var obj = new CMSBox();
        obj.id = divClientID;
        
        this.CMSBoxList.push(obj);
        
        return obj;
    }
    this.InsertCMSBox = function(divClientID) {
        return this.GetCMSBox(divClientID);
    }

    this.UpdateContainerHeight = function() {
        var maxHeightTop = 0;
        for (x in this.CMSBoxList) {
            if (this.CMSBoxList[x].heightTop > maxHeightTop) {

                maxHeightTop = this.CMSBoxList[x].heightTop;
            }
        }
        //alert(maxHeightTop);
        if (maxHeightTop > 0) {
            var divContainer = document.getElementById(this.divContainerClientID);
            divContainer.style.height = maxHeightTop + 'px';
        }
    }
}

function UseResizer(cmsContainer) {
    this.dragresize = new DragResize('ANYNAME', { minWidth: 50, minHeight: 50, minLeft: 0, minTop: 0 });
    
    //Is able to move
    this.dragresize.isElement = function(elm) {
        for (x in cmsContainer.CMSBoxList) {
            cmsBox = cmsContainer.CMSBoxList[x];
            if (elm.id == cmsBox.id && cmsBox.canMoveAndResize) {
                return true;
            }
        }

    };
    
    //Is able to resize
    this.dragresize.isHandle = this.dragresize.isElement;
    
    this.dragresize.ondragmove = function(isResize, element_id) {
               
        var cmsBox = cmsContainer.GetCMSBox(element_id);
        cmsBox.heightTop = dragresize.elmY + dragresize.elmH;

        var hddPosition = document.getElementById(cmsBox.hddPosition);
        
        hddPosition.value = dragresize.elmY + ';' + (dragresize.elmX - (document.body.clientWidth / 2)) + ';';
        hddPosition.value = hddPosition.value + dragresize.elmW + ';' + dragresize.elmH;
        
        cmsContainer.UpdateContainerHeight();
    }

    this.dragresize.ondragend = function(isResize) {
    
        
    };

    this.dragresize.apply(document);

    for (x in cmsContainer.CMSBoxList) {
        cmsBox = cmsContainer.CMSBoxList[x];

        cmsBox.heightTop = parseInt(document.getElementById(cmsBox.id).style.top);
        cmsBox.heightTop += parseInt(document.getElementById(cmsBox.id).style.height);
        
        cmsContainer.UpdateContainerHeight();
    }

}
function UseResizerAdmin(divBoxID, hddPositionID){

    var dragresize = new DragResize(divBoxID, { minWidth: 50, minHeight: 50, minLeft: 0, minTop: 0, zIndex:9999 });
    dragresize.isElement = function(elm){            
        if (elm.id == divBoxID) {
            return true;
        }
    };
    dragresize.isHandle = function(elm){       
        if (elm.id == divBoxID) {
            return true;
        }
    };        
    dragresize.ondragmove = function(isResize) {        
    }        
    dragresize.ondragend = function(isResize) {
        var hhdPosition = document.getElementById(hddPositionID);
        hhdPosition.value = dragresize.elmY + ';' + (dragresize.elmX-(document.body.clientWidth / 2)) + ';';
        hhdPosition.value = hhdPosition.value +  dragresize.elmW + ';' + dragresize.elmH;       
    };        
    dragresize.apply(document);          
}
/*
function debug(msg) {

    document.getElementById('txtdebug').value = msg + '\n' + document.getElementById('txtdebug').value;
}*/
