var youtazpod = null;
var ptimeout = null;
var lst = new Array();
var path = 'FrAsTas5kUt4';
var reg=new RegExp("\.mp3$", "gi");
var crossdomain=new RegExp("\.youtaz\.net$", "gi");
var frameregexp = /\/f\.php$/;

function GenPlaylistData() {
	if (!document.getElementsByTagName) return false;
	if (!document.getElementById("placeholder1")) return false;
        var links = document.getElementsByTagName("a");
        var j=0;
        for (var i=0; i < links.length; i++) {
                if(links[i].href.match(reg) && links[i].hostname.match(crossdomain)){
			var newlink = null;
			if(links[i].href.indexOf('content.youtaz.net', 0) > -1){
				newlink = links[i].href.substr(0,links[i].href.indexOf('content.youtaz.net', 0)+19)+path+'/'+links[i].href.substr(links[i].href.indexOf('content.youtaz.net', 0)+19, links[i].href.length);
			}
                        lst[j] = {
                                file:newlink,
                                title:links[i].firstChild.nodeValue,
                                type:"mp3"
                                }
			links[i].href="javascript:pod("+j+")";
			j++;
                }
        }
        if(lst.length > 0) { createPlayer(); }
}

function createPlayer() {
	document.getElementById("placeholder1").innerHTML = '<a href="http://www.adobe.com/go/getflashplayer">Get flash</a> to see this player';
        var flashvars = {
                width:"380",
                height:"40",
                playlistsize:"20",
                autostart:"false",
                shuffle:"false",
                repeat:"list",
                playlist:"bottom"
        }

        var params = {
                allowfullscreen:"false",
                allowscriptaccess:"always"
        }

        var attributes = {
                id:"youtazpod",
                name:"youtazpod"
        }
        swfobject.embedSWF("/swf/player.swf", "placeholder1", "380", "40", "9.0.115", false, flashvars, params, attributes);
}

function playerReady(thePlayer) {
        youtazpod = document.getElementById(thePlayer.id);
        youtazpod.sendEvent('LOAD', lst);
        youtazpod.addModelListener("STATE", "stateListener");
}

function stateListener(obj) { //IDLE, BUFFERING, PLAYING, PAUSED, COMPLETED
	//alert(obj.oldstate+'->'+obj.newstate);
	if (typeof(ptimeout) != "undefined") clearTimeout(ptimeout);
	if((obj.oldstate=="PLAYING" && obj.newstate=="COMPLETED")||(obj.oldstate=="PLAYING" && obj.newstate=="IDLE")) ptimeout = setTimeout("replayYoutaz()",5000);
}

function replayYoutaz(){
        if (frameregexp.test(window.parent.location.pathname) == true) {
		if (typeof(parent.frames['ytplayer'].player) == "undefined") exit;
		parent.frames['ytplayer'].v=0; parent.frames['ytplayer'].voluleflag=false;
        	parent.frames['ytplayer'].player.sendEvent('volume', parent.frames['ytplayer'].v);
		parent.frames['ytplayer'].player.sendEvent('PLAY');
		//parent.frames['ytplayer'].fadein();
        }
}

function pod(podid){
	if (frameregexp.test(window.parent.location.pathname) == true) {
		if (typeof(parent.frames['ytplayer'].player) != "undefined") parent.frames['ytplayer'].player.sendEvent('STOP');
	}
	youtazpod.sendEvent('ITEM',podid);
}

