var LoadingScreen = 
{
	show: function()
    {
		// Create Loading div
		this.create_div();

		// Set initial position				
		this.set_position();

		// Attach event handler to onScroll to keep in position
		addEventHandler(window, 'scroll', this.set_position);
	},

	hide: function()
    {
		var div = document.getElementById('loading_message');
		if (div == null || div == '' || typeof(div) != 'object') { return false; }

		document.body.removeChild(div);
	},

	set_position: function()
    {
		var div = document.getElementById('loading_message');
		if (div == null || div == '' || typeof(div) != 'object') { return false; }
		
		// Calculate position
		var left = f_scrollLeft();
		var top = f_scrollTop();
			
		// Set position
		div.style.position = 'absolute';
		div.style.left = left + 'px';
		div.style.top = top + 'px';
	},

	create_div: function()
    {
        // Remove existing div
        this.hide();
        
		var div = document.createElement('DIV');
		div.id = 'loading_message';
		
		div.style.opacity = 0.7;
        div.style.MozOpacity = 0.7;
        div.style.filter='alpha(opacity=70)';
        div.style.zIndex = 50;        
		
		div.style.width = '100%';
		div.style.padding = '5px 0 7px 0';
		div.style.textAlign = 'center';
		div.style.background = '#666';
		div.style.color = '#FAFAFA';
		div.style.fontSize = '.9em';
		
		div.innerHTML = 'Loading&hellip;';
		document.body.appendChild(div);
		return div;
	}
}

var AlertScreen = 
{
	show: function()
    {
		// Create Loading div
		this.create_div();

		// Set initial position				
		this.set_position();

		// Attach event handler to onScroll to keep in position
		addEventHandler(window, 'scroll', this.set_position);
	},

	hide: function()
    {
		var div = document.getElementById('alert_message');
		if (div == null || div == '' || typeof(div) != 'object') { return false; }

		document.body.removeChild(div);
	},

	set_position: function()
    {
		var div = document.getElementById('alert_message');
		if (div == null || div == '' || typeof(div) != 'object') { return false; }

		// Calculate position
		var left = f_clientWidth() / 2 + f_scrollLeft() - 190;
		var top = f_clientHeight() / 2 + f_scrollTop() - 20;
			
		// Set position
		div.style.position = 'absolute';
		div.style.left = left + 'px';
		div.style.top = top + 'px';
	},

	create_div: function()
    {
        // Remove existing div
        this.hide();
        
		var div = document.createElement('DIV');
		div.id = 'alert_message';
		div.style.background = "#FFFFFF";
		div.style.border = "1px solid #224488";
		div.style.padding = "1px";
		div.innerHTML = '<table class="alert-box">' +
        '<tr><th class="alert-title">Alert</th><th class="alert-title" style="text-align: right"><img onclick="AlertScreen.hide()" class="clickable" src="img/close.gif" alt="Close" /></th></tr>' +
        '<tr><td colspan="2" class="alert-message">Login or Register</td></tr>' + 
        '</table>';
		document.body.appendChild(div);
		return div;
	}
}

function f_clientHeight() 
{
	return this.f_filterResults 
    (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}
    
function f_clientWidth() 
{
	return this.f_filterResults 
    (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}

function f_scrollLeft()
{
	return this.f_filterResults 
    (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}

function f_scrollTop()
{
	return this.f_filterResults
    (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}

function f_filterResults(n_win, n_docel, n_body)
{
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
	n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

function addEventHandler(oTarget, sEventType, fnHandler)
{
	if (oTarget.addEventListener)
		oTarget.addEventListener(sEventType, fnHandler, false);
    else if (oTarget.attachEvent)
		oTarget.attachEvent("on" + sEventType, fnHandler);
	else
		oTarget["on" + sEventType] = fnHandler;
}

function createXMLHttpRequest() 
{
    var ua;
    
    if(window.XMLHttpRequest) 
        ua = new XMLHttpRequest();
    else if(window.ActiveXObject) 
        ua = new ActiveXObject("Microsoft.XMLHTTP");

    return ua;
}

function rate_album(album_id, rating)
{
    function handleResponse() 
    {
        if(req.readyState == 4)
        {
            var user_rating = document.getElementById('album_user_rating_' + album_id);
            user_rating.style.width = rating * 20 + '%';
            user_rating.innerHTML = rating;
    
            var response = req.responseText.split("|");
            
            var average_rating = document.getElementById('album_average_rating_' + album_id)
            if(average_rating)
            {
                average_rating.innerHTML = response[0];
            }
            
            var num_votes = document.getElementById('album_num_votes_' + album_id)
            if(num_votes)
            {
                num_votes.innerHTML = response[1];
            }
                
            var average_rating_bar = document.getElementById('album_average_rating_bar_' + album_id)
            if(average_rating_bar)
            {
                var width = response[0] * 20;
                if(width < 20)
                {
                    width = 20;
                }
                average_rating_bar.style.width = width + 'px';
                average_rating_bar.innerHTML = response[0];
            }

            LoadingScreen.hide();
        }
    }

    LoadingScreen.show();
    var req = createXMLHttpRequest();
    req.open('get', 'ajax.php?function=rate_album&album_id=' + album_id + '&rating=' + rating);
    req.onreadystatechange = handleResponse;
    req.send(null);
}

function rate_song(song_id, rating) 
{
    function handleResponse() 
    {
        if(req.readyState == 4)
        {
            var user_rating = document.getElementById('song_user_rating_' + song_id);
            user_rating.style.width = rating * 20 + '%';
            user_rating.innerHTML = rating;
    
            var response = req.responseText.split("|");
            
            var average_rating = document.getElementById('song_average_rating_' + song_id)
            if(average_rating)
            {
                average_rating.innerHTML = response[0];
            }
            
            var num_votes = document.getElementById('song_num_votes_' + song_id)
            if(num_votes)
            {
                num_votes.innerHTML = response[1];
            }
                
            var average_rating_bar = document.getElementById('song_average_rating_bar_' + song_id)
            if(average_rating_bar)
            {
                var width = response[0] * 20;
                if(width < 20)
                {
                    width = 20;
                }
                average_rating_bar.style.width = width + 'px';
                average_rating_bar.innerHTML = response[0];
            }
            
            LoadingScreen.hide();
        }
    }
    
    LoadingScreen.show();
    var req = createXMLHttpRequest();
    req.open('get', 'ajax.php?function=rate_song&song_id=' + song_id + '&rating=' + rating);
    req.onreadystatechange = handleResponse;
    req.send(null);
}

function update_artist_list(letter) 
{
    function handleResponse() 
    {
        if(req.readyState == 4)
        {
            var artist_list = document.getElementById('artist_list');
            var response = req.responseText;
            
            if(document.all)
            {
                response = '<option>&nbps;</option>' + response;
            }
            artist_list.innerHTML = response;
            if(document.all)
            {
                artist_list.outerHTML = artist_list.outerHTML;
            }
            
            var artist_list = document.getElementById('artist_list');
            var artist = document.getElementById('artist');
            artist.firstChild.href = 'showArtist.php?artist_id='+artist_list.options[artist_list.selectedIndex].value;
            
            LoadingScreen.hide();
        }
    }
    
    LoadingScreen.show();
    var req = createXMLHttpRequest();
    req.open('get', 'ajax.php?function=update_artist_list&letter=' + letter);
    req.onreadystatechange = handleResponse;
    req.send(null);
}

function show_video(url, autoplay)
{
    if(autoplay == 1)
        url = url + "&autoplay=1";
        
    var so = new SWFObject(url, "embedded_video", "300", "250", "7", "#FFFFFF");
    so.write("flashcontent");
}

function update_content(id, text)
{
    document.getElementById(id).innerHTML = text;
}

function disable_forms()
{
    for (i = 0; i < document.forms.length; i++)
    {
        for(j = 0; j < document.forms[i].length; j++)
        { 
            document.forms[i].elements[j].disabled = true;
        }
    }
}