//  Buttons.js

var Buttons = {
	
	createOuter : function(id) {
		var outer = document.createElement("div");
		outer.id = id;
		outer.style.position = "relative";
		outer.style.left = "0px";
		outer.style.background="none";
		return outer;
	},
	
	createCustomAnchor : function(id,tip,enabled,class_Prefix,caption) {
		
		var theBackground = Buttons.createBackground(id);
		theBackground.className = class_Prefix+"_background";

		var theAnchor = document.createElement("a");
		theAnchor.href="#";
		theAnchor.className=class_Prefix+"_button";
		theAnchor.id = id+"_a";
		theAnchor.style.textDecoration = "none";
		
		if(enabled)
		{
			if(tip!=null)
			{
				theAnchor.onmouseover = function(){buttonOn(class_Prefix,id);return false;};
				theAnchor.onmouseout =  function(){buttonOff(class_Prefix,id);return false;};				
			}
			else
			{
				theAnchor.onmouseover = function(){buttonOn(class_Prefix,id);return false;};
				theAnchor.onmouseout =  function(){buttonOff(class_Prefix,id);return false;};					
			}
		}
		else
		{
			if(tip!=null)
			{
				theAnchor.onmouseover = function(){return false;};
				theAnchor.onmouseout = function(){return false;};
			}
		}
		
		var leftImage = Buttons.createLeftImage(id);

		leftImage.src = pathToButtons+eval(class_Prefix+"_leftImage") ;
		leftImage.className = class_Prefix+"_leftImage" ;
		
		var theCaption = Buttons.createCaption (id, caption) ;
		
		if(enabled)
			theCaption.className = class_Prefix+"_button_enabled";
		else
			theCaption.className = class_Prefix+"_button_disabled";
			
		var rightImage = Buttons.createRightImage(id);
		rightImage.src = pathToButtons+eval(class_Prefix+"_rightImage") ;
		rightImage.className = class_Prefix+"_rightImage" ;
		
		theBackground.appendChild(leftImage);
		theBackground.appendChild(theCaption);
		theBackground.appendChild(rightImage);
		theAnchor.appendChild(theBackground);
		return theAnchor;
	},
	
	createButtonAnchor : function(id,tip,enabled,class_Prefix,caption) {
		
		var theAnchor = document.createElement("input");
		theAnchor.type = "button";
		theAnchor.value = caption;
		theAnchor.className=class_Prefix+"_button";
		theAnchor.id = id+"_button";
		if(!enabled)
			theAnchor.disabled=true;
		else
			theAnchor.disabled=false;

		return theAnchor;
	},
	
	createLinkAnchor : function(id,tip,enabled,class_Prefix,caption) {
		
		var theAnchor = document.createElement("a");
		theAnchor.href="#";
		theAnchor.className=class_Prefix+"_button";
		theAnchor.id = id+"_a";
		theAnchor.innerHTML = caption;
		
		
		return theAnchor;
	},
	createImageButton : function(id,enabled,image,imageOn) {
		var theAnchor = document.createElement("img");
		theAnchor.src = pathToButtons+"/"+image;
		theAnchor.id = id+"_button";	

		if(enabled)
		{
			theAnchor.style.cursor = "pointer";

		}
		return theAnchor;
	},
	
	createBackground : function(id) {
		var theButtonBackground = document.createElement("div");
		theButtonBackground.id = id+"_background";
		theButtonBackground.style.width="0px";
		theButtonBackground.style.padding="0px";
		theButtonBackground.style.margin="0px";
		theButtonBackground.style.top="0px";
		theButtonBackground.style.overflow = "hidden";
		return theButtonBackground;
	},
	createLeftImage : function(id) {
		var leftImage = document.createElement("img");
		leftImage.id=id+"_button_left";
		return leftImage;
	},
	createCaption : function(id, caption ) {
		var theButtonCaption = document.createElement("span");
		theButtonCaption.id = id+"_caption";
		theButtonCaption.innerHTML = caption;
		theButtonCaption.style.position = "absolute";
		theButtonCaption.style.left = "0px";
		theButtonCaption.style.top = "0px";
		theButtonCaption.style.whiteSpace = "nowrap";
		theButtonCaption.style.padding="0px";
		theButtonCaption.style.marginTop="0px";
		return theButtonCaption;
	},
	createRightImage : function(id) {
		var rightImage = document.createElement("img");
		rightImage.id=id+"_button_right";
		rightImage.style.top="0px";
		return rightImage;
	},	

	create : function(options) {
		/*
		* OPTIONS
		* id, theCaption, enabled, onclick_actions, positionning, child_of, tip
		*/
		
		var id = options.id;
		var caption = options.theCaption;
		var enabled = options.enabled;
		var onclick_actions = options.onclick_actions;
		var positionning = options.positionning;
		var child_of = options.child_of;
		var tip = options.tip;
		var class_Prefix = options.class_Prefix ;
		var type = options.type;
		var outer = Buttons.createOuter(id);
		outer.className = class_Prefix+"_container";
		var image = options.image;
		var imageOn = options.imageOn;
		
		if(child_of==null)
			child_of="button_container";
		
		
		
		var theAnchor;
		if( (Object.isUndefined(type) ) || (type==null) || (type=="button") )
		{
			theAnchor = Buttons.createButtonAnchor(id,tip,enabled,class_Prefix,caption);
		}
		else if(type=="custom")
		{
			theAnchor = Buttons.createCustomAnchor(id,tip,enabled,class_Prefix,caption);
		}
		else if(type=="link")
		{
			theAnchor = Buttons.createLinkAnchor(id,tip,enabled,class_Prefix,caption);
		}
		else if(type=="image")
			theAnchor = Buttons.createImageButton(id,enabled,image,imageOn);

		theAnchor.onclick = onclick_actions;
		
		var show_tip = (!Object.isUndefined(tip) && (tip != null) && (tip != "")) ;
		
		if(enabled)
		{
			theAnchor.style.cursor = "pointer";
			theAnchor.disabled=false;
			theAnchor.onmouseover = function(){
					if(type=="custom")
						buttonOn(class_Prefix,id);
					else if(type=="image")
						this.src = pathToButtons + imageOn;

					
					return false;
				};
			theAnchor.onmouseout =  function(){
					if(type=="custom")
						buttonOff(class_Prefix,id);
					else if(type=="image")
						this.src = pathToButtons + image;

					return false;
				};					
		}	
		else{
			theAnchor.disabled=true;
		}	
		
		if(type=="link")
		{
			theAnchor.onmouseover=function(){is_selected(theAnchor.id);};
			theAnchor.onmouseout=function(){is_deselected(theAnchor.id);};
		}
		
		
		outer.appendChild(theAnchor);
		
		$(child_of).appendChild(outer);

	if($(id)!=null)	$(id).setStyle(positionning);

		if( type=="custom" )// placing image in background
		{
			
			$(id+"_background").style.width=$(id+"_caption").getWidth()+($(id+"_button_right").getWidth()*2)+"px";	
			center_element_in_div($(id+"_caption"),$(id+"_background"));	
			$(id+"_button_right").style.left= $(id+"_caption").getWidth()+$(id+"_button_right").getWidth()+"px";

		}
	}
}
