﻿// ******** BEGIN CUSTOM CONFIGURATION ********* //
// things you can change to customize your forums
	
	// images directory
	var imagesDir = "http://media.ubi.com/fr/missdecouverte/forums/images/";
	var categoryIconsDir = "icones_categories/";
	var forumsIconsDir = "icones_forums/";
	var interfaceIconsDir = "icones_interface/";
	
	// default icon for the categories (in case there's no custom icon for a category)
	// => custom icons are named : imagesDir/categoryIconsDir/categorie_CATEGORYNAME.png
	var defaultCategoryIcon = "http://webtools.fr.ubi.com/forums/MissDecouverte/silhouette_saut.gif";
	// default icon for the forums (in case there's no custom icon for a forum)
	// => custom icons are named : imagesDir/forumsIconsDir/forum_CATEGORYNAME_FORUMNAME.png
	var defaultForumIcon = "http://webtools.fr.ubi.com/forums/MissDecouverte/silhouette_saut.gif";
	
	// background colors for each category
	var bkgColorsArray = new Array();
	bkgColorsArray["univers_miss_decouverte"] = "fef1fa";
	bkgColorsArray["jeux_video"] = "dbfff5";
	bkgColorsArray["blabla_de_miss"] = "ecf6ff";
	bkgColorsArray["le_cirque_des_miss"] = "f8ffe0";

	// border colors for each category
	var borderColorsArray = new Array();
	borderColorsArray["univers_miss_decouverte"] = "f7d8e8";
	borderColorsArray["jeux_video"] = "a9e8d5";
	borderColorsArray["blabla_de_miss"] = "bedcf4";
	borderColorsArray["le_cirque_des_miss"] = "e4eeba";
	
	// text colors for each category
	var textColorsArray = new Array();
	textColorsArray["univers_miss_decouverte"] = "d17aaf";
	textColorsArray["jeux_video"] = "29a17b";
	textColorsArray["blabla_de_miss"] = "0c68b3";
	textColorsArray["le_cirque_des_miss"] = "a4d100";
	
// ******** END CUSTOM CONFIGURATION ********* //


function changeCategoriesDisplay()
{
	var categories;
	var categoriesName = new Array();
	var categoriesUrl = new Array();
	var links = new Array();
	var forums = new Array();
	var nbForums = 0;
	var forumsName = new Array();

	// get all categories titles boxes
	categories = getElementsByClass('ubbx_cfrm_cat_tbl', null, null);
	
	// if there are no categories, exit
	if(categories.length == 0)
		return false;

	var node;
	
		// *** loop to change categories display *** //
		
	// for each forum category
	for(var i = 0; i < categories.length; i++)
	{
		// get the first element		
		if(categories[i].firstChild.innerHTML)
			node = categories[i].firstChild; // IE
		else node = categories[i].childNodes[1]; // firefox
		
		// can't remember what this is for...
		node.firstChild.style.height = '41px';
		node.firstChild.style.padding = '0px';
		node.firstChild.style.margin = '0px';
		node.firstChild.style.overflow = 'hidden';
		node.firstChild.style.border = 'none';
		node.style.border = 'none';

		// get the text of the link of the category (the category name)
		links = getElementsByClass('ubbx_cfrm_com_title_link', null, categories[i]);

		// get the category name (format to remove special chars)
		categoriesName[i] = formatImageName(links[0].innerHTML);
		// get the url of the category
		categoriesUrl[i] = links[0].href;
		// remove the link (as we'll replace it with an image)
		links[0].innerHTML = "";
		
		// change the category title box layout
		categories[i].style.backgroundColor = 'white';
		categories[i].style.backgroundImage = 'url('+imagesDir+'categorie_bkg.jpg)';
		categories[i].style.backgroundRepeat = 'repeat-x';
		categories[i].style.border = 'none';
		// 2px border at the bottom
		if(textColorsArray[categoriesName[i]] != null)
			categories[i].style.borderBottom="solid 2px #"+textColorsArray[categoriesName[i]];
	}

	// loop for the category icons
	categories = getElementsByClass('ubbx_cfrm_cat_icon', null, null);
	// and change them
	for(var i = 0; i < categories.length; i++)
		categories[i].innerHTML = '<a href="'+categoriesUrl[i]+'"><img src="'+imagesDir+categoryIconsDir+'categorie_'+categoriesName[i]+'.png"></a>';
		//categories[i].innerHTML = '<a href="'+categoriesUrl[i]+'"><img src="'+imagesDir+categoryIconsDir+'categorie_'+categoriesName[i]+'.png" onerror="this.src='+"'"+defaultCategoryIcon+"'"+'></a>';
	
		// *** loop to change forums display *** //
		
	// get the categories forums box
	categories = getElementsByClass('ev_com_tbl', null, null);
	// foreach category
	var forumIterator = 0;
	for(var i = 0; i < categories.length; i++)
	{
			
			if(document.styleSheets[2].deleteRule)
				document.styleSheets[2].deleteRule(60);
		// change the background and border color of the category
		if(bkgColorsArray[categoriesName[i]] != null)
		{
			categories[i].style.backgroundColor="#"+bkgColorsArray[categoriesName[i]]; // doesn't work on IE
			for(var j in categories[i].childNodes)
				for(var k in categories[i].childNodes[j].childNodes)
				if(categories[i].childNodes[j].childNodes[k].style)
					categories[i].childNodes[j].childNodes[k].style.backgroundColor="#"+bkgColorsArray[categoriesName[i]];

		}
		if(borderColorsArray[categoriesName[i]] != null)
			categories[i].style.borderColor ="#"+borderColorsArray[categoriesName[i]];

		// get the text of the links of the forums of this category (their name)
		forums = getElementsByClass('ubbx_cfrm_com_title_link', null, categories[i]);
		// foreach forum
		for(var j = 0; j < forums.length; j++)
		{
			// get the forum name and format it (for the image)
			forumsName[nbForums] = formatImageName(forums[j].innerHTML);
			// count the forums
			nbForums++;
		}

		// loop for the forums icons
		categoriesContent = getElementsByClass('ev_forum_td_icon', null, categories[i]);
		// foreach forum
		for(var j = 1; j < categoriesContent.length; j++)
		{
			// replace the current image with our own (the onerror sets our default icon, in case we don't have an icon for this forum)
			categoriesContent[j].innerHTML = '<img src="'+imagesDir+forumsIconsDir+'forum_'+categoriesName[i]+'_'+forumsName[forumIterator]+'.png" onerror="this.src='+"'"+defaultForumIcon+"'"+'">';
			categoriesContent[j].valign="middle"; // center icon vertically (doesn't seem to be working)
			categoriesContent[j].style.textAlign="center";
			forumIterator++;
		}
		
		// loop for the links
		categoriesContent = categories[i].getElementsByTagName("a");
		// foreach forum link, set the color
		if(textColorsArray[categoriesName[i]] != null)
			for(var j = 0; j < categoriesContent.length; j++)
				categoriesContent[j].style.color = '#'+textColorsArray[categoriesName[i]];

		// loop for the dates
		categoriesContent = getElementsByClass('ev_text_timecolor', null, categories[i]);
		// foreach date, change the color
		for(var j = 0; j < categoriesContent.length; j++)
			categoriesContent[j].style.color = '#FFFFFF';

		// IE
		if(categories[i].firstChild.innerHTML)
		{
			node = categories[i].firstChild;

			// loop for the borders and bkg
			// foreach row
			for(var j=0; j < node.childNodes.length; j++)
				// foreach cell
				for(var k=0; k < node.childNodes[j].childNodes.length; k++)
				{
					if(borderColorsArray[categoriesName[i]] != null)
						node.childNodes[j].childNodes[k].style.borderColor="#"+borderColorsArray[categoriesName[i]];
					if(textColorsArray[categoriesName[i]] != null)
						node.childNodes[j].childNodes[k].style.color="#"+textColorsArray[categoriesName[i]];
				}
		}
		// firefox
		else
		{
			node = categories[i].childNodes[1];

			// loop for the borders and bkg
			// foreach row
			for(var j=0; j < node.childNodes.length; j=j+2)
				// foreach cell
				for(var k=1; k < node.childNodes[j].childNodes.length-1; k++)
				{
					if(borderColorsArray[categoriesName[i]] != null)
						node.childNodes[j].childNodes[k].style.borderColor="#"+borderColorsArray[categoriesName[i]];
					if(textColorsArray[categoriesName[i]] != null)
						node.childNodes[j].childNodes[k].style.color="#"+textColorsArray[categoriesName[i]];
				}
		}
	}
}

function changeHotTopicIcon()
{
	var icons = getElementsByClass('ev_ubbx_frm_topicicon', null, null);
	for(var i = 1; i < icons.length; i++)
	{
		if(icons[i].childNodes.length == 2 && icons[i].childNodes[1].className == "ubb_hot")
		{
			if(icons[i].firstChild.title == "Pas de nouveau contenu")
				icons[i].firstChild.src = imagesDir+interfaceIconsDir+"icone_nonewmanymessage.png";
			else icons[i].firstChild.src = imagesDir+interfaceIconsDir+"icone_manymessage.png";
			icons[i].firstChild.style.background="none";
			icons[i].firstChild.style.height="32px";
			icons[i].childNodes[1].style.display = "none";
		}
	}
}

function changePostIcon()
{
	var icons = getElementsByClass('ubb_post_icon1', null, null);
	for(var i = 0; i < icons.length; i++)
	{
		icons[i].src = imagesDir+interfaceIconsDir+"icone_sujet.png";
		icons[i].style.height = "24px";
		icons[i].style.width = "24px";
		icons[i].style.background='none';
		icons[i].parentNode.parentNode.style.padding='2px';

	}
}

function changeImages()
{
	var images = document.getElementsByTagName('img');
	for(var i = 0; i < images.length; i++)
	{
		if(images[i].src == "http://forumsforeveryone-fr.ubi.com/groupee_common/platform_images/blank.gif")
		{
			images[i].src = images[i].background;
			images[i].background = "none";
		}
	}
}

function setMDAvatars()
{
	var avatars = getElementsByClass('ev_ubbx_tpc_avatar', null, null);
	
	for(var j = 0; j < avatars.length; j++)
	{
		avatars[j].src = 'http://webtools.fr.ubi.com/forums/MissDecouverte/'+avatars[j].title.substring(9)+'.gif';
	}
}

function getElementsByClass(className,tag,node)
{
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';

	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;

	for (i = 0, j = 0; i < elsLen; i++)
		if(els[i].className == className)
		{
			classElements[j] = els[i];
			j++;
		}

	return classElements;
}

function hide(divId)
{
	if(document.getElementById(divId) != null)
		document.getElementById(divId).style.display='none';
}

function formatImageName(string)
{
	string = string.replace(/(%C3%A9)/g,"e");
	string = string.replace(/(&eacute;)/g,"e");
	string = string.replace(/[éèêë]/g,"e");
	string = string.replace(/à/g,"a");
	string = string.replace(/ \?/g,"");
	string = string.toLowerCase();
	string = string.replace(/\s/g, "_");

	return string;
	
}

function processModifications()
{
	hide('gs_tab_notify');
	hide('gs_tab_tools');
	
	changeCategoriesDisplay();
	changeHotTopicIcon();
	changePostIcon();
//	changeImages();

	if(document.getElementById('mainTable').height < 624)
		document.getElementById('mainTable').height = 624;	
}