if ( ! window.libri ) {
	window.libri = {};
}
if ( ! window.libri.accordion ) {
	window.libri.accordion = (function() {
		var _selectedIndexes = {};
		var _getSelectedIndex = function ( accordionId ) {
			//console.debug("getSelectedIndex: "+accordionId);
			if ( ! _selectedIndexes[accordionId] ) {
				return 0;
			}
			return Number(_selectedIndexes[accordionId]);
		}
		var _setSelectedIndex = function ( accordionId, value ) {
			//console.debug("setSelectedIndex: "+accordionId+" -> "+value);
			_selectedIndexes[accordionId] = value;
 		}
		return {
			"getSelectedIndex": _getSelectedIndex,
			"setSelectedIndex": _setSelectedIndex
		};
	})();
}

$(document).ready(function() {
    $(".accordion").each( function () {
    	var id = $(this).attr("id");
    	$(this).accordion( {
	    	autoHeight: false, 
	    	clearStyle: true, 
	    	collapsible: true,
	    	header: 'h3',
	    	active: libri.accordion.getSelectedIndex( id )
    	} )
    } );
    
    $("#bestsellerAccordion").accordion({
    	autoHeight: true, 
    	clearStyle: false, 
    	collapsible: true,
    	header: 'h3',
	active: true
    });

    $(".accordion h3", "#leftColumn").click(function(e) {
    	var contentDiv = $(this).next("div");
    	var a = $(this).find("a");
        if ( a && a.attr("href") && !contentDiv.is(".accordionLoaded") ) {
        	contentDiv.load($(this).find("a").attr("href"));
            contentDiv.addClass("accordionLoaded");
        }
    });

    //$(document).pngFix();
});
