/**********************************************************************************
**
** Object: STYLE_PANEL
**
** 18.06.07 	mp 	safari bug, error unless object member functions are anonymous,
**					made all func anonymous
**
**
**********************************************************************************/
var STYLE_PANEL =
{
	objDocument: 		null,
	aSheets:	 	new Array(),
	objRenderer: 		null,
	selectedStyle: 		null,
	strDefaultSheet: 	'deafult',
	strFolderPrefix:	"",

	/**
	* Called on body load to initialise the object with required information
	*/
	initialise : 		function ()
				{
					this.objDocument = frames['contentFrame'].document;

					// set the active style
					var strPreferredStyle = top.strPreferredStyle;

					this.aSheets = new Array();

					// get all style sheets in page, forcing common.css to be default named sheet
					this.strDefaultSheet = 'default'; // Pst: HACK! overide for this prog only (70033)
					this.getStyleSheetsInPage(this.strDefaultSheet);


					// add alternative stylesheets - we do it here as FireFox can show these in a menu
					this.addStyleSheet('_css/content/alternatives/default.css', 'Default');
					this.addStyleSheet('_css/content/alternatives/plainandsimple.css', 'Plain and Simple');
					this.addStyleSheet('_css/content/alternatives/highcontrast.css', 'High Contrast');
					this.addStyleSheet('_css/content/alternatives/highcontrast_soft.css', 'High Contrast (Soft)');
					this.addStyleSheet('_css/content/alternatives/inverted.css', 'Inverted');
					this.addStyleSheet('_css/content/alternatives/highcontrast_inverted.css', 'High Contrast Inverted');
					this.addStyleSheet('_css/content/alternatives/highcontrast_soft_inverted.css', 'High Contrast Inverted (Soft)');
					this.addStyleSheet('_css/content/alternatives/blackandgreen.css', 'Green on Black');

					// change to the saved state...
					if( strPreferredStyle )
					{
						this.setActiveStyleSheetByName(strPreferredStyle);
					}
					else
					{
						// or force default sheet
						this.setActiveStyleSheetByName('Default');

					}

					//alert( "dynstyle.js initialise ");
				},


	replaceStyleSheet:	function (sOld, sNew)
				{
					// remove style sheet
					var iLinkArr = this.objDocument.getElementsByTagName('link');
					for(var i = 0; i < iLinkArr.length; i++)
					{
						var objLink = iLinkArr[i];

						if(objLink.getAttribute('href').toString().indexOf(sOld) != -1)
						{
							objLink.parentNode.removeChild(objLink);
						}
					}
					// add style sheet
					this.addStyleSheet(sNew);
				},

	/**
	* Add a stylesheet to the objects store and also into the current DOM
	* @param {String} sPath The full URI to the file, including filname itself, as you would specify in the href of a link tag
	* @param {String} sTitle The title of the stylesheet which will be used as switch title also
	* @member STYLE_PANEL
	*/
	addStyleSheet : 	function (sPath, sTitle)
				{
					// check we don't already have
					for (var _intSheet=0; _intSheet < this.aSheets.length; _intSheet++)
					{
						if (this.aSheets[_intSheet] == sTitle)
						{
							return;
						}
					}

					if(this.strFolderPrefix.length == 0)
					{
						// look for beginning of the module
						var strDocumentLocation = this.objDocument.location.toString();
						var intModuleStart = strDocumentLocation.lastIndexOf('/root/')+6;
						var intLastFolder = strDocumentLocation.lastIndexOf('/');
						var folderCount = strDocumentLocation.substring(intModuleStart, intLastFolder).split('/').length;
						for (var strFolderPrefix = "", i=0; i < folderCount;i++)
						{
							strFolderPrefix+="../";
						}

					}
					else
					{
						var strFolderPrefix = this.strFolderPrefix;
					}

					// create the link object
					domLink = this.objDocument.createElement('link');

					// set some sttributes
					if(sTitle)
					{
						domLink.setAttribute('rel', 'alternate stylesheet');
						domLink.setAttribute('title', sTitle);
					}
					else
					{
						domLink.setAttribute('rel', 'stylesheet');
					}

					domLink.setAttribute('type', 'text/css');
					domLink.setAttribute('href', strFolderPrefix+sPath);

					// add to the current document
					head = this.objDocument.getElementsByTagName('head')[0];
					head.appendChild(domLink);

					if(sTitle)
					{
						// search for it and if found add to objects store
						// could just add it but this is more robust
						aLinks = this.objDocument.getElementsByTagName('link');

						// loop through array
						for( i = 0; i < aLinks.length; i++)
						{
							// if this is a stylesheet and has a title equal to that just created
							if( aLinks[i].getAttribute("rel").indexOf("style") != -1 && aLinks[i].getAttribute("title") == sTitle )
							{
								// then add to store
								this.aSheets[this.aSheets.length] = aLinks[i];
							}
						}
					}
				},

	/**
	* Runs through DOM and picks out existing style sheets adding to store
	*/
	getStyleSheetsInPage: 	function (strDefault)
				{
					// get all link tags
					aLinks = this.objDocument.getElementsByTagName('link');

					// loop through array
					for( i = 0; i < aLinks.length; i++)
					{
						// if this is a stylesheet and has a title
						if( aLinks[i].getAttribute("rel").indexOf("style") != -1 )
						{
							strSheetName = aLinks[i].getAttribute("href");
							intStartPointer = strSheetName.lastIndexOf('/')+1;
							intEndPointer = strSheetName.lastIndexOf('.');

							if (strDefault == strSheetName.substring(intStartPointer, intEndPointer))
							{
								aLinks[i].setAttribute('title', 'Default');
							}

							if (aLinks[i].getAttribute("title"))
							{
								// add to store
								this.aSheets[this.aSheets.length] = aLinks[i];

								// if not alternate then assume current
								if(aLinks[i].getAttribute("rel").indexOf("alternate") == -1)
								{
									this.selectedStyle = this.aSheets.length-1;
								}
							}
						}
					}
				},

	/**
	*
	*/
	setActiveStyleSheetByName: function (sName)
				{
					// alert("setActiveStyleSheetByName "+sName);
					for (var i=0; i< this.aSheets.length; i++)
					{
						this.aSheets[i].disabled = true;
						if (this.aSheets[i].getAttribute("title") == sName) {
							this.aSheets[i].disabled = false;
						}
					}
					// now save the setting!
					top.strPreferredStyle = sName;
				},

	/**
	* Removes any style in the document using three steps.
	* 1. Find and remove all external style links
	* 2. Find and remove all style tags and content in between
	* 3. Look at each element and make sure it doesn't have a style attributes
	*/
	removeAllDocumentStyle:	function ()
				{
					// while they exist - remove them
					iLinkArr = this.objDocument.getElementsByTagName('link');
					for(i = 0; i < iLinkArr.length; i++)
					{
						iLinkArr[i].removeNode();
						if(iLinkArr[i].getAttribute('rel') == 'stylesheet')
						{
							//iLinkArr[i].setAttribute('href', '');
							iLinkArr[i].removeNode();
						}
					}
					// while they exist - remove them
					for(;this.objDocument.getElementsByTagName('style').length > 0;)
					{
						this.objDocument.getElementsByTagName('style')[0].removeNode(true);
					}
					//this.removeStyleAttributes(this.objDocument.body);
				},

	/**
	* Look at each element and make sure it doesn't have a style attributes
	* USING THE JAWSOME POWER OF XPATH
	*/
	removeStyleAttributes:	function (domElement)
				{
					try
					{
						var findPattern = "//@style";
						var resultLinks = this.objDocument.evaluate( findPattern, document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null );
						var i=0; while ( (res = resultLinks.snapshotItem(i) ) !=null ){ i++ }

						// look at element itself
						domElement.removeAttribute('style');
					}
					catch(err) //grrrr @ IE
					{
						try
						{
						}
						catch(err)
						{
						}
					}
				},

	/**
	* Simply checks if a renderer has been instantiated and calls render on it
	* Will use default renderer if not
	*/
	render:		function ()
			{
				if(this.aSheets.length == 0)
				{
					STYLE_PANEL.initialise();
				}

				objWindowManager = new WindowManager();
				objWindow = objWindowManager.openWindow('style_pane', 'Preferences');
				objWindow.bIsModal = true;
				objWindow.show();

				if(!objWindow.getItem('close_button_link'))
				{
					// create some text and add to the close button
					objCloseLink = this.objDocument.createElement('a');
					objCloseLink.href = '#';
					objCloseLink.appendChild( this.objDocument.createTextNode('X Close') );
					objWindow.addItem(objCloseLink, 'close_button_link', 'close_button');
				}
				if(!objWindow.getItem('initial_text_one'))
				{
					// create initial paragraph
					objInitialText = this.objDocument.createElement('p');
					objInitialText.appendChild(this.objDocument.createTextNode('In the preferences window you can adjust the way that the program looks.'));
					objWindow.addItem(objInitialText, 'initial_text_one', 'container');

					objInitialText = this.objDocument.createElement('p');
					objInitialText.appendChild(this.objDocument.createTextNode('There are a number of options available, select an option from the list below and the changes will be applied. When you close the window the changes will be saved.'));
					objWindow.addItem(objInitialText, 'initial_text_two', 'container');
				}
				if(!objWindow.getItem('style_list'))
				{
					// create a list of style types
					var domList = this.objDocument.createElement('ul');

					for ( var i = 0; i < this.aSheets.length; i++)
					{
						domListItem = this.objDocument.createElement('li');

						domLink = this.objDocument.createElement('a');
						domLink.href = '#';//javascript:'
						domLink.name = this.aSheets[i].getAttribute("title");
						domLink.onclick = function()
						{
							try
							{
								top.STYLE_PANEL.setActiveStyleSheetByName(this.name);
							}
							catch(e)
							{
								alert(e.description);
							}
						}
						domLink.appendChild(this.objDocument.createTextNode( this.aSheets[i].getAttribute("title") ));

						domListItem.appendChild(domLink);

						domList.appendChild(domListItem);
					}
					objWindow.addItem(domList, 'style_list', 'container');
				}
			}
};




