﻿var SRH = new Array();
var SRH_ViewZoneID = new Array();
var SRH_ViewZoneProp = new Array();
var SRH_Prefix = new Array();
var SRH_SuffixStart = new Array();
var SRH_ContentZoneProp = new Array();
var SRH_SuffixCurrent = new Array();
var SRH_Delay = new Array();
var SRH_Interval = new Array();
var SRH_PreCall = new Array();
var SRH_PostCall = new Array();

/* A Dependant Function to create an automatic content switching functionality that cusotmizes the intervals, delays, preCalls and postCalls and other features.
Author : Alaa Sarhan (2010).
Depends On:
	- the functions {SwitchContents, FireSwitch}.
	- the arrays {SRH, SRH_ViewZoneID, SRH_Prefix, SRH_SuffixStart, SRH_SuffixCurrent, SRH_Delay, SRH_Interval, SRH_Interval, SRH_PostCall}.
Params:
	id : A unique id of the Automatic Switcher(AS).
	viewZoneID : The ID of the HTML Element (View Zone) that will receive the new contents on each switch.
	contentContainerPrefix : The prefix that is used as a prefix of the id of the HTML Elements that contain the
							 contents that will set into the View Zone.
							 The HTML Elements ids are Cosisted of two values: this suffix followed by a successive
							 numeric values (index) that determines the order of the contents in the switch process.
	suffixStart : The index of the first HTML Element in the switch process.
				  this value will be used to reset the current index when a final container reached to loop again.
	delay : The delay time that will be used before the first siwtch in the switch process after it was stop
			or hasn't started before yet.
	interval : The interval that is used between a switch and the next one in milliseconds.
	fireImmediatly : A value of true to fire the switch process of this AS after the creation, or a value of false
					 to leave the firing to the user using the FireSwitch function.
	preCall : a function that is called immediatly before the switching contents.
	postCall : a function that is called immediatly afret the switching contents.
Returns:
	A value of true on success in creating the automatic switcher, or an error message.
Error:
	the functions returns an error message in any of the following cases:
	- the given id is assigned to another AS.
	- View Zone HTML Element doesn't exist in the document.
	- A Content HTML Element which its index is the given starting index doesn't exist in the document.
*/
function CreateAutomaticSwitcher(id, viewZoneID, viewZoneProp, contentContainerPrefix, suffixStart, contentZoneProp, delay, interval, fireImmediatly, preCall, postCall)
{
	
	if(id in SRH)
		return "Can't Create Automatic Switcher with id = '" + id + "'. Another Siwtcher with the same ID exists.";
	
	if(!document.getElementById(viewZoneID))
		return "Can't Create Automatic Switcher. Can't find the View Zone HTML Element with id = '" + viewZoneID + "'.";
	
	if(viewZoneProp.length == 0)
		viewZoneProp = "innerHTML";
	
	if(contentZoneProp.length == 0)
		contentZoneProp = "innerHTML";
	
	if(!document.getElementById(contentContainerPrefix + suffixStart))
		return "Can't Create Automatic Switcher. Can't find the First Content Container HTML Element. At least one Content Container HTML Element must exist and it must have the index of the given starting index.";
		
	if(delay < 0)
		delay = 0;
		
	interval = Number(interval);
	if(interval < 1000)
		interval = 1000;
		
	if(typeof(preCall) != "function")
		preCall = null;
	
	if(typeof(postCall) != "function")
		postCall = null;
		
	SRH[id] = null;
	SRH_ViewZoneID[id] = viewZoneID;
	SRH_ViewZoneProp[id] = viewZoneProp
	SRH_Prefix[id] = contentContainerPrefix;
	SRH_SuffixStart[id] = suffixStart;
	SRH_SuffixCurrent[id] = suffixStart;
	SRH_ContentZoneProp[id] = contentZoneProp;
	SRH_Delay[id] = delay;
	SRH_Interval[id] = interval;
	SRH_PreCall[id] = preCall;
	SRH_PostCall[id] = postCall;
	
	if(fireImmediatly)
		FireSwitch(id);
		
	return true;
}


/* A Dependant function to Fire a previously created AS's switch process.
Author : Alaa Sarhan (2010)
Depends on:
	- the functions {PutOutSwitch, DoSwitchRequest}.
	- the arrays {SRH, SRH_Delay}.
Params :
	id : the id of the AS to fire its switch process.
Returns:
	A value of true on success, or an error message.
Errors:
	An error message is returnd in any of the following cases:
	- The AS with the given id was not found neither in the array SRH nor in the array SRH_Delay.
*/
function FireSwitch(id)
{	
	if(id in SRH && id in SRH_Delay)
	{
		PutOutSwitch(id);
		SRH[id] = setTimeout("DoSwitchRequest('" + id + "');", SRH_Delay[id]);
	}
	else
		return "Automatic Switcher Couldn't be fired. Can't find Automatic Switcher with id = '" + id + "'.";
		
	return true;
}

/* A Dependant to PutOutSwitch (Stop Switch) that was previously created.
Author : Alaa Sarhan (2010).
Depends on:
	- the arrays {SRH}
Params:
	id : the id of the AS to Put Out its Switch Process.
Returns:
	A value of true on success, or an error message.
Error:
	An error message is return in any of the following cases:
	- the AS with the given id was not found in the array SRH.
*/
function PutOutSwitch(id)
{
	if(id in SRH && SRH[id] != null)
	{
		clearTimeout(SRH[id]);
		SRH[id] = null;
	}
	else
		return "Automatic Switcher Couldn't be Put Out. Can't find Automatic Switcher with id = '" + id + "'.";
		
	return true;
}

/* A Dependant function to Reset the index of the switch process to its stating index
Author : Alaa Sarhan (2010)
Depends on:
	- the arrays {SRH, SRH_SuffixStart, SRH_SuffixCurrent}
Params:
	id : the id of the AS to reset its index.
Returns:
	A value of true on success, or an error message
Errors:
	An error message is returned in any of the following cases:
	- The AS doesn't exist.
*/
function ResetSwitch(id)
{
	if(id in SRH && id in SRH_SuffixStart && id in SRH_SuffixCurrent)
		SRH_SuffixCurrent[id] = SRH_SuffixStart[id];
	else
		return "Error Resetting The Index of the AS. The AS wasn't found with id = '" + id + "'.";
	
	return true;
}

/* A Dependant function to determine wheather the AS is fired or put out.
Author: Alaa Sarhan (2010)
Depends on:
	- the arrays {SRH}
Params:
	id : the id of the AS to check.
Returns:
	A value of true if the AS with the given id is fired and in process, or a value of false if it is put out and
	not in process, or an error message.
Errors:
	An error message is returned in any of the following cases:
	- The AS with the given id doesn't exist in the array SRH.
*/
function IsFired(id)
{
	if(!(id in SRH))
		return "The Automatic Switcher doesn't exist with id = '" + id + "'.";
	if(SRH[id] != null)
		return true;
	else
		return false;
}

/* A Dependant function to Destroy a previously created AS
Author : Alaa Sarhan (2010)
Depends on:
	- the functions {PutOutSwitch}.
	- the arrays {SRH, SRH_ViewZoneID, SRH_Prefix, SRH_SuffixStart, SRH_SuffixCurrent, SRH_Delay, SRH_Interval, SRH_Interval, SRH_PostCall}.
Params:
	id : the id of the AS to destroy.
Returns:
	A value of true on success, or false on failure.
*/
function DestroyAutomaticSwitcher(id)
{
	PutOutSwitch(id);
	result = false;
	result = result || delete SRH[id];
	result = result || delete SRH_ViewZoneID[id];
	result = result || delete SRH_Prefix[id];
	result = result || delete SRH_SuffixStart[id];
	result = result || delete SRH_SuffixCurrent[id];
	result = result || delete SRH_Delay[id];
	result = result || delete SRH_Interval[id];
	result = result || delete SRH_Interval[id];
	result = result || delete SRH_PostCall[id];
	
	return result;
}

/* A Dependant recursive function that is used to acheive the switch process functionality.
Author: Alaa Sarhan (2010)
Depends on:
	- the functions {PutOutSwitch, SwitchContents}.
*/
function DoSwitchRequest(id)
{
	if(!(id in SRH_ViewZoneID) || !(id in SRH_Prefix) || !(id in SRH_SuffixCurrent) || !(id in SRH_Delay) || !(id in SRH_Interval)
		|| !(id in SRH_Interval) || !(id in SRH_PostCall) || !(id in SRH_SuffixStart))
	{
		PutOutSwitch(id);
		return "Error Doing Switch Request. Some Properties of Automatic Switcher can't be found with id = '" + id + "'.";
	}
	
	if((switchResult = SwitchContents(SRH_ViewZoneID[id], SRH_ViewZoneProp[id], SRH_Prefix[id] + SRH_SuffixCurrent[id], SRH_ContentZoneProp[id], false, SRH_Interval[id], SRH_PostCall[id])) != true)
	{
		PutOutSwitch(id);
		alert(switchResult);
		return "Error Doing Switch Request. " + switchResult + " Putting Out Switch Job for A.S. with id = '" + id + "'.";
	}
	
	SRH_SuffixCurrent[id] += 1;
	if(!document.getElementById(SRH_Prefix[id] + SRH_SuffixCurrent[id]) && (resetRes = ResetSwitch(id)) != true)
		return "Error Doing Switch Request. " + resetRes;
		
	SRH[id] = setTimeout("DoSwitchRequest(\"" + id + "\");",SRH_Interval[id]);
	
	return true;
}


/* An independant function to switch contents of an HTML element with another's one.
Author: Alaa Sarhan (2010)
Params:
	currentContainerID : the id of the HTML element that its contents will be replaced.
	desiredCotainerID : the id of the HTML element that its contents will replace or be appended to the contents of the
						given element in the previous parameter.
	append : use value of true to append the content to the old one, or false to replace.
	preCall : a function that will be called before the replacement or appending occure, the currentContainerID and desiredContainerID params will be passed to this function respectivly.
	postCall : a function that will be called after the replacement or appending occure, the currentContainerID and desiredContainerID params will be passed to this function respectivly.
Accepted Cases:
	This function now can deal with HTML elements that have a value property or innerHTML property, and if
	the value property was found it will be used to get or set contents, and if not the innerHTML propery 
	will be used instead.
	if neather the value nor the innerHTML property was found in on of the containers an error is returned
Returns:
	A value of true when the job of the function finishes successfully.
	On Error, a message describing the error is returned;
Errors:
	An Error Message will be returned by this function on any of the following cases:
	- An HTML element with currentContainerID or desiredContainerID was not found in the document.
	- one of the elements at leats doesn't support neather value nor innerHTML properties.
	
*/
function SwitchContents(currentContainerID, currentProp, desiredContainerID, desiredProp, append, preCall, postCall)
{
	// Finding Elements to Switch
	current = document.getElementById(currentContainerID);
	if(!current)
		return "Coudln't find Container of the Current Contents (" + currentContainerID + ").";
	desired = document.getElementById(desiredContainerID);
	if(!desired)
		return "Coudln't find Container of the Disered Contents (" + desiredContainerID + ").";
	
	// Getting the Desired Contents
	if(desiredProp in desired)
		content = eval("desired." + desiredProp);
	else
		return "Can't reach the Content of the Desired Container (" + desiredContainerID + "), Property " + desiredProp;
	
	// Replacing Current Contents
	if(currentProp in current)
	{
		if(typeof(preCall) == "function")
			preCall(currentContainerID, desiredContainerID);
			
		if(!append)
			current[currentProp] = "";
		current[currentProp] += content;
		
		if(typeof(postCall) == "function")
			postCall(currentContainerID, desiredContainerID);
	}
	else
		return "Can't Reach the Content of the Current Container (" + currentContainerID + "), Property " + currentProp;
		
	return true;
}
