var moduleManager =
{
  unloadModule: function(moduleName)
  {
    cv.unloadCategory(moduleName);
  }
}

var cv = 
{
	blocs: new Array(),
	blocGroups: new Array(),
	
	init: function()
	{
		var icons = dom.get("categoryBloc").getElementsByTagName("img");
		for (var i=0; i<icons.length; i++)
		{
			var icon = icons[i];
			dom.addEvent(icon, "click", function() 
			{
				var categoryName = this.id.substring(0, this.id.length -4);
				this.src = this.src.substring(0, this.src.length - 6) + "Off.gif";
				this.onclick = "";
				this.style.cursor = "default";
				cv.loadCategory(categoryName);
			});
		}
		
		var picture = dom.get("identityPicture");
		dom.addEvent(picture, "mouseover", function() { this.src=dom.get("identityPictureMouseOver").src });
		dom.addEvent(picture, "mouseout", function() { this.src=dom.get("identityPictureMouseOut").src });
		
		this.blocs["identity"] = this.initSimpleBloc(dom.get("identity"), "identityBloc", function() {cv.unloadCategory("identity");}, 0, 400);
    this.blocGroups["experience"] = this.initDl(dom.get("experience"), "experienceBloc", "experienceBloc", function() {cv.unloadCategory("experience"); }, 270);
    this.blocGroups["education"] = this.initDl(dom.get("education"), "educationBloc", "educationBloc", function() {cv.unloadCategory("education"); }, 640);
    this.blocs["activities"] = this.initSimpleBloc(dom.get("activities"), "activitiesBloc", function() {cv.unloadCategory("activities");}, 640, 160);
    this.blocs["languages"] = this.initSimpleBloc(dom.get("languages"), "languagesBloc", function() {cv.unloadCategory("languages");}, 270, 400);
    this.blocs["computer"] = this.initSimpleBloc(dom.get("computer"), "computerBloc", function() {cv.unloadCategory("computer");}, 550, 400);
    
    dom.addEvent(dom.get("showCategoryButton"), "click", function() {cv.showCategoryList(true)});
    dom.addEvent(dom.get("hideCategoryButton"), "click", function() {cv.showCategoryList(false)});
    this.showCategoryList(true);
	},
	
	showCategoryList: function(show)
	{
	 dom.get("showCategoryButton").style.display = show?"none":"";
	 dom.get("categoryBloc").style.display = show?"":"none";
	},
	
  loadCategory: function(categoryName)
  {
			var bloc = this.blocs[categoryName];
			if (bloc)
			{
        blocManager.showBloc(bloc);
			}
			else
			{
			 var blocs = this.blocGroups[categoryName];
			 if (blocs)
			 {
			   for (var i=0; i<blocs.length; i++)
			   {
            blocManager.showBloc(blocs[i]);
			   }
			 }
			 else
			 {
			   eval(categoryName + "Init()");
			 }
			}
  },
  
	unloadCategory: function(categoryName)
	{
			var bloc = this.blocs[categoryName];
			if (bloc)
			{
        blocManager.hideBloc(bloc);
			}
			else
			{
			 var blocs = this.blocGroups[categoryName];
			 if (blocs)
			 {
			   for (var i=0; i<blocs.length; i++)
			   {
            var currentBloc = blocs[i];
            if (currentBloc.htmlElement.id == "")
            {
              blocManager.maximizeBloc(currentBloc.id);
            }
            blocManager.hideBloc(currentBloc);
			   }
			 }
			}

		var icon = dom.get(categoryName + "Icon");
		icon.src = icon.src.substring(0, icon.src.length - 7) + "On.gif";
		icon.style.cursor = "pointer";
		
		dom.addEvent(icon, "click", function() 
		{
			var categoryName = this.id.substring(0, this.id.length -4);
			this.src = this.src.substring(0, this.src.length - 6) + "Off.gif";
			this.onclick = "";
			this.style.cursor = "default";
			cv.loadCategory(categoryName);
		});
	},

	initDl: function(parent, idBase, className, unloadFunc, xPos)
	{
	 var dl = parent.getElementsByTagName("dl")[0].childNodes;
   var blocList = new Array();
   var title;
   var j=0;
   for (var i=0; i<dl.length; i++)
	 {
	   var element = dl[i];
     if (element.tagName)
	   {
	     if (element.tagName == "dt" || element.tagName == "DT")
	     {
	       title = dom.getText(element);
	     }
	     else if (element.tagName == "dd" || element.tagName == "DD")
	     {
	       var bloc = blocManager.createBloc(idBase + j, title, className, unloadFunc, xPos, 0, 360, 240, 100, 60, 600, 600);
	       blocManager.hideBloc(bloc);
	       while (element.hasChildNodes())
	       {
          bloc.contentElement.appendChild(element.firstChild);
	       }
	       blocList[j++] = bloc;
	     }
	   }
	 }
	 
	 return blocList;
	 
	},
	
 initSimpleBloc: function(parent, id, unloadFunc, xPos, height)
 {
		var title = dom.getText(parent.getElementsByTagName("h2")[0]);
		var bloc = blocManager.createBloc(id, title, "", unloadFunc, xPos, 0, 260, height, 260, 60, 300, 600);
    blocManager.hideBloc(bloc);

		var children = parent.childNodes;
		for (var i=0; i<children.length; i++)
		{
			var child = children[i];
			if (child.tagName && child.tagName != "h2" && child.tagName != "H2")
			{
				bloc.contentElement.appendChild(child);
				i--;
			}
		}
		return bloc;
 }
}

addLoadEvent( function() { cv.init() });
