// A JavaScript object for fixing some broken CSS support (mostly in Internet Explorer)

fixer={
	
	olIds: [ 'statementOfFaith', 'god' ],
	olHeadingType: [ 'h3', 'h4'],
	olStyles: [ 'upper-roman', 'upper-alpha' ],
	olUpperRoman: [ 'I. ', 'II. ', 'III. ', 'IV. ', 'V. ', 'VI. ', 'VII. ', 'VIII. ', 'IX. ', 'X. ', 'XI. ', 'XII. ', 'XIII. ', 'XIV. ', 'XV. ', 'XVI. ', 'XVII. ', 'XVIII. ' ],
	olUpperAlpha: [ 'A. ', 'B. ', 'C. ', 'D. ', 'E. ' ],
	

	init:function() {
		
		fixer.fixQuotes();
		fixer.fixCites();
		fixer.fixOrderedLists();
		
	},
	
	/**
	* This method fixes MSIE browsers that do not support the :before and :after
	* pseudo-elements in CSS. It adds quotation marks before and after all <q>
	* elements.
	*/
	
	fixQuotes:function() {
		
		if( document.getElementsByTagName('q').length == 0 ) return;
		
		var quotes = document.getElementsByTagName( 'q' );
		
		for( var i=0; i<quotes.length; i++ ) {
			
			if( !quotes[i].currentStyle ) return;
			
			var text = quotes[i].lastChild.nodeValue;
			
			quotes[i].lastChild.nodeValue = '"' + text + '"';
			
		}
		
	},
	
	/**
	* This method fixes MSIE browsers that do not support the :before and :after
	* pseudo-elements in CSS. It adds a dash at the beginning of all <cite>
	* elements.
	*/
	
	fixCites:function() {
		
		if( document.getElementsByTagName('cite').length == 0 ) return;
		
		var cites = document.getElementsByTagName( 'cite' );
		
		for( var i=0; i<cites.length; i++ ) {
			
//			alert( 'looping' );
			
			if( !cites[i].currentStyle ) return;
			
			var text = cites[i].lastChild.nodeValue;
			
			cites[i].lastChild.nodeValue = '- ' + text;
			
		}
		
	},
	
	/**
	* This method fixes all browsers that do not support styling of list item
	* bullet types (as of December 2007, it is believed that this refers to all
	* browsers). It also fixes improper implementation of 
	* 'list-style-position: inside' in certain browsers. It takes specific lists
	* (as specified by the olIds property of this object) and applies the
	* necessary styles with the help of two parallel arrays and an array of
	* bullets (chosen by a switch statement).
	*/
	
	fixOrderedLists:function() {
		
		if( document.getElementsByTagName( 'ol' ).length == 0 ) return;
		
		var lists = new Array();
		
		for( var i=0; i<fixer.olIds.length; i++ ) {

			if( !document.getElementById( fixer.olIds[i] ) ) continue;
			
			lists[ lists.length ] = document.getElementById( fixer.olIds[i] );
			
		}
		
		if( lists.length == 0 ) return;
		
		for( var i=0; i<lists.length; i++ ) {

			var listItems = new Array();

			// The splice() method does not seem to work when using an array reference
			// for an array called by the getElementsById() method.
			for( var j = 0; j<lists[i].getElementsByTagName('li').length; j++ ) {
				
				listItems[listItems.length] = lists[i].getElementsByTagName('li')[j];
					
				
			}
			
			for( var j=0; j<listItems.length; j++ ) {
								
				if( listItems[j].parentNode.getAttribute( 'id' ) != fixer.olIds[i] ) {
					
					listItems.splice(j, 1);
					j--;
					
				}
				
			}
			
			for( var j=0; j<listItems.length; j++ ) {
				
				listItems[j].style.listStylePosition = 'outside';
				listItems[j].style.listStyleType = 'none';
				listItems[j].style.paddingLeft = '0';
				
				if( listItems[j].getElementsByTagName( fixer.olHeadingType[i] ) == 0 )  {
					
					continue;
					
				} else {
					
//					alert( j + ': ' + fixer.olHeadingType[i] );
				
					var heading = listItems[j].getElementsByTagName( fixer.olHeadingType[i] )[0];
					
					switch( fixer.olStyles[i] ) {
						
						case 'upper-roman' :
						heading.innerHTML = fixer.olUpperRoman[j] + heading.innerHTML;
						break;
						
						case 'upper-alpha' :
						heading.innerHTML = fixer.olUpperAlpha[j] + heading.innerHTML;
						
					}
					
				}
				
			}
			
		}
		
	},
	
	/**
	* Simple utility method for allowing the use of multiple window.onload functions.
	* Written by Simon Willison
	*/
	
	addLoadEvent:function( func ) {
		
		var oldonload = window.onload;
		
		if( typeof window.onload != 'function' ) {
			
			window.onload = func;
			
		} else {
			
			window.onload = function() {
				
				oldonload();
				func();
				
			}
			
		}
		
	}
	
}

fixer.addLoadEvent( fixer.init );
