/*** menu code ***/
var timeout	= closetimer = ddmenuitem = 0;
// open hidden layer
function mopen(id)
{	
	// roll over state
	document.images.but8.src='images/layout/menu/store_up.gif'

	// cancel close timer
	mcancelclosetime();

	// close old layer
	if(ddmenuitem) ddmenuitem.style.visibility = 'hidden';

	// get new layer and show it
	ddmenuitem = document.getElementById(id);
	ddmenuitem.style.visibility = 'visible';
}
// close showed layer
function mclose()
{
	if(ddmenuitem) ddmenuitem.style.visibility = 'hidden';
}
// go close timer
function mclosetime()
{
	closetimer = window.setTimeout(mclose, timeout);
	
	// roll out state
	document.images.but8.src='images/layout/menu/store.gif'
}

// cancel close timer
function mcancelclosetime()
{
	if(!closetimer) return;
	window.clearTimeout(closetimer);
	closetimer = null;
	
	// roll over state
	document.images.but8.src='images/layout/menu/store_up.gif'
}

// close layer when click-out
document.onclick = mclose;

/*** player interface ***/
var PlayerHandler = 
{
	playlistItemTemplate: "\n" + 
'	<ul class="playlist_row" id="row_#{aIndex}_#{tIndex}" ' + 
'			onmouseover="PlayerHandler.onMouseOver(this);" '+
'			onmouseout="PlayerHandler.onMouseOut(this);"' +
'			onclick="PlayerHandler.onClick(this, #{aIndex}, #{tIndex});">' + "\n" + 
'		<li class="pt_no">#{tNumber}</li>' + "\n" + 
'		<li class="pt_title">#{title}</li>' + "\n" + 
'		<li class="pt_length">#{duration}</li>' + "\n" + 
'	</ul>',
	flashObj: null,

	init: function() 
	{
		this.flashObj = document.getElementById("mp3PlayerObject");
	},
	
	highlightPlaylistRow: function( albumIndex, trackIndex )
	{
		$('.playlist_row').removeClass('selected');
		$('#row_'+albumIndex+"_"+trackIndex).addClass('selected');
	},
	
	addPlaylistItem: function( albumIndex, trackIndex, linkText, trackLength )
	{
		this.debug("addPlaylistItem: function( "+albumIndex+", "+trackIndex+", "+linkText+", "+trackLength+" )");
		var containerElt = $('#album_'+albumIndex);
		if (!containerElt.length) {
			this.debug("No container element #album_"+albumIndex+" found!");
			return;
		}
		this.debug("Found container #album_"+albumIndex);
		
		var replaceObj = {
			aIndex:albumIndex,
			tIndex:trackIndex,
			tNumber:parseInt(trackIndex,10) + 1,
			title:linkText,
			duration:trackLength
		};
		var newRow = this.playlistItemTemplate;
		for( var i in replaceObj ) 
		{
			var regex = new RegExp( '#{'+i+'}', 'g' );
			newRow = newRow.replace( regex, replaceObj[i] );
		}
		
		this.debug("Appending row HTML: "+newRow);
		
		containerElt.append( newRow );
	},
	
	onMouseOver: function( elt )
	{
		if (!$(elt).hasClass('selected')) $(elt).addClass('mouseover');
	},
	
	onMouseOut: function( elt )
	{
		$(elt).removeClass('mouseover');
	},
	
	onClick: function( elt, albumIndex, trackIndex )
	{
		PlayerHandler.highlightPlaylistRow( albumIndex, trackIndex );
		PlayerHandler.flashObj.onPlaylistRowClick( albumIndex, trackIndex );
	},
	
	debug: function(s)
	{
		// console.log(s);
	}
};

/*** flash player code ***/

var flashvars = { playlist:"playlist.xml", autoplay:true, loop:true, volume:0.5, startAlbum:2, startTrack:1 };
var params = { allowNetworking:"true", allowscriptaccess:"internal", wmode:"transparent" };
var attributes = { id:"mp3PlayerObject" };
swfobject.embedSWF("MP3Player.swf", "music_player", "586", "28", "9", null, flashvars, params, attributes);

/*** mailing list form code ***/
var emailRegex = /[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/i;
jQuery(document).bind('ready', function(){
	jQuery('#txtMlistEmail').focus(function(){ $(this).select(); });
	jQuery('#frmMailingList').submit(function(){ 
		if (!emailRegex.test(jQuery('#txtMlistEmail').val())) {
			jQuery('#MList_ErrorHolder').html('<div class="form_errors">The email you entered was not valid.</div>');
			return false;
		}
	});
});