/**
* @desc Class that will move objects as stack
* @author Boris Vujicic <09/04/2010 (dd/mm/yyyy)> 
* @author http://www.borisvujicic.com
* @version 1.0                                                                            
* ----------------------------------------------------------------------------------------
*/
function slideObject(){
  this.stream = true;                                     // false - vertical | true - horizontal
  this.holder;                                            // Div which holds all objects
  this.width = 0;                                         // Width of the div
  this.height = 0;                                        // Height of the div
  
  /**
  * @desc Setup slide options
  * @param id id of DOM element
  */
  this.setup = function (id){
    
    this.holder = document.getElementById(id);            // Holder of slide object
    var objects = getChildNodes(this.holder);             // Nodes inside slide objects
    
    // Loop through objects and setup position
    for(i=0; i<objects.length; i++){
      // Set object position to be absolute towards parent div. Make sure parent is set to relative
      objects[i].style.position = "absolute";
      objects[i].style.left = this.stream?'0px':this.width+'px';
      objects[i].style.top = this.stream?this.height+'px':'0px';
      
      // Increse height and width
      this.height += objects[i].offsetHeight;
      this.width += objects[i].offsetWidth;
    }
  }
  
  // Move slide for one object
  this.move = function(direction){ 
    var objects = getChildNodes(this.holder);             // Get all childe nodes                      
    var first = direction?0:objects.length-1;             // If direction is down get first object
    var last  = direction?objects.length-1:0;             // If direction is up get last object
    div = objects[first].cloneNode(true);                 // Clone first object, can be li also
    this.holder.appendChild(div);                         // Add object to end of stack
    // definde movement px                                           
    var move_size = this.stream?objects[first].offsetHeight:objects[first].offsetWidth;
    move_size = direction?move_size:move_size *-1;
    div.style.top = objects[last].style.top;
    div.style.left = objects[last].style.left;
    
    // Let move all the objects. Would be nice to implement some effect here :)
    for(j=0; j<1; j++){
      for(i=0; i<objects.length; i++)
        this.stream?
          objects[i].style.top = objects[i].style.top.replace("px", '')-move_size+'px':
          objects[i].style.left = objects[i].style.left.replace("px", '')-move_size+'px';
    }
    
    // Lets crete new element. for now only works with div bcs of IE, we should add simple check and implement
    // li also. Check the holder type
    var newdiv = document.createElement("div");
    // Add elements to new object
    if(direction){
      for(i=1; i<objects.length; i++) newdiv.appendChild(objects[i]);
      newdiv.appendChild(div);  
    }else{
      newdiv.appendChild(div);  
      for(i=0; i<objects.length-1; i++)newdiv.appendChild(objects[i]);
    }
    // Switch content of objects.
    this.holder.innerHTML = newdiv.innerHTML;
    
  }
  
  this.down = function(){ this.move(false)};              // Move stack down
  this.up   = function(){ this.move(true)};               // Move stack up
}
   
// get all children nodes. Function taken from net :) Cant remeber the link :|
function getChildNodes(obj){
  var children = obj.childNodes;
  var nodes = [];
  for(var i=0; i<children.length; i++){
    if((children[i].nodeType == document.TEXT_NODE || children[i].nodeType == document.CDATA_NODE) && children[i].nodeValue.match(/^\s*$/)) continue; //it's an empty text node
    nodes.push(children[i]);
  }
  return nodes;
}
