
/*************************************************************************
*
* ADOBE CONFIDENTIAL
* ___________________
*
*  Copyright 2011 Adobe Systems Incorporated
*  All Rights Reserved.
*
* NOTICE:  All information contained herein is, and remains
* the property of Adobe Systems Incorporated and its suppliers,
* if any.  The intellectual and technical concepts contained
* herein are proprietary to Adobe Systems Incorporated and its
* suppliers and may be covered by U.S. and Foreign Patents,
* patents in process, and are protected by trade secret or copyright law.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from Adobe Systems Incorporated.
*
* $DateTime:  $
*
* Adobe Output Module 4.0
*
* MediaGallery Originally Written by Quality Process Incorporated, Enhanced by
* Adobe Systems China. Contact Sheet written by Adobe Systems China.
* 
*
**************************************************************************/

/**
  * Cabinet class
  * define a special container
  *
  *  @Date: 2008-1-7
*/


AOM.Cabinet = function(group) {
	this.sections = {};
	this.group = group;
	this.group.maximumSize.height =AOM.MAXIMUM_SIZE_HEIGHT;
	this.create();
};




AOM.Cabinet.prototype.create = function() {
	var mainGroup = this.group.add(AOM.Cabinet.mainGroupResource);
	mainGroup.maximumSize.height =AOM.MAXIMUM_SIZE_HEIGHT;
	mainGroup.children[0].maximumSize.height =AOM.MAXIMUM_SIZE_HEIGHT;
	mainGroup.children[0].children[0].maximumSize.height =AOM.MAXIMUM_SIZE_HEIGHT;
	
	mainGroup.vscrollCurrentValue = 0;
	
	if (mainGroup.children[0].vscrollbar != undefined) {
		//custom scrollbar
		var cvscroller = new AOM.CustomScrollbar(mainGroup.children[0].vscrollbar);
		
		mainGroup.children[0].vscrollbar.onChanging = function() {
			AOM.Cabinet.vscroll(mainGroup, this);
			this.notify("onDraw");
		}
	}
	
	if(mainGroup.hscrollbar != undefined) {
		// custom scrollbar
		var chscroller = new AOM.CustomScrollbar(mainGroup.hscrollbar);
		
		mainGroup.hscrollbar.onChanging = function() {
			AOM.Cabinet.hscroll(mainGroup, this);
			this.notify("onDraw");
		}
	}

	this.scrollgroup = mainGroup.children[0].children[0];
}


AOM.Cabinet.vscroll = function(mainGroup, vscrollbar) {
	var scrollgroup = mainGroup.children[0].scrollgroup;
	var vscroller = mainGroup.children[0].vscrollbar;
	

	scrollgroup.location.y = - vscrollbar.value;
	scrollgroup.size.height =  vscroller.size.height - scrollgroup.location.y;  
	
	mainGroup.vscrollCurrentValue = vscrollbar.value;
}


AOM.Cabinet.hscroll = function(mainGroup, hscrollbar) {
	var scrollgroup = mainGroup.children[0].scrollgroup;
	var hscroller = mainGroup.hscrollbar;
	
	scrollgroup.location.x = - hscrollbar.value;
	scrollgroup.size.width = hscroller.size.width - scrollgroup.location.x;  
}


AOM.Cabinet.prototype.addFoldableSection  = function(sectionResource, sectionName, sectionTitle) {
	var cabinetSection  = new AOM.CabinetSection(this.scrollgroup, sectionResource, sectionName, sectionTitle, this);
	
	this.sections[sectionName] = cabinetSection.contentGroup;
	this.sections[sectionName].margins = [20, 10, 5, 10];
	
	return this.sections[sectionName];
};

AOM.Cabinet.prototype.onClickFoldableSection = function(openCloseButton) {
	//
}

AOM.Cabinet.prototype.removeAllFoldableSections = function()
{
    for(var i = 0; i < this.scrollgroup.children.length; ++i) {
        var child = this.scrollgroup.children[i];
        if(child.openCloseBtn != undefined) {
            this.scrollgroup.remove(i);
            --i;
        }
    }
}

AOM.Cabinet.prototype.addSection = function(sectionResource, sectionName) {
	var sectionGroup = this.scrollgroup.add(sectionResource);	
	sectionGroup.maximumSize.height =AOM.MAXIMUM_SIZE_HEIGHT;
	sectionGroup.isCabinetSection = false;
	
	this.sections[sectionName] = sectionGroup;
	
	return this.sections[sectionName];
}


AOM.Cabinet.prototype.getSection = function(sectionName) {
	if (sectionName == undefined) {
		return undefined;
	}
	
	return this.sections[sectionName];
}


/**
  * CabinetSection
  * Define a special container section
  *
  *  @Date: 2007-1-7
*/
AOM.CabinetSection = function(scrollgroup, contentResource, sectionName,  sectionTitle, foldableSet) {
	this.create(scrollgroup, contentResource, sectionName, sectionTitle, foldableSet);

	this.initialize();
};

AOM.CabinetSection.prototype.create = function(scrollgroup, contentResource, sectionName, sectionTitle, foldableSet) {
	this.sectionGroup = scrollgroup.add(AOM.Cabinet.sectionResource);
	this.sectionGroup.maximumSize.height =AOM.MAXIMUM_SIZE_HEIGHT;
	this.sectionGroup.children[1].maximumSize.height =AOM.MAXIMUM_SIZE_HEIGHT;
	
	this.sectionGroup.sectionName = sectionName;
	
	this.contentGroup = this.sectionGroup.children[1].add(contentResource);
	this.contentGroup.maximumSize.height =AOM.MAXIMUM_SIZE_HEIGHT;
	
	if (this.sectionGroup.children[0] instanceof IconButton == false) {
		return undefined;
	}

	var openCloseBtn = this.sectionGroup.children[0];
	openCloseBtn.text = sectionTitle;
	openCloseBtn.name = "isOpen" + sectionName;
	openCloseBtn.onClick = function() {
		AOM.CabinetSection.accordionClick(this, scrollgroup);
		foldableSet.onClickFoldableSection(this);
	}	
};

AOM.CabinetSection.prototype.initialize = function() {
	this.sectionGroup.isCabinetSection = true;
    var aCabinetGroup = this;
    
    this.sectionGroup.expand = function() {
         aCabinetGroup.setVisible(this, true);
		return AOM.CabinetSection.expand(this);
	}

	this.sectionGroup.collapse = function() {
        aCabinetGroup.setVisible(this, false);
        return AOM.CabinetSection.collapse(this);
	}
};

AOM.CabinetSection.prototype.setVisible = function(group, isVisible)
{
    for (var i = 0; i < group.children.length; i++) {
        var child = group.children[i];	
        var type = child.type;
        if (child.themeType != undefined)  {type = child.themeType;}
        if (type != undefined) type = type.toLowerCase();
        
        if (type == "group" || type == "panel") {
            this.setVisible(child, isVisible);
        } else {
            if (type != "openclosebtn")  
                child.visible = isVisible;
        }
    }
}

AOM.CabinetSection.accordionClick = function(openCloseBtn, scrollgroup) {
	openCloseBtn.isOpen = !openCloseBtn.isOpen;
	scrollgroup.parent.parent.parent.layout.layout(true);
};

AOM.CabinetSection.expand = function(sectionGroup) {
	//return sectionGroup.size.height;
};

AOM.CabinetSection.collapse = function(sectionGroup) {
	sectionGroup.size.height = AOM.OPENCLOSEBTN_HEIGHT;
	return sectionGroup.size.height;
};



AOM.CabinetLayoutManager = function(group) {
	this.initSelf(group);
}

AOM.CabinetLayoutManager.prototype.initSelf = function(group) {
	this.group = group;
}

AOM.CabinetLayoutManager.prototype.layout = function() {
	//1,get container dimension
	var group = this.group;
	var groupWidth = group.size.width;
	var groupHeight = group.size.height;
		
	//2 get group object
	var maingroup = group.children[0];
		
	//3 layout		
	var scrollgroup = maingroup.children[0].children[0];
	var scrollgroupCompactHeight = scrollgroupCompactWidth = 0;
	for (var i = 0; i < scrollgroup.children.length; i++) {
		var child = scrollgroup.children[i];
			
		//before recalculate, init openclosebtn size
		child.children[0].size = [AOM.OPENCLOSEBTN_HEIGHT, AOM.OPENCLOSEBTN_HEIGHT];
			
		if (child.layout != undefined) {
			child.layout.layout(true);
		}
		child.size = child.preferredSize;

		if (child.isCabinetSection) {
            if (child.children[0].isOpen) {
                child.expand();
            } else {
                child.collapse();
            }
		} 

		scrollgroupCompactHeight += child.size.height;
		if (scrollgroupCompactWidth < child.size.width)  {
				scrollgroupCompactWidth = child.size.width;
		}
	}

	//4, location
	maingroup.location = [0, 0];
	maingroup.children[0].location = [0, 0];
	maingroup.children[0].children[0].location = [0, 0];
		
	var nextLocation = scrollgroup.location.y;
	for (var i = 0; i < scrollgroup.children.length; i++) {
		var child = scrollgroup.children[i];
		child.location = [0, nextLocation];
		if (child.isCabinetSection && !child.children[0].isOpen) {
			nextLocation += child.size.height - 1;    
		} else {
			nextLocation += child.size.height;
		}
	}
		
	//5, scroll
	var vscroller = maingroup.children[0].children[1];
	var hscroller = maingroup.children[1];
	vscroller.visible = hscroller.visible = false;
		
	var GX = groupWidth,  GY = groupHeight;
	var hx = AOM.HSCROLLER_HEIGHT, hy =  AOM.VSCROLLER_WIDTH;
	var x = scrollgroupCompactWidth, y = scrollgroupCompactHeight;
		
	var hasVScroller, hasHScroller;
	if ( x <= GX && y <= GY) {
		hasVScroller = false;
		hasHScroller = false;
	} 
	if (x > GX && y <=(GY - hx)) {
		hasVScroller = false;
		hasHScroller = true;
	}
	if (y > GY && x <= (GX - hy)) {
		hasVScroller = true;
		hasHScroller = false;
	}
	if ((y > GY && x > (GX - hy)) || (x> GX && y > (GY - hx)) ) {
		hasVScroller = true;
		hasHScroller = true;
	}
	
	var expandOpenClosedBtn = function(scrollgroup, expandWidth) {
		for (var i = 0; i < scrollgroup.children.length; i++) {
			var sectionChild = scrollgroup.children[i];
			sectionChild.children[0].size.width = expandWidth;
			sectionChild.size.width = expandWidth;
		}
	};
		
	if (!hasVScroller && !hasHScroller) 
	{
		scrollgroup.size = [GX, y];    //x -> GX
		scrollgroup.parent.size = scrollgroup.size;
		scrollgroup.parent.parent.size = scrollgroup.size;
			
		expandOpenClosedBtn(scrollgroup, GX);
			
		vscroller.visible = false;
		hscroller.visible = false;
	} else if (hasVScroller && !hasHScroller) {
		vscroller.size = [hy, GY];   
		vscroller.location = [GX - hy, 0];
			
		scrollgroup.size = [GX - hy,  GY];       //x -> GX - hy
		scrollgroup.parent.size = [GX, GY];
		scrollgroup.parent.parent.size = [GX, GY];
			
		vscroller.value = vscroller.minvalue =0;			
		vscroller.maxvalue= y - GY;
		vscroller.stepdelta = 10;
		vscroller.recalculate();
			
		expandOpenClosedBtn(scrollgroup, GX - hy);
			
		vscroller.visible = true;
		hscroller.visible = false;
	} else if (!hasVScroller && hasHScroller) {
		hscroller.size = [GX, hx];
		hscroller.location = [0, GY - hx];
			
		scrollgroup.size = [GX, y];          
		scrollgroup.parent.size = [GX, GY - hx];	
		scrollgroup.parent.parent.size = [GX, GY];
			
		hscroller.value = hscroller.minvalue =0;			
		hscroller.maxvalue= x - GX;
		hscroller.stepdelta = 10;
		hscroller.recalculate();
			
		expandOpenClosedBtn(scrollgroup, x);
			
		vscroller.visible = false;
		hscroller.visible = true;
	} else if (hasVScroller && hasHScroller) {
		hscroller.size = [GX-hy, hx];
		hscroller.location = [0, GY - hx];
			
		vscroller.size = [hy, GY - hx];
		vscroller.location = [GX - hy, 0];
						
		scrollgroup.size = [GX-hy, GY-hx];
		scrollgroup.parent.size = [GX, GY - hx];	
		scrollgroup.parent.parent.size = [GX, GY];
			
		vscroller.value = vscroller.minvalue =0;			
		vscroller.maxvalue= y - GY + hx;
		vscroller.stepdelta = 10;
		vscroller.recalculate();
		
		hscroller.value = hscroller.minvalue =0;			
		hscroller.maxvalue= x - GX + hy;
		hscroller.stepdelta = 10;
		hscroller.recalculate();
			
		expandOpenClosedBtn(scrollgroup, x);
			
		vscroller.visible = true;
		hscroller.visible = true;
	}

	if (hasVScroller && (maingroup.vscrollCurrentValue > 0) ) {
		var currentValue = maingroup.vscrollCurrentValue;
		if (currentValue > vscroller.maxvalue) {
			currentValue = vscroller.maxvalue;
		}
		vscroller.value = currentValue;
		vscroller.indicatorLocationValue = vscroller.getIndLocationByValue(currentValue);
		scrollgroup.location.y = - vscroller.value;
		scrollgroup.size.height = vscroller.size.height - scrollgroup.location.y;
	}
}


AOM.Cabinet.mainGroupResource = 
"group{\
	orientation:'column', alignment:['fill', 'top'], alignChildren:['fill', 'top'], margins:0, spacing:0,\
	vgroup:Group{\
		margins:0, spacing:0, \
		scrollgroup:Group{\
			orientation:'column', alignment:['left', 'top'], margins:0, spacing:0,\
		},\
		vscrollbar:Custom{type:'customBoundedValue', themeType:'Scrollbar', alignment:['right', 'top'], preferredSize:["+AOM.VSCROLLER_WIDTH+", "+AOM.VSCROLLER_HEIGHT+"]}\
	},\
	hscrollbar:Custom{type:'customBoundedValue', themeType:'Scrollbar', alignment:['left', 'bottom'], preferredSize:["+AOM.HSCROLLER_WIDTH+", "+AOM.HSCROLLER_HEIGHT+"]}\
}";


AOM.Cabinet.sectionResource = 
"group{\
	orientation:'column', alignment:['fill', 'top'], alignChildren:['left', 'top'], margins:0, spacing:0,\
	openCloseBtn:IconButton{text:'openCloseBtn', isOpen:true, themeType:'openclosebtn'},\
	contentgroup:Group{\
		alignChildren:['left', 'top'], margin:0, spacing:0, \
	}\
}";
