/*
 © Copyright (c) 2006-2008 Apple Inc.  All rights reserved.
 
 IMPORTANT:  This Apple software ("Apple Software") is supplied to you in consideration of your agreement to the following terms. Your use, installation and/or redistribution of this Apple Software constitutes acceptance of these terms. If you do not agree with these terms, please do not use, install, or redistribute this Apple Software.
 
 Provided you comply with all of the following terms, Apple grants you a personal, non-exclusive license, under Apple’s copyrights in the Apple Software, to use, reproduce, and redistribute the Apple Software for the sole purpose of creating Dashboard widgets for Mac OS X. If you redistribute the Apple Software, you must retain this entire notice in all such redistributions.
 
 You may not use the name, trademarks, service marks or logos of Apple to endorse or promote products that include the Apple Software without the prior written permission of Apple. Except as expressly stated in this notice, no other rights or licenses, express or implied, are granted by Apple herein, including but not limited to any patent rights that may be infringed by your products that incorporate the Apple Software or by other works in which the Apple Software may be incorporated.
 
 The Apple Software is provided on an "AS IS" basis.  APPLE MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, REGARDING THE APPPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
 
 IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

function CreateStackLayout(elementOrID, spec)
{
    var containerElement = elementOrID;
    if (elementOrID.nodeType != Node.ELEMENT_NODE) {
        containerElement = document.getElementById(elementOrID);
    }
	if (!containerElement.loaded) {
		containerElement.loaded = true;
		containerElement.object = new StackLayout(containerElement, spec);
		
		return containerElement.object;
	}
}

function StackLayout(containerElement, spec)
{
    this.element = containerElement;
    
    var subviewsTransitions = [];
    if (typeof spec.subviewsTransitions == "string") {
        try { eval('subviewsTransitions = ' + spec.subviewsTransitions); } catch(e) {}
	}
    
    this._views = [];
    var kids = containerElement.childNodes;
    var inDesign = dashcode && dashcode.inDesign;
    var firstElementDone = false;
    for (var i=0; i<kids.length; i++) {
        if (kids[i].nodeType == Node.ELEMENT_NODE) {
            this._views[this._views.length] = kids[i];
            // Doing this only during runtime since we need the subviews to be relative in design time where view swapping is done using the general mechanism of using 'display'.
            if (!inDesign) {
                kids[i].style.display = (firstElementDone) ? 'none' : 'block';
                firstElementDone = true;
            }
        }
    }
    
    if (this._views.length > 0) {
        this._resetCurrentView();
        this.setCurrentView(this._views[0]);
        
        if (this._views.length == subviewsTransitions.length) {
            for (var i=0; i<this._views.length; i++) {
                this._views[i]._transition = CreateTransitionWithProperties(subviewsTransitions[i]);
            }            
        }
    }
}

StackLayout.prototype._getView = function(view)
{
    if (view.nodeType == Node.ELEMENT_NODE) {
        // Already an element
        return view;
    }
    if (view.element) {
        // It's a part object
        return view.element;
    }
    // Try it as an id
    return document.getElementById(view);
}

StackLayout.prototype.getAllViews = function()
{
    return this._views;
}

StackLayout.prototype._resetCurrentView = function()
{
    for (var i=0; i<this._views.length; i++) {
        delete this._views[i]._activeView;
    }
}

StackLayout.prototype.getCurrentView = function()
{
    for (var i=0; i<this._views.length; i++) {
        var curView = this._views[i];
        if (curView._activeView) return curView;
    }
    return null;
}

StackLayout.prototype._setCurrentViewPrimitive = function(newView, oldView, transition, isReverse)
{
    // newView must be the element now and all error checking has been done.

    if (oldView) {
        delete oldView._activeView;
    }
    
    var inDesign = dashcode && dashcode.inDesign;
    // View swapping in design time is done in application. Also, we want to be very careful about performing transition during design time because the attributes that get added during the transition into the DOM will get persisted into the HTML during regeneration.
    if (transition && !inDesign) {
        if (oldView) oldView.style.display = 'block';
        if (newView) newView.style.display = 'block';
        transition.perform(newView, oldView, isReverse);
    }
    else {
        if (oldView) oldView.style.display = 'none';
        if (newView) newView.style.display = 'block';
    }
    
    if (newView) newView._activeView = true;
}

StackLayout.prototype.setCurrentView = function(newView, isReverse)
{
    // Look up by id if necessary
    newView = this._getView(newView);
    var oldView = this.getCurrentView();
	
    if (!newView || (oldView == newView)) {
        return;
    }
    
    // Make sure the view is ours
    if (!newView.parentNode == this.element) {
        return;
    }
    
    var transition = newView._transition;
    if (!transition) transition = new Transition(Transition.NONE_TYPE);
    
    if (oldView) {
        if (isReverse) {
            transition = oldView._transition;
        }
    }
    
    this._setCurrentViewPrimitive(newView, oldView, transition, isReverse);
}

StackLayout.prototype.setCurrentViewWithTransition = function(newView, transition, isReverse)
{
    // Look up by id if necessary
    newView = this._getView(newView);
    if (!newView) {
        return;
    }
    
    // Make sure the view is ours
    if (!newView.parentNode == this.element) {
        return;
    }
    var oldView = this.getCurrentView();

    this._setCurrentViewPrimitive(newView, oldView, transition, isReverse);
}
