﻿
/*************************************************************************
*
* 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.
* 
*
**************************************************************************/

/**
 * CustomPanel class
 * Define a special container, user can add footer sections or header sections to this panel, but can
 * only insert on cabinet or accordion section.
 *
 * @Date: 2008-9-18
 */


AOM.CustomPanel = function(group) {
	this.headerSections = {};
	this.footerSections = {};
	this.mainGroup = group;
	this.mainGroup.maximumSize.height = AOM.MAXIMUM_SIZE_HEIGHT;
	this.cabinet = undefined;
	this.accordion = undefined;
};

/**
 * Add a header section.
 *
 * @param sectionResource The section resource.
 * @param sectionName The section name.
 * @param dividerPosition Whether to insert a divide line, its value can be: "top", "bottom" and undefined,
 *                            undefined means don't add the divide line.
 */
AOM.CustomPanel.prototype.addHeaderSection = function(sectionResource, sectionName, dividerPosition) {
	var headerSection = this.mainGroup.add(AOM.CustomPanel.CustomSubSectionResource);
	headerSection.maximumSize.height = AOM.MAXIMUM_SIZE_HEIGHT;

	if (dividerPosition == "top") {
		headerSection.divideLineSection = headerSection.add(AOM.CustomPanel.DividerResource);
		headerSection.contentSection = headerSection.add(sectionResource);
	} else if (dividerPosition == "bottom") {
		headerSection.contentSection = headerSection.add(sectionResource);
		headerSection.divideLineSection = headerSection.add(AOM.CustomPanel.DividerResource);
	} else {
		headerSection.contentSection = headerSection.add(sectionResource);
	}

	headerSection.contentSection.maximumSize.height = AOM.MAXIMUM_SIZE_HEIGHT;
	this.headerSections[sectionName] = headerSection;

	return headerSection.contentSection;
};

/**
 * Add a footer section.
 *
 * @param sectionResource The section resource.
 * @param sectionName The section name.
 * @param dividerPosition Whether to insert a divide line, its value can be: "top", "bottom" and undefined,
 *                            undefined means don't add the divide line.
 */
AOM.CustomPanel.prototype.addFooterSection = function(sectionResource, sectionName, dividerPosition) {
	var footerSection = this.mainGroup.add(AOM.CustomPanel.CustomSubSectionResource);
	footerSection.maximumSize.height = AOM.MAXIMUM_SIZE_HEIGHT;

	if (dividerPosition == "top") {
		footerSection.divideLineSection = footerSection.add(AOM.CustomPanel.DividerResource);
		footerSection.contentSection = footerSection.add(sectionResource);
	} else if (dividerPosition == "bottom") {
		footerSection.contentSection = footerSection.add(sectionResource);
		footerSection.divideLineSection = footerSection.add(AOM.CustomPanel.DividerResource);
	} else {
		footerSection.contentSection = footerSection.add(sectionResource);
	}

	footerSection.contentSection.maximumSize.height = AOM.MAXIMUM_SIZE_HEIGHT;
	this.footerSections[sectionName] = footerSection;

	return footerSection.contentSection;
};

/**
 * Add a cabinet section.
 *
 * @return The added Cabinet object.
 */
AOM.CustomPanel.prototype.addCabinetSection = function() {

	if (this.cabinet != undefined || this.accordion != undefined) {
		throw new Error("AOM.CustomPanel only allows one Cabinet or Accordion in each panel.");
	}

	this.cabinetSection = this.mainGroup.add(AOM.CustomPanel.CabinetResource);
	this.cabinetSection.maximumSize.height = AOM.MAXIMUM_SIZE_HEIGHT;
	this.cabinetSection.layout = new AOM.CabinetLayoutManager(this.cabinetSection);

	this.cabinet = new AOM.Cabinet(this.cabinetSection);
	return this.cabinet;
};

/**
 * Add a accordion section.
 *
 * @return The added Accordion object.
 */
AOM.CustomPanel.prototype.addAccordionSection = function() {

	if (this.cabinet != undefined || this.accordion != undefined) {
		throw new Error("AOM.CustomPanel only allows one Cabinet or Accordion in each panel.");
	}

	this.accordionSection = this.mainGroup.add(AOM.CustomPanel.AccordionResource);
	this.accordionSection.maximumSize.height = AOM.MAXIMUM_SIZE_HEIGHT;
	this.accordionSection.layout = new AOM.AccordionLayoutManager(this.accordionSection);

	this.accordion = new AOM.Accordion(this.accordionSection);
	return this.accordion;
};

/**
 * Define a layout manager for AOM.CustomPanel.
 *
 */
AOM.CustomPanelLayoutManager = function(group) {
	this.initSelf(group);
}

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

AOM.CustomPanelLayoutManager.prototype.layout = function() {
   	var containerWidth = this.group.size.width;
	var containerHeight = this.group.size.height;
	this.group.location = [0, 0];

	var childIndex = 0;
	var distanceFromTop = 0;

	// 1. Deal with header sections.
	for (; childIndex < this.group.children.length; ++childIndex) {
		var headerSection = this.group.children[childIndex];

		// Meet with the Cabinet or Accordion section
		if (headerSection.customPanelType != undefined)
			break;

		headerSection.size = headerSection.preferredSize;
		headerSection.location = [0, distanceFromTop];
		if (headerSection.layout != undefined) {
			headerSection.layout.layout(true);
		}
		if (headerSection.divideLineSection != undefined) {
			headerSection.divideLineSection.line.size = [containerWidth, 1];
			headerSection.divideLineSection.size = [containerWidth, 1];
		}
		headerSection.size = [containerWidth, headerSection.preferredSize.height];

		distanceFromTop += headerSection.preferredSize.height;

	}

	// 2. Check if all children have been proceeded.
	if (childIndex == this.group.children.length)
		return;

	// 3. Deal with footer sections.
	var distanceFromBottom = 0;
	for (var m = this.group.children.length -1; m > childIndex; --m) {
		var footerSection = this.group.children[m];

		footerSection.size = footerSection.preferredSize;
		distanceFromBottom += footerSection.size.height;
		
		footerSection.location = [0, containerHeight - distanceFromBottom];
		if (footerSection.layout != undefined) {
			footerSection.layout.layout(true);
		}
		if (footerSection.divideLineSection != undefined) {
			footerSection.divideLineSection.line.size = [containerWidth, 1];
			footerSection.divideLineSection.size = [containerWidth, 1];
		}
		footerSection.size = [containerWidth, footerSection.preferredSize.height];
	}

	// 4. Deal with the cabinet or accordion section.
	var specialSection = this.group.children[childIndex];
	specialSection.size = [containerWidth, containerHeight - distanceFromTop - distanceFromBottom];
	specialSection.location = [0, distanceFromTop];
	if(specialSection.layout != undefined) {
		specialSection.layout.layout(true);
	}
}

AOM.CustomPanel.CustomSubSectionResource =
"group{\
	orientation:'column', alignment:['left', 'top'], alignChildren:['left', 'top'], margins:0, spacing:0,\
}";

AOM.CustomPanel.DividerResource =
"group{\
	alignment:['left', 'top'], margins:0, \
	line:Custom{type:'customBoundedValue', text:'', margins:0, alignment:['left', 'top'], preferredSize:[100, 1], themeType:'spline'},\
}";

AOM.CustomPanel.CabinetResource =
"group{\
	orientation:'column', alignment:['left', 'top'], alignChildren:['left', 'top'], margins:0, spacing:6, customPanelType:'cabinet',\
}";

AOM.CustomPanel.AccordionResource =
"group{\
	orientation:'column', alignment:['left', 'top'], alignChildren:['left', 'top'], margins:0, spacing:6, customPanelType:'accordion',\
}";
