
/*************************************************************************
*
* ADOBE CONFIDENTIAL
* ___________________
*
*  Copyright 2008 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: 2008/04/10 13:35:00 $
*
* Adobe Output Module 2.0
*
* MediaGallery Originally Written by Quality Process Incorporated, Enhanced by
* Adobe System China. Contact Sheet written by Adobe System China.
* 
*
**************************************************************************/



/**
  * Accordion Class
  * define a special container
  *
  *  @Date: 2008-1-18
*/
AOM.Accordion = function(group) {
	this.sections = {};
	this.group = group;
	this.group.maximumSize.height =AOM.MAXIMUM_SIZE_HEIGHT;
	
	this.create();
}


AOM.Accordion.prototype.create = function() {
	//
}

AOM.Accordion.prototype.addFoldableSection = function(sectionResource, sectionName, sectionTitle) {
	var accordionSection = new AOM.AccordionSection(this.group, sectionResource, sectionName, sectionTitle, this);
	
	this.sections[sectionName] = accordionSection.contentGroup;
	this.sections[sectionName].margins = [20, 10, 5, 10];
	accordionSection.sectionGroup.foldable = true;
	
	return this.sections[sectionName];
}

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

AOM.Accordion.prototype.getSelectedSectionByName = function(name) {
	var selectedSection = "";
	if (name == undefined || name.length == 6) {
		return selectedSection;
	}
	selectedSection = name.substr(6, name.length - 6);
	return selectedSection;
}

AOM.Accordion.prototype.removeAllFoldableSections = function()
{
    for(var i = 0; i < this.group.children.length; ++i) {
        var child = this.group.children[i];
        if(child.foldable == true) {
            this.group.remove(i);
            --i;
        }
    }
}

AOM.Accordion.prototype.addSection = function(contentResource, sectionName) {
	var section = this.group.add(contentResource);
	section.maximumSize.height =AOM.MAXIMUM_SIZE_HEIGHT;
	
	section.isAccordion = function() { return false;}
	section.sectionName = sectionName;
	
	this.sections[sectionName] = section;
	return this.sections[sectionName];
}

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

AOM.Accordion.hasSectionName = function(group, selectedSection) {
	for (var i = 0; i < group.children.length; i++) {
		var child = group.children[i];
		if (child.sectionName != undefined && child.sectionName == selectedSection) {
			return true;
		}
	}
	return false;
}


/**
  * AccordionSection
  * Define a special container section
  *
  *  @Date: 2007-1-18
*/
AOM.AccordionSection = function(group, contentResource, sectionName, sectionTitle, foldableSet){
	this.create(group, contentResource, sectionName, sectionTitle, foldableSet);
	this.initializeSectionGroup();
};

AOM.AccordionSection.prototype.create = function(group, contentResource, sectionName, sectionTitle, foldableSet) {
	this.sectionGroup = group.add(AOM.Accordion.sectionResource);
	this.sectionGroup.children[1].maximumSize.height =AOM.MAXIMUM_SIZE_HEIGHT;
	this.sectionGroup.children[1].children[0].maximumSize.height =AOM.MAXIMUM_SIZE_HEIGHT;
	this.sectionGroup.children[1].children[0].children[0].maximumSize.height =AOM.MAXIMUM_SIZE_HEIGHT;
	
	this.sectionGroup.sectionName = sectionName;
	
	this.contentGroup = this.sectionGroup.children[1].children[0].children[0].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.AccordionSection.accordionClick(group, sectionName);
		foldableSet.onClickFoldableSection(this);
	}	
	
	return this.sectionGroup;
}

AOM.AccordionSection.prototype.initializeSectionGroup = function() {
	var sectionGroup = this.sectionGroup;
	
	sectionGroup.isAccordion = function() {return true;};
	
	sectionGroup.expand = function(viewHeight) {
		return AOM.AccordionSection.expand(this, viewHeight);
	}

	sectionGroup.collapse = function() {
		return AOM.AccordionSection.collapse(this);
	}

	if(sectionGroup.children[1].children[0].vscrollbar != undefined) {
		//custom scrollbar
		var cvscroller = new AOM.CustomScrollbar(sectionGroup.children[1].children[0].vscrollbar);
		
		sectionGroup.children[1].children[0].vscrollbar.onChanging = function() {
			AOM.AccordionSection.vscroll(sectionGroup, this);
			this.notify("onDraw");
		}
	}

	if(sectionGroup.children[1].hscrollbar != undefined) {
		//custom scrollbar
		var chscroller = new AOM.CustomScrollbar(sectionGroup.children[1].hscrollbar);
		
		sectionGroup.children[1].hscrollbar.onChanging = function() {
			AOM.AccordionSection.hscroll(sectionGroup, this);
			this.notify("onDraw");
		}
	}
}

AOM.AccordionSection.accordionClick = function(group, name) {
try {
	if (group.selectedSection == name) {
		return;
	}
	group.selectedSection = name;
	group.parent.layout.layout(true);
	
} catch(e) {
	AOM.CS.Log.writeError("error:" + e);
}
}


AOM.AccordionSection.expand = function(sectionGroup, viewHeight)  {
	//sectionGroup.size.height = viewHeight;
	//return sectionGroup.size.height;
}

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

AOM.AccordionSection.vscroll = function(sectionGroup, vscrollbar) {
	var scrollgroup = sectionGroup.children[1].children[0].scrollgroup;
	var vscroller =sectionGroup.children[1].children[0].children[1];
	
	scrollgroup.location.y = - vscrollbar.value;
	scrollgroup.size.height =  vscroller.size.height - scrollgroup.location.y;  
}

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



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

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



AOM.AccordionLayoutManager.prototype.layout = function() {
	try {		
		//1, get container dimension,
		var group = this.group;
		var groupWidth = group.size.width;
		var groupHeight = group.size.height;
		
		//2. get group object
		var groupBottomHeight = 0;
		var groupCompactHeight = 0;
		var isCalculatedOpenAccordionSection = false;
		var selectedAccordionSection = undefined;
		
		//3, layout
		for (var i = 0; i < group.children.length; i++) {
			var child = group.children[i];
			
			//before re-caculate, set the size of openclosebtn
			var childOpenCloseBtn = child.children[0];
			childOpenCloseBtn.size = [groupWidth, AOM.OPENCLOSEBTN_HEIGHT];
			if (child.isAccordion()) {		
				var vscroller = child.children[1].children[0].children[1];
				var hscroller = child.children[1].children[1];
				vscroller.visible = hscroller.visible = false;
			}
		
			if (child.layout != undefined) {
				child.layout.layout(true);
			}
			child.size = child.preferredSize;
			
			if (child.isAccordion()) {
				if(isCalculatedOpenAccordionSection) {
					child.collapse();
					groupBottomHeight += child.size.height - 1;
					
					childOpenCloseBtn.isOpen = false;
				} else {
					if (child.sectionName == group.selectedSection) {
						selectedAccordionSection = child;
						isCalculatedOpenAccordionSection = true; 
						
						childOpenCloseBtn.isOpen = true;
					} else {
						child.collapse();
						
						childOpenCloseBtn.isOpen = false;
					}
				}
			} else {
				if(isCalculatedOpenAccordionSection)  groupBottomHeight += child.size.height;
			}
			groupCompactHeight += child.size.height;
		}
	
		//4, location
		var nextLocation = 0;  //relate  to group
		for (var i = 0; i < group.children.length; i++) {
			var child = group.children[i];
			child.location = [0, nextLocation];
			
			if(child.isAccordion()) {
				if (child.sectionName == group.selectedSection) {
					nextLocation = groupHeight - groupBottomHeight ;
				} else {
					nextLocation += child.size.height - 1;
				}
			} else {
				nextLocation += child.size.height ;
			}
		}
		
		//6, scroller
		if (selectedAccordionSection == undefined) {
			return;
		}
	
		var scrollgroup = selectedAccordionSection.children[1].children[0].children[0];
		var vscroller = selectedAccordionSection.children[1].children[0].children[1];
		var hscroller = selectedAccordionSection.children[1].children[1];
		
		var GX = groupWidth;
		var GY = groupHeight - selectedAccordionSection.location.y - groupBottomHeight - AOM.OPENCLOSEBTN_HEIGHT;
		var hx = AOM.HSCROLLER_HEIGHT, hy =  AOM.VSCROLLER_WIDTH;
		var x = scrollgroup.size.width, y = scrollgroup.size.height;
		
		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;
		}

		if (!hasVScroller && !hasHScroller) {
			scrollgroup.size = [GX, GY];
			scrollgroup.parent.size = scrollgroup.size;
			scrollgroup.parent.parent.size = scrollgroup.size;
			
			vscroller.visible = false;
			hscroller.visible = false;
		} else if (hasVScroller && !hasHScroller) {
			vscroller.size = [hy, GY];   
			vscroller.location = [GX - hy, 0];
			
			scrollgroup.size = [GX - hy,  GY];    
			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();
			
			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();
			
			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();
			
			vscroller.visible = true;
			hscroller.visible = true;
		}
		//sectionGroup always one value
		selectedAccordionSection.size = [GX, GY + AOM.OPENCLOSEBTN_HEIGHT];
	} catch(e) {
		AOM.CS.Log.writeError("Error:" + e);
	}	
}


AOM.Accordion.sectionResource = 
"group{\
	orientation:'column', alignment:['fill', 'top'], alignChildren:['fill','top'], margins:0, spacing:0,\
	openCloseBtn:IconButton{alignment:['left', 'top'], text:'openCloseBtn', isOpen:false, themeType:'openclosebtn'},\
	cgrp:Group{\
		orientation:'column', alignChildren:['fill', 'top'], margins:0, spacing:0, \
		vgrp:Group{\
			margins:0, spacing:0, alignChildren:['left', 'top'], \
			scrollgroup:Group{\
				alignChildren:['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+"]}\
	}\
}";
