var last_artist_id;
var last_album_id;
var debug = false;

//used in IE to store track buy areas outside of the document
//they cause layout problems even when display: none
var stored_track_buys;


function showAlbumArtist1(artist_id,artist_name)
{
    if (artist_id == last_artist_id) return;
    if (last_artist_id) {
        var art = document.getElementById('artist_'+last_artist_id);
        if( art != null ) {
            art.className='item';
        }
    }
    var ele = document.getElementById('artist_'+artist_id);
    if( ele != null)
    {
        ele.className='selected';
    }
    last_artist_id = artist_id;
    url = "includes/artistlist.php?artist_id="+artist_id
        +"&artist_name="+artist_name;
    $('#albums').load(url);
    clearTracks();
}

function showAlbum(id,artist_id)
{
    if (id == last_album_id) return;
    if (last_album_id) {
        var ele = document.getElementById('album_'+last_album_id);
        if( ele != null) {
            ele.className='item';
        }
    }
    var ele2 =	document.getElementById('album_'+id);
    if ( ele2 != null)
    {
        ele2.className='selected';
    }
    url = "includes/tracklist.php?site_id=2"
        +"&product_id="+id;
    $('#tracks').load(url);
    last_album_id = id;
    //return false;
}

function showAlbumArtist(id)
{
    if (id == last_album_id) return;
    if (last_album_id) {
        $('#album_detail_' + last_album_id).hide();
        $('#' + last_album_id).attr('class', 'album_cover');
    }

    //hide all open retail reveals
    $('div.b3_album, div.b3_track').hide();

    $('#' + id).attr('class', "selected");
    $('#album_detail_' + id).show().css('visibility', 'visible');

    if (!$('#tracks_' + id).html()) {
        $('#tracks_' + id).html('Loading tracklist...');
        var url = "_artist_av_tracklist.php?artist_id="
            + location.search.match(/artist_id=(\d+)/)[1]
            + "&product_id=" + id
            + "&ajax_parent=" + location.pathname.replace(/^.*\//, '');

        $('#tracks_' + id).load(url, function()
        {
            //bind track-level buy buttons
            $('div.av_asset_base div.buy_button a.buy').click(toggleTrackBuy);

            //do rollovers on buy buttons
            ir_bindRollovers($('#tracks_' + id));

            //deal with some IE problems
            if ($.browser.msie) {
                //remove and store track buy areas
                //they will be inserted as needed in toggleTrackBuy()
                stored_track_buys = $('div.b3_track').remove();
                //alert(stored_track_buys.length);

                //ie fix footer layout bug
                $('body').hide().show();

            }
        });

        var url1 = "_artist_av_children.php?artist_id="
            + location.search.match(/artist_id=(\d+)/)[1]
            + "&product_id=" + id + "&ajax_parent=" + location.pathname.replace(/^.*\//, '');

        $('#cpalbum_detail_' + id).load(url1, function()
        {
            //bind track-level buy buttons
            $('div.av_asset_base div.buy_button a.buy').click(toggleTrackBuy);

            openRequestedTrackBuy();

            //do rollovers on buy buttons
            ir_bindRollovers($('#cpalbum_detail_' + id));

            //bind child album-level buy button
            $('div.child a.buy_album').click(toggleChildAlbumBuy);

            //ie fix footer layout bug
            $('body').hide().show();

        });
    }

    //ie fix footer layout bug
    // (needed if just switching albums w/o loading new content
    $('body').hide().show();

    last_album_id = id;
}

function clearTracks()
{
    $('#tracks').html('');
}

function Div(elt, color) {
    this.div = elt;
    this.color = color;
}

var previousDivs = null;

function changeColor(elt, color) {
    if (previousDivs != null) {
        for (var i = 0 ; i < previousDivs.length ; i++) {
            previousDivs[i].div.style.backgroundColor = previousDivs[i].color;
        }
    }

    previousDivs = new Array();
    previousDivs[0] = new Div(elt, elt.style.backgroundColor);

    elt.style.backgroundColor = color;
    for (var i = 2; i < arguments.length; i++) {
        var currentDiv = document.getElementById(arguments[i]);
        previousDivs[previousDivs.length] = new Div(currentDiv,currentDiv.style.backgroundColor);
        currentDiv.style.backgroundColor = color;
    }
}

function findPosX(obj)
//Added by Viraj (02/16/05) to Calculate the absolute position of an element element is passed as a parameter
// This can be used to assign a style.left property of the destination element
{
    var curleft = 0;
    if (obj.offsetParent)
    {
        while (obj.offsetParent)
        {
            curleft += obj.offsetLeft
            obj = obj.offsetParent;
        }
    }
    else if (obj.x)
        curleft += obj.x;
    return curleft;
}

function findPosY(obj)
//Added by Viraj (02/16/05) to Calculate the absolute position of an element element is passed as a parameter
// This can be used to assign a style.top property of the destination element
{
    var curtop = 0;
    if (obj.offsetParent)
    {
        while (obj.offsetParent)
        {
            curtop += obj.offsetTop
            obj = obj.offsetParent;
        }
    }
    else if (obj.y)
        curtop += obj.y;
    return curtop;
}

function position_buyals(id)
{
    document.getElementById(id).style.position = "absolute";
    //alert(findPosY(document.getElementById('album_detail'))+' '+findPosX(document.getElementById('album_detail')));
    document.getElementById(id).style.top = findPosY(document.getElementById('album_details'))-100;
    document.getElementById(id).style.left = findPosX(document.getElementById('album_details'))-100;
}

function firstalbum(id)
{
    var buy = document.getElementById(id);
    if( buy != null) {
        buy.className = "selected";
    }
}

function writeMsg()
{
    $('#tracks').html('<div style="padding: 10px 0 0 10px;">Please choose an album to view the tracks</div>');
}