
var module ;
var row_template1;
var row_template2;
var separate_line = "<tr><td colspan=\"4\" align=\"left\" valign=\"top\"><img src=\"/images/wight_line.png\" width=\"660\" height=\"4\" /></td></tr>";
var remixPageIndex
var directorPageIndex
var defaultLocation="latest:1:1";

function initializeList()
{
    row_template1 = ($('#RemixList > tr').get(0)).innerHTML;
    row_template2 = ($('#DirectorList > tr').get(0)).innerHTML;
    dhtmlHistory.initialize();
    dhtmlHistory.addListener(historyChange);

    var currentLocation =
        dhtmlHistory.getCurrentLocation();

    if (currentLocation == "") {
        currentLocation = "latest:1:1";
    }

    var params = currentLocation.split(":")
    module = params[0]
    remixPageIndex = params[1]
    directorPageIndex = params[2]

    if (module=="latest")
        defaultTabIndex = 0
    else if (module=="mostdownloads")
        defaultTabIndex = 1
    else if (module=="toprated")
        defaultTabIndex = 2
    else if (module=="hotcomments")
        defaultTabIndex = 3

    if (dhtmlHistory.isFirstLoad())
    {
        getListFromServer( remixPageIndex );
        getDirectorListFromServer( directorPageIndex )
    }
}

function pageNav(pageIndex){
    remixPageIndex=pageIndex;
    dhtmlHistory.add(module+":"+remixPageIndex+":"+directorPageIndex, true);
    getListFromServer(remixPageIndex);
    //scrollToTableBegin('tabHead');
}

function directorPageNav(pageIndex){
    directorPageIndex=pageIndex;
    dhtmlHistory.add(module+":"+remixPageIndex+":"+directorPageIndex, true);
    getDirectorListFromServer(directorPageIndex);
    //scrollToTableBegin('topDirector');
}

function getListFromServer( pageIndex ) {
    $('#RemixList').hide();
    $('#pageList').empty();
    $('#RemixList').empty();

    if (module=="latest")
    {
        remixAction.getNewRemix( pageIndex,displayRow);
    }
    else if (module=="mostdownloads")
    {
        remixAction.getTopDownloadRemix( pageIndex,displayRow);
    }
    else if (module=="toprated")
    {
        remixAction.getTopRatedRemix( pageIndex,displayRow);
    }
    else if (module=="hotcomments")
    {
        //remixAction.getMostCommentRemix( pageIndex,displayRow);
        var memberId = parseInt($('#MemberId').html());        
        remixAction.getIOwn(memberId,pageIndex,displayRow);        
    }

    return false;
}

function getDirectorListFromServer( pageIndex ) {
    $('#DirectorList').hide();
    $('#directorPageList').empty();
    $('#DirectorList').empty();

    remixAction.getTopDirector(pageIndex, regionLanguage, displayDirector);

    return false;
}

function displayRow( recordArray )
{
	if (  recordArray.remix.size  > 0 ) {
	    for ( var i=0 ; i < recordArray.remix.pageSize ; i++ )
	        {
	            if ( recordArray.remix.items[i] != null )
	            {
	                var col = "<tr>" + row_template1;
	
	                col = col.replace( "##remix_name##",recordArray.remix.items[i].remixName);
	                col = col.replace( /'##remix_id##'/g,recordArray.remix.items[i].remixId);
                    col = col.replace( /##remix_id##/g,recordArray.remix.items[i].remixId);
                    col = col.replace( "##movie_name##",recordArray.remix.items[i].movieName);
	                col = col.replace( "##discset_id##",recordArray.remix.items[i].discSetId);
                    col = col.replace( "##regionLanguage##",recordArray.remix.items[i].regionLanguage);
                    if ( recordArray.remix.items[i].coverArt != null )
	                    col = col.replace( "/##remix_post##/",recordArray.remix.items[i].coverArt.url['1']);
	
	                col = col.replace("##global_ratingcount##",recordArray.remix.items[i].globalRatingCount)
	                col = col.replace("##global_rating_avg##", recordArray.remix.items[i].ratingAvg);
	
	                col = col.replace("##gid##",recordArray.remix.items[i].discSetId)
	                col = col.replace("##download_count##",recordArray.remix.items[i].downloadCount)
	                col = col.replace("##remix_description##",recordArray.remix.items[i].remixDesp)
	                var _fileURL = recordArray.remix.items[i].remixFile;
	                if(_fileURL.indexOf("?") != -1) {
                    recordArray.remix.items[i].remixFile = _fileURL.substring(0, _fileURL.lastIndexOf("/") + 1) + recordArray.remix.items[i].remixName + ".Rmx";
                  }
	                col = col.replace("##file_url##",escape(recordArray.remix.items[i].remixFile))
	                col = col.replace("##director_name##",recordArray.member.items[i].displayName)
	                col = col.replace("##memberId##",recordArray.member.items[i].memberId)
	                $('#RemixList').append( col + "</tr>" );
	                $('#RemixList').append(separate_line);
	            }
	        }
	
	    $('#RemixList').fadeIn("slow");
	    var pList = new PageList(recordArray.remix.pageIndex,recordArray.remix.totalPage,recordArray.remix.totalSize,"pageNav").displayList();
	    $('#pageList').append( pList );
	
	    initRating("ShowRating",false)

    }
}

function displayDirector( recordArray )
{
    for ( var i=0 ; i < recordArray.pageSize ; i++ )
        {
            if ( recordArray.items[i] != null )
            {
                var col = "<tr>" + row_template2;

                col = col.replace( "##member_id##",recordArray.items[i].memberId);
                col = col.replace( "##member_id##",recordArray.items[i].memberId);
                col = col.replace( "##member_name##",recordArray.items[i].displayName);
                col = col.replace( "/##avatar##/",recordArray.items[i].avatar);

                col = col.replace("##remix_count##",recordArray.items[i].remixCount)
                col = col.replace("##comment_count##", recordArray.items[i].remixReviewedCount);

                col = col.replace("##download_count##",recordArray.items[i].remixDownloadedCount);
                $('#DirectorList').append( col + "</tr>" );
            }
        }

    $('#DirectorList').fadeIn("slow");
    var pList = (new SmallPageList(recordArray.pageIndex,recordArray.totalPage,recordArray.totalSize,"directorPageNav").displayList());
  
    $('#directorPageList').append( pList );
}

function historyChange (newLocation,historyData)
{
    if (newLocation == "") {
        newLocation = "latest:1:1";
    }
    var params = newLocation.split(":")
    module = params[0]
    remixPageIndex = params[1]
    directorPageIndex = params[2]

    onHistoryBack( document.getElementById(module) );
    getListFromServer( remixPageIndex );
    getDirectorListFromServer(directorPageIndex);
}

function changeModule(id)
{
    module=id;
    pageNav( 1 );
}

