/////////////////////////////////////////////////////
//
//  Live game info objects.
//
/////////////////////////////////////////////////////

function Template(){
    function addTemplate(templ){
        if( this._arrTemplates == null )
            this._arrTemplates = new Array();
        this._arrTemplates[this._arrTemplates.length] = templ;
        }
        
    // Variables.
    this._arrTemplates = null;
    // Methods
    this.addTemplate = addTemplate;
    }
    
// Snatch object
function Snatch(){
    this._snatchId = 0;
    this._snatchValue = "1.0";
    this._snatchLabel = "";
    this._snatchClass = "";
    }
   
// Live info item. (Red card count or things like that.) 
function LiveInfoItem(){
    this._itemId = 0;
    this._itemValue1 = ""; 
    this._itemValue2 = ""; 
    this._itemLabel = "";
    }

// Live object
function LiveItem(){
    function addSnatch(snatchId, snatchValue, snatchLabel, snatchClass){
        if( this._arrSnatches == null )
            this._arrSnatches = new Array();
        
        var snatchObj = new Snatch();
        snatchObj._snatchId = snatchId;
        snatchObj._snatchLabel = snatchLabel;
        snatchObj._snatchValue = snatchValue;
        snatchObj._snatchClass = snatchClass;
        this._arrSnatches[this._arrSnatches.length] = snatchObj;
        return snatchObj;
        }
        
    function getSnatch(index){
        if( this._arrSnatches == null || index >= this._arrSnatches.length)
            return null;
        return this._arrSnatches[index];
        }
        
    function getSnatchCount(){
        if( this._arrSnatches == null )
            return 0;
        return this._arrSnatches.length;
        }
        
    function getChildsDiv(){
        return "#snatch_" + this._liveItemId.toString();
        }
    
    // Variables.
    this._liveItemId = 0;
    this._arrSnatches = null;
    this._lockState = false;
    this._expanded = false;
    this._starred = false; 
    this._itemLabel = "";
    
    // Methods
	this.addSnatch = addSnatch;
	this.getSnatch = getSnatch;
	this.getChildsDiv = getChildsDiv;
	this.getSnatchCount = getSnatchCount;
    }

function Live(){
    function addLiveItem(liveItemId, lockState, starred, expanded, itemLabel){
        if( this._arrLiveItems == null )
            this._arrLiveItems = new Array();
            
        var itemNew = new LiveItem();
        itemNew._liveItemId = liveItemId;
        itemNew._lockState = lockState;
        itemNew._expanded = expanded;
        itemNew._starred = starred;
        itemNew._itemLabel = itemLabel;
        
        this._arrLiveItems[this._arrLiveItems.length] = itemNew;
        return itemNew;
        }
        
    function getLiveItemById(liveItemId){
        var i = 0;
        for(i=0; i<this._arrLiveItems.length; i++){
            if( this._arrLiveItems[i] != null && 
                this._arrLiveItems[i]._liveItemId == liveItemId ){
                return this._arrLiveItems[i];
                }
            }
        return null;
        }
        
    function getLiveItem(index){
        if( this._arrLiveItems == null || index >= this._arrLiveItems.length )
            return null;
        return this._arrLiveItems[index];
        }
        
    function getInfoItem(index){
        if( this._arrInfoItems == null || index >= this._arrInfoItems.length )
            return null;
        return this._arrInfoItems[index];
        }
            
    function addInfoItem(itemId, itemValue1, itemValue2, itemLabel){
        if( this._arrInfoItems == null )
            this._arrInfoItems = new Array();
            
        var infoItem = new LiveInfoItem();
        infoItem._itemId = itemId;
        infoItem._itemValue1 = itemValue1;
        infoItem._itemValue2 = itemValue2;
        infoItem._itemLabel = itemLabel;
        this._arrInfoItems[this._arrInfoItems.length] = infoItem;
        return infoItem;        
        }
        
    function getItemsCount(){
        if( this._arrLiveItems == null )
            return 0;
        return this._arrLiveItems.length;
        }
        
    function getChildsDiv(){
        return "#liveitems_" + this._liveId.toString();
        }

    // Variables.
    this._liveId = 0;
    this._arrLiveItems = null;
    this._openState = 0;
    this._updateDateTime = "";
    this._started = 0;
    this._startDateTime = "";
    this._endDateTime = "";
    this._time_add = "";
    this._time_elem_name = "";
    this._stream_id = -1;
    this._team1="";
    this._team2="";
    this._championat="";
    this._time="";
    this._gameStartTimeTick = 0;
    this._orderId = 0;
    this._arrInfoItems = null;
    
    // Methods
	this.addLiveItem = addLiveItem;
	this.getLiveItemById = getLiveItemById;
	this.getInfoItem = getInfoItem;
	this.addInfoItem = addInfoItem;
	this.getLiveItem = getLiveItem;
	this.getChildsDiv = getChildsDiv;
	this.getItemsCount = getItemsCount;
    }
    
function sortLiveGames(a,b){return a._orderId-b._orderId;}
function LiveGames(){
    function parseLiveGameInfoXMLfromString(xmlString){
        var xmlDoc = null;
        if(window.ActiveXObject){
            xmlDoc = new ActiveXObject('Microsoft.XMLDOM');
            xmlDoc.async='false';
            xmlDoc.loadXML(xmlString);
            } 
        else{
            var parser = new DOMParser();
            xmlDoc = parser.parseFromString(xmlString,'text/xml');
            }            
        if( xmlDoc == null )
            return false;
        return this.parseLiveGameInfoXML(xmlDoc);
        }

    function parseLiveGameInfoXML(xmlDoc){
        if( xmlDoc == null || xmlDoc.documentElement == null || xmlDoc.documentElement.tagName != "lives" )
            return false;

        var loopLive = 0;  var liveNode = null;
        var loopItem = 0;  var liveItemNode = null;
        var loopSnatch = 0;  var snatchNode = null;
        this._updateTime = xmlDoc.documentElement.attributes.getNamedItem("update").nodeValue;
        this._liveCount = parseInt(xmlDoc.documentElement.attributes.getNamedItem("livecount").nodeValue);

         if( xmlDoc.documentElement.childNodes == null )
            return;

        while( xmlDoc.documentElement.childNodes[loopLive] != null ){ // <live> loop
            liveNode = xmlDoc.documentElement.childNodes[loopLive];
            if( liveNode.tagName != "live" ){ // skip node
                loopLive ++;
                continue;
                } 
                
            // Add live object.     
            var liveObj = this.addLive(
                parseInt(liveNode.attributes.getNamedItem("id").nodeValue), 0/*open status*/,
                liveNode.attributes.getNamedItem("update_time").nodeValue,
                parseInt(liveNode.attributes.getNamedItem("started").nodeValue),
                liveNode.attributes.getNamedItem("start_time").nodeValue,
                liveNode.attributes.getNamedItem("end_time").nodeValue,
                liveNode.attributes.getNamedItem("time_add").nodeValue,
                liveNode.attributes.getNamedItem("time_elem_name").nodeValue,
                liveNode.attributes.getNamedItem("stream_id").nodeValue,
                liveNode.attributes.getNamedItem("team1").nodeValue,
                liveNode.attributes.getNamedItem("team2").nodeValue,
                liveNode.attributes.getNamedItem("championat").nodeValue,
                liveNode.attributes.getNamedItem("time").nodeValue,
                liveNode.attributes.getNamedItem("game_start_time").nodeValue
                );       
            
            loopItem = 0;
            while( liveNode.childNodes[loopItem] != null ){
                liveItemNode = liveNode.childNodes[loopItem];
                
                if( liveItemNode.tagName == "info" ){ // Add info item.
                    liveObj.addInfoItem(
                         parseInt(liveItemNode.attributes.getNamedItem("id").nodeValue),
                         liveItemNode.attributes.getNamedItem("value1").nodeValue,
                         liveItemNode.attributes.getNamedItem("value2").nodeValue,
                         liveItemNode.attributes.getNamedItem("label").nodeValue);
                    }
                else
                if( liveItemNode.tagName == "item" ){ // Add live item.
                    var item = liveObj.addLiveItem(
                        parseInt(liveItemNode.attributes.getNamedItem("id").nodeValue),
                        parseInt(liveItemNode.attributes.getNamedItem("lock").nodeValue), 0/*starred*/, 1/*expanded*/,
                        liveItemNode.attributes.getNamedItem("label").nodeValue);
                    
                    loopSnatch = 0; 
                    while(liveItemNode.childNodes[loopSnatch] != null){ // Add snatches
                        snatchNode = liveItemNode.childNodes[loopSnatch];
                        if( snatchNode.tagName != "snatch" ){
                            loopSnatch ++;
                            continue;
                            }
                        if( item != null ){
                        
                            var snatchLabel = snatchNode.attributes.getNamedItem("label").nodeValue;
                            snatchLabel = replaceAll(snatchLabel, "%2C", "<");
                            snatchLabel = replaceAll(snatchLabel, "%2E", ">");
                            
                            item.addSnatch(
                                 parseInt(snatchNode.attributes.getNamedItem("id").nodeValue),
                                 snatchNode.attributes.getNamedItem("value").nodeValue,
                                 snatchLabel, "" /*snatch class*/
                                );
                            }
                        
                        loopSnatch ++;
                        }
                    }
                loopItem ++;    
                }
            loopLive ++;
            }   
        return true;
        }
    
    function addLive(liveId, openState, updateDateTime, started, startDateTime, 
                    endDateTime, time_add, time_elem_name, stream_id, team1, team2, championat, time, gameStartTimeTick ){   
        if( this._arrLives == null )
            this._arrLives = new Array();

        var liveObj = new Live();
        liveObj._liveId = liveId;
        liveObj._openState = openState;
        liveObj._updateDateTime = updateDateTime;
        liveObj._startDateTime = startDateTime;
        liveObj._endDateTime = endDateTime;
        liveObj._started = started;
        liveObj._time_add = time_add;
        liveObj._time_elem_name = time_elem_name;
        liveObj._stream_id = stream_id;
        liveObj._team1 = team1;
        liveObj._team2 = team2;
        liveObj._championat = championat;
        liveObj._time = time;
        liveObj._gameStartTimeTick = gameStartTimeTick;
        liveObj._orderId = this._arrLives.length;
        this._arrLives[this._arrLives.length] = liveObj;
        return liveObj;
        }
      
    function getLiveById(liveGameId){
        if( this._arrLives == null ) return null;
        var i = 0;
        for(i=0; i<this._arrLives.length; i++){
            if( this._arrLives[i] != null && 
                this._arrLives[i]._liveId == liveGameId ){
                return this._arrLives[i];
                }
            }
        return null;
        }
        
    function getLiveIndexByLiveId(liveGameId){
        if( this._arrLives == null ) return -1;
        var i = 0;
        for(i=0; i<this._arrLives.length; i++){
            if( this._arrLives[i] != null && 
                this._arrLives[i]._liveId == liveGameId ){
                return i;
                }
            }
        return -1;
        }
          
    function getLive(index){
        if( this._arrLives == null || index >= this._arrLives.length )
            return null;
        return this._arrLives[index];
        }
        
    function getCount(){
        if( this._arrLives == null )
            return 0;
        return this._arrLives.length;
        }
             
    function sortLives(){
        if( this._arrLives == null || this._arrLives.length == 0 )
            return false;
        this._arrLives.sort(sortLiveGames);
        return true;
        }
        
    function copyFrom(livesFrom){
        if( livesFrom == null )
            return false;
       
        var loop = 0;
        var liveObj = livesFrom.getLive(loop);     
        var index = 0;     
        var copyAllLives = false;
        
        // Copy all lives.
        if( livesFrom._liveCount == livesFrom._arrLives.length ){
            this._arrLives = new Array();
            }
       
        while( liveObj != null ){
            index = this.getLiveIndexByLiveId(liveObj._liveId);
            if( index != -1 ){ // Replace existing live object.
                this._arrLives[index] = liveObj;
                }
            else{   // Add new live object.
                if( this._arrLives == null )
                    this._arrLives = new Array();
                this._arrLives[this._arrLives.length] = liveObj;
                }
        
            loop ++;
            liveObj = livesFrom.getLive(loop); 
            }
       
        if( livesFrom._updateTime != null && livesFrom._updateTime.length > 0 ){
            this._updateTime = livesFrom._updateTime;
            }
        return true;
        }
        
     // Variables.
    this._arrLives = null;
    this._divId = "#ctl00_ContentPlaceHolder1_UpdatePanelLiveGames";
    this._updateTime = "";
    this._liveCount = 0;
    
    // Methods
    this.addLive = addLive;
	this.getLive = getLive;
	this.getLiveById = getLiveById;
	this.getCount = getCount;
	this.parseLiveGameInfoXMLfromString = parseLiveGameInfoXMLfromString;
	this.parseLiveGameInfoXML = parseLiveGameInfoXML;
	this.sortLives = sortLives;
	this.getLiveIndexByLiveId = getLiveIndexByLiveId;
	this.copyFrom = copyFrom;
    }
    
/////////////////////////////////////////////////////
// returns LiveGames object created and initialized from xmlDocument.
// http://localhost:65096/Max_v2.0_beta_test/GenericHandlers/LiveGameHandler.ashx?req=needupdate&ct=1&param0=9/28/2011%201:42:15%20AM&livegid=-433178
/*
<lives>
	// Live game.
	<live id='' open='' started='' start_time='' end_time='' time_add='' time_elem_name=''>
		<info id='' value='' label=''/>
		// Live item.
		<item id='' lock='' starred='' expanded='' label=''>
			// Live snatch.
			<snatch id='' value='' label=''/>
		</item>
	</live>
</lives>
*/
 
var _openLiveTemplIdx = 0;
var _closeLiveTemplIdx = 1;
var _openLiveItemTemplIdx = 2;
var _closeLiveItemTemplIdx = 3;
var _openLiveLockedItemTemplIdx = 4;
var _snatchTemplIdx = 5;
var _snatchTemplClosedIdx = 6;

function replaceAll(txt, replace, with_this) {
    return txt.replace(new RegExp(replace, 'g'),with_this);
    }

function renderClosedLive(objLive, livesDivId, index, replaceDivId){
    var loopItems = 0;
    var loopSnatches = 0;
    var objItem = null;
    var objSnatch = null;
    var replaceHTML = index == 0 ? true : false;

    var htmlLive = "";
    htmlLive = _templ._arrTemplates[_closeLiveTemplIdx];
    htmlLive = replaceAll(htmlLive, "{ANIME}", (objLive._started == 1) ? "gif" : "png");
    
    if( objLive._started == 1 ){
        htmlLive = replaceAll(htmlLive, "{SCORE}", String.format("{0}:{1}", 
                                objLive.getInfoItem(0)._itemValue1,
                                objLive.getInfoItem(0)._itemValue2));
        }
    else{
         htmlLive = replaceAll(htmlLive, "{SCORE}", objLive._time);
         
         var scoreVal1 = objLive.getInfoItem(0)._itemValue1;
         var scoreVal2 = objLive.getInfoItem(0)._itemValue2;
         if( parseInt(scoreVal1) < 10 )
            scoreVal1 = "0" + scoreVal1;
         if( parseInt(scoreVal2) < 10 )
            scoreVal2 = "0" + scoreVal2;
         
         htmlLive = replaceAll(htmlLive, "{TIME}", String.format("{0}:{1}", scoreVal1, scoreVal2));
        }
        
     /* Live items */ 
    loopItems = 0;
    objItem = objLive.getLiveItem(loopItems);

    htmlLive = replaceAll(htmlLive, "{TICK}", "1");
    htmlLive = replaceAll(htmlLive, "{STARTED}", objLive._started.toString());
    htmlLive = replaceAll(htmlLive, "{GAME_ID}", objLive._liveId.toString());
    htmlLive = replaceAll(htmlLive, "{START_TIME}", objLive._startDateTime);
    htmlLive = replaceAll(htmlLive, "{END_TIME}", objLive._endDateTime);
    htmlLive = replaceAll(htmlLive, "{TIME_ADD}", objLive._time_add);
    htmlLive = replaceAll(htmlLive, "{TIME_ELEM_NAME}", objLive._time_elem_name);
    htmlLive = replaceAll(htmlLive, "{CHAMPIONAT_NAME}", objLive._championat);
    htmlLive = replaceAll(htmlLive, "{TEAM1}", objLive._team1);
    htmlLive = replaceAll(htmlLive, "{TEAM2}", objLive._team2);
    if( objLive._started == 0 )
        htmlLive = replaceAll(htmlLive, "{TIME}", objLive._time);
    else
       htmlLive = replaceAll(htmlLive, "{TIME}", "");
    htmlLive = replaceAll(htmlLive, "{LIVE_ID}", (objItem == null ? objLive._liveId.toString() : objItem._liveItemId.toString()));
        
    // Add live HTML.
    if( replaceHTML ){
        if( replaceDivId.length > 0 )
            $(replaceDivId).replaceWith(htmlLive);
        else
            $(livesDivId).html(htmlLive);
        replaceHTML = false;
        }
    else
        $(livesDivId).append(htmlLive);
    
    if( objItem != null ){ // Live items loop.
        var htmlLive = "";
        if( objItem._lockState == 1 ) // Locked item.
            htmlLive = _templ._arrTemplates[_openLiveLockedItemTemplIdx];
        else
            htmlLive = _templ._arrTemplates[_openLiveItemTemplIdx];
            
        htmlLive = replaceAll(htmlLive, "{LIVE_ID}", objItem._liveItemId.toString());
        htmlLive = replaceAll(htmlLive, "{GAME_ID}", objLive._liveId.toString());
        htmlLive = replaceAll(htmlLive, "{LIVE_TEXT}", objItem._itemLabel);
        htmlLive = replaceAll(htmlLive, "{STATUS}", objItem._expanded ? "table" : "none");
        htmlLive = replaceAll(htmlLive, "{OPEN}", objItem._expanded ? "open" : "close");
        htmlLive = replaceAll(htmlLive, "{STARRED}", objItem._starred ? "enabled" : "disabled");
        // Append live item.
        if( loopItems == 0 )
            $(objLive.getChildsDiv()).html(htmlLive);
        else
            $(objLive.getChildsDiv()).append(htmlLive);
        
        loopSnatches = 0;
        objSnatch = objItem.getSnatch(loopSnatches);
        while( objSnatch != null ){
            var htmlSnatch = _templ._arrTemplates[_snatchTemplClosedIdx];
            htmlSnatch = replaceAll(htmlSnatch, "{SNATCH_ID}", objSnatch._snatchId.toString());
            htmlSnatch = replaceAll(htmlSnatch, "{GAME_ID}", objLive._liveId.toString());
            htmlSnatch = replaceAll(htmlSnatch, "{SNATCH}", objSnatch._snatchValue.toString());
            htmlSnatch = replaceAll(htmlSnatch, "{SNATCH_NAME}", objSnatch._snatchLabel.toString());
            htmlSnatch = replaceAll(htmlSnatch, "{SNATCH_CSS}", objSnatch._snatchClass);

            // Append snatch item.
            if( loopSnatches == 0 )
                $(objItem.getChildsDiv()).html(htmlSnatch);
            else
                $(objItem.getChildsDiv()).append(htmlSnatch);
        
            loopSnatches ++;
            objSnatch = objItem.getSnatch(loopSnatches);
            }
        }
    else{ // When live item not exists.
        $("#snatch_"+objLive._liveId.toString()).html("");// Empty snatches div.
        }
    }
    
function renderOpenedLive(objLive, livesDivId, index, replaceDivId){
    var htmlLive = "";
    htmlLive = _templ._arrTemplates[_openLiveTemplIdx];
    
    var loopItems = 0;
    var loopSnatches = 0;
    var loopInfoItem = 0;
    
    var objItem = null;
    var objSnatch = null;
    var objInfoItem = null;
    var replaceHTML = index == 0 ? true : false;

    htmlLive = replaceAll(htmlLive, "{STARTED}", objLive._started.toString());
    htmlLive = replaceAll(htmlLive, "{TICK}", "1");
    htmlLive = replaceAll(htmlLive, "{GAME_ID}", objLive._liveId.toString());
    htmlLive = replaceAll(htmlLive, "{START_TIME}", objLive._startDateTime);
    htmlLive = replaceAll(htmlLive, "{END_TIME}", objLive._endDateTime);
    htmlLive = replaceAll(htmlLive, "{TIME_ADD}", objLive._time_add);
    htmlLive = replaceAll(htmlLive, "{TIME_ELEM_NAME}", objLive._time_elem_name);
    htmlLive = replaceAll(htmlLive, "{CHAMPIONAT_NAME}", objLive._championat);
    htmlLive = replaceAll(htmlLive, "{TEAM1}", objLive._team1);
    htmlLive = replaceAll(htmlLive, "{TEAM2}", objLive._team2);
    if( objLive._started == 0 )
        htmlLive = replaceAll(htmlLive, "{TIME}", objLive._time);
    else
       htmlLive = replaceAll(htmlLive, "{TIME}", "");
       
    htmlLive = replaceAll(htmlLive, "{VIS}", ((objLive._stream_id == -1) ? "hidden" : "visible"));
    htmlLive = replaceAll(htmlLive, "{STREAM_ID}", objLive._stream_id.toString());
    
    /* Live info items */  
    loopInfoItem = 0;       
    objInfoItem = objLive.getInfoItem(loopInfoItem);
    while( objInfoItem != null ){
        if( objInfoItem._itemId == 0 ){
            htmlLive = replaceAll(htmlLive, "{SCORE1}", objInfoItem._itemValue1.toString());
            htmlLive = replaceAll(htmlLive, "{SCORE2}", objInfoItem._itemValue2.toString());
            }
        else
        if( objInfoItem._itemId == 1 ){
            htmlLive = replaceAll(htmlLive, "{CORNER1}", objInfoItem._itemValue1.toString());
            htmlLive = replaceAll(htmlLive, "{CORNER2}", objInfoItem._itemValue2.toString());
            }
        else
        if( objInfoItem._itemId == 2 ){
            htmlLive = replaceAll(htmlLive, "{YELLOW1}", objInfoItem._itemValue1.toString());
            htmlLive = replaceAll(htmlLive, "{YELLOW2}", objInfoItem._itemValue2.toString());
            }
        else
        if( objInfoItem._itemId == 3 ){
            htmlLive = replaceAll(htmlLive, "{RED1}", objInfoItem._itemValue1.toString());
            htmlLive = replaceAll(htmlLive, "{RED2}", objInfoItem._itemValue2.toString());
            }
            
        loopInfoItem ++;       
        objInfoItem = objLive.getInfoItem(loopInfoItem);
        }
        
    // Add live HTML.
    if( replaceHTML ){
        if( replaceDivId.length > 0 )
            $(replaceDivId).replaceWith(htmlLive);
        else
            $(livesDivId).html(htmlLive);
        replaceHTML = false;
        }
    else
        $(livesDivId).append(htmlLive);
       
    /* Live items */ 
    loopItems = 0;
    objItem = objLive.getLiveItem(loopItems);
    if( objItem == null ){
        $("#liveitems_"+objLive._liveId.toString()).html(""); // Empty div.
        }
        
    var snatchesHTML = "";
    while( objItem != null ){ // Live items loop.
        var htmlLive = "";
        if( objItem._lockState == 1 ) // Locked item.
            htmlLive = _templ._arrTemplates[_openLiveLockedItemTemplIdx];
        else
            htmlLive = _templ._arrTemplates[_openLiveItemTemplIdx];
            
        htmlLive = replaceAll(htmlLive, "{LIVE_ID}", objItem._liveItemId.toString());
        htmlLive = replaceAll(htmlLive, "{GAME_ID}", objLive._liveId.toString());
        htmlLive = replaceAll(htmlLive, "{LIVE_TEXT}", objItem._itemLabel);
        htmlLive = replaceAll(htmlLive, "{STATUS}", objItem._expanded ? "table" : "none");
        htmlLive = replaceAll(htmlLive, "{OPEN}", objItem._expanded ? "open" : "close");
        htmlLive = replaceAll(htmlLive, "{STARRED}", objItem._starred ? "enabled" : "disabled");
        // Append live item.
        if( loopItems == 0 )
            $(objLive.getChildsDiv()).html(htmlLive);
        else
            $(objLive.getChildsDiv()).append(htmlLive);
        
        loopSnatches = 0;
        objSnatch = objItem.getSnatch(loopSnatches);
        snatchesHTML = "";
        while( objSnatch != null ){
            var htmlSnatch = _templ._arrTemplates[_snatchTemplIdx];
            htmlSnatch = replaceAll(htmlSnatch, "{SNATCH_ID}", objSnatch._snatchId.toString());
            htmlSnatch = replaceAll(htmlSnatch, "{LIVE_ID}", objItem._liveItemId.toString());
            htmlSnatch = replaceAll(htmlSnatch, "{SNATCH}", objSnatch._snatchValue.toString());
            htmlSnatch = replaceAll(htmlSnatch, "{SNATCH_NAME}", objSnatch._snatchLabel.toString());
            htmlSnatch = replaceAll(htmlSnatch, "{SNATCH_CSS}", objSnatch._snatchClass);
            
            //snatchesHTML += htmlSnatch;
            // Slower methode
            // Append snatch item.
            if( loopSnatches == 0 )
                $(objItem.getChildsDiv()).html(htmlSnatch);
            else
                $(objItem.getChildsDiv()).append(htmlSnatch);
        
            loopSnatches ++;
            objSnatch = objItem.getSnatch(loopSnatches);
            }    
        
        //$(objItem.getChildsDiv()).html(snatchesHTML);
        loopItems ++;
        objItem = objLive.getLiveItem(loopItems);
        }
    }
    
function renderLive(lives, liveGameId, replaceDivId){
    if( lives == null ) return;
    var loopLives = 0;    
    var objLive = lives.getLiveById(liveGameId);

    if( objLive != null ){
        if( objLive._openState == 0 ){ // Closed live
            // closedlive_{GAME_ID}
            renderClosedLive(objLive, lives._divId, 0, replaceDivId);
            }
        else{
            // openedlive_{GAME_ID}
            renderOpenedLive(objLive, lives._divId, 0, replaceDivId);
            }
        }
    }
         
function renderLives(lives){
    if( lives == null ) return;
    var loopLives = 0;   
    lives.sortLives(); 
    var objLive = lives.getLive(loopLives);
    while( objLive != null ){
        if( objLive._openState == 0 ){ // Closed live
            renderClosedLive(objLive, lives._divId, loopLives, "");
            }
        else{
            renderOpenedLive(objLive, lives._divId, loopLives, "");
            }
 
        loopLives ++;
        objLive = lives.getLive(loopLives);
        }
    }
    
// ================================================
// User configuration info object.
// ================================================
function UserConfig(){
    function parseFromXMLString(xmlString){
        var xmlDoc = null;
        if(window.ActiveXObject){
            xmlDoc = new ActiveXObject('Microsoft.XMLDOM');
            xmlDoc.async='false';
            xmlDoc.loadXML(xmlString);
            } 
        else{
            var parser = new DOMParser();
            xmlDoc = parser.parseFromString(xmlString,'text/xml');
            }
            
        var loop = 0;
        var loopChild = 0;
        var node = null;
        var nodeChild = null;
        
        if( xmlDoc == null || 
            xmlDoc.documentElement == null || 
            xmlDoc.documentElement.tagName != "user" )
            return false;
            
            
        // Empty arrays.
        this._arrOpenedLiveIds = null;
        this._arrClosedIds = null; 
        this._arrSelSnatchIds = null; 
        this._arrStarredItemIds = null; 
        this._arrCollapsedItemIds = null;
        
        
        while( xmlDoc.documentElement.childNodes[loop] != null ){
            node = xmlDoc.documentElement.childNodes[loop];
            
            if( node.tagName == "sel_snatches" ){
                loopChild = 0;
                while( node.childNodes[loopChild] != null ){
                    nodeChild = node.childNodes[loopChild];
                    if( nodeChild.tagName == "id" )
                        this.addSelectedSnatchId(parseInt(nodeChild.attributes[0].nodeValue));
                    loopChild ++;
                    }
                }
            else
            if( node.tagName == "opened_lives" ){
                loopChild = 0;
                while( node.childNodes[loopChild] != null ){
                    nodeChild = node.childNodes[loopChild];
                    if( nodeChild.tagName == "id" )
                        this.addOpenedLiveId(parseInt(nodeChild.attributes[0].nodeValue));
                    loopChild ++;
                    }
                }    
            else
            if( node.tagName == "closed_lives" ){
                loopChild = 0;
                while( node.childNodes[loopChild] != null ){
                    nodeChild = node.childNodes[loopChild];
                    if( nodeChild.tagName == "id" ){
                        this.addClosedLiveId(parseInt(nodeChild.attributes[0].nodeValue));
                        }
                        
                    loopChild ++;
                    }
                }
            else
            if( node.tagName == "starred_items" ){
                loopChild = 0;
                while( node.childNodes[loopChild] != null ){
                    nodeChild = node.childNodes[loopChild];
                    if( nodeChild.tagName == "id" )
                        this.addStarredItemId(parseInt(nodeChild.attributes[0].nodeValue));
                    loopChild ++;
                    }
                }
            else
             if( node.tagName == "collapsed_items" ){
                loopChild = 0;
                while( node.childNodes[loopChild] != null ){
                    nodeChild = node.childNodes[loopChild];
                    if( nodeChild.tagName == "id" )
                        this.addCollapsedItemId(parseInt(nodeChild.attributes[0].nodeValue));
                    loopChild ++;
                    }
                }
            loop ++;
            }  
        return true;
        }
        
    function completeLiveGamesInfo(lives){
        if( lives == null ) return 0;
        
        var loopLives = 0;
        var loopItems = 0;
        var loopSnatches = 0;
        
        var liveObj = null;
        var liveItemObj = null;
        var snatchObj = null;
        
        while( loopLives < lives.getCount() ){
            liveObj = lives.getLive(loopLives);
            if( liveObj == null ){
                loopLives ++;
                continue;
                }
            
            // Live open state.
            liveObj._openState = 0;
            liveObj._openState = this.openedLiveIdExists(liveObj._liveId) ? 1 : 0;
            if( liveObj._openState == 1  ){
                // Needed to order opened live games when render.
                liveObj._orderId = this.openedLiveIdIndex(liveObj._liveId);
                }
            else{
                liveObj._orderId = liveObj._gameStartTimeTick;
                }
  
            loopItems = 0;
            while(loopItems < liveObj.getItemsCount()){
                liveItemObj = liveObj.getLiveItem(loopItems);
                if( liveItemObj == null ){
                    loopItems ++;
                    continue;
                    }
                    
                liveItemObj._expanded = !this.collapsedItemIdExists(liveItemObj._liveItemId);
                liveItemObj._starred = this.starredItemIdExists(liveItemObj._liveItemId); 
                    
                loopSnatches = 0;
                while( loopSnatches < liveItemObj.getSnatchCount() ){
                    snatchObj = liveItemObj.getSnatch(loopSnatches);
                    if( snatchObj == null ){
                        loopSnatches ++;
                        continue;
                        }
                    snatchObj._snatchClass = this.snatchIdExists(snatchObj._snatchId) ? "b1_hover" : "b1";    
                    loopSnatches ++;
                    }
                loopItems ++;
                } 
            loopLives ++;
            }
        return loopLives;
        }
        
    function addSelectedSnatchId(snatchId){
        if( this._arrSelSnatchIds == null )
            this._arrSelSnatchIds = new Array();
        this._arrSelSnatchIds[this._arrSelSnatchIds.length] = snatchId;
        }
    
    function getSelectedSnatchId(index){
        if( this._arrSelSnatchIds == null || index >= this._arrSelSnatchIds.length )
            return 0;
        return this._arrSelSnatchIds[index];
        }
        
    function snatchIdExists(snatchId){
        if( this._arrSelSnatchIds == null )
            return false;
            
        var loop = 0;
        while( loop < this._arrSelSnatchIds.length ){
            if( snatchId == this._arrSelSnatchIds[loop] )
                return true;
            loop ++;
            }
        return false;
        }
        
    function snatchIdIndex(snatchId){
        if( this._arrSelSnatchIds == null )
            return -1;
            
        var loop = 0;
        while( loop < this._arrSelSnatchIds.length ){
            if( snatchId == this._arrSelSnatchIds[loop] )
                return loop;
            loop ++;
            }
        return -1;
        }
        
    function addStarredItemId(itemId){
        if( this._arrStarredItemIds == null )
            this._arrStarredItemIds = new Array();
            alert(itemId);
        this._arrStarredItemIds[this._arrStarredItemIds.length] = itemId;
        }
    
    function getStarredItemId(index){
        if( this._arrStarredItemIds == null || index >= this._arrStarredItemIds.length )
            return 0;
        return this._arrStarredItemIds[index];
        }
        
    function starredItemIdExists(itemId){
        if( this._arrStarredItemIds == null )
            return false;
            
        var loop = 0;
        while( loop < this._arrStarredItemIds.length ){
            if( itemId == this._arrStarredItemIds[loop] )
                return true;
            loop ++;
            }
        return false;
        }
        
    function addCollapsedItemId(itemId){
        if( this._arrCollapsedItemIds == null )
            this._arrCollapsedItemIds = new Array();
        this._arrCollapsedItemIds[this._arrCollapsedItemIds.length] = itemId;
        }
    
    function getCollapsedItemId(index){
        if( this._arrCollapsedItemIds == null || index >= this._arrCollapsedItemIds.length )
            return 0;
        return this._arrCollapsedItemIds[index];
        }
        
    function collapsedItemIdExists(itemId){
        if( this._arrCollapsedItemIds == null )
            return false;
            
        var loop = 0;
        while( loop < this._arrCollapsedItemIds.length ){
            if( itemId == this._arrCollapsedItemIds[loop] )
                return true;
            loop ++;
            }
        return false;
        }
        
    function addOpenedLiveId(liveId){
        if( this._arrOpenedLiveIds == null )
            this._arrOpenedLiveIds = new Array();
        this._arrOpenedLiveIds[this._arrOpenedLiveIds.length] = liveId;
        }
        
    function openedLiveIdIndex(liveId){
        if( this._arrOpenedLiveIds == null )
            return -1;
            
        var loop = 0;
        while( loop < this._arrOpenedLiveIds.length ){
            if( liveId == this._arrOpenedLiveIds[loop] )
                return loop;
            loop ++;
            }
        return -1;
        }
            
    function openedLiveIdExists(liveId){
        if( this._arrOpenedLiveIds == null )
            return false;
            
        var loop = 0;
        while( loop < this._arrOpenedLiveIds.length ){
            if( liveId == this._arrOpenedLiveIds[loop] )
                return true;
            loop ++;
            }
        return false;
        }
        
    function getOpenedLiveId(index){
        if( this._arrOpenedLiveIds == null || index >= this._arrOpenedLiveIds.length )
            return 0;
        return this._arrOpenedLiveIds[index];
        }
        
    function addClosedLiveId(liveId){
        if( this._arrClosedIds == null )
            this._arrClosedIds = new Array();
        this._arrClosedIds[this._arrClosedIds.length] = liveId;
        }
    
    function closedLiveIdExists(liveId){
        if( this._arrClosedIds == null )
            return false;
            
        var loop = 0;
        while( loop < this._arrClosedIds.length ){
            if( liveId == this._arrClosedIds[loop] )
                return true;
            loop ++;
            }
        return false;
        }
        
    function closedLiveIdIndex(liveId){
        if( this._arrClosedIds == null )
            return -1;
            
        var loop = 0;
        while( loop < this._arrClosedIds.length ){
            if( liveId == this._arrClosedIds[loop] )
                return loop;
            loop ++;
            }
        return -1;
        }
        
    function getClosedLiveId(index){
        if( this._arrClosedIds == null || index >= this._arrClosedIds.length )
            return 0;
        return this._arrClosedIds[index];
        }
        
    function removeSnatch(snatchId){
         if( this._arrSelSnatchIds == null || this._arrSelSnatchIds.length == 0 )
            return;
         
         $("#snatch_" + snatchId.toString()).removeClass("b1_hover").addClass("b1");
         $("#snatch_" + snatchId.toString()).removeClass("ac_hover").addClass("ac");   
         var index = snatchIdIndex(snatchId);
         if( index != -1 ){
            this._arrSelSnatchIds.splice (index, 1);
            }
        }
            
    function clearSnatches(){
        if( this._arrSelSnatchIds == null || this._arrSelSnatchIds.length == 0 )
            return;
        var loop = 0;
        while( loop < this._arrSelSnatchIds.length ){
            $("#snatch_" + this._arrSelSnatchIds[loop].toString()).removeClass("b1_hover").addClass("b1");
            $("#snatch_" + this._arrSelSnatchIds[loop].toString()).removeClass("ac_hover").addClass("ac");
            loop ++;
            }
            
        this._arrSelSnatchIds = null;
        }
        
    function openLive(liveGames, liveGameId){ 
        if( liveGames == null ) return false;
        var index = this.closedLiveIdIndex(liveGameId);
        if( index == -1 ) return false;
        
        var liveGame = liveGames.getLiveById(liveGameId);
        if( liveGame == null )
            return false;

        var loop = 0;    
        var liveGameObj = null;
        var orderId = 0;
        
        if( this._arrOpenedLiveIds != null ){
            while( loop != this._arrOpenedLiveIds.length ){
                liveGameObj = liveGames.getLiveById(liveGameId);
                if( liveGameObj != liveGame ){
                    liveGameObj._orderId = loop + 1;
                    }
                loop ++;
                }
            }
            
         // Remove from array.
        this._arrClosedIds.splice(index, 1);
        // Add as first element of array.
        if( this._arrOpenedLiveIds != null ){
            loop = this._arrOpenedLiveIds.length - 1;
            while( loop >= 0 ){
                this._arrOpenedLiveIds[loop + 1] = this._arrOpenedLiveIds[loop];
                loop --;
                }
            }
        else
            this._arrOpenedLiveIds = new Array();
        // Add live id as closed live.
        this._arrOpenedLiveIds[0] = liveGameId;
            
        liveGame._orderId = orderId;    
        liveGame._openState = 1; 
        return true;
        }
        
    function closeLive(liveGames, liveGameId){
        if( liveGames == null ) return false;
        var index = this.openedLiveIdIndex(liveGameId);
        if( index == -1 ) return false;
        
        var liveGame = liveGames.getLiveById(liveGameId);
        if( liveGame == null )
            return false;
        
        // Remove from array.
        if( this._arrOpenedLiveIds != null )
            this._arrOpenedLiveIds.splice(index, 1);
        // Add live id as closed live.
        if( this._arrClosedIds == null )
            this._arrClosedIds = new Array();
        this._arrClosedIds[this._arrClosedIds.length] = liveGameId;
        
        liveGame._orderId = liveGame._gameStartTimeTick;
        liveGame._openState = 0;
        return true;
        } 
        
    // Variables
    this._arrOpenedLiveIds = null; // Selected snatch ids.
    this._arrClosedIds = null; // Selected snatch ids.
    this._arrSelSnatchIds = null; // Selected snatch ids.
    this._arrStarredItemIds = null; // Starred live item ids ids.
    this._arrCollapsedItemIds = null; // Collapsed live item ids. (all others are expanded)
    
    // Methods
    this.addOpenedLiveId = addOpenedLiveId;
    this.getOpenedLiveId = getOpenedLiveId;
    this.addClosedLiveId = addClosedLiveId;
    this.getClosedLiveId = getClosedLiveId;
    
    this.addSelectedSnatchId = addSelectedSnatchId;
    this.getSelectedSnatchId = getSelectedSnatchId;
    this.snatchIdExists = snatchIdExists;
    
    this.addStarredItemId = addStarredItemId;
    this.getStarredItemId = getStarredItemId;
    this.starredItemIdExists = starredItemIdExists;
    
    this.addCollapsedItemId = addCollapsedItemId;
    this.getCollapsedItemId = getCollapsedItemId;
    this.collapsedItemIdExists = collapsedItemIdExists;
    this.completeLiveGamesInfo = completeLiveGamesInfo;
    this.closedLiveIdExists = closedLiveIdExists;
    this.openedLiveIdExists = openedLiveIdExists;
    this.openedLiveIdIndex = openedLiveIdIndex;
    this.closedLiveIdIndex = closedLiveIdIndex;
    
    this.parseFromXMLString = parseFromXMLString;
    this.clearSnatches = clearSnatches;
    this.snatchIdIndex = snatchIdIndex;
    this.removeSnatch = removeSnatch;
    this.openLive = openLive;
    this.closeLive = closeLive;
    }
