var input_text = 'Start searching...';

function alert(s) { $('#infobar').html(s).slideDown(400).click(function(){ $(this).slideUp(400); }); }

function show_ads() { $('#adbar').slideDown(400).click(function(){ $(this).slideUp(400); }); }

function showPl() { $('#playlist-container').show(); $('h3').html('Now playing...'); }

$(document).ready(function(){

	if ($('#showPl').length)  setTimeout('showPl()', Math.floor(Math.random()*10+5) * 1000);

	$('h1').click(function(){ window.location = 'http://www.musmi.com'; });

	$('#results span').mouseover(function(){ $(this.childNodes[1]).show(); $(this.childNodes[2]).show(); });
	$('#results span').mouseout(function(){ $(this.childNodes[1]).hide(); $(this.childNodes[2]).hide(); });
	
	if (!$('form[name=search] input[type=text]').val()) $('form[name=search] input[type=text]').val(input_text);
	$('form[name=search] input[type=text]').focus(function(){ $(this).css('color', '#000'); if ($(this).val() == input_text) $(this).val(''); }).blur(function(){ $(this).css('color', '#c4c4c4'); if ($(this).val() == '') $(this).val(input_text); });

	$('.disabled').fadeTo(0, 0.2).click(function(){ return false; });

	$("#sharebox").sharebox();
	$('#sharebox > li > a').tipsy({ gravity: 'n' });
	$('#sharebox > li > img, #sharebox > li > a > img').each(function(){ $(this).attr('src', $(this).attr('urn')); });

	$('.popup').each(function(){
		$(this).click(function(){
			window.open($(this).attr('href'), 'playlist', 'width=600,height=235');
			return false;
		});
	});

	$('.ajax').live('click', function(){
		if ($(this).hasClass('playlist')) {
			$.ajax({
				type: 'POST',
				url: '/_pl.php',
				data: $(this).attr('href'),
				success: function(r) {
					if (r != 'error') alert('Added <strong>'+r+'</strong> to playlist');
				}
			});
		}
		else if ($(this).hasClass('delete')) {
			$.ajax({
				type: 'POST',
				url: '/_pl.php',
				data: $(this).attr('href'),
				success: function(r) {
					if (r != 'error') {
						$('#'+r).remove();
						if ($('.delete').length == 0) $('.boxy-wrapper .title-bar .close').click();
					}
				}
			});
		}
		return false;
	});

	$('.save_pl').live('click', function(){
		$('.save_pl').remove();
		$.ajax({
			type: 'POST',
			url: '/_pl.php',
			data: 'action=save',
			success: function(r) {
				$('.playlist').html(r);
			}
		});
	});

	$('.playlist_box').click(function(){
		$.ajax({
			type: 'POST',
			url: '/_pl.php',
			data: 'action=show',
			success: function(r) {
				if (r) {
					$('#infobar').click();
					var pl = new Boxy(r, { title: 'Playlist', modal: true });
				}
				else alert('Nothing in the playlist...');
			}
		})
	});

	$('.images a').click(function(){
		var img = new Boxy('<img src="'+$(this).attr('href')+'" />', { title: 'Related image', modal: true });
		return false;
	});

	setTimeout('show_ads()', Math.floor(Math.random()*6+5) * 1000);

});
