// --------------------------------------------------------------------

function displayDom () 
{
	i = 0 ;
	level = 1 ;
	padding = '                                                                                                ' ;
	var text ;

	var rootNode = document.documentElement ;
	followTree ( rootNode ) ;

}

function followTree ( node )
{
	i++ ;
	var nodeType       = node.nodeType ;
	var nodeName       = node.nodeName ;
	var nodeAttributes = node.attributes ;
	var nodesList      = node.childNodes;
	var nodeValue      = node.nodeValue ;

	if ( i > 99 ) { debug ( "--- max loop reached ---" ) ; return ; }

	level++ ;
	if ( nodeType == 1 ) 
	{
		//var txtAttributes = ' , atttributes :' ;
		//if ( nodeAttributes.length ) 
		//	for ( x=0 ; ( a = node.attributes[x] ) ; x++ ) txtAttributes += '[' + a.name + "=" + a.value + ']; ' ;
		//else txtAttributes = '' ;
		//debug ( "type " + nodeType + " , " + nodeName + txtAttributes ) ;
		debug ( "type " + nodeType + " , " + nodeName  ) ;
		// follow attributes
		if ( nodeAttributes.length ) for ( var j = 0 ; ( a = nodeAttributes[j] ) ; j++ )  followTree ( a ) ;
		// follow child Nodes
		if ( nodesList )             for ( var k = 0 ; ( n = nodesList[k] ) ; k++ )       followTree ( n ) ;
	}
	else if ( nodeType == 2 )
	{
		debug ( ". attribut [" + nodeName + " = " + nodeValue + "]" ) ;
	}
	else if ( nodeType == 3 )
	{
		var regexp1 = /(\r\n|\r|\n)/g ;
		value = nodeValue.replace ( regexp1 , '' ) ;

		debug ( "type 3 ,  " + nodeName + " , [" + value + "]" ) ;
	}

	level-- ;

}


// --------------------------------------------------------------------
function debug ( text )
{
	var box = document.getElementById("domStructureBox") ;
	box.value = box.value + ".[" + level + "]" + padding.slice(0,level) + padding.slice(0,level) + padding.slice(0,level) + ' ' + text + "\n"
}
function addEvent ( node , e , f )
{
	if ( node == null) return ;
	//alert ( 'event: ' + e ) ;
	eie = 'on' + e.substr(0,1).toUpperCase() + e.substr(1,e.length).toLowerCase() ;

	if (window.addEventListener) // firefox, opera, ... etc
		node.addEventListener( e , f , false );
	else // ie
		node.attachEvent( eie , f  );

}

function hideBox ( boxId )
{
	var box = document.getElementById(boxId) ;
	if ( box ) box.style.display = 'none' ;
}

function displayBox ( boxId )
{
	var box = document.getElementById(boxId) ;
	if ( box ) box.style.display = 'block' ;
}

// --------------------------------------------------------------------
function readJSONattributesFromBox ( tag )
{
	var box           = 'attributes:' + tag ;
	var attributesBox = document.getElementById(box);
	var attributes    = attributesBox.getElementsByTagName('input') ;
	var reg           = new RegExp( '^' + tag + ':' , 'g' ) ;
	var control       = new Array () ;

	var JSON = '{ ' ;
	var SEP = '' ;
	for ( var key in attributes )
	{
		if ( ! reg.test(key) ) continue ;
		if ( control[key] ) continue ;
		control[key] = 1 ;
		if ( attributes[key].value )
		{
			JSON += SEP + '"' + key.replace( reg , '' ) + '" : "' + attributes[key].value + '" ' ;
			SEP = ' , ' ;
		}
	}
	var attributes    = attributesBox.getElementsByTagName('select') ;
	for ( var key in attributes )
	{
		if ( ! reg.test(key) ) continue ;
		if ( control[key] ) continue ;
		control[key] = 1 ;
		if ( attributes[key].value )
		{
			JSON += SEP + '"' + key.replace( reg , '' ) + '" : "' + attributes[key].value + '" ' ;
			SEP = ' , ' ;
		}
	}
	JSON += ' }' ; 
	JSON = '{ "tag" : "' + tag + '" , "attributes" : ' + JSON + ' } ' ;
	return JSON ;
}

// --------------------------------------------------------------------
function addElementInSelectBox ( combo , json ) 
{
	id     = combo.length ;
	data   = eval ( '(' + json + ')' ) ;
	tag = data.tag ;
	option = document.createElement('option') ;
	txt    = document.createTextNode(json) ;
	option.setAttribute( 'id' , id + tag ) ;
	option.appendChild(txt) ;
	combo.appendChild(option) ;
	combo.size = combo.length ;

}

// --------------------------------------------------------------------
function loadChildWindow ( )
{
	window.open ( location.href , "generated" , "channelmode=no" , false ) ;
}

// --------------------------------------------------------------------
function generateHtmlCodeFromCombo ( combo )
{
	document.write ( "<h1>je suis la fille!</h1>" ) ;
	tagsList = combo.childNodes ;
	for ( var i=0 ; ( jsonTag = tagsList[i] ) ; i++ ) createTag ( jsonTag.value ) ;
}

function createTag ( jsonTagValue )
{
	//document.write ( '<li>' + jsonTagValue + '</li>' ) ;
	element = eval ( '(' + jsonTagValue + ')' ) ;
	node = document.createElement ( element.tag ) ;
	if ( element.attributes ) for ( var a in element.attributes )
	{
		//document.write ( '<li> --- ' + a + ' = ' + element.attributes[a] + '</li>' ) ;
		node.setAttribute ( a , element.attributes[a] ) ;
	}

	document.body.appendChild( node ) ;

}


// --------------------------------------------------------------------
function initDocument () 
{
	// ajout d'un listener "onChange" sur le menu deroulant
	// pour afficher le div avec les attributs
	/*
	var combo = document.getElementById('tagCombo');
	addEvent ( combo , 'change' , function(){ 
			var visibleBox = document.getElementById('visibleBox') ;
			hideBox( 'attributes:' + visibleBox.value ) ;
			displayBox( 'attributes:' + combo.value ) ;
			document.getElementById('visibleBox').value = combo.value ;
			}
		 ) ;
	// ajout d'un listener "onDblClick" sur le menu deroulant
	// pour ajouter automatiquement la balise dans la liste
	addEvent ( combo , 'dblClick' , function(){ 
			var tag = combo.value ;
			var json = readJSONattributesFromBox(tag) ;
			addElementInSelectBox ( document.getElementById ('tagsCombo') , json ) ;
			}
		 ) ;

	// ajout d'un listener "onClick" sur le bouton createNewNodedButton
	// pour ajouter la balise avec ses attributs dans la liste
	var createNewNodedButton = document.getElementById('createNewNodedButton') ;
	addEvent ( createNewNodedButton , 'click' , function(){
			var tag = combo.value ;
			var json = readJSONattributesFromBox(tag) ;
			addElementInSelectBox ( document.getElementById ('tagsCombo') , json ) ;
			}
		 ) ;

	// ajout d'un listener "onClick" sur le bouton generateCodeButton
	// pour ajouter la balise avec ses attributs dans la liste
	var generateCodeButton = document.getElementById('generateCodeButton') ;
	addEvent ( generateCodeButton , 'click' , function(){
			loadChildWindow( ) ;
			}
		 ) ;
	*/


	var flipButton1 = document.getElementById('uk_button01') ;
	if ( flipButton1 ){
		addEvent ( flipButton1 , 'mouseover' , function(){
			flipButton1.src = "images/uk_button_over.png" ;
			}
		) ;
	addEvent ( flipButton1 , 'mouseout' , function(){
			flipButton1.src = "images/uk_button.png" ;
			}
		) ;
	}

	var flipButton2 = document.getElementById('uk_button02') ;
	if ( flipButton2 ) {
		addEvent ( flipButton2 , 'mouseover' , function(){
			flipButton2.src = "images/uk_button_over.png" ;
			}
		) ;
		addEvent ( flipButton2 , 'mouseout' , function(){
			flipButton2.src = "images/uk_button.png" ;
			}
		) ;
	}

	var flipButton3 = document.getElementById('fr_button01') ;
	if( flipButton3) {
		addEvent ( flipButton3 , 'mouseover' , function(){
			flipButton3.src = "images/fr_button_over.png" ;
			}
		) ;
		addEvent ( flipButton3 , 'mouseout' , function(){
			flipButton3.src = "images/fr_button.png" ;
			}
		) ;
	}

	var homepageButton = document.getElementById('box_accueil') ;
	if ( homepageButton ) {
		addEvent ( homepageButton , 'mouseover' , function(){
			this.style.cursor='hand' ;
			}
		) ;

	}

}


if (window.addEventListener) // firefox, opera, ... etc
	window.addEventListener('load', initDocument , false );
else // ie
	window.attachEvent('onload', initDocument );



