function TimeLineTab(tabID) {
    document.getElementById("timeline"+tabID).style.display = "block";
    document.getElementById("timeline"+currentTimeline).style.display = "none";
    document.getElementById("tab"+currentTimeline).style.backgroundImage = "url(./deployment/_images/tab-back.jpg)";
    document.getElementById("tab"+currentTimeline).style.backgroundColor = "#DDDDDD";
    document.getElementById("tab"+tabID).style.backgroundImage = "none";
    document.getElementById("tab"+tabID).style.backgroundColor = "#F9F9F9";
    currentTimeline = tabID;
}

var workingTab = "";

function StyleTab(tabID) {
    if (tabID!=currentStyleTab) {
        updateStyleTab(tabID);
    }
}

function updateStyleTab(styleId) {
    
    // update flash applet with the same one with a parameter
    
    document.getElementById("styletab"+styleId).style.backgroundImage = "none";
    document.getElementById("styletab"+styleId).style.backgroundColor = "#F9F9F9";
    document.getElementById("styletab"+currentStyleTab).style.backgroundImage = "url(./deployment/_images/tab-back.jpg)";
    document.getElementById("styletab"+currentStyleTab).style.backgroundColor = "#DDDDDD";

    currentStyleTab = styleId;

    injSQF( currentStyleTab );
    
}


function displayLogin() {
     $("loginbox").style.left = calculateOffsetLeft( $("loginlink") ) - 220 + "px";
     $("loginbox").style.top = calculateOffsetTop( $("loginlink") ) + 25 + "px";
     $("loginbox").style.display = "block";
}

function getPlaylistPrefix() {
   return (baseURLjs + CurrAccount + "/playlist/");
}

function displayPlaybox(imageID, trackID) {
    $("playbox-winamp-this-href").href = getPlaylistPrefix() + "albtrack" + imageID + "-" + currentRecord + ".m3u8";
    $("playbox-cog-this-href").href = getPlaylistPrefix() + "cog-albtrack" + imageID + "-" + currentRecord + ".m3u";

     $("playbox").style.left = calculateOffsetLeft( $("imagehint"+imageID) ) + 11 + "px";
     $("playbox").style.top = calculateOffsetTop( $("imagehint"+imageID) ) - 5 + "px";
     $("playbox").style.display = "block";
}

function displayPlayboxGeneric(imageID, recordID) {
    $("playbox-winamp-generic-href").href = getPlaylistPrefix() + "album" + recordID + ".m3u8";
    $("playbox-winamp-favs-generic-href").href = getPlaylistPrefix() + "albest" + recordID + ".m3u8";
    $("playbox-cog-generic-href").href = getPlaylistPrefix() + "cog-album" + recordID + ".m3u";
    $("playbox-cog-favs-generic-href").href = getPlaylistPrefix() + "cog-albest" + recordID + ".m3u";

    $("playboxGeneric").style.left = calculateOffsetLeft( $("imagehint"+imageID) ) + 11 + "px";
    $("playboxGeneric").style.top = calculateOffsetTop( $("imagehint"+imageID) ) - 5 + "px";

    $("playboxGeneric").style.display = "block";

    

    // alert($("playboxGeneric").style.left + " ||| " + $("playboxGeneric").style.top);

}


function hidePlaybox() {
     $("playbox").style.display = "none";
}

function hidePlayboxGeneric() {
     $("playboxGeneric").style.display = "none";
}

function PlayBackSection() {

    var pbWhere = "winamp";
    if ( $("playback-where")!=null ) pbWhere = $("playback-where").value.toLowerCase();

    if ( pbWhere == 'winamp') {
        if ( $("playback-what").value.toLowerCase() == 'favorites') playbacktype = "albest";
        var actionCommand = baseURLjs + currentAcc + playlistSuffix + playbacktype + currentRecord + ".m3u8";
        window.location = actionCommand;
    }
    else if ( pbWhere == 'cog') {
        if ( $("playback-what").value.toLowerCase() == 'favorites') playbacktype = "albest";
        var actionCommand = baseURLjs + currentAcc + playlistSuffix + "cog-" + playbacktype + currentRecord + ".m3u";
        window.location = actionCommand;
    }
}

function PlayBackSectionArtist() {

    var pbWhere = "winamp";
    var playbacktype = "artist";
    if ( $("playback-where")!=null ) pbWhere = $("playback-where").value.toLowerCase();

    if ( pbWhere == 'winamp') {
        if ( $("playback-what").value.toLowerCase() == 'favorites') playbacktype = "artbest";
        var actionCommand = baseURLjs + currentAcc + playlistSuffix + playbacktype + currentArtistId + ".m3u8";
        window.location = actionCommand;
    }
    else if ( pbWhere == 'cog') {
        if ( $("playback-what").value.toLowerCase() == 'favorites') playbacktype = "artbest";
        var actionCommand = baseURLjs + currentAcc + playlistSuffix + "cog-" + playbacktype + currentArtistId + ".m3u";
        window.location = actionCommand;
    }
}

artworkWorking = 0;
artistWorking = "";
albumWorking = "";
IDWorking = "";
sizeWorking = 0;
updateIDWorking = 0;


// records routines

var currentRecordDisplay = 0;

function ShowAdvancedRecord(id) {

    if (id==currentRecordDisplay) {
        $("advanced-"+currentRecordDisplay).style.display = "none";
        currentRecordDisplay = 0;
    } else {
        if (currentRecordDisplay>0) {
            $("advanced-"+currentRecordDisplay).style.display = "none";
        };
        $("advanced-"+id).style.display = "block";
        currentRecordDisplay = id;
    };


}

function selectContinent(continent) {
    $(currContinent).style.display = "none";
    $(continent).style.display = "block";
    currContinent = continent;
}

// filter routines
var recordtypes = new Array("album", "single", "comp", "other");
var datatable = "datatable";

function FilterList(id) {

    numID = 1;
    if (id=="single") numID = 2;
    if (id=="comp") numID = 3;
    if (id=="other") numID = 4;

    id = "list-filter-"+id;
    if ( $(id).src.indexOf("-on.gif")>0 ) {
       $(id).src = $(id).src.replace("-on.gif", "-off.gif"); 
    } else {
       $(id).src = $(id).src.replace("-off.gif", "-on.gif"); 
    }

    for (i=1; i<$(datatable).childNodes.length; i++) {

        if ( $(datatable).childNodes[i].attributes!=null ) {
            //alert( $(datatable).childNodes[i].getAttribute('rtype') );
            if ($(datatable).childNodes[i].getAttribute('rtype')==numID) {
                if ( ($(datatable).childNodes[i].style.display.length<2 ) ||
                      ($(datatable).childNodes[i].style.display == "block" ) ) {
                    $(datatable).childNodes[i].style.display = "none";
                } else {
                    $(datatable).childNodes[i].style.display = "block";
                }
            }
        }
        }

    fixBackgrounds();

}

// sorting routines


var row_data, row_dataTEXT, row_dataVisibility, row_RecordType, ir, lastseq;
var tableStructure = new Array(0,'artist','album','year','rate','added',0);
var internalArrayStructure = new Array('artist','album','year','rate','added');


function doSort(sortElement) {
    if (row_data==null) { ParseTable(); }

    sortindex = getStructureIndex(sortElement);

    // new array of visibility

    counterVisible = 0;
    for (i=1; i<$(datatable).childNodes.length; i++) {

        if ( $(datatable).childNodes[i].attributes!=null ) {
            if ($(datatable).childNodes[i].getAttribute('rtype')!=null) {
                 row_dataVisibility[counterVisible] = $(datatable).childNodes[i].style.display;
                 counterVisible++;
            }
        }
     };

    // if the same sorting, invert the sorting

    if( sortElement != lastSort )
        lastseq = 'A';
    else
    {
        if( lastseq == 'A' ) lastseq = 'D'; else lastseq = 'A';
    }

    id=row_data.length;

    for (i=0; i < id; i++) {
        bswap = false;
        for( j=0; j < id - 1; j++ )
        {
            row1 = row_dataTEXT[j];
            row2 = row_dataTEXT[j+1];
            rowhtml1 = row_data[j];
            rowhtml2 = row_data[j+1];
            rowBlock1 = row_dataVisibility[j];
            rowBlock2 = row_dataVisibility[j+1];
            rowType1 = row_RecordType[j];
            rowType2 = row_RecordType[j+1];

            if( lastseq == "A" )
            {
                if( row1[ sortindex ] > row2[ sortindex ] )
                {
                    row_dataTEXT[j+1] = row1;
                    row_dataTEXT[j] = row2;
                    row_data[j+1] = rowhtml1;
                    row_data[j] = rowhtml2;
                    row_dataVisibility[j+1] = rowBlock1;
                    row_dataVisibility[j] = rowBlock2;
                    row_RecordType[j+1] = rowType1;
                    row_RecordType[j] = rowType2;
                    bswap=true;
                }
            }
            else
            {
                if( row1[ sortindex ] < row2[ sortindex ] )
                {
                    row_dataTEXT[j+1] = row1;
                    row_dataTEXT[j] = row2;
                    row_data[j+1] = rowhtml1;
                    row_data[j] = rowhtml2;
                    row_dataVisibility[j+1] = rowBlock1;
                    row_dataVisibility[j] = rowBlock2;
                    row_RecordType[j+1] = rowType1;
                    row_RecordType[j] = rowType2;
                    bswap=true;
                }
            }
        }
        if( bswap == false ) break;
    }

   reGenerate();

   // fix sort arrows
   if ( lastseq=="A" ) {
       lastBitOfArrow = "down";
   } else {
       lastBitOfArrow = "up";
   }

   $("sortarrow-"+lastSort).src = "./deployment/_images/sortarrow-none.gif";
   $("sortarrow-"+sortElement).src = "./deployment/_images/sortarrow-" + lastBitOfArrow + ".gif";

   lastSort = sortElement;
}

function reGenerate() {

   counterVisible = 0;
    for (i=1; i<$(datatable).childNodes.length; i++) {

        if ( $(datatable).childNodes[i].attributes!=null ) {
            if ($(datatable).childNodes[i].getAttribute('rtype')!=null) {

                 $(datatable).childNodes[i].innerHTML = row_data[counterVisible];

                 $(datatable).childNodes[i].removeAttribute( "rtype" );
                 $(datatable).childNodes[i].style.display = row_dataVisibility[counterVisible];
                 $(datatable).childNodes[i].setAttribute('rtype', row_RecordType[counterVisible]);

                 counterVisible++;
            }
        }
     }

    fixBackgrounds();

}

function fixBackgrounds() {

    if (currentRecordDisplay>0) {
        $("advanced-"+currentRecordDisplay).style.display = "none";
    }


    backCounter = 0;
    for (i=1; i<$(datatable).childNodes.length; i++) {
    if ( $(datatable).childNodes[i].attributes!=null ) {
            if ($(datatable).childNodes[i].getAttribute('rtype')!=null) {

                if ( $(datatable).childNodes[i].style.display!="none" ) {

                    bgCLR = "#E7E7E7";
                    if (backCounter % 2 == 0) {
                      bgCLR = "#FFFFFF";
                    };

                    $(datatable).childNodes[i].style.backgroundColor = bgCLR;
                    backCounter++;
                }

            }
        }
    }



}


function getStructureIndex (id) {
    //id = tableStructure[id];
    //return id;
    results = null;
    //if (id!=0) {
        for (b=0; b<internalArrayStructure.length; b++) {
            //alert(b);
            if (internalArrayStructure[b].toLowerCase()==id.toLowerCase()) {
                results = b;
                break;
            };
       // }
    }
    return results;
}

function ParseTable() {
    lastseq = 'A';

    datatable = "datatable";

    numberOfRecords = 0;
    for (i = 0; i<$(datatable).childNodes.length; i++ ) {
        if ( $(datatable).childNodes[i].attributes!=null ) {
            if ($(datatable).childNodes[i].getAttribute('rtype')!=null) {
                numberOfRecords++;
            }
        }
    }

    //alert(counter);
    row_data = Array( numberOfRecords );
    row_dataTEXT = Array( numberOfRecords );
    row_dataVisibility = Array( numberOfRecords );
    row_RecordType = Array( numberOfRecords );
    // parse whole objects into arrays

    counter = 0;
    for (i = 0; i<$(datatable).childNodes.length; i++ ) {
        if ( $(datatable).childNodes[i].attributes!=null ) {
            if ($(datatable).childNodes[i].getAttribute('rtype')!=null) {

                row_data[counter] = $(datatable).childNodes[i].innerHTML;
                row_RecordType[counter] = $(datatable).childNodes[i].getAttribute('rtype');
                //alert( $(datatable).childNodes[i].childNodes.length );

                tmpArray = new Array();

                internalCounter = 0;
                for (j = 0; j<$(datatable).childNodes[i].childNodes.length; j++) {
                    if ($(datatable).childNodes[i].childNodes[j].nodeName.toLowerCase()=="div") {
                       //alert ( getStructureIndex( internalCounter ) );

                       if (tableStructure[ internalCounter ]!=null) {
                          if (tableStructure[ internalCounter ]!=0) {
                              currentSchemaOfParsing = getStructureIndex ( tableStructure[ internalCounter ] );
                              // parsing artist / album / year
                              if (currentSchemaOfParsing<3) {
                                  tmpArray[currentSchemaOfParsing] = StripAHREF($(datatable).childNodes[i].childNodes[j].innerHTML);
                                  //alert(tmpArray[currentSchemaOfParsing]);
                              };
                              if (currentSchemaOfParsing==3) {
                                  //st_r3.0.gif
                                  firstIndex = $(datatable).childNodes[i].childNodes[j].innerHTML.indexOf("st_r")+4;
                                  lastIndex = $(datatable).childNodes[i].childNodes[j].innerHTML.indexOf(".gif");
                                  tmpArray[currentSchemaOfParsing] = $(datatable).childNodes[i].childNodes[j].innerHTML.substring(firstIndex,lastIndex);
                              };
                              if (currentSchemaOfParsing==4) {
                                  //calendar/2005/10">
                                  firstIndex = $(datatable).childNodes[i].childNodes[j].innerHTML.indexOf("/calendar/")+10;
                                  lastIndex = $(datatable).childNodes[i].childNodes[j].innerHTML.indexOf("\">");
                                  tmpArray[currentSchemaOfParsing] = $(datatable).childNodes[i].childNodes[j].innerHTML.substring(firstIndex,lastIndex);
                              };
                          }
                       }

                       internalCounter++;
                    }
                }
                //alert(tmpArray);
                row_dataTEXT[counter] = tmpArray;
                counter++;

            }
        }
    }

}

function StripAHREF (input) {

    // <a href="./aulismedia/Andy+Bell">Andy Bell</a>
    firstIndex = input.indexOf("\">")+2;
    lastIndex = input.indexOf("</a>");
    return input.substring(firstIndex, lastIndex);

}

function slimBoxRequest ( rqST ) {
    // alert(rqST);
    rqstURL = "./" + CurrAccount + rqST;
    var ajx = new Ajax.Request(rqstURL, {
            method: 'get',
            parameters: null,
            onSuccess: function(transport){
                var response = transport.responseText || "!!! No text received";
            }
    });
}


