if (!window.HN)
	HN = window.HN = {};
HN.MSN = {};
HN.MSN.PostSales = {};
// Constants
HN.MSN.PostSales.HOME_BUBBLE_TOP_INDEX = 0;
HN.MSN.PostSales.HOME_BUBBLE_LEFT_INDEX = 1;
HN.MSN.PostSales.HOME_BUBBLE_RIGHT_INDEX = 2;
HN.MSN.PostSales.base_url = "/";
HN.MSN.PostSales.base_folder = "astuces/";
HN.MSN.PostSales.INDEX_NAME = "";

// Global Vars
HN.MSN.PostSales.xml = {
	products: { file: HN.MSN.PostSales.base_url + HN.MSN.PostSales.base_folder + "products.xml", data: null },
	folders: { file: HN.MSN.PostSales.base_url + HN.MSN.PostSales.base_folder + "folders.xml", data: null },
	blocks_home: { file: HN.MSN.PostSales.base_url + HN.MSN.PostSales.base_folder + "blocks_home.xml", data: null },
	blocks_products: { file: HN.MSN.PostSales.base_url + HN.MSN.PostSales.base_folder + "blocks_products.xml", data: null },
	blocks_categories: { file: HN.MSN.PostSales.base_url + HN.MSN.PostSales.base_folder + "blocks_categories.xml", data: null },
	blocks_folders: { file: HN.MSN.PostSales.base_url + HN.MSN.PostSales.base_folder + "blocks_folders.xml", data: null },
	blocks_folder: { file: HN.MSN.PostSales.base_url + HN.MSN.PostSales.base_folder + "blocks_folder.xml", data: null }
}
HN.MSN.PostSales.MainDomains = [{
		folder: "astuces", // folder name on server
		title: "Astuces", // title of this part of the arborescence
		xml: "products", // the xml file associated which describe the subsequent folders
		visible: false, // wether this domain must me visible/writted or not in the arborescence
		subDomains : [{ // sub domains
			folder: "dossiers",
			title: "Les dossiers",
			xml: "folders",
			visible: true
		}]
}];

// UseFull Global Functions

// Singleton per URL
HN.MSN.PostSales.URLinfos_instances = [];
HN.MSN.PostSales.URLinfos = function () {
	var url;
	if (arguments.length == 0)
		url = window.location.href;
	else
		url = arguments[0];
	
	if (!HN.MSN.PostSales.URLinfos_instances[url]) {
		HN.MSN.PostSales.URLinfos_instances[url] = new function () {
			this.absURL = url;
			this.protocol = this.absURL.substring(0, this.absURL.indexOf("://"));
			var noprotocolURL = this.absURL.substring(this.protocol.length + "://".length, this.absURL.length);
			this.siteURL = noprotocolURL.substring(0, noprotocolURL.indexOf("/"));
			this.baseURL = this.protocol + "://" + this.siteURL + HN.MSN.PostSales.base_url;
			this.baseFolder = this.baseURL + HN.MSN.PostSales.base_folder;
			this.relURL = this.absURL.substring(this.baseURL.length, this.absURL.length);
			
			this.tree = [];
			var treeURLparts = this.relURL.split("/");
			var relURL = "";
			for (var i = 0; i < treeURLparts.length; i++) {
				relURL += treeURLparts[i] + (i != treeURLparts.length-1 ? "/" : "");
				this.tree[i] = {
					text: treeURLparts[i],
					relURL: relURL,
					absURL: this.baseURL + relURL
				};
			}
			
			var lastURLpart = this.tree[this.tree.length-1].text.split("#");
			
			this.fileName = lastURLpart[0];
			this.fileNameNoExt = this.fileName.substr(0, this.fileName.lastIndexOf("."));
			this.fileNameExt = this.fileName.substr(this.fileName.lastIndexOf(".")+1, this.fileName.length);
			this.isIndex = this.fileName.toLowerCase() == HN.MSN.PostSales.INDEX_NAME.toLowerCase();
			
			this.anchor = lastURLpart.length > 1 ? lastURLpart[1] : "";
		}
	}
	return HN.MSN.PostSales.URLinfos_instances[url];
};

HN.MSN.PostSales.ArborescenceBuilder = function (_elem) {
	var _this = this;
	var urlInfos = new HN.MSN.PostSales.URLinfos();
	
	this.elem = typeof _elem == "string" ? document.getElementById(_elem) : _elem;
	this.folderTree = [];
	this.mainDomain = null;
	
	this.getXMLData = function () {
		HN.MSN.PostSales.GetXMLData(this.mainDomain.xml, function (xml) {
				if (typeof xml != "string") {
					// Once here, we already have Accueil > Astuces (> Les dossiers) written in the arborescence element
					switch (_this.mainDomain.xml) {
						case "products" :
							var curFolder = xml;
							for (var i = 1; i < urlInfos.tree.length-1; i++) {
								
								var a = document.createElement("a");
								curPath = curPath + urlInfos.tree[i].text + "/";
								a.href = curPath + HN.MSN.PostSales.INDEX_NAME;
								switch (i) {
									case 1 :
										curFolder = $(curFolder).find("product[folder=\"" + urlInfos.tree[1].text + "\"]");
										a.innerHTML = $(curFolder).attr("title");
										break;
									case 2 :
										curFolder = $(curFolder).find("category[folder=\"" + urlInfos.tree[2].text + "\"]");
										a.innerHTML = $(curFolder).attr("title");
										break;
									default : break;
								}
								
								_this.elem.appendChild(document.createTextNode(" > "));
								_this.elem.appendChild(a);
							}
							if (!urlInfos.isIndex) {
								var a = document.createElement("a");
								a.href = curPath + urlInfos.fileName;
								a.innerHTML = $(curFolder).find("tip[url=\"" + urlInfos.fileName + "\"]").attr("title").replace(/\\n/g, "");
								_this.elem.appendChild(document.createTextNode(" > "));
								_this.elem.appendChild(a);
							}
							break;
							
						case "folders" :
							var curFolder = xml;
							if (!urlInfos.isIndex) {
								var a = document.createElement("a");
								a.href = curPath + urlInfos.fileName;
								a.innerHTML = $.trim($($(curFolder).find("folder[url=\"" + urlInfos.fileName + "\"] title").get(0)).text());
								_this.elem.appendChild(document.createTextNode(" > "));
								_this.elem.appendChild(a);
							}
							break;
					}
				}
		});
	};
	
	// Constructor
	var curPath = urlInfos.baseURL;
	
	// Accueil
	var home = document.createElement("a");
	home.href = urlInfos.baseFolder + HN.MSN.PostSales.INDEX_NAME;
	home.innerHTML = "Accueil - Astuces";
	this.elem.appendChild(home);
	
	// > Astuces
	/*var tip = document.createElement("a");
	tip.href = curPath + HN.MSN.PostSales.INDEX_NAME;
	tip.innerHTML = "Astuces";
	this.elem.appendChild(document.createTextNode(" > "));
	this.elem.appendChild(tip);*/
	
	if (urlInfos.tree.length > 1) { // We're not on the home directory
		for (var i = 0; i < HN.MSN.PostSales.MainDomains.length; i++) {
			if (urlInfos.tree[0].text == HN.MSN.PostSales.MainDomains[i].folder) {
				this.mainDomain = HN.MSN.PostSales.MainDomains[i];
				curPath += this.mainDomain.folder + "/";
				
				if (this.mainDomain.visible) {
					var a = document.createElement("a");
					a.href = curPath + HN.MSN.PostSales.INDEX_NAME;
					a.innerHTML = this.mainDomain.title;
					this.elem.appendChild(document.createTextNode(" > "));
					this.elem.appendChild(a);
				}
				
				if (urlInfos.tree.length > 2 || !urlInfos.isIndex) { // We're not on an index of a main domain
					for (var j = 0; j < this.mainDomain.subDomains.length; j++) {
						if (urlInfos.tree[1].text == this.mainDomain.subDomains[j].folder) {
							this.mainDomain = this.mainDomain.subDomains[j];
							curPath += this.mainDomain.folder + "/";
							if (this.mainDomain.visible) {
								var a = document.createElement("a");
								a.href = curPath + HN.MSN.PostSales.INDEX_NAME;
								a.innerHTML = this.mainDomain.title;
								this.elem.appendChild(document.createTextNode(" > "));
								this.elem.appendChild(a);
							}
							break;
						}
					}
					this.getXMLData();
				}
				
				break;
			}
		}
	}
};

HN.MSN.PostSales.GetXMLData = function (xml_file, callback_function) {
	if (!HN.MSN.PostSales.xml[xml_file]) {
		callback_function("XML doesn't exists");
		return false;
	}
	
	if (HN.MSN.PostSales.xml[xml_file].data) {
		callback_function(HN.MSN.PostSales.xml[xml_file].data);
	}
	else {
		$.ajax({
			async: false,
			data: "",
			dataType: "xml",
			error: function (XMLHttpRequest, textStatus, errorThrown) {
				callback_function("Bad XML URL or XML is Malformed");
			},
			success: function (xml, textStatus) {
				HN.MSN.PostSales.xml[xml_file].data = xml;
				callback_function(HN.MSN.PostSales.xml[xml_file].data);
			},
			type: "GET",
			url: HN.MSN.PostSales.xml[xml_file].file
		});
	}
	return true;
};

// Objects
HN.MSN.PostSales.HomeTips = function (_bt, _bl) {
	var _this = this;
	
	this.bt = typeof _bt == "string" ? [document.getElementById(_bt)] : _bt;
	this.bl = typeof _bl == "string" ? [document.getElementById(_bl)] : _bl;
	this.tips = [];
	
	this.getXMLData = function () {
		HN.MSN.PostSales.GetXMLData("products", function (xml) {
				if (typeof xml == "string" && _this.bt.length > 0) _this.bt[0].innerHTML = xml;
				else {
					var xml_tips = $(xml).find("products > home_tips");
					//_this.bt.innerHTML = $(xml_tips).attr("description").replace(/\\n/g, "<br/>");
					$(xml_tips).find("home_tip").each(function () {
						_this.tips.push({
							title: $(this).attr("title"),
							position: $(this).attr("position"),
							image: $(this).attr("image"),
							url: $(this).attr("url")
						});
					});
					_this.build_DOM();
				}
			}
		);
	};
	
	this.build_DOM = function () {
		for (var tipIndex = 0; tipIndex < this.tips.length; tipIndex++) {
			var bubble_index;
			switch (this.tips[tipIndex].position) {
				case "top" : bubble_index = HN.MSN.PostSales.HOME_BUBBLE_TOP_INDEX; break;
				case "left" : bubble_index = HN.MSN.PostSales.HOME_BUBBLE_LEFT_INDEX; break;
				case "right" : bubble_index = HN.MSN.PostSales.HOME_BUBBLE_RIGHT_INDEX; break;
				default : bubble_index = HN.MSN.PostSales.HOME_BUBBLE_TOP_INDEX; break;
			}
			if (this.bt.length > bubble_index) {
				this.bt[bubble_index].innerHTML = this.tips[tipIndex].title.replace(/\\n/g, "<br/>");
				this.bl[bubble_index].href = this.tips[tipIndex].url;
			}
		};
	};
	
	this.clean_DOM = function () { $(this.list).empty(); };
	
	this.getXMLData();
};

HN.MSN.PostSales.BottomTipList = function (_block) {
	var _this = this;
	var urlInfos = new HN.MSN.PostSales.URLinfos();
	var limited_pdt_tips = [30,10,10];
	var base_tip_url = urlInfos.baseFolder;
	
	this.block = typeof _block == "string" ? document.getElementById(_block) : _block;
	this.xml = null;
	this.table2 = null;
	
	
	this.buildPdtBlock = function (xml_pdt_block, max_tips) {
		var pdt_folder = $(xml_pdt_block).attr("folder");
		var pre_url = base_tip_url + pdt_folder + "/";
		
		var div = document.createElement("div");
		var h3 = document.createElement("h3");
		div.appendChild(h3);
		h3.innerHTML = $(xml_pdt_block).attr("title");
		var ul = document.createElement("ul");
		div.appendChild(ul);
		
		var tip_num = 0;
		var categories = $(xml_pdt_block).find("categories > category");
		if (categories.length > 0) {
			categories.each(function () {
				var category_folder = $(this).attr("folder");
				$(this).find("tips tip").each(function () {
					if (!max_tips || tip_num < max_tips) {
						ul.appendChild(_this.buildTip(this, pre_url + category_folder + "/"));
						tip_num++;
					}
				});
				
			});
		}
		else {
			$(xml_pdt_block).find("tips tip").each(function () {
				if (!max_tips || tip_num < max_tips) {
					ul.appendChild(_this.buildTip(this, pre_url));
					tip_num++;
				}
			});
		}
		
		return div;
	};
	
	this.buildTip = function (xml_tip, pre_url) {
		var li = document.createElement("li");
		var a = document.createElement("a");
		a.innerHTML = $(xml_tip).attr("title").replace(/\\n/g, "<br/>");
		a.href = pre_url + $(xml_tip).attr("url");
		li.appendChild(a)
		var short_desc = $(xml_tip).attr("short_desc");
		if (short_desc && short_desc != "") {
			var span = document.createElement("span");
			span.className = "short-desc";
			span.innerHTML = "&rarr; " + short_desc.replace(/\\n/g, "<br/>");
			li.appendChild(span);
		}
		
		return li;
	};
	
	this.buildClearDIV = function () {
		var div = document.createElement("div");
		div.className = "zero";
		return div;
	};
	
	this.build_DOM = function () {
		HN.MSN.PostSales.GetXMLData("products", function (xml) {
			if (typeof xml == "string") _this.block.innerHTML = xml;
			else {
				_this.xml = xml;
				var table = document.createElement("table");
				_this.block.appendChild(table);
				table.cellpadding = 0;
				table.cellspacing = 0;
				table.border = 0;
				var tbody = document.createElement("tbody");
				table.appendChild(tbody);
				var tr = document.createElement("tr");
				tbody.appendChild(tr);
				var td_left = document.createElement("td");
				tr.appendChild(td_left);
				td_left.className = "left";
				var td_right = document.createElement("td");
				tr.appendChild(td_right);
				td_right.className = "right";
				
				var pdt_num = 0;
				$(xml).find("product").each(function () {
					if (pdt_num < limited_pdt_tips.length) {
						switch ($(this).attr("bottom-pos")) {
							case "left" : td_left.appendChild(_this.buildPdtBlock(this, limited_pdt_tips[pdt_num])); break;
							case "right" : td_right.appendChild(_this.buildPdtBlock(this, limited_pdt_tips[pdt_num])); break;
							default: break;
						}
						pdt_num++;
					}
				});
				_this.block.appendChild(_this.buildClearDIV());
				
				var div_button = document.createElement("div");
				_this.block.appendChild(div_button);
				div_button.className = "button-all-tips";
				var div_bg = document.createElement("div");
				div_bg.className = "bg show";
				div_button.appendChild(div_bg);
				var a = document.createElement("a");
				div_button.appendChild(a);
				a.bg = div_bg;
				a.onclick = function () {
					if ($(this.bg).hasClass("show")) {
						$(_this.table2).show();
						this.bg.className = "bg hide";
					}
					else {
						$(_this.table2).hide();
						this.bg.className = "bg show";
					}
				};
				
				_this.table2 = document.createElement("table");
				_this.block.appendChild(_this.table2);
				_this.table2.style.display = "none";
				_this.table2.cellpadding = 0;
				_this.table2.cellspacing = 0;
				_this.table2.border = 0;
				var tbody2 = document.createElement("tbody");
				_this.table2.appendChild(tbody2);
				var tr2 = document.createElement("tr");
				tbody2.appendChild(tr2);
				var td_left2 = document.createElement("td");
				tr2.appendChild(td_left2);
				td_left2.className = "left";
				var td_right2 = document.createElement("td");
				tr2.appendChild(td_right2);
				td_right2.className = "right";
				
				$(xml).find("product").each(function () {
					switch ($(this).attr("bottom-pos")) {
						case "left" : td_left2.appendChild(_this.buildPdtBlock(this, 0)); break;
						case "right" : td_right2.appendChild(_this.buildPdtBlock(this, 0)); break;
						default: break;
					}
				});
				_this.block.appendChild(_this.buildClearDIV());
				//var s="";for(var k in tips) s+=k+"="+tips[k];alert(s);
			}
		});
	};
	
	this.clean_DOM = function () { $(this.block).empty(); };
	
	this.clean_DOM();
	this.build_DOM();
};

HN.MSN.PostSales.GlobalBlocks = function (_blocksXML, _container, _shadow) {
	var _this = this;
	
	this.blocksXML = _blocksXML;
	this.container = typeof _container == "string" ? document.getElementById(_container) : _container;
	this.shadow = _shadow;
	this.blocks2show = null; // contain the XML blocks (jquery elements)
	this.blocks = []; // contain the DOM blocks (pure DOM elements)
	this.xml = null;
	
	this.getXMLData = function () {
		HN.MSN.PostSales.GetXMLData(this.blocksXML, function (xml) {
				if (typeof xml == "string" && _this.bodyText.length > 0) _this.bodyText[0].innerHTML = xml;
				else {
					_this.xml = xml;
					_this.clean_DOM();
					_this.build_DOM();
				}
			}
		);
	};
	
	this.buildBlock_DOM = function (xml_block) {
		var urlInfos = new HN.MSN.PostSales.URLinfos();
		var header_image = $(xml_block).find("header_image").get(0);
		var body_text = $(xml_block).find("body_text").get(0);
		var footer_link = $(xml_block).find("footer_link").get(0);
		var picto = $(xml_block).find("picto").get(0);
		
		var div = document.createElement("div");
		this.blocks.push(div);
		
		div.className = "block-tip";
		if (this.blocks.length == 1) div.className += " first";
		else if (this.blocks.length == this.blocks2show.length) div.className += " last";
		
		var url = $(footer_link).attr("url");
		var glink = $(footer_link).attr("glink");
		var rel = (glink && glink != "") ? " rel=\"" + glink + "\"" : "";
		var html = "" +
			(this.shadow ? "<div class=\"shadow\"></div>" : "") +
			"<div class=\"block\">" +
				"<div class=\"header\">" +
					"<img " +
						"src=\"" + urlInfos.baseFolder + $(header_image).attr("src") + "\" " +
						"alt=\"" + $(header_image).attr("alt") + "\" " +
						"title=\"" + $(header_image).attr("title") + "\" " +
						"class=\"\"/>" +
				"</div>" +
				"<div class=\"body\">" +
					"<div class=\"bg\"></div>" +
					"<div class=\"content\">" +
						"<a href=\"" + url + "\" class=\"text inline-block\"" + rel + "><div>" + $.trim($(body_text).text()) + "</div></a><div class=\"vspacer inline-block\"></div>" +
					"</div>" +
				"</div>" +
				"<div class=\"footer\">" +
					"<a href=\"" + url + "\" class=\"inline-block\"" + rel + "><div>" + $.trim($(footer_link).text()) + "</div></a><div class=\"vspacer inline-block\"></div>" +
				"</div>" +
				"<img " +
					"src=\"" + urlInfos.baseFolder + $(picto).attr("src") + "\" " +
					"alt=\"" + $(picto).attr("alt") + "\" " +
					"title=\"" + $(picto).attr("title") + "\" " +
					"class=\"picto\"/>" +
			"</div>";
		div.innerHTML = html;
		//$("*[href]", div).each(function () { if (this.rel && this.rel != "") this.onclick = function () { (new Image()).src = this.rel } });
		this.container.appendChild(div);
	};
	
	this.build_DOM = function () {
		if (this.xml) {
			this.blocks2show = $(this.xml).find("block");
			this.blocks2show.each(function () { _this.buildBlock_DOM(this); });
			//this.blocks2show = this.blocks2show.get();
		}
	};
	
	this.clean_DOM = function () { $(this.container).empty(); };
	
	this.getXMLData();
};

HN.MSN.PostSales.FlaggedTips = function (_container, _left_arrow, _right_arrow) {
	var _this = this;
	var urlInfos = new HN.MSN.PostSales.URLinfos();
	var tip_width = 168;
	var tips_h_offset = -1;
	var glink = "http://g.msn.fr/FR1003/1067";
	
	this.container = typeof _container == "string" ? document.getElementById(_container) : _container;
	this.left_arrow = typeof _left_arrow == "string" ? document.getElementById(_left_arrow) : _left_arrow;
	this.right_arrow = typeof _right_arrow == "string" ? document.getElementById(_right_arrow) : _right_arrow;
	this.tips = [];
	this.curTipIndex = 0;
	this.tipsPerPage = 3;
	
	this.getXMLData = function () {
		HN.MSN.PostSales.GetXMLData("products", function (xml) {
				if (typeof xml == "string") _this.container.innerHTML = xml;
				else {
					var xml_tips = $(xml).find("flagged_tips");
					//_this.bt.innerHTML = $(xml_tips).attr("description").replace(/\\n/g, "<br/>");
					$(xml_tips).find("flagged_tip").each(function () {
						_this.tips.push({
							title: $(this).attr("title"),
							image: $(this).attr("image"),
							url: $(this).attr("url")
						});
					});
					_this.shuffleTips();
					_this.build_DOM();
				}
			}
		);
	};

	// Fisher-Yates
	this.shuffleTips = function () {
		var i = this.tips.length;
		if (!i) return false;
		while (--i) {
			var j = Math.floor(Math.random() * (i+1));
			var tmpi = this.tips[i];
			this.tips[i] = this.tips[j];
			this.tips[j] = tmpi;
		}
	};
	
	this.build_DOM = function () {
		this.container.style.width = (this.tips.length * tip_width) + "px";
		
		for (var tipIndex = 0; tipIndex < this.tips.length; tipIndex++) {
			/*
			<div class="block">
				<img src="../images/changer-statut.jpg" alt=""/>
				<a href="" class="see"></a>
			</div>
			*/
			var div = document.createElement("div");
			div.className = "block";
			this.container.appendChild(div);
			
			var img = document.createElement("img");
			img.src = urlInfos.baseFolder + this.tips[tipIndex].image;
			img.alt = "";
			img.title = this.tips[tipIndex].title;
			div.appendChild(img);
			
			var a = document.createElement("a");
			a.className = "see";
			a.href = urlInfos.baseFolder + this.tips[tipIndex].url;
			a.rel = glink;
			div.appendChild(a);
			
			/* Disable Selection */
			this.left_arrow.onselectstart = function () { return false; };
			this.left_arrow.unselectable = "on";
			this.left_arrow.style.MozUserSelect = "none";
			this.left_arrow.cursor = "default";
			
			this.right_arrow.onselectstart = function () { return false; };
			this.right_arrow.unselectable = "on";
			this.right_arrow.style.MozUserSelect = "none";
			this.right_arrow.cursor = "default";
		};
		
		this.left_arrow.onmousedown = function () {
			if (_this.curTipIndex > 0)
				_this.gotoTip(--_this.curTipIndex);
		};
		this.right_arrow.onmousedown = function () {
			if (_this.curTipIndex < _this.tips.length-_this.tipsPerPage)
				_this.gotoTip(++_this.curTipIndex);
		};
		
	};
	
	this.gotoTip = function (tipIndex) {
		$(_this.container).animate({
			left : (tipIndex * (-tip_width) + tips_h_offset) + "px"
		}, 500);
	};
	
	this.clean_DOM = function () { $(this.list).empty(); };
	
	this.getXMLData();
};

HN.MSN.PostSales.CategoryList = function (_sb, _list) {
	var _this = this;
	var urlInfos = new HN.MSN.PostSales.URLinfos();

	this.sb = typeof _sb == "string" ? document.getElementById(_sb) : _sb;
	this.list = typeof _list == "string" ? document.getElementById(_list) : _list;
	this.categories = [];
	
	this.getXMLData = function () {
		HN.MSN.PostSales.GetXMLData("products", function (xml) {
				if (typeof xml == "string") _this.sb.innerHTML = xml;
				else {
					var xml_categories = $(xml).find("product[folder=\""+urlInfos.tree[1].text+"\"] categories");
					_this.sb.innerHTML = $(xml_categories).attr("description").replace(/\\n/g, "<br/>");
					$(xml_categories).find("category").each(function () {
						var category = {
							title: $(this).attr("title"),
							folder: $(this).attr("folder"),
							options: []
						};
						$(this).find("option").each(function () {
							category.options.push({
								title: $(this).attr("title"),
								url: $(this).attr("url")
							});
						});
						_this.categories.push(category);
					});
					_this.build_DOM();
				}
			}
		);
	};
	
	this.build_DOM = function () {
		var html = "";
		for (var catIndex = 0; catIndex < this.categories.length; catIndex++) {
			html += "" +
			"<div class=\"cat-title\">" +
				"<a href=\"" + urlInfos.tree[1].absURL + this.categories[catIndex].folder + "/" + HN.MSN.PostSales.INDEX_NAME + "\">" +
					this.categories[catIndex].title +
				"</a>" +
			"</div>" +
			"<div class=\"cat-links\">" +
				"<ul>";
			for (var optionIndex = 0; optionIndex < this.categories[catIndex].options.length; optionIndex++) {
					html += "" +
					"<li>" +
						"<a href=\"" + urlInfos.tree[1].absURL + this.categories[catIndex].folder + "/" + this.categories[catIndex].options[optionIndex].url + "\">" +
							this.categories[catIndex].options[optionIndex].title +
						"</a>" +
					"</li>";
			}
			html += "" +
				"</ul>" +
				"<div class=\"cat-all-tips\">" +
					"<a href=\"" + urlInfos.tree[1].absURL + this.categories[catIndex].folder + "/" + HN.MSN.PostSales.INDEX_NAME + "\">" +
						"Voir toutes les astuces <i>" + this.categories[catIndex].title + "</i>" +
					"</a>" +
				"</div>" +
			"</div>";
		}
		html += "<div class=\"zero\"></div>";
		this.list.innerHTML = html;
	};
	
	this.clean_DOM = function () { $(this.list).empty(); };
	
	this.getXMLData();
};

HN.MSN.PostSales.FolderList = function (_list, _ps) {
	var _this = this;
	var foldersPerPage = 3;
	var urlInfos = new HN.MSN.PostSales.URLinfos();
	
	this.list = typeof _list == "string" ? document.getElementById(_list) : _list;
	this.ps = typeof _ps == "string" ? document.getElementById(_ps) : _ps;
	this.folders = [];
	this.curPage = -1;
	this.maxPage = -1;
	
	this.getXMLData = function () {
		HN.MSN.PostSales.GetXMLData("folders", function (xml) {
				if (typeof xml == "string") _this.list.innerHTML = xml;
				else {
					var xml_folders = $(xml).find("folders");
					$(xml_folders).find("folder").each(function () {
						var url = $(this).attr("url");
						var glink = $(this).attr("glink");
						var title = $.trim($(this).find("title").text());
						var summary = $.trim($(this).find("summary").text());
						var thumbnail = $(this).find("thumbnail");
						
						_this.folders.push({
							title: title,
							url: url,
							glink: glink,
							summary: summary,
							thumbnail: {
								title: $(thumbnail).attr("title"),
								alt: $(thumbnail).attr("alt"),
								src: $(thumbnail).attr("src")
							}
						});
					});
					_this.maxPage = Math.floor((_this.folders.length-1) / foldersPerPage) + 1;
					var page = parseInt(urlInfos.anchor);
					if (isNaN(page)) page = 1;
					_this.gotoPage(page);
				}
			}
		);
	};
	
	this.gotoPage = function (page) {
		//alert("page="+page+" maxPage="+this.maxPage+" curPage="+this.curPage);
		if (page < 1) page = 1;
		else if (page > this.maxPage) page = this.maxPage;
		if (page > 0 && page != this.curPage) {
			this.curPage = page;
			this.cleanList_DOM();
			this.cleanPS_DOM();
			this.buildList_DOM((page-1)*foldersPerPage, foldersPerPage);
			this.buildPS_DOM();
		}
	};
	
	this.buildList_DOM = function (index_start, count) {
		var index_end = index_start + count;
		if (index_end > this.folders.length)
			index_end = this.folders.length;
		
		for (var i = index_start; i < index_end; i++) {
			var div = document.createElement("div");
			var rel = (this.folders[i].glink && this.folders[i].glink != "") ? " rel=\"" + this.folders[i].glink + "\"" : "";
			var html = "" +
				"<div class=\"folder-pic\">" +
					"<img " +
					"title=\"" + this.folders[i].thumbnail.title + "\" " +
					"src=\"" + this.folders[i].thumbnail.src + "\" " +
					"alt=\"" + this.folders[i].thumbnail.alt + "\"/>" +
				"</div>" +
				"<div class=\"folder-desc\">" +
					"<h3>&gt; " + this.folders[i].title + "</h3>" +
					this.folders[i].summary +
					"<a href=\"" + this.folders[i].url + "\"" + rel + " class=\"see\"></a>" +
				"</div>";
			div.innerHTML = html;
			this.list.appendChild(div);
		}
	};
	
	this.buildPS_DOM = function () {
		// <
		var a = document.createElement("a");
		a.appendChild(document.createTextNode("<"));
		this.ps.appendChild(a);
		this.ps.appendChild(document.createTextNode(" "))
		a.href = "#" + (this.curPage-1);
		if (this.curPage > 1) {
			a.onclick = function () { _this.gotoPage(_this.curPage-1); };
		}
		else
			a.style.visibility = "hidden";
		
		// 1 - 2 - 3 ..
		if (this.maxPage > 1) {
			for (var i = 1; i <= this.maxPage; i++) {
				var a = document.createElement("a");
				a.appendChild(document.createTextNode(i));
				a.pageIndex = i;
				a.href = "#" + i;
				if (i == this.curPage) {
					a.className = "current";
				}
				else {
					a.onclick = function () { _this.gotoPage(this.pageIndex); };
				}
				if (i > 1) this.ps.appendChild(document.createTextNode(" - "));
				this.ps.appendChild(a);
			}
		}
		
		// >
		this.ps.appendChild(document.createTextNode(" "))
		var a = document.createElement("a");
		a.appendChild(document.createTextNode(">"));
		this.ps.appendChild(a);
		a.href = "#" + (this.curPage+1);
		if (this.maxPage > 1 && this.curPage < this.maxPage) {
			a.onclick = function () { _this.gotoPage(_this.curPage+1); };
		}
		else
			a.style.visibility = "hidden";
	};
	
	this.cleanList_DOM = function () { $(this.list).empty(); };
	this.cleanPS_DOM = function () { $(this.ps).empty(); };
	
	this.getXMLData();
};

HN.MSN.PostSales.TipList = function (_sb, _list, _ps) {
	var _this = this;
	var tipsPerPage = 15;
	var urlInfos = new HN.MSN.PostSales.URLinfos();
	
	this.sb = typeof _sb == "string" ? document.getElementById(_sb) : _sb;
	this.list = typeof _list == "string" ? document.getElementById(_list) : _list;
	this.ps = typeof _ps == "string" ? document.getElementById(_ps) : _ps;
	this.tips = [];
	this.curPage = -1;
	this.maxPage = -1;
	
	this.getXMLData = function () {
		HN.MSN.PostSales.GetXMLData("products", function (xml) {
				if (typeof xml == "string") _this.sb.innerHTML = xml;
				else {
					var xml_tips;
					if (!urlInfos.tree[3] && urlInfos.isIndex) // no parent category
						xml_tips = $(xml).find("product[folder="+urlInfos.tree[1].text+"] tips");
					else // there is a parent category
						xml_tips = $(xml).find("product[folder="+urlInfos.tree[1].text+"] category[folder=\""+urlInfos.tree[2].text+"\"] tips");
					
					_this.sb.innerHTML = $(xml_tips).attr("description").replace(/\\n/g, "<br/>");
					$(xml_tips).find("tip").each(function () {
						_this.tips.push([$(this).attr("title"), $(this).attr("url")]);
					});
					_this.maxPage = Math.floor((_this.tips.length-1) / tipsPerPage) + 1;
					var page = parseInt(urlInfos.anchor);
					if (isNaN(page)) page = 1;
					_this.gotoPage(page);
					//var s="";for(var k in tips) s+=k+"="+tips[k];alert(s);
				}
			}
		);
	};
	
	this.gotoPage = function (page) {
		//alert("page="+page+" maxPage="+this.maxPage+" curPage="+this.curPage);
		if (page < 1) page = 1;
		else if (page > this.maxPage) page = this.maxPage;
		if (page > 0 && page != this.curPage) {
			this.curPage = page;
			this.cleanList_DOM();
			this.cleanPS_DOM();
			this.buildList_DOM((page-1)*tipsPerPage, tipsPerPage);
			this.buildPS_DOM();
		}
	};
	
	this.buildList_DOM = function (index_start, count) {
		var index_end = index_start + count;
		if (index_end > this.tips.length)
			index_end = this.tips.length;
		
		for (var i = index_start; i < index_end; i++) {
			var li = document.createElement("li");
			this.list.appendChild(li);
			var a = document.createElement("a");
			li.appendChild(a);
			a.appendChild(document.createTextNode(this.tips[i][0].replace(/\\n/g, "")));
			a.href = this.tips[i][1];
		}
	};
	
	this.buildPS_DOM = function () {
		// <
		var a = document.createElement("a");
		a.appendChild(document.createTextNode("<"));
		this.ps.appendChild(a);
		this.ps.appendChild(document.createTextNode(" "))
		a.href = "#" + (this.curPage-1);
		if (this.curPage > 1) {
			a.onclick = function () { _this.gotoPage(_this.curPage-1); return false; };
		}
		else
			a.style.visibility = "hidden";
		
		// 1 - 2 - 3 ..
		if (this.maxPage > 1) {
			for (var i = 1; i <= this.maxPage; i++) {
				var a = document.createElement("a");
				a.appendChild(document.createTextNode(i));
				a.pageIndex = i;
				a.href = "#" + i;
				if (i == this.curPage) {
					a.className = "current";
				}
				else {
					a.onclick = function () { _this.gotoPage(this.pageIndex); };
				}
				if (i > 1) this.ps.appendChild(document.createTextNode(" - "));
				this.ps.appendChild(a);
			}
		}
		
		// >
		this.ps.appendChild(document.createTextNode(" "))
		var a = document.createElement("a");
		a.appendChild(document.createTextNode(">"));
		this.ps.appendChild(a);
		a.href = "#" + (this.curPage+1);
		if (this.maxPage > 1 && this.curPage < this.maxPage) {
			a.onclick = function () { _this.gotoPage(_this.curPage+1); return false; };
		}
		else
			a.style.visibility = "hidden";
	};
	
	this.cleanList_DOM = function () { $(this.list).empty(); };
	this.cleanPS_DOM = function () { $(this.ps).empty(); };
	
	this.getXMLData();
};

HN.MSN.PostSales.TipVideo = function (_elem, callback_function) {
	var _this = this;
	var urlInfos = new HN.MSN.PostSales.URLinfos();
	this.elem = typeof _elem == "string" ? document.getElementById(_elem) : _elem;
	
	this.getXMLData = function () {
		HN.MSN.PostSales.GetXMLData("products", function (xml) {
				if (typeof xml != "string") {
					var xml_tip;
					if (!urlInfos.tree[3]) // no parent category
						xml_tip = $(xml).find("product[folder="+urlInfos.tree[1].text+"] tips tip[url=\""+urlInfos.fileName+"\"]");
					else // there is a parent category
						xml_tip = $(xml).find("product[folder="+urlInfos.tree[1].text+"] category[folder=\""+urlInfos.tree[2].text+"\"] tips tip[url=\""+urlInfos.fileName+"\"]");
					
					var video_uuid = $.trim($(xml_tip).attr("video_uuid"));
					if (video_uuid != "") {
						_this.elem.innerHTML = "\
						<div class=\"border-top\"></div>\
						<div class=\"header\"></div>\
						<div class=\"bg\">\
							<div id=\"player1\"></div>\
							<div class=\"zero\"></div>\
						</div>\
						<div class=\"border-bottom\"></div>";
						// Msn video function
						callback_function(video_uuid);
					}
				}
			}
		);
	};
	
	if (!urlInfos.isIndex)
		this.getXMLData();
};

HN.MSN.PostSales.OtherTips = function (_content) {
	var _this = this;
	var urlInfos = new HN.MSN.PostSales.URLinfos();
	var base_tips_url = urlInfos.baseFolder;
	this.content = typeof _content == "string" ? document.getElementById(_content) : _content;
	
	this.getXMLData = function () {
		HN.MSN.PostSales.GetXMLData("products", function (xml) {
			if (typeof xml == "string") _this.content.innerHTML = xml;
			else {
				if (!urlInfos.tree[3] && urlInfos.isIndex) { // no parent category
					$(_this.content).remove();
				}
				else { // there is a parent category
					var xml_category = $(xml).find("product[folder="+urlInfos.tree[1].text+"] category[folder=\""+urlInfos.tree[2].text+"\"]");
					var xml_other_tips = $(xml_category).find("other_tips");
					if (xml_other_tips.length == 0) {
						$(_this.content).remove();
					}
					else {
						var category_url = base_tips_url+urlInfos.tree[1].text+"/"+urlInfos.tree[2].text+"/";
						var html = "\
						<div class=\"bg\"></div>\
						<div class=\"content\">\
							<div class=\"title\">" + $(xml_other_tips).attr("description") + "</div>\
							<div class=\"other-tips\">";
							$(xml_other_tips).find("other_tip").each(function () {
								html += "<a href=\""+category_url+$(this).attr("url")+"\">&gt; " + $(this).attr("title") + "</a><br/>";
							});
						html += "\
							</div>\
							<div class=\"all-tips\">\
								<a href=\""+category_url+"\">Voir toutes les astuces <i>"+$(xml_category).attr("title")+"</i></a>\
							</div>\
						</div>";
						_this.content.innerHTML = html;
					}
				}
			}
		});
	};
	
	this.getXMLData();
};
