Site = {
    applySIFR: function()
    {
        $(document).ready(function(){
    	    if (typeof sIFR == 'function')
        	{
        		sIFR.replaceElement(
        			named(
        				{
        					sSelector: "h1.siffer",
        					sFlashSrc: "/sifr/tradegothic.swf",
        					sColor: "#d7c99f",
        					sBgColor: "#007dc3",
        					sFlashVars: "textalign=left"
        				}
        			)
        		);
        	}
    	});
    }
}
Player = {
    popPlayer: false,
	player: null,
	songs: [],
	current_song: 0,
	playing: false,
	songLoaded: false,
	muted: false,
	attachToClicks: function()
	{
	    // Inline player AJAX loading
		$(document).ready(function(){
		    $('#nav-inner li a, .news a, #filter a, .talent-item a, #talent-nav a, table#tour-dates a').each(function(k, v){
    		    $(this).unbind('click');
    		    $(this).click(function(){
    		        if ($(this).parent().attr('id').match(/^n-/))
    		        {
    		            $('#nav-inner li').removeClass('current');
    		            $(this).parent().addClass('current');
    		        }
        		    var url = $(this).attr('href');
        		    $.post(url, {ajax: true}, function(data){
        		        var m = data.match(/--slug:(.+)--/);
        		        $('body').attr('class', 'page-' + m[1]);
        		        
        		        // Which one?
        		        if ($('#ajax-content').attr('id') == 'ajax-content')
        		            $('#ajax-content').html(data);
        		        else
        		            $('#content').html(data);
        		            
        		        Site.applySIFR();
        		    });
        		    return false;
        		});
    		});
		});
	},
	init: function(songs)
	{
	    // Only attach if this is *not* available songs
	    if (location.href.match(/available/) == null)
	        Player.attachToClicks();
	    
	    $('#player').hide();
	    
		// Initialize songs
		$.each(songs, function(k, v){
			Player.songs.push(v);
		});
		
		// Create the player
		var flashvars = {
			file: '',
			autostart: 'false'
		};
		var params = {
			allowfullscreen: 'true',
			allowscriptaccess: 'always'
		};
		var attributes = {
			id: 'playz0r',
			name: 'playz0r'
		};
		swfobject.embedSWF('/player.swf', 'player-swf', '300', '120', '9.0.115', false, flashvars, params, attributes);
		$('#playz0r').css('position', 'absolute').css('top', '-1000em').css('left', '-1000em');
		$('#player-song').html('');
		
		// Control states
		$('#player-controls a[id!=ctl-pop]').addClass('disabled');
		$('#ctl-play,#ctl-prev,#ctl-next').removeClass('disabled');
	},
	play: function()
	{
		if (!Player.hasSongs() || Player.playing)
			return;
			
		if (!Player.current_song) Player.current_song = 0;
		
		var song = Player.songs[Player.current_song];
		
		// Only load if not loaded already
		if (!Player.songLoaded)
			Player.player.sendEvent('LOAD', song['file']);
			
		Player.player.sendEvent('PLAY', true);
		Player.playing 		= true;
		Player.songLoaded 	= true;
		
		// Status
		if ($.browser.safari)
			$('#player-song').html(song['title']).show();
		else
			$('#player-song').html(song['title']).show();
		
		// Pub only
		var splitsie = song['title'].match(/(.+)&ndash;(.+)/);
		$('#player-song-wrap b').html(splitsie[2]);
		$('#player-song-wrap i').html(splitsie[1]);
		
		// Controls
		$('#ctl-play').addClass('disabled');
		$('#ctl-pause,#ctl-stop').removeClass('disabled');
		$('#player-volume').show();
		
	},
	playSongExtern: function(hash, site_code)
	{
	    if (Player.popPlayer)
	    {
	        // Nothing.
	    }
	    else
	    {
	        // Voodoo.
	        var w = window.open('/' + site_code + '/songs/pop_player', 'popplayer', 'width=512,height=31,scrollbar=no,menubar=no,status=no,statusbar=no,toolbar=no');
	        Player.popPlayer = w;
	    }
	    Player.popPlayer.focus();
	    Player.popPlayer.Player.playSong(hash);
	    return false;
	},
	playSong: function(hash)
	{
		Player.stop();
		
		// Find the proper song index and update Player.current_song
		$.each(Player.songs, function(k, v){
			if (v['hash'] == hash)
			{
				Player.current_song = k;
				return false;
			}
		});
		
		Player.play();
	},
	playNext: function()
	{
		if (!Player.hasSongs())
			return;
			
		Player.stop();
		
		if (Player.current_song == (Player.songs.length - 1))
			Player.current_song = 0;
		else
			Player.current_song++;
		
		Player.play();
	},
	playPrev: function()
	{
		if (!Player.hasSongs())
			return;
		
		Player.stop();
			
		if (Player.current_song == 0)
			Player.current_song = (Player.songs.length - 1);
		else
			Player.current_song--;
		
		Player.play();
	},
	pause: function()
	{
		if (!Player.hasSongs() || !Player.playing)
			return;
			
		Player.player.sendEvent('PLAY', false);
		Player.playing = false;
		
		$('#ctl-play').removeClass('disabled');
		$('#ctl-pause,#ctl-stop').addClass('disabled');
	},
	stop: function()
	{
		if (!Player.hasSongs() || !Player.playing)
			return;
			
		Player.player.sendEvent('PLAY', false);
		Player.playing 		= false;
		Player.songLoaded 	= false; // So play will load from the beginning
		
		$('#player-song').html('').hide();
		$('#player-time').html('');
		$('#player-volume').hide();
		
		$('#ctl-play').removeClass('disabled');
		$('#ctl-pause,#ctl-stop').addClass('disabled');
		
		// Reset timer
		Player.lastTime = 0;
	},
	unmute: function()
	{
		Player.muted = false;
		Player.player.sendEvent('MUTE', false);
	},
	mute: function()
	{
		if (Player.muted)
		{
			Player.muted = false;
			$('#ctl-volume').css('background-image', 'url(/assets/images/deniserich/player/volume_on.png)');
			Player.player.sendEvent('MUTE', false);
		}
		else
		{
			Player.muted = true;
			$('#ctl-volume').css('background-image', 'url(/assets/images/deniserich/player/volume_off.png)');
			Player.player.sendEvent('MUTE', true);
		}
	},
	hasSongs: function()
	{
		return (Player.songs.length > 0) ? true : false;
	}
}

// Callbacks
Player.lastTime			 = 0;
Player.callbackModelTime = function(o)
{
	// Reduce the number of times we actually update the time display to reduce flicker
	var position = o.position;
	if (Player.lastTime == 0 || (position - Player.lastTime >= 0.5))
	{
		var sec = Math.floor(position % 60);
		var min	= Math.floor((position - sec) / 60);
		
		var display_time = min + ':' + (sec < 10 ? '0' + sec : sec);
		$('#player-time').html(display_time);
		
		Player.lastTime = position;
	}
}

function playerReady(obj)
{
	// Can't use $() as jquery hoses the swfobject stuff
	Player.player = document.getElementById('playz0r');
	if (Player.player === false)
		Player.player = document.getElementsByName('playz0r')[0];
		
	// Assign callbacks
	Player.player.addModelListener('TIME', 'Player.callbackModelTime');
	
	$('#player').show();
	
	// Auto-play if popup
	//if (/pop_player/.test(window.location))
	//{
	    setTimeout('Player.play();', 125);
	//    Player.popPlayer                = window;
    //   window.opener.Player.popPlayer  = window;
	//}
	
	// If IE, hide everything...
	if ($.browser.msie)
	{
	    //$('#player-swf embed,#player-swf object,#player-swf').css('visibility', 'hidden').css('left', '-1000em').css('top', '-1000em');
	}
}

/**
* Override IE's default document.getElementById() behavior (stupidly matching on id *and* name)
* From: http://msdn.microsoft.com/en-us/library/ms536437(VS.85).aspx
*/
if (/msie/i.test (navigator.userAgent)) //only override IE
{
	document.nativeGetElementById = document.getElementById; 
	document.getElementById = function(id)
	{
		var elem = document.nativeGetElementById(id);
		if(elem)
		{
			//make sure that it is a valid match on id
			if(elem.attributes['id'].value == id)
			{
				return elem;
			}
			else
			{
				//otherwise find the correct element
				for(var i=1;i<document.all[id].length;i++)
				{
					if(document.all[id][i].attributes['id'].value == id)
					{
						return document.all[id][i];
					}
				}
			}
		}
		return null;
	}
}