$(function() {

	// Embed preview SWF
	swfobject.embedSWF('/swf_ddl/preview-masked.swf', 'audioPreviewer', '100', '20', '9.0.0', '/swf_ddl/expressInstall.swf', {}, {}, {id: 'PreviewPlayer', name: 'PreviewPlayer'});

	// Setup audio preview buttons
	$('a.audioPreview').click(playPreview);

});

/**
 * Play Track Preview (via Flash/ExternalInterface)
 */
function playPreview() {

    var swf = ($.browser.msie ? window : document)['PreviewPlayer'];

    if(!$(this).data('playing')) {

        $('a.audioPreview').each(function() {
            $(this).data('playing', false).css('backgroundPosition', '0 0');
        });

        $(this).data('playing', true).css('backgroundPosition', '0 -16px');

        try {
			var hash = this.href.match(/\/?(\w+)$/)[1]; // make sure we're JUST getting the hash, not a full URL that the browser might generate from the HREF attribute
            swf.sendTrack(hash); // play audio via Flash/ExternalInterface
        } catch(e) {
            if(console && console.log) console.log(e.message);
        }

    } else {

        $(this).data('playing', false).css('backgroundPosition', '0 0');
        swf.stopTrack(); // stop audio via Flash/ExternalInterface
    }

	return false;
}


/**
 * Stop Track Preview (called from Flash via Flash/ExternalInterface)
 */
function stopPreview() {
    $('a.audioPreview').data('playing', false).css('backgroundPosition', '0 0');
}