var loaded=true;
if(MlsPageButtons == null){
	var MlsPageButtons = new Array();
	loaded=false;
}

function Button()
{					  
    var name;
    var isNotIE = false;
    var id;
    var text;
    var spanText;
    var onclick;
    var onmouseoverclass;
    var onmouseoutclass;
    var classname;
    var buttonImage;
    var objButtonImage;
    var container;
    var containerLevel;
    var buttonElement;
    var baseImagePath;
    var enableAutoClick = false;
    var leftBorder;
    var rightBorder;
    var okToClean = true;
    
    if(Button.arguments.length > 0)
	{
		var oArg = Button.arguments[0];
		
		// get id of button.
		if(oArg.id != null)
		{
			// Get ID button.
			id = oArg.id;
		}// if(oArg.id != null)
		
		// get name of button.
		if(oArg.name != null)
		{
			// Get name of button.
			name = oArg.name;
		}// if(oArg.id != null)
		
		// get name of button.
		if(oArg.text != null)
		{
			// Get name of button.
			text = oArg.text;
		}// if(oArg.id != null)
		
		// get name of button.
		if(oArg.buttonImage != null)
		{
			// Get name of button.
			buttonImage = oArg.buttonImage;
		}// if(oArg.id != null)
		
		// get if we need to use auto click instead of onclick event.
		if (oArg.enableAutoClick)
		{
			enableAutoClick = oArg.enableAutoClick == '1';			
		}

		// get onclick event
		if (oArg.onclick != null)
		{
		    onclick = oArg.onclick;
		}
		// get onmouseover class name
		if (oArg.onmouseoverclass != null)
		{
		    if(document.all)
		    {
		        onmouseoverclass = oArg.onmouseoverclass + "Bar";
		    }
		    else if(!document.all)
		    {
		        onmouseoverclass = oArg.onmouseoverclass + "BarNoIE";
		    }
		}
		// get onmouseout class name
		if (oArg.onmouseoutclass != null)
		{
            if(document.all)
		    {
		        onmouseoutclass = oArg.onmouseoutclass + "Bar";
		    }
		    else
		    {
		        onmouseoutclass = oArg.onmouseoutclass + "BarNoIE";
		    }
		}
		// get  class name
		if (oArg.classname != null)
		{
		    classname = oArg.classname + "Bar";
		}
		// get  class name
		if (oArg.baseImagePath != null)
		{
		    baseImagePath = oArg.baseImagePath;
		}
		//get container id
		if (oArg.container != null)
		{
		    container = oArg.container;
		}
		
		// get level
		if (oArg.containerLevel != null)
		{
		    containerLevel = oArg.containerLevel;
		}
    }
    
    // Define Properties
    this.ID = id;
    this.OnClick = onclick;
    this.Text = text;
    this.SpanText = spanText;
    this.ButtonImage = buttonImage;
    this.ObjButtonImage = objButtonImage;
    this.ButtonElement = buttonElement;
    this.BaseImagePath = baseImagePath;
    this.Container = container;
    this.ContainerLevel = containerLevel;
    this.ClassName = classname;
    this.LeftBorder = leftBorder;
    this.RightBorder = rightBorder;
    this.OkToClean = okToClean;
    
    // Define Methods
    this.Init = Init;
    this.Add = Add;
    this.Remove = Remove;
    this.GetContainerFrame = GetContainerFrame;
    this.GetBorder = GetBorder;
    this.SetText = SetText;
    this.GetText = GetText;
    this.Hide = Hide;
    this.Show = Show;
    this.SetGraphic = SetGraphic;
    this.SetOnClick = SetOnClick;
    this.SetDisable = SetDisable;
    this.SetUnloadBlock = SetUnloadBlock;
    
    // Events
    this.OnClickRaise = OnClickRaise;
    this.OnMouseOverRaise = OnMouseOverRaise;
    this.OnMouseOutRaise = OnMouseOutRaise;
    this.AutoClick = AutoClick;
    
    // Initialize button
    function Init()
    {
        var containerFrame = this.GetContainerFrame();
        buttonElement = containerFrame.document.createElement("button");
        buttonElement.id = id;
        
        if(!document.all)
        {
            this.ClassName = this.ClassName + "NoIE";
        }

        buttonElement.className = this.ClassName;

        if (enableAutoClick)
        {
			buttonElement.onclick = this.AutoClick;
        }
        else
        {
			buttonElement.onclick = this.OnClickRaise;
        }
        
        buttonElement.onmouseover = this.OnMouseOverRaise;
        buttonElement.onmouseout = this.OnMouseOutRaise;
        var objButtonSpan = containerFrame.document.createElement("span");

        var objContentDiv = containerFrame.document.createElement("div");

		// Add button image       
        if(this.ButtonImage != null)
		{
			var span = containerFrame.document.createElement("span");
			var img = containerFrame.document.createElement("img");
			img.src = this.BaseImagePath + this.ButtonImage;
			img.style.marginRight = "2px";
			img.style.marginLeft="2px"
			img.style.height = "23px";
			img.style.width="28px";
			if(isIE6())
			{
				var imgID = (img.id) ? "id='" + img.id + "' " : "";
				var imgClass = (img.className) ? "class='" + img.className + "' " : "";
				var imgTitle = (img.title) ? img.title : img.alt;
				var imgStyle = "display:inline-block;" + img.style.cssText ;
				if (img.align == "left") imgStyle = "float:left;" + imgStyle;
				if (img.align == "right") imgStyle = "float:right;" + imgStyle;
				//if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle;                        
				span.id=imgID;
				span.className=imgClass;
				span.title = imgTitle;
				span.style.width = "28px";
				span.style.height = "23px";
				span.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader"+
				"(src='"+img.src+"',sizingMethod='scale')";
				//span.appendChild(img);
				this.ObjButtonImage = span;
				//objContentDiv.appendChild(span);
				objButtonSpan.style.position = "relative";
				objButtonSpan.style.marginRight = "-2px";
				objButtonSpan.style.top = "-3px";
			}else{
				//this.ObjButtonImage = img;
				//objContentDiv.appendChild(img);
			}
        }

        // Add button text.
        objButtonSpan.style.background = "transparent url(" + this.BaseImagePath + this.ButtonImage + ") no-repeat 0px -2px";
        objButtonSpan.innerHTML = this.Text;
        objContentDiv.appendChild(objButtonSpan);
        buttonElement.appendChild(objContentDiv);
	
        this.ButtonElement = buttonElement;
        this.SpanText = objButtonSpan;
    }
    
    // On click raise.
    function OnClickRaise()
    {
        eval(onclick + "(this)");
    }
    
    // On mouse over raise.
    function OnMouseOverRaise()
    {
        eval("this.className='" + onmouseoverclass + "'");
    }
    
    // On mouse out raise.
    function OnMouseOutRaise()
    {
        eval("this.className='" + onmouseoutclass + "'");
    }
    
    // auto click on button with same id in child frame.
    function AutoClick()
    {
		var eventHandleButton = document.getElementById(id);
		
		if (eventHandleButton != null)
		{
			eventHandleButton.click();	
		}
		else
		{
			alert('Unable to find button in child frame with id: ' + id + '. Please look at id passed to button object.');
		}
		
    }
    
    // Add the button.
    function Add()
    {
        var containerFrame = this.GetContainerFrame();
        var containerObj = null;
        
        if (containerFrame != null)
        {
            containerObj = containerFrame.document.getElementById(this.Container);
			
			//Add Left Border if it exists
			if (this.LeftBorder != null)
			{
				containerObj.appendChild(this.LeftBorder);
			}
         
            containerObj.appendChild(this.ButtonElement);
            
            //Add Right Border is it exists
			if (this.RightBorder != null)
			{
				containerObj.appendChild(this.RightBorder);
			}
			CheckResolution(containerObj);
        }
    }
    
    function GetBorder(position, total)
	{
		var containerFrame = this.GetContainerFrame();
        var containerObj = containerFrame.document.getElementById(this.Container);
		
		//Add left and right borders (General Case)
		leftBorder = containerFrame.document.createElement("img");
		leftBorder.src = this.BaseImagePath + "/toolbarseparator.png";
		this.LeftBorder = leftBorder;
	
		rightBorder = containerFrame.document.createElement("img");
		rightBorder.src = this.BaseImagePath + "/toolbarseparator.png";
		this.RightBorder = rightBorder;
		
		if (position != 0)
		{
			this.LeftBorder = null;
		}
	}

    
    // Remove the button.
    function Remove()
    {
        var containerFrame = this.GetContainerFrame();
        var containerObj = null;
        
        if (containerFrame != null)
        {
            containerObj = containerFrame.document.getElementById(this.Container);
            
            if(containerObj.children.length > 0)
            {
				//Remove left border if it exists
					try{
						containerObj.removeChild(this.LeftBorder);
					}catch(err){}
				containerObj.removeChild(this.ButtonElement);
				//Remove right border if it exists
				try
				{
					containerObj.removeChild(this.RightBorder);
				}catch(err){}
            }
        }
    }
    
    // Returns the container.
    function GetContainerFrame()
    {
        var containerFrame = self; 
        
        for (var counter = 0; counter < this.ContainerLevel; counter++)
        {
            containerFrame = containerFrame.parent;
        }
        
        return containerFrame;
    }
    
	// Mutator/Accessor Methods
	function SetText(newText)
	{
		this.Text = newText;      
		var containerFrame = this.GetContainerFrame();
        var containerObj = null;
        
        if(this.ButtonElement!=null){
			// Remove old text. 
			if (containerFrame != null)
			{
				containerObj = containerFrame.document.getElementById(this.Container);
				if (this.ButtonElement.childNodes.length > 0) 
				{
				    this.SpanText.innerHTML = newText;
				}
			}
		}
	}    
    
    function GetText()
	{
		return this.SpanText.innerHTML;
    }
    
    function Hide()
    {
        this.ButtonElement.style.display="none";
        this.RightBorder.style.display="none";
    }
    
    function Show()
    {
        this.ButtonElement.style.display="";
        this.RightBorder.style.display="";
    }
    
    function SetGraphic(imgPath)
    {
		this.ButtonImage = imgPath;
		var containerFrame = this.GetContainerFrame();
        var containerObj = null;
		
		// Remove old image. 
        if (containerFrame != null)
        {
            containerObj = containerFrame.document.getElementById(this.Container);
        
			if(this.ButtonElement.children.length > 0)
            {
				this.ButtonElement.removeChild(this.ObjButtonImage);
            }
        }
		
		// Add button image.       
        if(this.ButtonImage != null)
        {
			var objButtonImage = containerFrame.document.createElement("img");
			objButtonImage.src = this.BaseImagePath + this.ButtonImage;
			objButtonImage.style.marginRight = "2px";
			objButtonImage.style.marginLeft = "2px";
			//this.ButtonElement.appendChild(objButtonImage);
			buttonElement.style.background = "transparent url(" + this.BaseImagePath + this.ButtonImage + ") no-repeat 5px 5px";			
			this.ObjButtonImage = objButtonImage;
        }
        this.SetText(this.Text);
    }
    
    function SetOnClick(fcnName)
    {
		this.OnClick = fcnName;
    }
}


//Method to add page buttons.
function AddPageButtons()
{
    if (document.readyState == "complete" && !isNotIE)
    {
		var pageButtonObj;
		var position = 0;
		var count = 0;
		
		for(var pageButton in MlsPageButtons)
        {
			count = count + 1;
        }

        for(var pageButton in MlsPageButtons)
        {
            pageButtonObj = MlsPageButtons[pageButton];
		    pageButtonObj.GetBorder(position, count);
            pageButtonObj.Init();
            pageButtonObj.Add();
            position = position + 1;
        }

        document.body.onbeforeunload = RemovePageButtons;
    }else if(isNotIE)
    {
        var pageButtonObj;
		var position = 0;
		var count = 0;
		
		for(var pageButton in MlsPageButtons)
        {
			count = count + 1;
        }

        for(var pageButton in MlsPageButtons)
        {
            pageButtonObj = MlsPageButtons[pageButton];
            
		    pageButtonObj.GetBorder(position, count);
            pageButtonObj.Init();
            pageButtonObj.Add();
            position = position + 1;
        }
        //to change this for Firefox
        document.body.onbeforeunload = RemovePageButtons;
    }
}

// Method to remove page buttons.
function RemovePageButtons()
{
    CheckResolution();
	if(this.OkToClean){
		for(var pageButton in MlsPageButtons)
		{
			var pageButtonObj = MlsPageButtons[pageButton]
			pageButtonObj.Remove();
		}
	}
} 

function SetDisable(isDisabled)
{
	this.ButtonElement.disabled = isDisabled;
	if(isDisabled == false) 
	{
	   
	    this.ButtonElement.className = this.ClassName;
	}
	else 
	{
	    if(document.all)
	    {
	        this.ButtonElement.className = "DisToolBarButton";
	    }
	    else if(!document.all)
	    {
	        this.ButtonElement.className = "DisToolBarButtonNoIE";
	    }
	}
}

function SetUnloadBlock()
{
	this.OkToClean=false;
}

function UpdateButton(ButtonID)
{
	alert(MlsPageButtons[ButtonID]);
}

function isIE6(){
	version=10;
	if (navigator.appVersion.indexOf("MSIE")!=-1){
		temp=navigator.appVersion.split("MSIE");
		version=parseFloat(temp[1]);
	}
	if (version<7){
		return true;
	}
	return false;
}

function CheckResolution(contObj)
{
    try
    {
        if(contObj!=null && contObj.offsetWidth >= 645 && screen.width<1000)
	    {
	        if(parent.document.body != null)
	        {
	            parent.document.body.scroll = "yes";
	        }
    	    
	    }
	    else
	    {
	        if(parent.document.body != null)
	        {
	            parent.document.body.scroll = "no";
	        }
	    }
	}
	catch(problem){}
}

if(!loaded)
{
//AttachEvent(document, 'load', AddPageButtons);

	//document.attachEvent("onreadystatechange", AddPageButtons);
	if(window.addEventListener){ // Mozilla, Netscape, Firefox
	    AttachEvent(window, 'load', AddPageButtons);
	    //object.addEventListener('click', testevent, false);
	    //object.myflag = "test";
	    //object.mydata = "123";
	    isNotIE = true;
    } else { // IE
	    AttachEvent(document, 'readystatechange', AddPageButtons);
	    isNotIE = false;
    }
}

//Cross Browser AttachEvent try
function AttachEvent(obj, evt, func, useCap)
{
    if(!useCap)
    {
        useCap=false;
    }
    if(obj.addEventListener)
    {
        obj.addEventListener(evt, func, useCap)
        return true;
    }
    else if(obj.attachEvent)
    {
        return obj.attachEvent("on" + evt, func);
    }
}