//Global vars
keySpacing = 22;
entrySpacing = 16;
afterEntrySpacing = 5;
mWidth = 230;
mHeight = 394;
minYScale = 5;

//How much time a date stays new, in milliseconds (currently two weeks
stayNew = 14*24*60*60*1000;

function dateToNum(time)
{
	//Assigns a numerical value to a date
	var day = Number(time.substr(0,2));
	var month = Number(time.substr(3,2));
	
	//Since we will use UTC, we need years over 1970, hence the +100
	var year = Number(time.substr(6,4));
	
	//Use UTC date to get a number
	return Date.UTC(year, month - 1, day);
}

function Key(num, caption, entries)
{
	this.num = num;
	this.caption = caption;
	this.entries = entries;
}

Key.prototype.drawSelf = function()
{
	_root.main.content.attachMovie("mcKey", "key" add this.num, this.num);
	this.mc = _root.main.content["key" add this.num];
	this.mc.mcLeftArrow._visible = false;
	this.mc.mcDownArrow._visible = false;
	this.mc._y = keySpacing*this.num;
	
	this.mc.txtCaption.text = this.caption;
	
	//Operations on the bg button
	this.mc.btnKey._useHandCursor = false;
	this.mc.btnKey.onRollOver = handleKeyRollover;
	this.mc.btnKey.onRollOut = handleKeyRollOut;
	this.mc.btnKey.onPress = handleKeyPress;
	this.mc.btnKey.onReleaseOutside = handleKeyRollOut;
	this.mc.key = this;
	
	//Create place holder for entries
	this.mc.createEmptyMovieClip("entries", 0);
	this.mc.entries._y = keySpacing;
	this.mc.entries._visible = false;
	this.open = false;
	this.height = this.entries.length*entrySpacing + afterEntrySpacing;
	
	this.pos = this.mc._y;
}

Key.prototype.countNew = function()
{
	var count = 0;
	for(var i = 0; i < this.entries.length; i++)
	{
		if(this.entries[i].isNew)
		{
			count++;
		}
	}
	
	//We have a count for new and total entries
	var str = "<p align='right'>(" + this.entries.length + ") </p>";
	//var str = str + "<font color='#00f409'>(" +  + ")</font></p>";
	this.mc.txtCount.htmlText = str;
	_root.countTotal += this.entries.length;
	_root.countNew += count;
}

function Entry(num, name, added, src)
{
	this.num = num;
	this.name = name;
	this.added = added;
	this.src = src;
}

Entry.prototype.drawSelf = function(location)
{
	location.attachMovie("mcEntry", "entry" add this.num, this.num);
	this.mc = location["entry" add this.num];
	
	this.mc._y = this.num*entrySpacing;
	this.mc.txtName.text = this.name;
	
	this.markNew();
	this.mc.mcNew._visible = this.isNew;
	
	this.mc.btnEntry.onPress = handleEntryPress;
	this.mc.btnEntry.onRollOver = handleEntryRollover;
	this.mc.btnEntry.onRollOut = handleEntryRollOut;
	this.mc.btnEntry.entry = this;
	
	this.mc.mcHighlight._visible = false;
	
	this.selected = false;
}



//Event handlers
function handleKeyRollover()
{
	var c = this._parent;
	if(!c.key.open)
	{
		c.mcLeftArrow._visible = true;
	}
}

function handleKeyRollout()
{
	var c = this._parent;
	c.mcLeftArrow._visible = false;
}

function handleKeyPress()
{
	var c = this._parent;
	c.key.open = !c.key.open;
	c.mcDownArrow._visible = c.key.open;
	c.mcLeftArrow._visible = !c.key.open;
	c.key.mc.entries._visible = c.key.open;
	_root.calcNewPos();
}

function handleSBPress()
{
	this.startDrag(false, this._x,0,this._x,(1-this._yscale/100)*mHeight);
}

function handleSBRelease()
{
	this.stopDrag();
}

function handleEntryPress()
{
	if(!this.entry.selected)
	{
		for(i = 0; i < _root.keys.length; i++)
		{
			var c = _root.keys[i];
			for(j = 0; j < c.entries.length; j++)
			{
				c.entries[j].selected = false;
				c.entries[j].mc.mcHighlight._visible = false;
			}
		}
		this.entry.selected = true;
		this._parent.mcHighlight._visible = true;
		
		//Set the scource appropriately
		var s = this.entry.src+".htm";
		getURL(s, "izquierda");
		trace (s);
		
		//Set the title
		var lc  = new LocalConnection();
		lc.send("lcTitle", "changeTitle", "Témoignages" + "|" + this.entry.name);
		delete lc;
	}
	
}

function handleEntryRollOver()
{
	this._parent.mcHighlight._visible = true;
}

function handleEntryRollOut()
{
	if(!this.entry.selected)
	{
		this._parent.mcHighlight._visible = false;
	}
}

function refreshDisplay()
{
	for(var i = 0; i < keys.length; i++)
	{
		c = keys[i];
		c.mc._y = (c.pos - c.mc._y)/8 + c.mc._y;
	}
	var c = keys[keys.length - 1];
	
	//Handle the scrollbar
	var ys = 100*mHeight/(c.mc._y);
	_root.sb._yscale = Math.max(minYScale,ys);
	_root.sb._visible = _root.sb._yscale < 100;
	
	_root.sb._y = Math.max(0, Math.min(_root.sb._y, (1-_root.sb._yscale/100)*mHeight));
	var percentScroll = _root.sb._y/((1-_root.sb._yscale/100)*mHeight);
	var scrollLength = (100/ys - 1)*mHeight;
	_root.main.content._y = -1.003*percentScroll*scrollLength;
		
}

function calcNewPos()
{
	//Calculate new positions
	var d = 0;
	for(var i = 0; i < keys.length; i++)
	{
		c = keys[i];
		c.pos = d;
		d += c.open*c.height + keySpacing;
	}
}

function createAssets()
{
	//Create a global variable for the current date
	var today = new Date();
	todayStamp = Date.UTC(today.getFullYear(), today.getMonth(), today.getDay());

	_root.createEmptyMovieClip("main", 0);
	_root.main.createEmptyMovieClip("content", 0);
	_root.main.onEnterFrame = refreshDisplay;
	_root.createEmptyMovieClip("mask", 1);
	
	//Create the scrollbar
	_root.createEmptyMovieClip("sb", 2);
	_root.sb.onPress = handleSBPress;
	_root.sb.onRelease = handleSBRelease;
	_root.sb.onReleaseOutside = handleSBRelease;
	with(_root.sb)
	{
		beginFill(0xFBAE5C);
		lineTo(16, 0);
		lineTo(16, mHeight);
		lineTo(0,  mHeight);
		endFill();
	}
	
	_root.sb._x = 226;
	_root.sb._visible = false;
	
	_root.countTotal = 0;
	_root.countNew = 0;
	for(var i = 0; i < keys.length; i++)
	{
		var c = keys[i];
		c.drawSelf();
		for(var j = 0; j < c.entries.length; j++)
		{
			c.entries[j].drawSelf(c.mc.entries);
		}
		c.countNew();
	}
	
	//Hide last entry
	_root.main.content["key" + (keys.length - 1)]._visible =false;
	
	//Set the title
	var lc  = new LocalConnection();
	lc.send("lcTitle", "changeTitle", "Témoignages" + "|" + "Liste alphabétique");
	lc.send("lcTitle", "changeTotal", _root.countTotal + "|" + _root.countNew);
	delete lc;
}

function readXML()
{
	var main = file.firstChild.childNodes;
	keys = new Array();
	for(var i = 0; i < main.length; i++)
	{
		keys.push(new Key(i, main[i].attributes.caption, new Array()));
		var cKey = keys[i];
		for(var j = 0; j < main[i].childNodes.length; j++)
		{
			var ca = main[i].childNodes[j].attributes;
			cKey.entries.push(new Entry(j, ca.name, ca.added, ca.src));
		}
	}
	createAssets();
}

function xmlLoaded(success)
{
	if(success)
	{
		_root.nextFrame();
		readXML();
	}
	clearInterval(xmlInt);
}

function updateLoader()
{
	if(file.getBytesTotal() > 100)
	{
		_root.pc.text = Math.round(file.getBytesLoaded()/file.getBytesTotal()*100) + "%";
		//Draw appropriate triangles in mask
		var p = file.getBytesLoaded()/file.getBytesTotal();
		_root.pl.mask.clear();
		dt = 1/30*Math.PI;
		for(i = 0; i < 60; i++)
		{
			if(i+1 < p*60)
			{
				var ang = i/30*Math.PI;
				with(_root.pl.mask)
				{
					moveTo(0,0);
					beginFill(0x000000);
					lineTo(50*Math.cos(ang), 50*Math.sin(ang));
					lineTo(50*Math.cos(ang+dt), 50*Math.sin(ang+dt));
					endFill();
				}
			}
			else
			{
				break;
			}
		}
	}
}

file = new XML();
file.ignoreWhite = true;
file.onLoad = xmlLoaded;
file.load("comments.txt");
_root.pl.createEmptyMovieClip("mask", 0);
_root.pl.mask._rotation = -90;
_root.pl.ring.setMask(_root.pl.mask);
xmlInt = setInterval(updateLoader, 50);