
// Global login status flag - set by loadDataPostCache after page load
var gIsLoggedIn = false;


function loadContent( link )
{
    var browserURL = document.location.href;
    var secureURL  = 0;
		
    if ( browserURL.indexOf("https:") > -1 ) {
	secureURL = 1;
    }

    var url = link.href;
    url    += url.indexOf( '?' ) == -1 ? '?' : '&';
    url    += "ajax=1";

    if ( secureURL && (url.indexOf("http:") > -1) ) { // solve same-domain problem for SSL url / non-SSL'ed link
	url = "https:" + url.slice(5,url.length);
    }
    else if ( !secureURL && (url.indexOf("https:") > -1) ) { // solve same-domain problem for nonSSL url / SSL'ed link
	url = "http:" + url.slice(6,url.length);
    }		
    
    var mainContent = $('mainContent');
    mainContent.update('<img src="/images/ajax-loader-darkgreen.gif" style="margin: 320px 0 0 275px;" />');

    new Ajax.Request(
	url, 
	{
	    method:    'get',
	    onSuccess: function(transport) {
		
		var mainContent = $('mainContent');
		var tmpElement  = document.createElement("div");
	
		tmpElement.innerHTML = transport.responseText;
		
		Element.extend(tmpElement);
		
		tmpElement.select('div#ajaxContent').each(
		    function(elm) {
			mainContent.update(elm.innerHTML);
		    }
		);
	    }
	}
    );

    return false;
} // loadContent

function showMoreFrom( moreFromId )
{
    $( moreFromId ).select( 'dl' ).each( 
	function (el) {
	    el.show();
	}
    );

    $( moreFromId ).select( 'a.more' ).each( 
	function (el) {
	    el.hide();
	} 
    );

    return false;
}

/********************************************
 ** 
 ** Search Code
 ** 
 *******************************************/
var Search = new Object();  // create a namespace

Search.StartSearch = function (formElm) {
    formElm.select( 'button' )[0].addClassName( 'processing' );
    formElm.submit();
} // Search.StartSearch

Search.GetMoreResults = function ( searchForm ) {
    
    var params = {
	onSuccess : function (req) {
	    Search.ShowResults( searchForm, req );
	}
    };

    searchForm.request( params );
    searchForm.disable();

    var submitButton = searchForm.select( 'button' )[0];
	
    submitButton.addClassName( 'loading' );
    submitButton.innerHTML = 'Loading...';
    
    return false;
} // Search.GetMoreResults

Search.ShowResults = function (searchForm, req) {
    var wrapper = searchForm.parentNode.parentNode;

    wrapper.className = '';
    wrapper.innerHTML = req.responseText;

    return false;
} // Search.ShowResults

function fixedSearch(term) {
    window.location = "/search/index.html?s=1&type=more&action=search&search_field="+escape(term);
}

function place_order()
{
    var btn1 = dhtml_getElement( 'place1' );
    var btn2 = dhtml_getElement( 'place2' );

    if( btn1 ) btn1.disabled = true;
    if( btn2 ) btn2.disabled = true;

    
    if( btn1 ) btn1.src = '/images/buttons/button-place-order_off.gif';
    if( btn2 ) btn2.src = '/images/buttons/button-place-order_off.gif';
    
    return true;
} // place_order

function swap( img, isrc )
{
  if( !document.images ) return;
  document.images[img].src = isrc;
}

// this function shows/hides the text hints in form fields
function textHint( frm, element, event, text, hint )
{
    obj = document.forms[frm].elements[element];

    if( obj )
    {
	if( event == 'focus' )
	{
	    if( obj.value == text )
	    {
		obj.value = '';
	    }
	    
	    if( hint )
	    {
		window.status = hint;
	    }
	}
	else if( event == 'blur' )
	{
	    if( obj.value == '' )
	    {
	    	obj.value = text;
	    }

	    if( hint )
	    {
		window.status = '';
	    }
	}
    }
} // textHint()

function setWndLocation( wnd, url )
{
    wnd.location.href = url;		
} // dhtml_setWndLocation

function addToCartFromDropdown( master_product_id, container )
{
    var product_id;
	container = $(container);
    var dd = container.select( 'select' )[0];

    // get item product_id
    product_id = dd.value;

    // attempt to add to cart
    if( product_id )
    {
	// add to cart
	addToCart( product_id, container );
    }
    else
    {
	var firstOption = dd.options[0].text;

	alert( firstOption + " and try again." );
    }
} // addToCart()

function addToCart( product_id, container )
{
    var func = 'addToCart:';

    // Determine if we should add to cart, or if we should remove from cart
    var addToCartFlag = true;
    if( container ) {
	container = $(container);
	var addToCartBtn 
	    = container.hasClassName( 'atc' ) ? container 
	    :                                   container.select( '.atc' )[0]
	    ;

	log( 'button classes ', addToCartBtn.className );
	if( addToCartBtn && Element.hasClassName( addToCartBtn, 'added' ) ) {
	    addToCartFlag = false;
	}
    }

    if( addToCartFlag ) {
	// add item to cart
	log( func, 
	     "Adding product with id: ",
	     product_id, " to the user's cart" );
	dhtml_xmlHttpPost( '/cart/add_item.xml',
			   'product_id=' + escape( product_id ),
			   'addToCart_ItemAdded' );
    }
    else {
	log( 
	    func,
	    "Removing product with id",
	    product_id,
	    "from cart" );
	removeFromCart( product_id );
    }
} // addToCart()

function removeFromCart( product_id )
{
    dhtml_xmlHttpPost( '/cart/remove_item.xml',
		       'product_id=' + escape( product_id ),
		       'addToCart_ItemAdded' );
} // removeFromCart

// AJAX callback function
function addToCart_ItemAdded( req )
{
    //alert( req.responseText );
    var resp, code, productList, func = "addToCart_ItemAdded: ";
    code   = 0;
    
    // Get data from response
    log( func, "AJAX RESPONSE: ", req.responseText.escapeHTML() );
    response = req.responseXML.documentElement;
    if( response ) {
		code     = response.getAttribute( 'code' );
		var cart = response.getElementsByTagName( 'cart' )[0];
		if( cart ) {
		    productList = cart.getElementsByTagName( 'product' );
		}
		else {
		    productList = response.getElementsByTagName( 'product' );
		}
    }
    log( 'Code = ' + code + ' product list = ' + productList.length );
   
    if (code != 1) {
		alert( "We were unable to add or remove this product to your cart. Please try again." );
                return;
    }

    var cartcount = 0;

    // If product was successfully added to cart
    if( productList != null ) {
        
        // Loop through each product added to cart 
        // (usually only one;more than one for cached category pages)
        for( var prodIdx = 0; prodIdx < productList.length; prodIdx++ ) {
            
            // Get list of add and added buttons for this product
            var product_id = productList[prodIdx].getAttribute( 'id' );
            var qty = productList[prodIdx].getAttribute( 'qty' );
            var action     = productList[prodIdx].getAttribute( 'action' ) 
                          || 'added'
                           ;

            $$( '.atc_' + product_id )
                .each( function (btn) {
                            if( action == 'added' ) {
                                btn.addClassName( 'added' );
                            } else {
                                btn.removeClassName( 'added' );
                            }
                } );

            // count total number of items in cart, for "view cart (N)"
            if (action == 'added')
                cartcount += parseInt(qty);

        } // for each product in cart
    } // if products returned by xml


	var cartCountVal = cartcount;

	if ( cartCountVal < 10 && cartCountVal > 0 ) {
		cartcount = "0" + cartcount;
	}
	if ( cartCountVal < 1 ) {
		cartcount = "";
	}

    $$( '.cartcount' ).each( function(x) { x.innerHTML = cartcount });

} // addToCart_ItemAdded



function addToWishlist( product_id, container )
{
    var func = 'addToWishlist:';

    // If not logged, don't ajax, but let the link direct flow through the login page
    if ( !gIsLoggedIn)
      return true;
    
    // Determine if we should add to wishlist, or if we should remove from wishlist
    var addToWishFlag = true;
    if( container ) {
	container = $(container);
		var addToWishBtn 
		    = container.hasClassName( 'wish' ) ? container 
		    :                                   container.select( '.wish' )[0]
		    ;
	
		log( 'button classes ', addToWishBtn.className );
		if( addToWishBtn && Element.hasClassName( addToWishBtn, 'active' ) ) {
		    addToWishFlag = false;
		}
    }
    
    if( addToWishFlag ) {
		// add item to cart
		log( func, 
		     "Adding product with id: ",
		     product_id, " to the user's wishlist" );
		     dhtml_xmlHttpPost( '/account/add_to_wishlist.xml',
				   'product_id=' + escape( product_id ),
				   'addToWishlist_ItemAdded' );
    }
    else {
		log( 
		    func,
		    "Removing product with id",
		    product_id,
		    "from wishlist" );
		    
		removeFromWishlist( product_id );
    }

    return false;

} // addToWishlist()

function removeFromWishlist( product_id )
{
    dhtml_xmlHttpPost( '/account/remove_from_wishlist.xml',
		       'product_id=' + escape( product_id ),
		       'addToWishlist_ItemAdded' );
} // removeFromWishlist()

// AJAX callback function
function addToWishlist_ItemAdded( req )
{
    //alert( req.responseText );
    var resp, code, productList, func = "addToWishlist_ItemAdded: ";
    code   = 0;
    
    // Get data from response
    log( func, "AJAX RESPONSE: ", req.responseText.escapeHTML() );
    response = req.responseXML.documentElement;
    if( response ) {
		code     = response.getAttribute( 'code' );
		var wishlist = response.getElementsByTagName( 'wishlist' )[0];
		if( wishlist ) {
		    productList = wishlist.getElementsByTagName( 'product' );
		}
		else {
		    productList = response.getElementsByTagName( 'product' );
		}
	}
    log( 'Code = ' + code + ' product list = ' + productList.length );
    


    // If product was successfully added to wishlist
    if( code == 1 ) {
		if( productList != null ) {
		    
		    // Loop through each product added to cart 
		    // (usually only one;more than one for cached category pages)
		    for( var prodIdx = 0; prodIdx < productList.length; prodIdx++ ) {
				// Get list of add and added buttons for this product
				var product_id = productList[prodIdx].getAttribute( 'id' );
				var action     = productList[prodIdx].getAttribute( 'action' ) 
				              || 'active'
				               ;
		
				$$( '.wish_' + product_id )
				    .each( function (btn) {
						if( action == 'active' ) {
						    btn.addClassName( 'active' );
						} else {
						    btn.removeClassName( 'active' );
						}
				    } );
		    } // for each product in cart
		} // if products returned by xml
    } // if code = 1
	else if (code == 2) {     // need to log in first via lightbox

	}
    else { // show alert
		alert( "We were unable to add this product to your wishlist. Please try again." );
    }
} // addToWishlist_ItemAdded

function swapFieldSets( idx, dropdownName, visibleStyle )
{
    // visibleStyle is no longer used.  You can acheive the same effect 
    // by setting style.display = ''
    if( !visibleStyle )
	visibleStyle = 'block';
    visibleStyle = dhtml_convertDisplayType( visibleStyle );
    
    // Get fieldset names
    log( "Dropdown name: " + dropdownName );
    var setNames = eval( dropdownName + "_fsets" );
    var defaultName = eval( dropdownName + "_default_fset" );
    var activated = false;
    
    // Loop through fieldset names
    for( var i = 0; i < setNames.length; i++ ) {
	if( setNames[i] == null )
	    continue;
	
	var elemNames = setNames[i].split( ',' );
	log( "elemNames: " + elemNames );

	// Activate
	if( i == idx || setNames[i] == setNames[idx] ) {
	    for( var j = 0; j < elemNames.length; j++ ) {
		dhtml_getElement( elemNames[j] ).style.display = '';
	    }
	    activated = true;
	}
	else { // Deactivate
	    for( var j = 0; j < elemNames.length; j++ ) {
		log( "Hiding fieldset with id: " + elemNames[j] );
		dhtml_getElement( elemNames[j] ).style.display = 'none';
	    }
	}
	    
    } // for

    if( defaultName ) {
	var defElemNames = defaultName.split( ',' );
	
	if( !activated && defaultName != null ) {
	    for( var i = 0; i < defElemNames.length; i++ ) {
		dhtml_getElement( defElemNames[i] ).style.display = '';
	    }
	}
	else if( defaultName != null ) {
	    for( var i = 0; i < defElemNames.length; i++ ) {
		dhtml_getElement( defElemNames[i] ).style.display = 'none';
	    }
	}
    }

}

function changeAction( formName, actionValue )
{
    document.forms[formName].elements['action'].value = actionValue;
} // changeAction

function popupDigitalFormat()
{
    popWindow( '/digital/faq.html', 300, 385, 'digital_faq' );
} // popupDigitalFormat

function popupDigitalTerms()
{
    popWindow( '/digital/terms.html', 300, 385, 'digital_terms' );
} // popupDigitalTerms

function popWindow( url, width, height, name )
{
    if( !name ) name = '_blank';

    var wnd = window.open( 
	url,
	name,
	'height=' + height + ',width=' + width + ',status=no,location=no,menubar=no,scrollbars=no,resizable=no,toolbar=no'
    );

    wnd.focus();	
} // popWindow

function popWindowScroll( url, width, height )
{
    var wnd = window.open( url,
			   '_blank', 
			   'height=' + height + ',width=' + width + ',location=no,menubar=no,scrollbars=yes,resizable=no,toolbar=no'
			   );
    wnd.focus();	
} // popWindow

function popWindowCustom( url, width, height, features )
{
    var wnd = window.open( url,
			   '_blank', 
			   'height=' + height + ',width=' + width + ((features) ? (',' + features) : (''))
			   );
    wnd.focus();	
} // popWindow

function popWindowAll( url, width, height )
{
    var wnd = window.open( url,
			   '_blank', 
			   'height=' + height + ',width=' + width + ',location=yes,menubar=yes,scrollbars=yes,resizable=yes,toolbar=yes,directories=yes'
			   );
    wnd.focus();	
} // popWindow


function showDetails( pid, width, height )
{
    // set default width and height
    if( !width )
    {
	width = 425;
    }
    if( !height )
    {
	height = 500;
    }
    
    // popup window
    popWindow( "/products/details/" + pid + ".html", width, height )
} // showDetails()

function showImageDetail( numImage )
{
    var img, styleName;

    for( i = 0; i < 15; i++ ) // assume no more than 15 images/captions in popup
    {
	// select the new image, hide the rest
	img = dhtml_getElement( "img_" + i );
	if( img )
	{
	    if( i == numImage )
	    {
		styleName = 'block';
	    }
	    else
	    {
		styleName = 'none';
	    }
	    img.style.display = styleName;	    

	    // hilight the new caption, normalize the rest
	    //cap = dhtml_getElement( "cap_" + i );
	    //if( cap )
	    //{
	    //cap.style.font.decoration = 'none'; // this doesn't work
	    //}
	}
    }
} // showImageDetail

function customPkgInfo( section, ddName )
{
    var productId = dhtml_getFormValue( ddName );

    if( productId != null && productId != '' ) {
	popWindow( '/' + section + '/0/0/' + productId + '.html',
		   800,
		   600 );
    }
} // customPkgInfo

function showAllArtists()
{
    handleBrowseByTabs( 'browse_all_labels', 'browse_all_artists' );
} // showAllArtists

function showAllLabels()
{
    handleBrowseByTabs( 'browse_all_artists', 'browse_all_labels' );
} // showAllLabels

function handleBrowseByTabs( hideElem, showElem )
{
    $( hideElem ).className = '';
    //Element.removeClassName( hideElem, 'active' );

    Element.hide( hideElem + '-options' );
    
    Element.addClassName( showElem, 'active' );

    Element.show( showElem + '-options' );

} // handleBrowseByTabs

function selectBrowseByOption( selectbox )
{
    var url = '/digital/browse-by/';
    url    += selectbox.id.indexOf( 'artist' ) != -1 ? 'artist.html' : 'label.html';

    url += '?id=' + escape(selectbox.options[selectbox.selectedIndex].value);

    location.href=url;
} // selectBrowseByOption

function showFullReview( id )
{
    var linkSpan   = dhtml_getElement( 'more_info_link_' + id );
    var reviewSpan = dhtml_getElement( 'rest_of_review_' + id );
    
    if( linkSpan != null && reviewSpan != null ) {
	
	dhtml_changeDisplayForElement( linkSpan, 'none' );
	dhtml_changeDisplayForElement( reviewSpan, 'inline' );
    }
} // showFullReview

function toggleReview( evt )
{
    if( !evt ) evt = event;
    
    // Get the target of the event
    var target     = Event.element(evt);
    var targetType = target.tagName.toLowerCase();
    log( "Event type: " + evt.type + " Target: " + targetType );
    
    // We only care about events from the "R" image
    // and the review (which is in a p tag)
    if( targetType != 'img' && targetType != 'p' ) return;

    // If it is a mouseover event, then show the review
    var showFlag = evt.type == 'mouseover' ? 1 : 0;
    log( "Show Flag: " + showFlag );
    
    // Get the review
    var review  = targetType == 'img' ? target.parentNode.getElementsByTagName( 'P' )[0]
                :                       target
                ;
    if( !review ) return;

    // If the review is to be hidden
    if( !showFlag ) {
	// Make sure that the mouse is actually off the "R" trigger image
	// and off of the review
	var reltg = (evt.relatedTarget) ? evt.relatedTarget : evt.toElement;
	while (reltg != target && reltg != review && reltg.nodeName != 'BODY')
	    reltg = reltg.parentNode
	if (reltg == target || reltg == review) return;
    } // !showFlag

    // Make sure that the review will disappear
    // if the user moves their mouse off of it
    if( !review.onmouseout )
	    review.onmouseout = toggleReview;
    
    // Show or hide the review
    review.style.display = showFlag ? '' : 'none';
    
    // If hiding and the review is broken into two parts
    if( !showFlag ) {
	// Then make sure the second part is hidden
	var restOfReview = (document.getElementsByClassName( 'moreReview', review ))[0];
	var clickForMore = (document.getElementsByClassName( 'clickForMore', review ))[0];

	if( restOfReview && clickForMore ) {
	    Element.hide( restOfReview );
	    Element.show( clickForMore );
	}
    }
    
    return;
} // toggleReview

var global_remember_tracks = true;
function toggleTracks( evt )
{
    var meth = 'toggleTracks:';
    log( meth + "START" );

    if( !evt ) evt = event;
    
    var target = evt.type && evt.type == "tracks_response" ? evt.target
               :                                             Event.element( evt )
               ;
    log( meth + "Found target element: " + target );
    
    // Get the HTML element that contains the tracks
    var tracksEl = null;
    
    var parent = target;
    while( parent && !Element.hasClassName( parent, "album" ) ) {
	parent = parent.parentNode;
    }
    log( meth + "Found parent" );

    if( parent ) {
	tracksEl = parent.select( '.tracks' )[0];
    }
    log( meth + "Found tracks element" );

    if( tracksEl ) {
	if( !tracksEl.state ) tracksEl.state = "empty";

	// If user wants to hide tracks
	log( "About to check track status" );
	if( parent.hasClassName( 'tlExpanded' ) ) {
	    // Then hide the tracks
	    log( "About to hide tracks" );
	    Element.removeClassName( parent, 'tlExpanded' );
	    Element.addClassName( parent, 'tlCollapsed' );

	    if( !global_remember_tracks ) {
		tracksEl.innerHTML = "";
		tracksEl.state = "empty";
	    }
	}
	else { // Else, show me the tracks	   
	    log( "About to figure out how to show tracks" );

	    // If tracks were already loaded
	    if( tracksEl.state == "loaded" ) {
		// Then show unhide the tracks
		log( "About to show already loaded tracks" );
		Element.removeClassName( parent, 'tlCollapsed' );
		Element.addClassName( parent, 'tlExpanded' );
	    }
	    else { // Else, we need to load the tracks
		// If this is actually a response to our track info request
		if( evt.type == "tracks_response" ) {
		    // Create tracks HTML from response
		    var ajaxReq = evt.ajax_request;
		    log( "AJAX Response: " + ajaxReq.responseText.escapeHTML() );

		    var tracksHTML = ajaxReq.responseText;

		    // Stick tracks HTML into tracks element
		    var newDiv = document.createElement( 'DIV' );
		    newDiv.innerHTML = tracksHTML;
		    log( meth, 'Loaded tracks HTML into new div' );
		    
		    var newTracksEl = Element.select( newDiv, 'table' )[0];
		    log( meth, 'Retrieved new tracks element' );

		    tracksEl.parentNode.replaceChild( newTracksEl, tracksEl );
		    //tracksEl.innerHTML = tracksHTML;

		    // Unhide the tracks
		    log( "About to show tracks that were just loaded" );
		    Element.removeClassName( parent, 'tlCollapsed' );
		    Element.addClassName( parent, 'tlExpanded' );

		    tracksEl.state = "loaded"
		}
		else if( tracksEl.state != "loading" ) { // Else, we need to request track info
		    // Get album db id from parent html id
		    var albumId = parent.id.substring( "__product_".length );
		    
		    // Send out a request for track info
		    log( "About to send out request to load track info" );
		    var showTracks = function (req) {
			log( "onCompleted called" );
			var fakeEvent    = {
			    target: target,
			    type:   "tracks_response",
			    ajax_request: req
			};
			
			toggleTracks( fakeEvent );
		    }; // showTracks

		    tracksEl.state = "loading"
		    var myJax = new Ajax.Request( 
			 "/services/get_album_tracks.html",
			{
			    method:     'get',
			    parameters: 'aid=' + escape(albumId),
			    onComplete: showTracks
			} );
		} // else, send request for tracks
	    } // else, load the tracks
	} // else, show the tracks
    } // if found tracks container

    return false;
} // toggleTracks

function sendVote( evt )
{
    if( !evt ) evt = event;

    var target = Event.element( evt );

    var propsEl = null;
    var parent  = target;
    while( parent && parent.id.indexOf( "props-info-" ) != 0 ) {
	parent = parent.parentNode;
    }
    if( parent ) {
	propsEl = document.getElementsByClassName( "props", parent )[0];
    }

    if( propsEl ) {
	log( "Checking vote state of target" );
	if( !target.vote_state ) {
	    target.vote_state = "sending";
	    
	    var voteAccepted = function (req) {
		var fakeEvent = {
		    target:       target,
		    ajax_request: req
		};
	    
		log( "Calling sendVote with fake event" );
		sendVote( fakeEvent );
	    }; //voteAccepted

	    var productId = parent.id.substr( "props-info-".length );

	    log( "Sending ajax request for product id: " + productId );
	    var myJax = new Ajax.Request(
		 "/services/vote_for_product.xml",
		{
		    method:     'post',
		    parameters: 'pid=' + escape(productId),
		    onComplete:  voteAccepted
	    });

	    Element.hide( target );
	} // if no vote placed yet
	else if( target.vote_state == "sending" && evt.ajax_request ) {
	    log( "Got response from Ajax: " + evt.ajax_request.responseText.escapeHTML() );
	    var ajaxReq   = evt.ajax_request;
	    var response  = ajaxReq.responseXML.documentElement;
	    var propCount = response.getAttribute( 'prop-count' );
	    
	    log( "Updating props to " + propCount );
	    propsEl.innerHTML = propCount;

	    target.vote_state = "sent";
	} // if got response from ajax
    } // if found props element
} // sendVote

function vote( evt )
{
    if( !evt ) evt = event;

    var target = Event.element( evt );
    var status = target.status;

    if( !status ) {
	var attempts = target.vote_attempts || 1;
	var msg      = 'Voting not available at this time.';
	
	if( attempts <= 1 ) {
	    msg += '  Please wait a few seconds and then try again.';
	}
	
	attempts++;
	target.vote_attempts = attempts;

	alert( msg );
    } else if( status == 'login' ) {
	var parent    = target.parent;
	var login     = (document.getElementsByClassName( 'vote-login', parent ))[0];
	var loginLink = login ? (login.getElementsByTagName( 'A' ))[0] : null;

	if( loginLink ) {
	    Element.show( login );
	    target.onclick = function () { location.href = loginLink.href; };
	}
	else {
	    alert( 'Voting not available at this time.' );
	}
    } else if( status == 'vote' ) {
	return sendVote( evt );
    } else if( status == 'novote' ) {
	alert( 'You have already voted for this product.' );
    }

    return;
} // vote

function loadDataPostCache( req )
{
    var func = 'loadDataPostCache: ';
    log( func + "START" );
    
    // If this is the initial call from onLoad
    if( !req || !req.responseText ) {
    	// Get all products that need post cache data (props, rank, etc)
		var productIds = $A( document.getElementsByClassName( '__product_def' ) ).collect(
		      function(elem) {
				  var prefix = '__product_';
				  var id     = elem.id.substr( prefix.length );
				  return id;
		      } ); // productIds =
		log( func + "Found the following product ids: " + productIds );
	
		// Send out request for post cache data
		var myJax = new Ajax.Request( 
		     "/services/get_page_data.xml",				 
		    {
				method:     'get',
				parameters: 'pid=' + productIds.join( '&pid=' ),
				onComplete: loadDataPostCache
		    } );
    }
    // Else, if this is the response from our post cache data request
    else {
		log( func + "Response: " + req.responseText.escapeHTML() );
		
		// Flip any add to cart buttons
		addToCart_ItemAdded( req );
				
		// Flip any wishlist buttons
		addToWishlist_ItemAdded( req );
	
		// Get the catalog - post cache data
		var resp    = req.responseXML.documentElement;
		var catalog = resp.getElementsByTagName( 'catalog' )[0];
	
		var totalDigitalProducts = catalog.getAttribute( 'total-digital-products' );
		var products             = $A( catalog.getElementsByTagName( 'product' ) );
		
		products.each( 
		  function( prod ) {
		    var productId   = prod.getAttribute( 'id' );
		    var props       = prod.getAttribute( 'props' );
		    var digitalRank = prod.getAttribute( 'digital-rank' );
		    log( func + "Product ID: " + productId + "; Props: " + props + "; Digital Rank: " + digitalRank );
	
		    var propsElem = $( 'props-' + productId );
		    if( propsElem ) propsElem.innerHTML = props;
	
		    var horizontalRanking 
			= '<font color="#000000" size="-2" face="Arial, Helvetica, sans-serif">xx</font>'
			+ '<font color="#006600">rank :</font> <font color="#999999">' + digitalRank 
			+ ' of ' + totalDigitalProducts + '</font>'
			;
		    
		    var verticalRanking
			= '<table width="65" height="50" border="0" cellpadding="3" cellspacing="0" bgcolor="#000000"><tr>'
			+ '<td align="center"><font color="006600" size="-2" face="Arial, Helvetica, sans-serif">'
			+ '<span style="color: #006600;">' + digitalRank + '</span></font>'
			+ '<font color="#009900" size="-1" face="Arial, Helvetica, sans-serif"><br></font>'
			+ '<font color="#666666" size="-2" face="Arial, Helvetica, sans-serif">rank of<br>'
			+ totalDigitalProducts + '</font></td></tr></table>'
			;
		    
		    var horRankingElem = $( 'horizontal-ranking-' + productId );
		    if( horRankingElem ) horRankingElem.innerHTML = horizontalRanking;
	
		    var verRankingElem = $( 'vertical-ranking-' + productId );
		    if( verRankingElem ) verRankingElem.innerHTML = verticalRanking;
	
		} ); // products.each
	
		var loginStatusElm = resp.getElementsByTagName( 'login_status' )[0];

		if( loginStatusElm ) {
	
		    gIsLoggedIn =
			loginStatusElm.getAttribute( 'code' ) == '1' &&
			loginStatusElm.getAttribute( 'wantsnoaccount' ) != '1';
	
		    var votedFor = new Array();
		    $A( resp.getElementsByTagName( 'voted' ) ).each( 
			function (elm) {
			    votedFor[elm.getAttribute( 'id' )] = 1;
	            }); // votedFor
	
		    $A( document.getElementsByClassName( 'vote' ) ).each( 
			function (elm) {
			    var voteId = elm.id;
			    var prodId = voteId.substr( "vote-".length );
			    var status = !gIsLoggedIn      ? 'login'
			               : votedFor[prodId]  ? 'novote'
				       :                     'vote'
				       ;
			    
			    log( "Setting vote status for product, " 
				 + prodId + ", to: " + status );
	
			    elm.status = status;
		    }); // each vote
		} // if found logged in status element

		var userElem = resp.getElementsByTagName('user')[0];

		if (userElem && gIsLoggedIn) {

		    var username = userElem.getAttribute('username');

		    if (username.length > 20) {
			var i = username.indexOf('@');
			if (i > 20) i = 20;
			if (i > 0) username = username.substr(0, i);
		    }

		    $$('#userNav #username a').each( function(elm) {
			elm.update(username);
		    });

		    $$('#userNav #username').each( function(elm) {
			elm.removeClassName('hide');
		    });

		    $$('#userNav #login').each( function(elm) {
			elm.addClassName('hide');
		    });

		    $$('#userNav #logout').each( function(elm) {
			elm.removeClassName('hide');
		    });
		}
		else {

		    $$('#userNav #login').each( function(elm) {
			elm.removeClassName('hide');
		    });
		}

		// Populate sitewide alerts area

		var alerts = $A( resp.getElementsByTagName('alert') );
		var toptext = $( 'alerts' );

		alerts.each( function(alertelem) {

		    var index = alertelem.getAttribute('index');
		    var text = alertelem.getAttribute('text');
		    var link = alertelem.getAttribute('link');

		    var p = new Element('p', {
			'class' : 'toptext',
			'id'    : 'toptext-' + index,
			'style' : (index == 1 ? '' : 'display:none')
		    });

		    if (link) {
			var a = new Element('a', { 'href' : link }).update(text);
			p.appendChild(a);
		    }
		    else {
			p.update(text);
		    }

		    toptext.appendChild(p);

		} );

		// Kick off sitewide alerts animation

		if (alerts.length > 0) {
		    setupUtilityCyclers();
		    setTimeout( 'cycleToNextText(3500)', 3500 ); 
		}

    } // else

    return;
} // loadDataPostCache

function addEventHandler( elem, eventType, handler )
{
    var func = 'addEventHandler: ';
    log( func + "START" );
    
    // Make sure that eventType is formatted correctly (all lowercase, with no preceding "on")
    eventType = eventType.toLowerCase();
    if( eventType.substr( 0, 2 ) == "on" ) eventType = eventType.substr( 2 );
    log( func + "eventType: " + eventType );
    
    // If no handlers have been setup, yet, for this element/eventType yet
    if( !elem.handlers || !elem.handlers[eventType] ) {
	
	// Add a handlers array to this element, which contains
	// handlers for each event that this element can handle
	log( func + "Setting up handler functionality" );
	if( !elem.handlers ) elem.handlers = new Array();
	if( !elem.handlers[eventType] ) elem.handlers[eventType] = new Array();
	
	// If there already is a function assigned directly to this event type
	var currHandler = elem["on" + eventType];
	if( currHandler ) {
	    // Then tack it onto the handlers array
	    elem.handlers[eventType].push( currHandler );
	}
	
	// Setup the main function that will get called when this event is triggered
	elem["on" + eventType] = function (evt) {
	    if( !evt ) evt = event;
	    
	    var func = "addEventHandler (anon): ";

	    var target = evt.target || evt.srcElement;
	    var type   = evt.type;
	    log( func + "Got event type: " + type );

	    if( type == "load" ) target = window;
	    
	    if( target.handlers && target.handlers[type] ) {
		log( func + "Calling handlers for " + type );
		for( var idx = 0; idx < target.handlers[type].length; idx++ ) {
		    target.handlers[type][idx]();
		} // foreach handler
	    } // if target is setup correctly
	} // onEventType handler
    } // if no handlers

    // Now, tack on a new handler for this event
    log( func + "Tacking on another handler" );
    elem.handlers[eventType].push( handler );
    
    return;
} // addEventHandler

var global_do_logging = false;
function log( str )
{
    if( global_do_logging && typeof console != "undefined" ) {
	str = $A( arguments ).join( " " );
	console.log( str );
    }
    else if( global_do_logging ) {
	var logWnd = arguments.callee.logging_window;
	
	if( !logWnd || !logWnd.document ) {
	    var width  = 600;
	    var height = 400;
	    
	    var x = (screen.availWidth - width)/2 + 50;
	    var y = (screen.availHeight - height)/2 + 50;

	    logWnd = window.open( "",
				  'ttl_log_wnd', 
				  'width='  + width  + 'px, ' +
				  'height=' + height + 'px, ' +
				  'left='   + x      + ',   ' +     
				  'top='    + y      + ',   ' +
				  'scrollbars='      + 'yes'
				);
	    if( logWnd.blur ) logWnd.focus();

	    logWnd.document.write( '<html><head><title>TTL - Logs</title></head><body>'
				   + '<div><input type="button" value="CLEAR" '
				   + 'onclick="document.getElementById(' + "'logs'" + ').innerHTML=' + "''" + '"/></div>'
				   + '<div id="logs" style="height: 300px;overflow: scroll">&nbsp;</div></body></html>' );
	    logWnd.document.close();
	    
	    var logs = logWnd.document.getElementById( 'logs' );
	    //logWnd.document.body.innerHTML = "";
	    logs.innerHTML = '';

	    arguments.callee.logging_window = logWnd;
	} // if need to create new window
	
	var logs = logWnd.document.getElementById( 'logs' );
	str = $A( arguments ).join( " " );
	logs.innerHTML += str + '<br/>';
    } // if do logging
} // log

//addEventHandler( window, 'onload', loadDataPostCache );

function showSubList(subListId) {

  // hide all subLists in the sidenav except the current one

  $A( $$('.subList') ).each( function(elem) { elem.addClassName('subListHidden'); });
  $(subListId).removeClassName('subListHidden');

  if (subListId.indexOf('subList-') < 0) return;
  var sideNavId = 'sideNav-'+subListId.substr('subList-'.length);

  // highlight the currently selected subList item in the subnav menu

  $A( $$('.sideNav') ).each( function(elem) { elem.removeClassName('selected'); });
  $(sideNavId).addClassName('selected');
}

// w3c compliant target=_blank replacement
// bind to onclick and onkeypress

function target_blank( link ) {
  window.open( link.href );
  return false;
}


// For links that are replaced with javascript at onload time,
// this gets called before the replacement has occurred.

function earlyClick(elem) {
  return false;
}


