 var currentItem = 0
        var currentState = "";
        var currentPosition = 0;

        var currentItemDuration = 0;

        function itemMonitor(obj) {
                currentItem = obj;
                $$('.playlistItem').removeClass('active');
                $('item_'+currentItem.index).addClass('active');
                $$('.nowplaying').set('html','');
                $$('.nowplaying_'+currentItem.index).set('html',' (currently playing)');
        }

        function stateMonitor(obj) {
                currentState = obj;

                if(currentState.newstate == 'PLAYING') {
                        // A new song is playing!
                        // Deliberately rough time logging
                        currentItemDuration = $('jwplayer').getPlaylist()[currentItem.index].duration.round(0)-1;
                        logRequest.send('track='+$('jwplayer').getPlaylist()[currentItem .index].title+' - start');
                }

                if(currentState.newstate == 'PLAYING') {
                        $('b_pause').setStyle('display','inline');
                        $('b_buffering').setStyle('display','none');
                        $('b_play').setStyle('display','none');
                } else if(currentState.newstate == 'BUFFERING') {
                        $('b_buffering').setStyle('display','inline');
                        $('b_pause').setStyle('display','none');
                        $('b_play').setStyle('display','none');
                } else {
                        $('b_play').setStyle('display','inline');
                        $('b_pause').setStyle('display','none');
                        $('b_buffering').setStyle('display','none');
                }

        }


        function positionListener(obj) {

                // I don't trust the JavaScript enough to deal in anything other than whole seconds
                if(obj.position.round(0) != currentPosition) {

                        currentPosition = obj.position.round(0);

                        if(currentPosition == (currentItemDuration/4).round(0)) {
                                logRequest.send('track='+$('jwplayer').getPlaylist()[currentItem .index].title+' - 25%');
                        } else if(currentPosition == ((currentItemDuration/4)*2).round(0)) {
                                logRequest.send('track='+$('jwplayer').getPlaylist()[currentItem .index].title+' - 50%');
                        } else if(currentPosition == ((currentItemDuration/4)*3).round(0)) {
                                logRequest.send('track='+$('jwplayer').getPlaylist()[currentItem .index].title+' - 75%');
                        } else if(currentPosition == currentItemDuration.round(0)) {
                                logRequest.send('track='+$('jwplayer').getPlaylist()[currentItem .index].title+' - 100%');
                        }

                }

        }

        function playerReadyCallback() {
                $('jwplayer').addControllerListener('ITEM', 'itemMonitor');
                $('jwplayer').addModelListener('STATE', 'stateMonitor');
                $('jwplayer').addModelListener('TIME', 'positionListener');
        }



window.addEvent('domready', function() {


        if($('mp3player')) {
	logRequest = new Request({method:'get',url: '/log.php'});


        $('b_play').addEvent('click', function(e){
                e.stop();
                $('jwplayer').sendEvent('PLAY','true');
        });





        $('b_pause').addEvent('click', function(e){
                e.stop();
                $('jwplayer').sendEvent('PLAY','false');
        });



        $$('.playlistStart').addEvent('click', function(e){
                e.stop();
                $('jwplayer').sendEvent('PLAY','true');
        });

        $$('.playlistItem').addEvent('click', function(e){
                e.stop();
                $('jwplayer').sendEvent('ITEM',this.id.replace('item_',''));
        });



        new Swiff('/js/mediaplayer/player.swf',{
                'id' : 'jwplayer',
                'name' : 'jwplayer',
                'container' : $('mp3player'),
                'allowscriptaccess': 'always',
                'width' : 300,
                'height' : 20,
                'vars' : {
                        'playerReady': 'playerReadyCallback',
                        'file': '/playlist-samples.xml',
                        'autostart': 'false',
                        'repeat': 'list',
                        'autostart': 'false'

                }
        });

        }


	$$('.tablink').addEvent('click',function(evt) {
		// Stop the event
		evt.stop();
		// Hide all tabs
		$$('.tab_content').setStyle('display','none');
		$$('UL.tabs LI.active').removeClass('active');
		// Show this tab
		$(this.get('id').replace('tablink','tab')).setStyle('display','block');
		this.getParent('li').addClass('active');
	});


if($('contact_form')) {
      var contactFormRequest = new Request.HTML({
                url:'/index.php?type=ajax',
                link: 'cancel',
                update:'contact_form_response',
                onRequest: function() {
                        $('contact_form_response').set('html','Sending...');
                },
                onSuccess: function(html){
                 	if($('contact_form_response').get('html').test("Thank you")) {
                                $('contact_form').destroy();
                        }
                }
        });


        $('contact_form').addEvent('submit', function(e) {
         	e.stop();
                contactFormRequest.post($('contact_form'));
        });

}

    if($$('h3.toggler')) {
            var myAccordion = new Accordion($('accordion'), 'h3.toggler', 'div.element', {
                    opacity: false,
                    alwaysHide: true,
                    display: -1
            });

    }



})
