





/**************************************************************************************************************
 stage.js 
***************************************************************************************************************/
// var STAGE_OBJECT_SPACE_WIDTH = 75;

var FX_ACTIVE = 0;

function Stage( STAGE_OBJECT_SPACE_WIDTH ){
    var infos = { 'objects' : new Array() , 'left' : -23 };

    this.init = function(options){
        Object.extend(infos, options);
        infos.target_cumulativeOffset = infos.target.cumulativeOffset();
    }

    this.addObjectToStage = function(object){ infos.objects.push(object); }

    this.createStage = function(){
        infos.objects.each(function(object){
            //we have a collection of objects
            if (object instanceof Array){
                var collectionObject = object;
                var x = 0;
                collectionObject.each(function(object){
                    //we scale the object and position them to the middle
                    var objectNode = this.setSizeAndDimensions(object);
        
                    //set the new left position for next object
                    infos.left = parseInt(infos.left);
        
                    objectNode.observe('mouseover', object.moveToFront.bind(object, collectionObject));
                    objectNode.observe('mouseover', this.moveAllObjectsToBackground.bind(this, collectionObject));
                    objectNode.observe('mouseover', this.sortCollectionOnBack.bind(this, collectionObject));
        
                    //we have 3 objects in a collection, so we set the middle object to the front
                    if ((collectionObject.length == 3) && (x == 1)) objectNode.setStyle({ 'zIndex' : 2 });
                    x++;
                    //we are on the last object
                    if (collectionObject.length == x) {
                        infos.left = infos.left + object.image.getDimensions().width  + STAGE_OBJECT_SPACE_WIDTH;
                    }else{
                        infos.left = infos.left + (object.image.getDimensions().width / 2) ;
                    }
                    
                }.bind(this));
    
            //we have a single object
            }else{
                //we scale the object and position them to the middle
                var objectNode = this.setSizeAndDimensions(object);
        
                //set the new left position for next object
                infos.left = parseInt(infos.left) + object.image.getDimensions().width + STAGE_OBJECT_SPACE_WIDTH;
        
                objectNode.observe('mouseover', object.moveToFront.bind(object));
                objectNode.observe('mouseover', this.moveAllObjectsToBackground.bind(this, object));
            }
    
        }.bind(this));
    }

    this.setSizeAndDimensions = function(object){
        var objectNode = object.html;
        var positions = object.getPositions();
    
        objectNode.setStyle({ 'width' : positions.middle});
        object.image.setStyle({ 'width' : '100%'});
    
        infos.target.insert(objectNode);
        objectNode.setStyle({
			// makes IE7 dizzy! mt 2010-09-03
            // 'top' : infos.target_cumulativeOffset[1] + 64 + 'px',
            'top' : 64 + 'px',
            'left' : infos.left + 'px'
        });
        return objectNode;
    }

    this.sortCollectionOnBack = function(collectionObject){
        var foundCollection = false;
        infos.objects.each(function(object){
            if (foundCollection == true){
                if (object instanceof Array){
                    object.each(function(obj){
                        obj.moveToRight(obj.getMoveToRightBackground());
                    });
                }else{
                    object.moveToRight(object.getMoveToRightBackground());
                }
            }
        
            //we have found our collection
            if (collectionObject == object){
                foundCollection = true;
            }
        });
    }

    this.moveAllObjectsToBackground = function(exceptObject){
        infos.objects.each(function(object){
            //the excepted object is a collection
            if (exceptObject instanceof Array){
                var isExceptObject = false;
                //we have found our collection, set as excepted
                if (exceptObject == object) isExceptObject = true;
                
                //if not a exepted object, move it to the background
                if (!isExceptObject){
                    if (object instanceof Array){    
                    object.each(function(obj){
                        obj.moveToBackground();
                    });
                    }else{
                    object.moveToBackground();
                    }
                }

            }else{
                if (exceptObject != object){
                    if (object instanceof Array){
                        var x = 0;
                        object.each(function(obj){
                            obj.moveToBackground();
            
                            //we have 3 objects in a collection, so we set the middle object to the front
                            if ((object.length == 3) && (x == 1)){
                                obj.html.setStyle({ 'zIndex' : 2 });
                            }
                            x++;
            
                        }.bind(this));
                    }else{
                        object.moveToBackground();
                    }
                }
            }
        });
    }
}

