$(document).ready(function() {

$("#seed_search_form").submit(function() {
	if ($("#seed_song").val() == $("#seed_song").attr('message') && $("#seed_artist").val() == $("#seed_artist").attr('message'))
		return false;
	$("#seed_song2").val($("#seed_song").val());
	$("#seed_artist2").val($("#seed_artist").val());
	$("#seed_song2").css("color", $("#seed_song").css("color"));
	$("#seed_artist2").css("color", $("#seed_artist").css("color"));
	find_song($("#seed_song").val(), $("#seed_artist").val());
	return false;
});

$("#seed_search_btn").click(function() {
	if ($("#seed_song").val() == $("#seed_song").attr('message') && $("#seed_artist").val() == $("#seed_artist").attr('message'))
		return false;
	$("#seed_song2").val($("#seed_song").val());
	$("#seed_artist2").val($("#seed_artist").val());
	$("#seed_song2").css("color", $("#seed_song").css("color"));
	$("#seed_artist2").css("color", $("#seed_artist").css("color"));
	find_song($("#seed_song").val(), $("#seed_artist").val());
	return false;
});

$("#seed_search_form2").submit(function() {
	if ($("#seed_song2").val() == $("#seed_song2").attr('message') && $("#seed_artist2").val() == $("#seed_artist2").attr('message'))
		return false;
	$("#seed_song").val($("#seed_song2").val());
	$("#seed_artist").val($("#seed_artist2").val());
	$("#seed_song").css("color", $("#seed_song2").css("color"));
	$("#seed_artist").css("color", $("#seed_artist2").css("color"));
	find_song($("#seed_song2").val(), $("#seed_artist2").val());
	return false;
});

$("#seed_search_btn2").click(function() {
	if ($("#seed_song2").val() == $("#seed_song2").attr('message') && $("#seed_artist2").val() == $("#seed_artist2").attr('message'))
		return false;
	$("#seed_song").val($("#seed_song2").val());
	$("#seed_artist").val($("#seed_artist2").val());
	$("#seed_song").css("color", $("#seed_song2").css("color"));
	$("#seed_artist").css("color", $("#seed_artist2").css("color"));
	find_song($("#seed_song2").val(), $("#seed_artist2").val());
	return false;
});

$(".submit_preset_btn").click(function() {
	if ($("#seed_song_permalink").val() != "") {
		$.ajax({
			type: "POST",
			url: "/discover/ajax/recommend-song/",
			data: {
				permalink: $("#seed_song_permalink").val(),
				genre_weight: $("#genre_weight").val(),
				era_weight: $("#era_weight").val(),
				origin_weight: $("#origin_weight").val()
			},
			beforeSend: function(){
				$("#recommendation_list").html('<div><img src="/images/common/loading.gif" align="absmiddle" style="padding-right: 5px;" />Loading</div>');
			},
			success: function(xml){
				$("#recommendation_list").html(xml);
				window.location.hash = 'recommendations';
				setup_playlist();
				setup_niftyplayer('#recommendation_list [id^=inline-player-]');
			}
		});
	}
});

$(".reset_preset_btn").click(function() {
	$("#genre_slider").slider("moveTo", 50, 0);
	$("#era_slider").slider("moveTo", 50, 0);
	$("#origin_slider").slider("moveTo", 50, 0);
});

$(".discover-slider").slider({
	handle: ".discover-slider-handle",
	min: 99,
	max: 1,
	change: slider_change,
	slide: slider_move
});

$(".discover-slider-value").change(function() {
	this.value = this.value.replace(/[^0-9]/g, '');
	if (this.value == "" || this.value < 1) this.value = 1;
	update_slider(this);
});

$(".search_song").focus(function() {
	if ($(this).val() == $(this).attr('default') || $(this).val() == $(this).attr('message')) {
		$(this).val("")
	}
	$(this).css("color", "black");
});

$(".search_song").blur(function() {
	if ($(this).val() == "") {
		$(this).val($(this).attr('message'))
		$(this).css("color", "#B8B8B8");
	}
});

//find_song($("#seed_song").val(), $("#seed_artist").val());
$(".search_song").blur();

});

function find_song(seed_song, seed_artist, skip) {
	if (seed_song == $("#seed_song").attr('message')) seed_song = "";
	if (seed_artist == $("#seed_artist").attr('message')) seed_artist = "";
	if (seed_song == "" && seed_artist == "") return false;

	$.ajax({
		type: "POST",
		url: "/discover/ajax/find-song/",
		data: {
			song: seed_song,
			artist: seed_artist,
			skip: skip ? skip : 0
		},
		beforeSend: function(){
			$("#seed_song_result").html('<img src="/images/common/loading.gif" align="absmiddle" style="padding-right: 5px;" />Loading');
		},
		success: function(xml){
			$("#seed_song_result").html(xml);
			$("#seed_song_result2").html($("#seed_song_result").html());
			$(".result_song_title").each(function(){
				$(this).attr("title", $(this).text());
				$(this).parent().siblings().attr("title", $(this).parent().siblings().text().replace(/^- /, ''));
/*
				len = $(this).width() + $(this).parent().siblings().width();
				if (len > 260) {
					if ($(this).width() > 160) {
						if ($.browser.mozilla) {
							$(this).width(250-Math.min($(this).parent().siblings().width(), 100));
							$(this).after("<b>...</b>");
						} else {
							$(this).width(260-Math.min($(this).parent().siblings().width(), 100));
						}

						if ($(this).parent().siblings().width() + 170 > 260) {
							if ($.browser.mozilla) {
								$(this).parent().siblings().width(80);
								$(this).parent().siblings().after("<span>...</span>");
							} else {
								$(this).parent().siblings().width(90);
							}
						}
					} else {
						if ($.browser.mozilla) {
							$(this).parent().siblings().width(250-$(this).width());
							$(this).parent().siblings().after("<span>...</span>");
						} else {
							$(this).parent().siblings().width(260-$(this).width());
						}
					}
				}
*/
			}); 
			$(".result_song_title").click(function(){
				select_seed_song($(this).attr("permalink"));
				return false;
			}); 

			if (jQuery.trim($("#seed_song_info").text()) == "") {
				select_seed_song($(".result_song_title:first").attr('permalink'), '1');
			}
		}
	});

	return false;
}

function select_seed_song(permalink, nojump) {
	$("#seed_song_permalink").val(permalink);

	$.ajax({
		type: "POST",
		url: "/discover/ajax/seed-song/",
		data: {
			permalink: permalink
		},
		beforeSend: function(){
			$("#seed_song_info").html('<img src="/images/common/loading.gif" align="absmiddle" style="padding-right: 5px;" />Loading');
		},
		success: function(xml){
			$("#seed_song_info").html(xml);
		}
	});

	$.ajax({
		type: "POST",
		url: "/discover/ajax/recommend-song/",
		data: {
			permalink: permalink,
			genre_weight: $("#genre_weight").val(),
			era_weight: $("#era_weight").val(),
			origin_weight: $("#origin_weight").val()
		},
		beforeSend: function(){
			$("#recommendation_list").html('<div><img src="/images/common/loading.gif" align="absmiddle" style="padding-right: 5px;" />Loading</div>');
		},
		success: function(xml){
			$("#recommendation_list").html(xml);
			if (!nojump) window.location.hash = 'results';
			setup_playlist();
			setup_niftyplayer('#recommendation_list [id^=inline-player-]');
		}
	});
}

function slider_move(e, ui) {
	var type = $(this).attr("type");
	$("#"+type+"_weight").val(ui.value);
}

function slider_change(e, ui) {
	var type = $(this).attr("type").match(/2/) ? $(this).attr("type").replace(/2/, '') : $(this).attr("type") + '2';

	if ($("#"+type+"_slider").slider("value", 0) != ui.value)
		$("#"+type+"_slider").slider("moveTo", ui.value, 0);
}

function update_slider(obj) {
	if (obj.value < 1 || obj.value > 99) return false;
	$("#"+obj.id.replace(/weight/, 'slider')).slider("moveTo", obj.value, 0);
}

function navigate(skip) {
	find_song($("#seed_song").val(), $("#seed_artist").val(), skip);
}

function save_all_tracks() {
	var ids = "";

	$("#recommendation_list img[playlist_song_id]").each(function() {
		ids = ids + $(this).attr("playlist_song_id") + ",";
	});

	if($("#playlist_song_id").length<1)
		$(document.body).append("<input type='hidden' id='playlist_song_id' value='"+ ids +"' />");
	else
		$("#playlist_song_id").val(ids);

	if($("#member_logged_in").val()=="yes")
	{
		show_playlist_dialog();
	} else
	{
		show_login_dialog("show_playlist_dialog");
	}
}
