﻿/*
    Created by Rob Ellis, 
    Website: http://www.jayandsilentrob.com 	
    Email: rob@jayandsilentrob.com
    August 21th 2006
   
   Description: An example on how to create a Live Spaces Layout Manager with Prototype.
   
 */
   
   var layoutManager = Class.create();

   layoutManager.prototype = {
       initialize: function(type) {
           // TODO check to see if type exist in layoutType Array           
           this.type = type;
           this.cols = Array();
       },

       drawLayout: function(dest_id) {
	       $(dest_id).innerHTML = '';
	       this.dest = dest_id;
		   
           numCols = layoutTypes[this.type][0];
           w = layoutTypes[this.type][1].split(',');

           for(var i=0;i<numCols;i++) {
		       innerDiv = document.createElement('div');
			   innerDiv.className = 'divstyle';
			   innerDiv.style.width = w[i] + '%';

               el = this.createColumn(i);
			   for (var n=0; n < this.cols[i].length;n++) {
		           ix = this.cols[i][n].buildWindow();
                   el.appendChild(ix);
               }
			   innerDiv.appendChild(el);
			   
			   $(dest_id).appendChild(innerDiv);
           }
		   this.makeSortable();

           return true;
       },
	   changeLayout: function(new_layout) {
	       this.type = new_layout;
		   this.drawLayout(this.dest);
	   },
	   
	   makeSortable: function() {
	       numCols = layoutTypes[this.type][0];
		   
		   var moveTo = Array();
		   for(var i=0;i<numCols;i++) { 
		       moveTo.push('_column_'+i);
		   }
		   
		   for(var i=0;i<numCols;i++) { 
	   	       Sortable.create("_column_"+i, {dropOnEmpty:true,containment:moveTo,constraint:false});
		   }
	   },
	   
       createColumn: function(colnum) {
             return Builder.node('ul',{id:'_column_'+ colnum});
       },

       addWindowSet: function(ws) {
           panels = ws.split(':');
           if (typeof(panels) == 'object') {
               for (var i=0;i<panels.length;i++) {
                   this.cols[i] = Array();
                   win = panels[i].split(',');
                   if (typeof(win) == 'object') {
                       for (n=0;n<win.length;n++) {
                           var winx = new windowObject(win[n]);
                           this.cols[i].push(winx);
                       }
                   }
               }
           }
       }
   }

   var windowObject = Class.create();

   windowObject.prototype = {
	initialize: function(id) {      
          this.id = id;
          this.title = windows[id][0];
          this.content = windows[id][1];
	},
    
	buildWindow: function() {
          this.li = document.createElement('li');
          var el = Builder.node('div',{className:'window'},[
                     Builder.node('span',{className:'handle'},this.title),
                     Builder.node('span',{className:'content'},this.content)
                 ]);
          this.li.appendChild(el);
          return (this.li);
      }
   } 

