<!--
/* ---------------------------------------------
 * Created:		22.02.2007
 * Revision:		11.03.2007
 * Author:		Philippe Jos
 * Copyrights:		Philippe Jos
 * Email:		jos.philippe@wanadoo.fr
 * ---------------------------------------------
 */


/*
 *  Static Singleton XML Database Parser (Sommaire)
 * -----------------------------------------------------------------------------------------------------------------------------------------
 */

XmlDB = {
	VSeparator: null,
	CSeparator: null,
	MaxCSeparator: null,
	MinCSeparator: null,
	Header: null,
	View: null,
	ViewHeader: null,
	RangeH: 0,
	CategoryH: 0,
	MarginBottom:0,
	Query: null,

	Init: function( headerId, viewId, margin, rangeH, categoryH ){						// Initialize the static fields
		XmlDB.VSeparator = appendNewDiv( null, null, "vseparator" );					// Create & store a vertical separator
		XmlDB.MaxCSeparator = appendNewDiv( null, null, "cseparator max margin_b1" );			// Create & store the horizontal cseparator max
		XmlDB.MinCSeparator = appendNewDiv( null, null, "vspacer" );					// Create & store the vspacer	
		XmlDB.CSeparator = appendNewDiv( null, null, "cseparator margin_b1" );				// Create & store a center horizontal separator			
		XmlDB.CSeparator.appendChild( appendNewDiv( null, null, "hseparator left" ) );			// Create & append a left horizontal separator	
		XmlDB.CSeparator.appendChild( appendNewDiv( null, null, "hseparator right" ) );			// Create & append a right horizontal separator
		XmlDB.Header = document.getElementById( headerId );						// store a temporary pointer to the header node
		XmlDB.View = document.getElementById( viewId );							// Store a temporary pointer to the view container node
		XmlDB.ViewHeader = appendNewDiv( viewId, null, null );						// Append view header and store a pointer to it
		XmlDB.MarginBottom = margin;									// Store range and category css margin bottom
		XmlDB.RangeH = rangeH;										// Store range title image height
		XmlDB.CategoryH = categoryH;									// Store category title image height
	},


	Flush: function(){
		XmlDB.VSeparator = null;									// Flush from memory templates nodes
		XmlDB.CSeparator = null;
		XmlDB.MaxCSeparator = null;
		XmlDB.MinCSeparator = null;
		XmlDB.Header = null;
		XmlDB.View = null;
		XmlDB.ViewHeader = null;
		XmlDB.Query = null;
	
		XmlDB.RangeH = 0;										// Flush from memory no more needed static fields
		XmlDB.CategoryH = 0;	
		XmlDB.MarginBottom = 0;
	},


	SelectAllFromRange: function( brand, range  ){
		XmlDB.Query = new Object();									// Create the query object
		XmlDB.Query.brand = brand;									// Store the brand name in query object
		XmlDB.Query.range = range;									// Store the range name in query object

		var xmlDocURI = "xml/" + brand +"/" + brand + ".xml";

		if(browser.opera) XmlDB.ParseBrand( XML.Load( xmlDocURI ) );					// NHSJAX: Synchronously load the xml document
		else if( browser.explorer6 || !isHttpProtocol() )						// Is it ie6 or Are we under a "file:" protocol -> Use NHAJAX
			XML.AsyncLoad( xmlDocURI, XmlDB.ParseBrand );						// NHAJAX: Asynchronously load the xml document
		else{												// We are behind a web server "http:" protocol and it is not ie6 or opera -> Use AJAX							
			//if(browser.safari) XmlDB.ParseBrand( XML.HTTPLoad( xmlDocURI ) );			// SJAX : Synchronously load the xml document SJAX
			XML.HTTPAsyncLoad( xmlDocURI, XmlDB.ParseBrand );					// AJAX: Asynchronously load the xml document AJAX
		}
	},

	ParseBrand: function( xmlDoc ){
		var brand, len, node;
		brand = xmlDoc.documentElement;									// Get the xml doc root node : brand
		removeCommentText( brand );									// Clear undesired text and comment element from brand
		len = brand.childNodes.length;									// Get the numbers of ranges in brand
			
		XmlDB.Query.currency = brand.getAttribute("currency");						// Store the database currency in query object
			

		for( var i=0; i<len; i++ ){									// For each node in range
			node = brand.childNodes[i];								// Get the next node
			switch( node.nodeName ){
				case "title":
					XmlDB.Query.titlew = node.getAttribute("width");			// Store the Title width in query object
					XmlDB.Query.titleh = node.getAttribute("height");			// Store the Title height in query object
					Tool.AppendBrandTitle( XmlDB );						// Append the brand title
				break;
				case "logo":									// Is it a logo node ? Yes -> append it to container
					XmlDB.Query.logow = node.getAttribute("width");				// Store the Logo width in query object
					XmlDB.Query.logoh = node.getAttribute("height");			// Store the Logo height in query object

					Tool.SetViewHeaderHeight(XmlDB);					// Set view header height to default
					Tool.AppendBrandLogo( XmlDB );						// Append the brand logo to the view header
				break;
				case "range":									// Is it a range node ?
					if( node.getAttribute("name") == XmlDB.Query.range )XmlDB.ParseRange( node );	// Is it the product range we are querying ? Yes -> Parse it
				break;
				default: continue; break;							// Is it something else? Yes -> skip it
			}
		}
		Tool.AppendButtonHautPage( XmlDB.View );							// Append the "haut de page" button
		Tool.AppendMinCSeparator( XmlDB );								// Append a blank clear vspacer
	
		XmlDB.Flush();											// Flush from memory XmlDB static fields
	},


	ParseRange: function( range ){
		var i, len, node;;
		removeCommentText(range);									// Clear undesired text and comment element from range
		len = range.childNodes.length;

		XmlDB.Query.rimage = range.getAttribute("image");						// Store the range title image name in query object
		XmlDB.Query.rwidth = range.getAttribute("width");						// Store the range title image width in query object
		XmlDB.Query.rheight = range.getAttribute("height");						// Store the range title image height in query object
		Tool.AppendRangeTitle( XmlDB );									// Append the range image title
		
		for( i=0; i< len; i++ ){									// For each category in range
			category = range.childNodes[i];								// Get the next category
			removeCommentText(category);								// Clear undesired text and comment element from category

			if(i!=0) appendNewDiv( XmlDB.View, null, "vspacer margin_b3" );			// If it is not the first category append a blank clear vspacer min with margin_b3
			XmlDB.ParseCategory( category, i );
		}
	},



	ParseCategory: function( category, index ){
		var i, len, product, side, last;

		XmlDB.Query.folder = category.getAttribute("folder");	 					// Store the category folder name in query object				
		XmlDB.Query.category = category.getAttribute("name");						// Store the category name in query object
		XmlDB.Query.cimage = category.getAttribute("image");						// Store the category title image name in query object
		XmlDB.Query.cwidth = category.getAttribute("width");						// Store the category title image width in query object
		XmlDB.Query.cheight = category.getAttribute("height");						// Store the category title image height in query object
				
		if(XmlDB.Query.cimage!="")Tool.AppendCategoryTitle( XmlDB, index );				// Append the category image title

		len = category.childNodes.length;								// Compute the number of nodes in category
		for( i=0; i< len; i++ ){									// For each product in category
			product = category.childNodes[i];							// Get the next product
			removeCommentText(product);								// Clear undesired text and comment element from product

			if(i == len-1) last = true;								// Is it the last product of category? Yes.
			else last = false;									// No -> set last to false

			if( i % 2 ) side = "right";								// If product index is odd set side to right
			else side = "left";									// If product index is even set side to left
	
			XmlDB.ParseProduct( product, side, last );						// Parse product and append product view and separators
		}

	},

	ParseProduct: function( product, side, last ){
		var i, len, node, model, alt, price, image, doc, file, sticker, query;
		len = product.childNodes.length;

		
		for( i=0; i< len; i++ ){									// For each node in product
			node = product.childNodes[i];
			switch( node.nodeName ){
				case "model":									// Get the model name
					model = node.firstChild.nodeValue;					// Set the image alt and title string	
					alt = ( (XmlDB.Query.category != "")? XmlDB.Query.category : XmlDB.Query.range ) + " - " + model;
				break;
				case "price":
					if( node.firstChild )price = node.firstChild.nodeValue;			// If there is a price text node
					else price = "";
				break;
				case "picture": image = node.firstChild.nodeValue; break;				// Do not concat the uri now, to be able to pass the image name in query string
				case "doc": doc = node.firstChild.nodeValue; break;				// Get the documentation file name
				case "file": file = node.firstChild.nodeValue; break;				// Get the xml file name
				case "new": sticker = createImageExt( "image/title/new.gif", 103, 18, null, "sticker", "Nouveauté" ); break;
				case "promotion": sticker = createImageExt( "image/title/promo.gif", 103, 18, null, "sticker", "Promotion" ); break;
				case "oldmodel": sticker = createImageExt( "image/title/oldmodel_sommaire.gif", 110, 18, null, "sticker", "Ancien modèle" ); break;
				default: continue; break;
			}
		}

		XmlDB.Query.model= model;									// Add model name to the query object
		XmlDB.Query.price = price;									// Add price string to the query object
		XmlDB.Query.image = image;									// Add image file name to the query object
		XmlDB.Query.doc = doc;										// Add documentation file name to the query object
		
		query ="fiche.html?file=" + file;								// Concat the query string
		for( var prop in XmlDB.Query ) query +=  "&" + prop + "=" + XmlDB.Query[prop];
		query = encodeURI( query );

 		if(!sticker) sticker = createImageExt( "image/spacer.gif", 103, 18, null, "sticker", null );	// If there is no "promo" or "new" banner? Create a dummy sticker spacer image									
		XmlDB.NewProductView( XmlDB.View, side, image, alt, model, price, query, doc, sticker );	// Create and Append product view to container

		if(side == "left") Tool.AppendVSeparator( XmlDB );						// Append a vertical separator, if needed
		if(last == true) Tool.AppendMaxCSeparator( XmlDB );						// Append an horizontal cseparator max, if needed
		else if(side == "right") Tool.AppendCSeparator( XmlDB );					// Append an horizontal cseparator, if needed
	},


	NewProductView: function( container, side, image, alt, model, price, query, doc, sticker ){
		doc = "pdf/" + XmlDB.Query.brand + "/" + XmlDB.Query.folder + "/" + doc;					// Concat the complete documentation uri 
		image = "image/" + XmlDB.Query.brand + "/" + XmlDB.Query.folder + "/thumb_" + image;			// Concat the complete image uri 
		if(price!="") price = price + " " + XmlDB.Query.currency;						// Append the currency symbol to the price


		var view = appendNewDiv( container, null, "column " + side );					// Create a product view and append it to the specified container
		view.appendChild( createImageExt( image, null, null, null, "thumb", alt ) );			// Create & Append the image thumb to the product view

		var description = appendNewDiv( view, null, "description" );					// Create a product description block and append it to the product view
		appendNewTextBlock( description, null, "p2", model );						// Append model caption paragraph
		if(price)appendNewTextBlock( description, null, "price", price );				// Append price paragraph
		view.appendChild(sticker);									// Append the sticker image or spacer
		view.appendChild( Button.Create( "ensavoirplus", 103, 18, query, "btn left" ) );		// Append button "en savoir plus" (fiche)
		view.appendChild( Button.Create( "prospectus", 103, 18, doc, "btn left" ) );			// Append button documentation (prospectus)
	
	}
};

// -->
