/* --------------------------------------------------------------------------------------------*/
/* --------------------------------------->>> DOCK V 1.0 <<<-----------------------------------*/
/* --------------------------------------------------------------------------------------------*/

var Dock = Class.create({
   initialize: function(btn, movable, container) {
      options = {
         moveBy: 194,
         open: false
      };
      this.btn = $(btn);
      this.movable = $(movable);
      this.container = $(container);
      this.options = options;
      this.moving=false;
      //Add Events
      this.eventClick = this.toggleDock.bindAsEventListener(this);
      this.registerEvents();
	  this.getDockItems();
   },
   registerEvents: function() {
      Event.observe(this.btn, "click", this.eventClick);
   },
   toggleDock: function(event){
      Event.stop(event);
      if(this.moving){
         return;
      }
      if(this.options.open){
         this.options.open = false;
         this.closeDock();
      }else{
         this.options.open = true;
         this.openDock();
      }
   },
   _toggleDock: function(){
      if(this.moving){
         return;
      }
      if(this.options.open){
         this.options.open = false;
         this.closeDock();
      }else{
         this.options.open = true;
         this.openDock();
      }
   },
   openDock: function(){
      this.moving = true;
      var that = this;
      new Effect.Parallel(
         [ new Effect.Move (this.movable,{ 
               x: 0, 
               y: this.options.moveBy, 
               mode: 'relative'
            }),
            new Effect.Scale(this.container, 100, {
               scaleX: false,
               scaleContent: false,
               scaleMode: { originalHeight: 203},
               scaleFrom: 0
            })
         ],
         { duration: 1, 
            afterFinish: function(effect) {
               that.moving = false;
            } 
         }
      );
      
   },
   
   getDockItems: function(){
		var myDockItems = Cookie.read('msoDock');
		if(myDockItems){
			readDockHash = unescape(myDockItems);
			this.addFullMarkup(readDockHash);
		}else{
			readDockHash = 'undefined';
		}
	},  
   
   addPage: function(href, title, tab){
		if(readDockHash.indexOf(href) < 0){//make sure its not already added
			var title = title;
			if(!tab){
				tab = 1;
			};
			var output = href + "|" + title + "|" + tab;
			
			fullDockHash = (readDockHash != 'undefined') ? output + "," + readDockHash : output;
			Cookie.create('msoDock',fullDockHash, 1000);
			this.getDockItems();
			//this.toggleDock(this.btn.click());
			if(this.options.open == false){
				this._toggleDock();
			};
		}
	},  
	
	addFullMarkup: function(dockHash){
		
		var items = dockHash.split(",");
		var newMarkup1 = '';
		var newMarkup2 = '';
		var newMarkup3 = '';
		
		for(var i = 0; i < items.length; i++){
			tmpStr = items[i];
			el = tmpStr.split("|");
			
			switch(el[2]){
				case '1':

					newMarkup1 += this.getanchor(el[0],el[1], 1);
				break;
				case '2':
					newMarkup2 += this.getanchor(el[0],el[1], 2);
				break;
				case '3':
					newMarkup3 += this.getanchor(el[0],el[1], 3);
				break;
			}
			
		}
		
		$('dockItems1').innerHTML = newMarkup1;
		$('dockItems2').innerHTML = newMarkup2;
		$('dockItems3').innerHTML = newMarkup3;
 		
	},
	getanchor: function(theLink, theTitle, col){
		return("<span class='dockItemHolder'><a class='dockremovelink col"+col+"' title='remove from dock' onclick=javascript:MSOFRAMEWORK.baseLoad.msoDoc.removeItem(this); href='javascript:void(0);'><img src='/images/bt_del.gif' /></a><a class='dockItem col"+col+"' href='"+theLink+"' title=\"" + theTitle + "\">"+ this.formatTitle(theTitle) + "</a></span>")
	},
	
	formatTitle: function(title){
		if(title){
			maxChars = 30
			newTitle = title.replace('Masonry Systems -','');
			newTitle = newTitle.replace('Inspiration - ', '');
			newTitle = newTitle.replace('Information - ', '');
			newTitle = newTitle.replace('Interaction - ', '');
			if(newTitle.length > maxChars){
				append = '...';
			}else{
				append = '';
			}
			newTitle = newTitle.substr(0,maxChars);
			newTitle += append;
			return(newTitle);
		}else{
			return('');
		}
	}, 
	
	linkOver: function(el){
		container = el
		Element.addClassName(container,'darkBg');
		delbtn = this.findDeleteButton(container);
		container.onmouseout = this.containerBlur;
		delbtn.style.display = "block";
	}, 
	
	findDockItemHash: function(container){
		var ndList = container.childNodes;
		var theLink;
		for(var i=0 ;i < ndList.length; i++){
			var theItem = ndList[i].className.split(' ')
			if(theItem[0] == "dockItem"){
				theLink = ndList[i];
			}
		}
		return(theLink.attributes.href.value + "|" + theLink.attributes.title.value);
	},
	
	findDeleteButton: function(container){
		var ndList = container.childNodes;
		for(var i= 0 ;i < ndList.length; i++){
			if(ndList[i].className.split(' ') == "dockremovelink"){
				return(ndList[i]);
			}
		}
	},
	
	removeItem: function(el){
		if(confirm("Are you sure you want to delete this link?")){
			container = el.parentNode;
			removeHash = this.findDockItemHash(container);
			saveHash = readDockHash.replace(removeHash,'');
			saveHash = saveHash.replace(',,',',');
			if(saveHash[0] == ",") saveHash = saveHash.substr(1,saveHash.length);
			Cookie.create('msoDock',saveHash, 1000);
			this.getDockItems();
		}
	},
   
   closeDock: function(){
      this.moving = true;
      var that = this;
      new Effect.Parallel(
         [ new Effect.Move (this.movable,{ 
               x: 0, 
               y: -(this.options.moveBy), 
               mode: 'relative'
            }),
            new Effect.Scale(this.container, 25, {
               scaleX: false,
               scaleContent: false,
               scaleMode: { originalHeight: 203},
               scaleFrom: 100
            })
         ],
         { duration: 1, 
            afterFinish: function(effect) {
               that.moving = false;
            } 
         }
      );      
   }
});

Cookie = {
	create: function(name,value,days) {
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}
		else var expires = "";
		document.cookie = name+"="+value+expires+"; path=/";
	},
	
	read: function(name) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	},
	
	erase: function(name) {
		create(name,"",-1);
	}
}

