//******************************************************************************
//Sami Class
//asdf
////////////////////////////////////////////////////////////////////////////////
//contructor
function Sami(file) {

    
	this._xml = xmlLoadFromFile(file);
	this._isNull = false;
	if(!this._xml)
	{
		this._isNull = true;
		return;
	}
	this._entries = new SamiEntries(this._xml.getElementsByTagName("SYNC"));
	this._languages = new SamiLanguages(this._xml);	
	
}

////////////////////////////////////////////////////////////////////////////////
//private members

Sami.prototype._isNull;
Sami.prototype._xml;
Sami.prototype._entries;
Sami.prototype._languages;

////////////////////////////////////////////////////////////////////////////////
//public members

Sami.prototype.IsNull = function(){
		return this._isNull;
	}

Sami.prototype.Entries = function(){
		return this._entries;
	}

Sami.prototype.Languages = function(){
		return this._languages;
	}


//******************************************************************************
//SamiEntries Class

////////////////////////////////////////////////////////////////////////////////
//contructor
function SamiEntries(xml)
{
	this._xml = xml;
	this._count = 0;
	this._entries = new Array();
	for(var i=0;i<this._xml.length;i++)
	{
		this._entries[this._count] = new SamiEntry(this._xml[i]);
		this._count += 1;		
	}
}

////////////////////////////////////////////////////////////////////////////////
//private members

SamiEntries.prototype._xml;
SamiEntries.prototype._count;
SamiEntries.prototype._entries;

////////////////////////////////////////////////////////////////////////////////
//public members

SamiEntries.prototype.GetCount = function(){
		return this._count;
	}

SamiEntries.prototype.GetEntry = function(index){
		return this._entries[index];
	}


//******************************************************************************
//SamiEntry Class

////////////////////////////////////////////////////////////////////////////////
//contructor
function SamiEntry(xml)
{
	this._xml = xml;
	var p = xml.getElementsByTagName("P");
	for(var i=0;i<p.length;i++)
	{
		if(p[i].getAttribute("ID")=="Source")
		{
			this._speaker = GetInnerText(p[i]);
		}
		else
		{
			this._text = GetInnerText(p[i]);
		} 
	}
	this._time = xml.getAttribute("Start");
	//this._languageCode = ;
	
}

function GetInnerText(xml)
{
	if(xml.firstChild != null)
	{
		return GetInnerText(xml.firstChild);
	}

	return xml.nodeValue;
}

////////////////////////////////////////////////////////////////////////////////
//private members

SamiEntry.prototype._xml;
SamiEntry.prototype._text;
SamiEntry.prototype._speaker;
SamiEntry.prototype._time;
SamiEntry.prototype._languageCode;

////////////////////////////////////////////////////////////////////////////////
//public members

SamiEntry.prototype.GetText = function(){
		return this._text;
	}

SamiEntry.prototype.GetSpeaker = function(){
		return this._speaker;
	}

SamiEntry.prototype.GetTime = function(){
		return this._time;
	}

SamiEntry.prototype.GetLanguageCode= function(){
		return this._languageCode;
	}

//******************************************************************************
//SamiLanguages Class

////////////////////////////////////////////////////////////////////////////////
//contructor
function SamiLanguages(xml)
{
	this._xml = xml;
	var s = xmlGetString(this._xml);
	var start = s.indexOf("<!--")+4;
	var end = s.indexOf("-->");
	this._styleBlock = s.slice(start,end);
	this._count=0;
	for(var i=0;i<this._xml.length;i++)
	{
		this._languages[this._count] = new SamiLanguage(this._styleBlock);
		this._count += 1;		
	}
}

////////////////////////////////////////////////////////////////////////////////
//private members

SamiLanguages.prototype._xml;
SamiLanguages.prototype._styleBlock;
SamiLanguages.prototype._count;
SamiLanguages.prototype._languages;

////////////////////////////////////////////////////////////////////////////////
//public members

SamiLanguages.prototype.GetCount = function(){
		return this._count;
	}

SamiLanguages.prototype.GetLanguage = function(index){
		return this._languages[index];
	}


//******************************************************************************
//SamiLanguage Class

////////////////////////////////////////////////////////////////////////////////
//contructor
function SamiLanguage(styleBlock)
{
	this._styleBlock = styleBlock;
}

////////////////////////////////////////////////////////////////////////////////
//private members

SamiLanguage.prototype._styleBlock;

////////////////////////////////////////////////////////////////////////////////
//public members

SamiLanguage.prototype.GetCode = function(){
		var s = this._styleBlock;
		var lines = s.replace(/^\s+|\s+$/g,"").split("\n");
		var start = lines[index].indexOf("lang:")+5;
		var end = start+lines[index].substr(start,lines[index].length-start).indexOf(";");
		return lines[index].slice(start,end).replace(/^\s+|\s+$/g,"");
	}

SamiLanguage.prototype.GetName = function(){
		var s = this._styleBlock;
		var lines = s.replace(/^\s+|\s+$/g,"").split("\n");
		var start = lines[index].indexOf("name:")+5;
		var end = start+lines[index].substr(start,lines[index].length-start).indexOf(";");
		return lines[index].slice(start,end).replace(/^\s+|\s+$/g,"");
	}



