function sortBlogs(sortby, cur_sort, cur_type)
{
	var form = $('sortForm');
	cur_type = cur_type.toLowerCase();
	if (sortby == cur_sort)
	{
		form.sort.value = cur_sort;
		form.type.value = (cur_type == 'asc' ? 'desc' : 'asc');
	}
	else if (sortby != cur_sort)
	{
		form.sort.value = sortby;
		form.type.value = cur_type;
	}
	form.submit();
}

function search(root, defValue, _form)
{
	if( !_form ) _form = 'searchForm';
		var form = $( _form );

	if( form.keyword.value != defValue && form.keyword.value != '' )
	{
	    var keyword = form.keyword.value;
	    keyword = keyword.replace(/\s+/g, '_');
		/*form.action = root + '/search.html?keyword=' + keyword + '&page=1';*/
		form.action = root + '/search/' + keyword + '/1/';
		form.submit();
		return true;
	}

	return false;
}

function validateColor( color )
{
	if( color != '' && color.match( /[0-9A-Fa-f]{3-6}/i ) )
		return true;
	return false;
}

function play(url, skip)
{
	window.open('/xspf.html?url='+url+(skip ? '&skip='+skip : ''), '_blank', 'height=230, width=520');
}

function go( url )
{
	if ( url )
		document.location.href=url;
}

function setCheck(state, frmID)
{
	if (!frmID) frmID = 'mainform';
	var list = $(frmID).getElementsByClassName('checkbox');
	for(var i=0; i<list.length; i++)
		list[i].checked = state;
}

function playList( baseURL )
{
    this.baseURL            = baseURL;

    this.setStatus = function(mes)
    {
        if ( !mes )
            mes = '<img src="/img/loading.gif" alt="loading" align="absmiddle" />'
        $('systemMessage').update(mes);
        Element.show('systemMessage');
        setTimeout("Element.hide('systemMessage')", 1000);
    }

    this.getSideBar = function()
    {
        this._play();
        this.updatePlaylist();
    }

    this.addToPlaylist = function(id)
    {
        new Ajax.Request(
            this.baseURL + '/ajax/add_to_playlist.html?id=' + id,
            {
                method: 'get',
                onCreate : function()
                {
                   playList.setStatus();
                },
                onSuccess: function(resp){
                    if ( resp.responseText == 'ok' )
                    {
                        playList.setStatus('Added successfully');
                        playList.updatePlaylist();
                    }
                    else
                        playList.setStatus('Already exists in your playlist');
                }
            }
        );
    }

    this.updatePlaylist = function( id )
    {
        if (!id)
            id = 0;

        new Ajax.Updater
        (
            'div_PlayList',
            this.baseURL + '/ajax/get_playlist_tracks.html' + ( id ? '?id=' + id : ''),
            {
                method: 'get',
                onSuccess : function(){
                        playList.getControls();
                        if ( id )
                            playList._play();
                },
                evalScripts : true
            }
        )
    }

    this.updateOrder = function(element)
    {
        var seq = Sortable.sequence(element);
        if ( seq == '' || !seq )
            return false;

        new Ajax.Request(
            this.baseURL + '/ajax/update_tracks_order.html?seq=' + seq,
            {
                method : 'get',
                onComplete : function()
                {
                    playList.updatePlaylist()
                }
            }
        );
    }

    this.savePlaylist = function( title )
    {
        if ( !title ) title = '';
        new Ajax.Request(
            this.baseURL + '/ajax/save_playlist.html' + (title != '' ? '?name=' + escape( title ) : '' ),
            {
                method: 'get',
                onComplete : function()
                {
                    playList.updatePlaylist();
                    playList._play();
                    return true;
                }
            }
        );
    }

    this.getControls = function()
    {
        new Ajax.Updater(
            'div_Controls',
            this.baseURL + '/ajax/get_controls.html',
            {
                method: 'get',
                evalScripts: true
            }
        );
    }

    this._delete = function()
    {
        var plId = $('selectPlaylist').value;
        new Ajax.Request(
            this.baseURL + '/ajax/delete_playlist.html?id=' + plId,
            {
                method: 'get',
                onComplete: function()
                {
                    playList.updatePlaylist();
                    playList._play();
                }
            }
        )
    }

    this._new = function()
    {
        alert('new');
        new Ajax.Request(
            this.baseURL + '/ajax/new_playlist.html',
            {
                method: 'get',
                onComplete : function()
                {
                    this.getControls();
                    this.updatePlaylist();
                }
            }
        );
    }

    this._play = function( skip )
    {
        if ( !skip )
            skip = 0;
        new Ajax.Updater(
            'div_Player',
            this.baseURL + '/ajax/get_player.html?skip=' + skip,
            {
                method: 'get'
            }
        );
    }

    this.deleteFromPlaylist = function( id )
    {
        new Ajax.Request(
            this.baseURL + '/ajax/delete_from_playlist.html?id=' + id,
            {
                method : 'get',
                onComplete : function(){ playList.updatePlaylist(); }
            }
        );
    }

    this.newPlaylist = function()
    {
        new Ajax.Request(
            this.baseURL + '/ajax/new_playlist.html',
            {
                method : 'get',
                onComplete : function()
                {
                    playList.updatePlaylist();
                    playList._play();
                }
            }
        );
    }

    this.shareForm = function(id)
    {
        new Ajax.Updater(
            'div_Share_or_Embed',
            this.baseURL + '/ajax/get_share_form.html',
            {
                method : 'get',
                onComplete : function(){
                    playList.calculateShareFormPosition();
                    $('share_form_plList_id').value=id;
                    Element.show('div_Share_or_Embed');
                }
            }
        )

    }

    this.hideShareOrEmbed = function()
    {
        var el = $('div_Share_or_Embed');
        el.update();
        Element.hide(el);
    }

    this.getEmbed = function(id)
    {
        new Ajax.Updater(
            'div_Share_or_Embed',
            this.baseURL + '/ajax/get_embed.html?id=' + id,
            {
                method : 'get',
                onComplete : function(){
                    playList.calculateShareFormPosition('div_Share_or_Embed');
                    Element.show('div_Share_or_Embed');
                }
            }
        );
    }

    this.calculateShareFormPosition = function( e )
    {
        if ( !e )
            e = 'div_Share_or_Embed';

        e = $(e);

        xOffset = this.getScrollXY()[0] || 0;
        yOffset = this.getScrollXY()[1] || 0;

        e.style.top = Math.ceil( yOffset + screen.availHeight/2 - Element.getHeight(e)/2 ) + 'px';
        e.style.left = Math.ceil( xOffset + screen.availWidth/2 - Element.getWidth(e)/2 ) + 'px';
    }

    this.share = function(id)
    {
        new Ajax.Request(
            this.baseURL + '/ajax/share_list.html',
            {
                method : 'post',
                parameters :
                {
                    id          : $('share_form_plList_id').value,
                    fr_emails   : $('share_form_fr_emails').value,
                    my_email    : $('share_form_my_email').value,
                    my_name     : $('share_form_my_name').value,
                    playlist_title  : $('share_form_playlist_name').value,
                    memo        : $('share_form_memo').value
                },
                onComplete : function(){
                    $('shareStatus').update('Shared successfully');
                    setTimeout("Element.hide('div_Share_or_Embed')", 1000);
                }
            }
        );
    }

    this.getScrollXY = function()
    {
      var scrOfX = 0, scrOfY = 0;
      if( typeof( window.pageYOffset ) == 'number' ) {
        //Netscape compliant
        scrOfY = window.pageYOffset;
        scrOfX = window.pageXOffset;
      } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
        //DOM compliant
        scrOfY = document.body.scrollTop;
        scrOfX = document.body.scrollLeft;
      } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
        //IE6 standards compliant mode
        scrOfY = document.documentElement.scrollTop;
        scrOfX = document.documentElement.scrollLeft;
      }
      return [ scrOfX, scrOfY ];
    }
}

var playList = new playList();