
// the function loop on the 'ButtonsArr' array and initiate a several actions
// relevant to the source code of the images or input image elemnts that their
// id and sources are in it
//****************************************
function Apply_sourse_param_and_events_for_images()
{
	var InputTag, InputData;
	// loop on array
	for (var i in ButtonsArr)
	{
		// a referance to elemnts javascript object
		InputData = ButtonsArr[i];
		// a referance to elemnts html object
		InputTag = document.getElementById(i);
		if (! InputTag){continue;}
		// attach the events to the element
		AttachEvent(InputTag, "onmouseover", new Function("ChangeButtonSrc('"+i+"', 'Src_Over')"));
		AttachEvent(InputTag, "onmouseout", new Function("ChangeButtonSrc('"+i+"', 'Src_Out')"));
		AttachEvent(InputTag, "onmousedown", new Function("ChangeButtonSrc('"+i+"', 'Src_Down')"));
		AttachEvent(InputTag, "onmouseup", new Function("ChangeButtonSrc('"+i+"', 'Src_Up')"));
		AttachEvent(InputTag, "onclick", new Function("ChangeButtonSrc('"+i+"', 'Src_Click')"));
		
		// if a source not found for the element- the source will apply on it now
		if(((InputTag.src == "") || (InputTag.src == BaseUrlParam.base_tag_href) 
		|| (InputTag.src == BaseUrlParam.folder_href) || (InputTag.src == window.location.href)) 
		&& (InputData.Src.src != ""))
		{
			InputTag.src = InputData.Src.src;
		}
		
		// apply title and alt now if the element dont have it all ready
		if (InputData.Alt != "")
		{
			if(InputTag.alt == "")
			{
				InputTag.alt = InputData.Alt;
			}
			if(InputTag.title == "")
			{
				InputTag.title = InputData.Alt;
			}
		}
				
		InputTag.style.visibility = "visible";	
	}
}


// the function return urls of base tag(if exist), the folder url...
function FoundBaseUrl()
{
	// base tag url
	 function base_tag_href()
	 {
		var BaseTag = document.getElementsByTagName("base");
		var BaseUrl = "";
		if(BaseTag.length){
			return BaseTag.item(0).href;
		}
		return "";
	}
	this.base_tag_href = base_tag_href();
	
	// folder url
	function folder_href()
	{
		var PageLocation = window.location.href;
		if (PageLocation.indexOf("?") > -1)
		{
			PageLocation = PageLocation.split("?")[0];
		}
		var LastIndexOf1 = PageLocation.lastIndexOf("/");
		var LastIndexOf2 = PageLocation.lastIndexOf("\\");
		var LastIndexOf3 = PageLocation.lastIndexOf("%5C");
		
		var LastIndexOf = (Math.max(Math.max(LastIndexOf1, LastIndexOf2), LastIndexOf3));
		
		return window.location.href.substring(0, LastIndexOf)+"/";
	}
	this.folder_href = folder_href();
}
var BaseUrlParam = new FoundBaseUrl();


// the function deal with loading images errors. the function can be call by the image
// up to 5 times- try to load the image again. each time updated a counter.
function SrcError(ID, Src_event)
{
	// a referance to elemnts javascript object
	var InputData = ButtonsArr[ID];
	var Loading_Attempt_Counter = InputData[Src_event+"_Loading_Attempt_Counter"];
	if (Loading_Attempt_Counter > 5){return;}
	
	InputData[Src_event+"_Loading_Attempt_Counter"] ++;
	if (Src_event == "Src")
	{
		var InputTag = document.getElementById(ID);
		if (! InputTag){return;}
		AttachEvent(InputTag, "onerror", new Function("SrcError('"+ID+"', 'Src')"));
		InputTag.src = InputTag.src;
		return;
	}
	InputData[Src_event].src = InputData[Src_event].src;
}

// the function apply different source to element. the function find the javascript
// object that contain the html element data- pull out and apply the src on the element 
function ChangeButtonSrc(ID, Src)
{
	var InputData = ButtonsArr[ID];
	var Image = InputData[Src];
	var InputTag = document.getElementById(ID);
	if (! Image){return null;}
	if (! InputTag){return;}
	InputTag.src = Image.src;
}

// the function present an mirror object to the html element that contain all the sources
// may be needed to the element. new Image object are been created for every source at the
// background and load it.
function ButtonObj(ID, PrefixUrl, Src, Src_Over, Src_Out, Src_Down, Src_Up, Src_Click, Alt)
{
	if (PrefixUrl === ""){PrefixUrl = "images/";}
	
	this.Src = CreateImg(Src, "Src");
	this.Src_Loading_Attempt_Counter = 0;
	
	this.Src_Over = CreateImg(Src_Over, "Src_Over");
	this.Src_Over_Loading_Attempt_Counter = 0;
	
	this.Src_Out = CreateImg(Src_Out, "Src_Out");
	this.Src_Out_Loading_Attempt_Counter = 0;
	
	this.Src_Down = CreateImg(Src_Down, "Src_Down");
	this.Src_Down_Loading_Attempt_Counter = 0;
	
	this.Src_Up = CreateImg(Src_Up, "Src_Up");
	this.Src_Up_Loading_Attempt_Counter = 0;
	
	this.Src_Click = CreateImg(Src_Click, "Src_Click");
	this.Src_Click_Loading_Attempt_Counter = 0;
	
	this.Alt = Alt;
	
	// the new Image created and get an "onerror" event so that it will try to load it
	// self again and again if the image broken.
	function CreateImg(ImageSrc, Src_event)
	{
		if (ImageSrc != "")
		{
			var NewImg = new Image();
			NewImg.src = PrefixUrl+ImageSrc;
			AttachEvent(NewImg, "onerror", new Function("SrcError('"+ID+"', '"+Src_event+"')"));
			return NewImg;
		}
		return null;
	}
}
