onDomReady(function () {
	// init
	homepage.initialize();
});
function parseLive(lis) {
    homepage.parseLive(lis);
}
var homepage = {
	matrix: new Array(),
	waiting: new Array(),
	initialize: function()
    {
		this.initMatrix();
		this.parseData($("moviesData"));
        header.liveCallback = parseLive;
	},
	initMatrix: function()
    {
		this.matrix = new Array();
		for (var r=0;r<4;r++)
        {
			this.matrix[r] = new Array();
			for (var c=0;c<5;c++) this.matrix[r][c] = null;
		}
	},
    parseLive: function(lis) {
        
        var newLiveMovies = new Array();
        for (var i=0;i<lis.length;i++)
        {
            var id = lis[i].getElementsByTagName("id")[0].firstChild.nodeValue;
            newLiveMovies[id] = {_obj:lis[i], _found:false}
        }
        var oldMovies = document.getElementsByTagName("td");
        for (var i=0;i<oldMovies.length;i++) {
            if (oldMovies[i]._movieId && oldMovies[i]._isLive) {
                try {
                    changeStatus(oldMovies[i], (newLiveMovies[oldMovies[i]._movieId]) ? true : false);
                    newLiveMovies[oldMovies[i]._movieId]._found = true;
                } catch (e) {
                }
            }
        }
        var redraw = false;
        for (var i in newLiveMovies) {
            if ((i+"").match(/^[0-9]*$/)) {
                if (!newLiveMovies[i]._found) {
                  this.pushImages(new Item(newLiveMovies[i]._obj,true));  
                  redraw = true;
                }
            }
        }
        if (redraw) this.drawData(false);                       
        
        
    },
    pushImages: function(movie) {
        this.sortedMovies.splice(0, 0, movie);
        this.sortedMovies.splice(13, 1);
    },
    drawData: function(create) {
        for (var i=0;i<this.sortedMovies.length;i++) {
            this.sortedMovies[i].y = Math.floor(i/5);
            this.sortedMovies[i].x = i%5;
            this.sortedMovies[i].draw(create);
        }
    },
    sortedMovies: new Array(),
	parseData: function(data) {
//    debugger;
        //var divs = data.getElementsByTagName("div");     
        //if (typeof(divs[0]) != "undefined" && divs[0].firstChild.nodeValue != "undefined") this.IMAGE_URL = divs[0].firstChild.nodeValue;
        //debugger;
		var items = data.getElementsByTagName("li");
		for (var i=0;i<items.length;i++) {
			var item = new Item(items[i],false);
            this.sortedMovies.push(item);
/*            
			if (this.matrix[item.y][item.x] == null) {
				// empty spot. show item in it.
				item.draw(true);
				this.matrix[item.y][item.x] = item;
			} else if (this.matrix[item.y][item.x].id != item.id) {
				// spot was changed. replace it.
				item.reset();
				this.matrix[item.y][item.x] = item;
				this.waiting.push(item);
//				item.draw();
			} else {
				// check if live status was changed
				if (item.isLive == null || item.isLive == "0") item.changeLiveStatus();
			}
*/
		}
        this.drawData(true);
		window.setTimeout(homepage.handleWaiting, 0);
        //window.setTimeout(this.getData, 15000);
	},
	getData: function() {
		var poster = new Ajax("/main/Data", {method:'get', onComplete:function() {
			homepage.parseData(this.transport.responseXML.documentElement);
		}}).request();
	},
	play: function() {
		if (this._isMovie == "1") document.location.href = "/watch/"+this._movieId;
		else document.location.href = "/userprofile/index?userId="+ this._movieId;
	},
	handleWaiting: function() {
		for (var i=0;i<homepage.waiting.length;i++) {
			homepage.waiting[i].draw();
		}
		homepage.waiting = new Array();
	}
}

function Item(node,isLive) {
    
    this.isMovie = true;

    if(isLive == false)
    {
        this.id = node.getAttribute("id");
        this.x = node.getAttribute("x");
        this.y = node.getAttribute("y");
        this.isLive = false;
        this.firstLine = node.getElementsByTagName("span")[0].firstChild.nodeValue;
        this.secondLine = node.getElementsByTagName("span")[1].firstChild.nodeValue;
        this.image = node.getElementsByTagName("span")[2].firstChild.nodeValue;
    }
    else
    {
        this.id = node.getElementsByTagName("id")[0].firstChild.nodeValue;
        this.x = 0;
        this.y = 0;
        this.isLive = true;
        this.firstLine = node.getElementsByTagName("title")[0].firstChild.nodeValue;
        this.secondLine = node.getElementsByTagName("username")[0].firstChild.nodeValue;
        this.image = node.getElementsByTagName("bt")[0].firstChild.nodeValue;
    }
}
Item.prototype.changeLiveStatus = function() {
	var tr = $("homepageGrid").getElementsByTagName("tr")[this.y*2];
	var td = tr.getElementsByTagName("td")[this.x*2];
	var divs = td.getElementsByTagName("div");
	if (divs[0].className == "videoBoxLive") {
		divs[0].className = (this.isLive && this.isLive == "1") ? "videoBoxLive" : "videoBox";
		var nobrs = td.getElementsByTagName("nobr");
		nobrs[0].className = (this.isLive && this.isLive == "1") ? "liveColor" : "";
		nobrs[1].className = (this.isLive && this.isLive == "1") ? "liveColor" : "notliveColor";
	}
}
Item.prototype.reset = function() {
	var tr = $("homepageGrid").getElementsByTagName("tr")[this.y*2];
	var t = this;
	var td = tr.getElementsByTagName("td")[this.x*2];
	var divs = td.getElementsByTagName("div");
	var img = divs[0].getElementsByTagName("img")[0];
	img.style.visibility = "hidden";
	divs[0].className = "videoBox";
	var nobrs = td.getElementsByTagName("nobr");
	nobrs[0].className = "";
	nobrs[1].className = "notliveColor";
	nobrs[0].innerHTML = "";
	nobrs[2].innerHTML = "";
}
Item.prototype.draw = function(create) {
	if (typeof(create) == "undefined") create = false;
	var tr = $("homepageGrid").getElementsByTagName("tr")[this.y*2];
	var t = this;
	var td = tr.getElementsByTagName("td")[this.x*2];
	td._movieId = this.id;
	td._isMovie = this.isMovie;
    td._isLive = (this.isLive && this.isLive == "1");
	td.onclick = homepage.play;
	var divs = td.getElementsByTagName("div");
	var img = create ? divs[0].appendChild(document.createElement("img")) : divs[0].getElementsByTagName("img")[0];
	img.style.visibility = "visible";
	img.src = this.image; //homepage.IMAGE_URL.replace(/\[SID\]/, this.id)+'&size=1'  : homepage.BROADCASTER_URL.replace(/\[UID\]/, this.id);
	//debugger;
    if (create) {
		img.width = "107"
		img.height = "80";
	}

	divs[0].className = (this.isLive && this.isLive == "1") ? "videoBoxLive" : "videoBox";
	var nobrs = td.getElementsByTagName("nobr");
	nobrs[0].className = (this.isLive && this.isLive == "1") ? "liveColor" : "";
	nobrs[1].className = (this.isLive && this.isLive == "1") ? "liveColor" : "notliveColor";
	nobrs[0].innerHTML = this.firstLine;
	nobrs[2].innerHTML = this.secondLine;
   // nobrs[2].style.color = "#FCD116"; // make the second line yellow
    nobrs[2].style.color = "#003F87"; // make the second line blue
    nobrs[2].href = "/"+this.secondLine; // make it reference to the user profile page

 }

function changeStatus(td, isLive) {
    var divs = td.getElementsByTagName("div");
    divs[0].className = (isLive) ? "videoBoxLive" : "videoBox";
    var nobrs = td.getElementsByTagName("nobr");
    nobrs[0].className = (isLive) ? "liveColor" : "";
    nobrs[1].className = (isLive) ? "liveColor" : "notliveColor";
    
    td._isLive = isLive;
}
