/*
*	File name: ScriptAndStyleLoader.js
*	Created by: Yossi Shalev of YOSHI Dynamic Design studio.
*	Contact: yossi@yoshi.co.il, http://www.yoshi.co.il
*	Last Modified: 07/01/2008
*
*	This page initialize all JavaScript settings and function for siteDisplay
*
*	Depandencies:
*		None
*	
*	Attributes:
*		rootPath - the path of the root folder
*		loadedStyleSheets - an array that hold's the indication which Style sheet was loaded
*
*	Function List:

*/



/**********************************************
*	Style Sheet Dynamic loading 
***********************************************/

//inicators for start and end of stylesheets list
var SHEETS_START = "<!--IncludeStyleSheet=";
var SHEETS_END = " sheetsEndIndecator-->";

var SHEETS_START_LEN = SHEETS_START.length;
var SHEETS_END_LEN = SHEETS_END.length;

//an associative array that hold's the indication which Style sheet was loaded
var loadedStyleSheets = {};

//the default templates
loadedStyleSheets.MainMenu = true;
loadedStyleSheets.SiteTemplate = true;


//get the list of style sheets to load from
//Start Indicator : <!--IncludeStyleSheet=
//End Indicator   : sheetEndIndecator-->
function get_styleSheets( content ){
	
	//get the start position of the StyleSheets area
	var start = content.search( SHEETS_START );
	var end; //the end of the tyle sheet string
	
	
	var stripedContent = content; //the returned content after stiping the style sheet area
	var styleSheetsNames = ""; //list of styles to load
	
	
	//if there is a style sheet area in the content
	if( start != -1 ){
		
		//search for the end of the stylesheets list  ( -1 because there is space beetwen the list and the End indicator )
		end = content.search( SHEETS_END );
		
		//strip the stylesheets area content 
		stripedContent = content.substr( 0, start ) + content.substr( (end + SHEETS_END_LEN ), ( content.length -  (end + SHEETS_END_LEN) ) );
	
		//get the list of style sheets
		styleSheetNames = content.substring( ( start +  SHEETS_START_LEN ), end );
	//alert( "Loading StyleSheet " +  styleSheetNames);
		//load the list of style sheets
		load_styleSheets( styleSheetNames );
		
		
	}
	
	//alert( stripedContent );
	
	return stripedContent;
}

//loading Style Sheet at run time
//insert an new sheet to the <HEAD>
function load_styleSheets( sheetNames ){
	
	if( !sheetNames ){
		return;
	}
	
	var sheets;
	
	sheets = sheetNames.split( "," );
	

	for( sheet = 0; sheet < sheets.length; sheet++ ){

		sheetName = sheets[ sheet ];
		styleLoaded = eval("loadedStyleSheets." + sheetName );

		if( ! styleLoaded ) {
					
			//alert( "Loading " + sheetName + " StyleSheet" );
			sheetPath = rootPath + "StyleSheets/" + sheetName + ".css";
	
			/*
			if( doc.createStyleSheet ){ //IE

				doc.createStyleSheet( sheetPath );	
		
			} else { // Others
	
				
				var head = doc.getElementsByTagName("head")[0];

				head.innerHTML += "<link id='" + sheetName + "' rel='stylesheet' type='text/css' href='" + sheetPath + "' />";
		
				alert( head.innerHTML );
			}
			*/
			
			var headID = document.getElementsByTagName("head")[0];
			var cssNode = document.createElement('link');cssNode.type = 'text/css';
			cssNode.rel = 'stylesheet';
			cssNode.href = sheetPath;
			cssNode.media = 'screen';
			headID.appendChild(cssNode);
	
			eval("loadedStyleSheets." + sheetName + " = true;" );
		}
	}
	
	return;
}


/**********************************************
*	Java Script Dynamic loading 
***********************************************/

//inicators for start and end of stylesheets list
var SCRIPTS_START = "<!--IncludeScripts=";
var SCRIPTS_END = " scriptsEndIndecator-->";

var SCRIPTS_START_LEN = SCRIPTS_START.length;
var SCRIPTS_END_LEN = SCRIPTS_END.length;

//an associative array that hold's the indication which Style sheet was loaded
var loadedScripts = {};

//the default templates
loadedScripts.Animation = true;
//loadedScripts.SiteTemplate = true;


//get the list of scripts to load from
//Start Indicator : <!--IncludeScripts=
//End Indicator   : scriptsEndIndecator-->
function get_scripts( content ){


	//get the start position of the Scripts area
	var start = content.search( SCRIPTS_START );
	var end; //the end of the tyle sheet string
	
	var stripedContent = content; //the returned content after stiping the script area
	var scriptsNames = ""; //list of scripts to load
	
	
	//if there is a scripts area in the content
	if( start != -1 ){
		
		//search for the end of the scripts list  ( -1 because there is space beetwen the list and the End indicator )
		end = content.search( SCRIPTS_END );
		
		//strip the scriptss area content 
		stripedContent = content.substr( 0, start ) + content.substr( (end + SCRIPTS_END_LEN ), ( content.length -  (end + SCRIPTS_END_LEN) ) );
	
		//get the list of scripts
		scriptsNames = content.substring( ( start +  SCRIPTS_START_LEN ), end );
	
		//load the list of scripts
		load_scripts( scriptsNames );
	
	}
	
	
	return stripedContent;
}

//loading Java Scripts at run time
//insert an new script to the <HEAD>
function load_scripts( scriptNames ){
	
	if( !scriptNames ){
		return;
	}
	
	var scripts;
	
	scripts = scriptNames.split( "," );
	

	for( script = 0; script < scripts.length; script++ ){

		scriptName = scripts[ script ];
		scriptLoaded = eval("loadedScripts." + scriptName );

		if( ! scriptLoaded ) {
					
			//alert( "Loading " + scriptName + " Script" );
			scriptPath = rootPath + "Scripts/" + scriptName + ".js";
			
			//ponter to the document head element
			var head = doc.getElementsByTagName('head').item(0);
			
			//new script object
		    var js = document.createElement('script');
		
			//set the new script attributes
			js.setAttribute('language', 'javascript');
		    js.setAttribute('type', 'text/javascript');
   			js.setAttribute('src', scriptPath );
			
			//append the new script to the head
		    head.appendChild(js);

	
			eval("loadedScripts." + scriptName + " = true;" );
		} 
		
	}
	
	return;
}


/**********************************************
*	Java Script Dynamic Executing 
***********************************************/



//inicators for start and end of stylesheets list
var SCRIPT_START = "<script>";
var SCRIPT_END = "</script>";

var SCRIPT_START_LEN = SCRIPT_START.length;
var SCRIPT_END_LEN = SCRIPT_END.length;




//execute all the script tags in the content
function execute_scripts( content ){
	
	var stripedContent = content; //the returned content after stiping the script area
	var start;
	var end;

	
	do {

	
		//get the start position of the Scripts area
		start = stripedContent.search(  SCRIPT_START );

		
		
		scriptLines = ""; //list of scripts to load

		//if there is a scripts area in the content
		if( start != -1 ){
	
	
			end = stripedContent.search( SCRIPT_END );
			
			//get the list of scripts
			scriptLines = stripedContent.substring( ( start +  SCRIPT_START_LEN ), end );
			
			//strip the scriptss area content 
			stripedContent = stripedContent.substr( 0, start ) + stripedContent.substr( (end + SCRIPT_END_LEN ), ( stripedContent.length -  (end + SCRIPT_END_LEN) ) );
//alert( scriptLines );
			//run the script lines
			eval( scriptLines );
		
		}	

	} while( start != -1 );
	

	return stripedContent;
	
}

