Number.prototype.NaN0 = function ()
{
    return isNaN(this) ? 0 : this;
}

if (!Array.prototype.push)
{
    Array.prototype.push = function (item)
    {
        this[this.length] = item;
    }
}

if (!Array.prototype.indexOf)
{
  Array.prototype.indexOf = function(elt /*, from*/)
  {
    var len = this.length;

    var from = Number(arguments[1]) || 0;
    from = (from < 0)
         ? Math.ceil(from)
         : Math.floor(from);
    if (from < 0)
      from += len;

    for (; from < len; from++)
    {
      if (from in this &&
          this[from] === elt)
        return from;
    }
    return -1;
  };
}

var currentWidth = 0;
var player = null;
var loading = null;

function addEvent(o, e, m)
{
	o.addEventListener(e, m, false);
}

function checkOrientAndLocation()
{

    if (window.innerWidth != currentWidth)
    {   
        currentWidth = window.innerWidth;
        var orient = currentWidth == 480 ? "landscape" : "profile";
        document.body.setAttribute("orient", orient);
		setTimeout(scrollTo, 0, 0, 1);
		//setTimeout(scrollTo, 100, 0, 1);
    }
}


function play(no, baidu)
{
	if (!loading)
	{
		createLoading();
		document.getElementById("player_" + no).appendChild(loading);
	}

	var data = "url=" + encodeURIComponent(baidu);
	downloadUrl('url.php', function (data, code) {
		loading.parentNode.removeChild(loading);
		loading = null;
		if (data.substr(0, 4).toUpperCase() != 'HTTP' && data.substr(0, 3).toUpperCase() != 'FTP')
		{			
			return;
		}

		if (!player)
		{
			createPlayer();
		}

		player.src = data;
		
		document.getElementById("player_" + no).appendChild(player);
	
	}, data);
}

function createPlayer()
{
	player = document.createElement('embed');
	player.className = 'player';
	player.type = 'audio/mpeg';
	player.src = 'about:blank';
}

function createLoading()
{
	loading = document.createElement('img');
	loading.className = 'loading_img';
	loading.src = 'misc/loading.gif';
}

function init()
{
	setTimeout(scrollTo, 0, 0, 1);
    setTimeout(checkOrientAndLocation, 0);
    checkTimer = setInterval(checkOrientAndLocation, 300);
}

addEvent(window, 'load', init);

function downloadUrl(url, callback, data)
{
    // init
    url += url.indexOf("?") >= 0 ? "&" : "?";
    url += "random_download_url=" + Math.random();
    
    if (typeof data == 'undefined')
    {
        var data = null;
    }

    method = data ? 'POST' : 'GET';
    
	// create XMLHttpRequest object
	if (window.XMLHttpRequest)
	{
		var objXMLHttpRequest = new XMLHttpRequest();
	}
	else
	{
		var MSXML = ['Msxml2.XMLHTTP.6.0', 'Msxml2.XMLHTTP.3.0'];
		for(var n = 0; n < MSXML.length; n ++)
		{
			try
			{
				var objXMLHttpRequest = new ActiveXObject(MSXML[n]);        
				break;
			}
			catch(e)
			{
			}
		}
	}
    
	// send request
    with(objXMLHttpRequest)
    {
        //setTimeouts(30*1000,30*1000,30*1000,30*60*1000);
        try
        {
            open(method, url, true);
            
            if (method == 'POST')
            {
                setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
            }
            
            send(data);            
        }
        catch(e)
		{
			alert(e);
        }
        
		// on ready
        onreadystatechange = function()
        {
            if (objXMLHttpRequest.readyState == 4)
            {
                callback(objXMLHttpRequest.responseText, objXMLHttpRequest.status);
				delete(objXMLHttpRequest);
            }
        }
    }
}
