	var timer;				//for sliding
	var ref;				//reference to the current toolbar
	var imageDirectory = "http://ak.imgfarm.com/images/sstb/";
	var toolbars = [];		//array of all toolbars on the page
	function stop(){
		clearTimeout(timer);
	}
	  	  	
  	//toolbar Object
	function Toolbar(divID,options){
		//initializes options
		toolbars.push(this);
		if(options==null)
			options = {};
			
		(options.slideable==null) ? options.slideable = false :  true
		(options.sortable==null) ? options.sortable = false : true
		(options.width==null) ? options.width=660 : true

		//defines some variables
		this.toolbarDiv = $(divID);				//saves the HTML Element ID as a JQuery Object
		this.divID = divID;						//saves the HTML Element ID as a string
		this.maxButtons = 15;					//constant for how many buttons a user can add
		this.order = [];						//an array of button IDs storing the order
		ref = this;						
		this.o = options;						
		var thisBar = this;
		
		//populates with default toolbar contents
		this.toolbarDiv.html("<span class='TB_logo'>Your Logo here&nbsp;</span>"
		+ '<input type="text" style="float:left; margin-left: 4px;" />'
		+ '<span class="search"></span>'
		+ '<ul class="toolbarButtons" style="margin-left:5px;"></ul>'); 
			
		this.toolbarDiv.wrap("<div style='width:" + this.o.width + "px; border: 1px solid black; overflow: hidden; float: left;'>");
		this.toolbarDiv.parent().wrap("<div style='width:" + this.o.width + "px; margin:0 auto; position: relative; overflow: visible'></div>");	
		
		//wraps in a container for overflowing elements
		if(this.o.slidable){
			this.toolbarDiv.before('<a href="javascript:void(0)" class="toolbar_arrow leftArrow" onmousedown="toolbars['+(toolbars.length-1)+'].slide(\'left\');"></a>');
			this.toolbarDiv.parent().after('<a href="javascript:void(0)" class="toolbar_arrow rightArrow" onmousedown="toolbars['+(toolbars.length-1)+'].slide(\'right\');"></a>');
		}
				
		//initialize toolbar functionality
		$(document).ready(function(){	
			if(ref.o.sortable){
				//makes the buttons dragable
				$(ref.divID + " ul").sortable({ 
					placeholder: "ui-selected", 
				    opacity: 0.5,
				    dragCompleteEvent: ref.trackChanges
				});
				
				//get order of butons
				ref.order = $(divID + " ul").sortable('toArray');
				order = $(divID + " ul").sortable('toArray');
			}
			
			if(options.slidable){
				//adds mouse over and mouse out events to arrows for sliding functionality
				$(".toolbar_arrow").mouseout(stop);
				$(".toolbar_arrow").mouseup(stop);
			}
			
			//for IE
			$(divID + " ul").css("position","static");
			
			thisBar.toolbarWidth();
	  	});
	}
	
	Toolbar.prototype = {
		customButtons: [],
		trackChanges: function() {
			if(ref.o.sortable){
				var order = $(ref.divID + " ul").sortable('toArray','btnid');
				$("#reorder").val(order.toString());

				ref.toolbarDiv.parent().parent().css("width",ref.o.width-25 + "px");
				ref.toolbarDiv.parent().css("width",ref.o.width-25 + "px");
				
				$("#toolbar_save_btn").fadeIn();
				$("#toolbar_save_btn").unbind("click");
				$("#toolbar_save_btn").click(function(){ document.forms["MainPostForm"].submit(); });

				hasChanged=true;		
			}
		},	
		
		getButtons: function(){
			return $(this.divID + " ul > li");
		},
		
		//Append new button to the toolbar
		addButton: function( id, url, name,icon) {

			var item = document.createElement('li');//create new item for list
			item.setAttribute("btnid",id);		
			
			if(icon && icon.indexOf('.bmp') != -1){ 
				if(icon.indexOf("ak.imgfarm.com") != -1)
					icon=icon.substring(0,icon.length-3) + "png";
				else	
					icon=icon.substring(0,icon.length-6) + "_1.png";
			}
			
			var img = document.createElement('img');
				img.src = (icon=="") ? imageDirectory + "/icons/icon_no_button.png" : icon;		
				item.appendChild(img);	
				
			var span = document.createElement("span");	
			span.appendChild( document.createTextNode(name) );
			
			item.appendChild(span);
			item.setAttribute("url",url);

			$(this.divID + " ul").append( item );
			
			if(id < 0) this.customButtons.push(item);
			
			return item;
		},
		
		//remove button from the toolbar
		removeButton: function(id){	
				//removes button from side nav widget
				var button = $("#button"+id).text();
										
				$(".tdNavItemBtn").each(function(){
					if($(this).text() == button)
						$(this.parentNode).remove();
				});
				
				//removes button from toolbar
				this.toolbarDiv.find("li[btnid="+id+"]").remove();
		},
		
		toolbarWidth: function(){
			var buttonsWidth=0;
			$(this.divID + " ul > li").each(function(){
				buttonsWidth += this.clientWidth;
			});
			
			//initializes width depending on number of buttons
			var toolbarWidth =  this.o.width;
			this.toolbarDiv.css("width",toolbarWidth + buttonsWidth + "px");
			return buttonsWidth;
		},
		
		//Add new text or image logo
		addLogo: function( name, type) {
			var logo = this.toolbarDiv.find(".TB_logo");
			switch(type) {
				case "text":
					logo.text(name);
					break;
				case "image":
					var img = new Image();//new image

					img.src = name;//set source
					logo.html(img);//add image to span
					
					this.toolbarDiv.find(".TB_logo").css("padding:","3px 2px");
				break;
			}
		},
		
		//slides the toolbar in a particular direction
		slide:	function slide(direction){
			var i = parseInt(this.toolbarDiv.css("marginLeft"));
			(isNaN(i)) ?  i=0 : false
			//if the toolbar is still within its constraints, allow the slide		
	 		if( (i > (this.toolbarWidth() * -1) && direction=="right") || (i < 0 && direction=="left") ){
	 			//determines the direction								
	 			(direction=="left") ? i+=20 : i -=20; 
	 			if(i > 0)	i=0;   
	 			if(i < -this.toolbarWidth()) i = -this.toolbarWidth();
	 			
				this.toolbarDiv.css("marginLeft",i + 'px' );
				
				ref = this;
				timer=setTimeout("ref.slide('"+direction+"')",10);
			} 
			else stop();
		},
		
		//changes the generic button to be a specific icon
		changeButtonIcon: function(id,icon){
			this.toolbarDiv.find("li[btnid="+id+"]")
			if(icon=="")
				icon = imageDirectory + "/global/spacer.png";
			else
			 	icon = icon.substring(0,icon.length-3) + "png";
	
			this.toolbarDiv.find("li[btnid="+id+"]").find("img").attr("src",icon);
			$("#button"+(id * -1) +"_image").attr("src",icon);
		},
		
		//changes the text associated with the icon
		changeButtonName: function(id,text){
			$("#button"+(id*-1)+"_name").text(text);
			this.toolbarDiv.find("li[btnid="+id+"]").find("span").text(text);
			
			$("a[btnid="+id+"].removeApplicationLink").parent().prev().text(text);
		},
		
		//changes the URL associated with the buttons 
		changeURL: function(id,element){
			var validURL = false;
			//checks for a period indicating a URL
			if(element.value.indexOf(".") != -1){
				element.style.backgroundColor = "";
				element.setAttribute("valid","");
				validURL=true;
			}
			else{	
				element.setAttribute("valid","false");
				element.style.backgroundColor = "#FFAFAF";
			}
			
			//check for protocol
			if(element.value.indexOf("http:")==-1 && element.value.indexOf("https:") == -1)
				element.value = "http://" + element.value;
			
			
			if(validURL){
				var buttonName = $(element).parent().prev().children("input").val();
				var alreadyThere=false;
				
				//checks to see if custom button is already there
				$("#widgetButtons td.customText").each(function(){
					if($(this).text()==buttonName)	
						alreadyThere=true;
				});
				
				if(!alreadyThere){
					if(!document.getElementById("widgetButtons")){
						$("#WidgetButtonsTitle").after('<div><table border="0" width="100%" id="widgetButtons"></table></div>');
						//gets rid of the text "none"
						$(".spanThemesClass").each(function(){
							if($(this).text().indexOf("Buttons") != -1)
								this.nextSibling.nodeValue="";	
						});
					}
					
					//adds the button to the table.
					$("#widgetButtons").append("<tr><td width='7'><img src='http://ak.imgfarm.com/images/sstb/my_toolbar/bullet_green.png'/></td>"
					+" <td class='tdNavItemBtn customText'>"+buttonName+"</td>"
					+'<td width="15"><a rel="#RemoveButton" href="javascript:void(0)" btnid="'+ id +'" class="affinityLink removeApplicationLink"></a></td>');
					
					//enables click event for the "remove application button"
					$("a[btnid="+ id +"]").click(function(){
						$("a[btnid="+ id +"]").each(function(){ changeApplication(this,"remove") } );
					});	
				}
			}
		},
		
		scrollTo: function(button,duration){	
			var buttonsWidth= 0;
			var ref = this;
			$(this.divID + " ul > li").each(function(){
				if(this == button)
					ref.toolbarDiv.animate({marginLeft: -buttonsWidth + "px"},1200);
				else
				buttonsWidth += this.clientWidth;
				
			});
			

						
		}
	}