$(document).ready(function() {
	window.dragging = 0;

	$("#ron_webwidget_playbackbar").click(function(){
		soundManager.destroySound('currentsong');
		$("#ron_webwidget_playwrapper").animate({
			left: '+=300'
		}, 200, function() {
			$("#ron_webwidget_track").html("<img class=\"ron_webwidget_loader\" src=\""+window.ron_webwidget_path+"assets/img/ajax-loader.gif\" alt=\"loading...\" />");
		});
	});

	$("#ron_webwidget_commentbackbar").click(function(){
		$("#ron_webwidget_commentwrapper").animate({
			left: '+=300'
		}, 200, function() {
			$("#ron_webwidget_trackcomments").html("<img class=\"ron_webwidget_loader\" src=\""+window.ron_webwidget_path+"assets/img/ajax-loader.gif\" alt=\"loading...\" />");
		});
	});

	$("#ron_webwidget_addcommentbackbar").click(function(){
		$("#ron_webwidget_addcommentwrapper").animate({
			left: '+=300'
		}, 200, function() {});
	});

	$("#ron_webwidget_flag1backbar").click(function(){
		$("#ron_webwidget_flag1wrapper").animate({
			left: '+=300'
		}, 200, function() {});
	});

	$("#ron_webwidget_flag2backbar").click(function(){
		$("#ron_webwidget_flag2wrapper").animate({
			left: '+=300'
		}, 200, function() {});
	});

	$("#ron_webwidget_addrating_s").live('click', function() {
		var song_id = $("#ron_webwidget_currentsong").val();
		$.ajax({
			url: window.ron_webwidget_path + 'ajax/rate.php?v=s&s='+song_id,
			success: function(data) {
				if(data == 'FALSE'){}else{
					$("#ron_webwidget_addrating_s").attr('id', 'ron_webwidget_addrating_s_active');
					$(".ron_webwidget_addrating_button").addClass('ron_webwidget_addrating_button_inactive');
				}
			}
		});
	});

	$("#ron_webwidget_addrating_m").live('click', function() {
		var song_id = $("#ron_webwidget_currentsong").val();
		$.ajax({
			url: window.ron_webwidget_path + 'ajax/rate.php?v=m&s='+song_id,
			success: function(data) {
				if(data == 'FALSE'){}else{
					$("#ron_webwidget_addrating_m").attr('id', 'ron_webwidget_addrating_m_active');
					$(".ron_webwidget_addrating_button").addClass('ron_webwidget_addrating_button_inactive');
				}
			}
		});
	});

	$("#ron_webwidget_addrating_l").live('click', function() {
		var song_id = $("#ron_webwidget_currentsong").val();
		$.ajax({
			url: window.ron_webwidget_path + 'ajax/rate.php?v=l&s='+song_id,
			success: function(data) {
				if(data == 'FALSE'){}else{
					$("#ron_webwidget_addrating_l").attr('id', 'ron_webwidget_addrating_l_active');
					$(".ron_webwidget_addrating_button").addClass('ron_webwidget_addrating_button_inactive');
				}
			}
		});
	});

	$("#ron_webwidget_addrating_xl").live('click', function() {
		var song_id = $("#ron_webwidget_currentsong").val();
		$.ajax({
			url: window.ron_webwidget_path + 'ajax/rate.php?v=xl&s='+song_id,
			success: function(data) {
				if(data == 'FALSE'){}else{
					$("#ron_webwidget_addrating_xl").attr('id', 'ron_webwidget_addrating_xl_active');
					$(".ron_webwidget_addrating_button").addClass('ron_webwidget_addrating_button_inactive');
				}
			}
		});
	});

	$("#ron_webwidget_addrating_xxl").live('click', function() {
		var song_id = $("#ron_webwidget_currentsong").val();
		$.ajax({
			url: window.ron_webwidget_path + 'ajax/rate.php?v=xxl&s='+song_id,
			success: function(data) {
				if(data == 'FALSE'){}else{
					$("#ron_webwidget_addrating_xxl").attr('id', 'ron_webwidget_addrating_xxl_active');
					$(".ron_webwidget_addrating_button").addClass('ron_webwidget_addrating_button_inactive');
				}
			}
		});
	});

	$("#ron_webwidget_readcomments_flag").live('click', function() {
		$("#ron_webwidget_flag1wrapper").animate({
			left: '-=300'
		}, 200, function() { });
	});

	$("#ron_webwidget_recent").click(function(){
		ron_webwidget_disable_buttons();
		$("#ron_webwidget_recent").addClass('ron_webwidget_recenton');
		$("#ron_webwidget_track_listings").html("<img class=\"ron_webwidget_loaderhome\" src=\""+window.ron_webwidget_path+"assets/img/ajax-loader.gif\" alt=\"loading...\" />");
		$("#ron_webwidget_track_listings").load(window.ron_webwidget_path + 'ajax/sort.php?s=recent');
	});
	$("#ron_webwidget_featured").click(function(){
		ron_webwidget_disable_buttons();
		$("#ron_webwidget_featured").addClass('ron_webwidget_featuredon');
		$("#ron_webwidget_track_listings").html("<img class=\"ron_webwidget_loaderhome\" src=\""+window.ron_webwidget_path+"assets/img/ajax-loader.gif\" alt=\"loading...\" />");
		$("#ron_webwidget_track_listings").load(window.ron_webwidget_path + 'ajax/sort.php?s=featured');
	});
	$("#ron_webwidget_heat").click(function(){
		ron_webwidget_disable_buttons();
		$("#ron_webwidget_heat").addClass('ron_webwidget_heaton');
		$("#ron_webwidget_track_listings").html("<img class=\"ron_webwidget_loaderhome\" src=\""+window.ron_webwidget_path+"assets/img/ajax-loader.gif\" alt=\"loading...\" />");
		$("#ron_webwidget_track_listings").load(window.ron_webwidget_path + 'ajax/sort.php?s=heatindex');
	});
	$("#ron_webwidget_top").click(function(){
		ron_webwidget_disable_buttons();
		$("#ron_webwidget_top").addClass('ron_webwidget_topon');
		$("#ron_webwidget_track_listings").html("<img class=\"ron_webwidget_loaderhome\" src=\""+window.ron_webwidget_path+"assets/img/ajax-loader.gif\" alt=\"loading...\" />");
		$("#ron_webwidget_track_listings").load(window.ron_webwidget_path + 'ajax/sort.php?s=toprated');
	});
	$("#ron_webwidget_fame").click(function(){
		ron_webwidget_disable_buttons();
		$("#ron_webwidget_fame").addClass('ron_webwidget_fameon');
		$("#ron_webwidget_track_listings").html("<img class=\"ron_webwidget_loaderhome\" src=\""+window.ron_webwidget_path+"assets/img/ajax-loader.gif\" alt=\"loading...\" />");
		$("#ron_webwidget_track_listings").load(window.ron_webwidget_path + 'ajax/sort.php?s=halloffame');
	});
	
	$('#ron_webwidget_searchbox').keyup(function(e) {
		if(e.keyCode == 13) {
			if($("#ron_webwidget_searchbox").val() != '')
			{
				ron_webwidget_disable_buttons();
				$("#ron_webwidget_track_listings").html("<img class=\"ron_webwidget_loaderhome\" src=\""+window.ron_webwidget_path+"assets/img/ajax-loader.gif\" alt=\"loading...\" />");
				$("#ron_webwidget_track_listings").load(window.ron_webwidget_path + 'ajax/sort.php?s=search&q='+$("#ron_webwidget_searchbox").val());
				$("#ron_webwidget_searchbox").attr('value', '');
			}else{
				
			}
		}
	});


	$(".ron_webwidget_controlplay").live('click', function() {
		$("#ron_webwidget_controlplaypause").removeClass("ron_webwidget_controlplay");
		$("#ron_webwidget_controlplaypause").addClass("ron_webwidget_controlpause");

		var mysound = soundManager.getSoundById('currentsong');

		mysound.play({
		  whileplaying:function() {
			  if(window.dragging == 0)
				$('#ron_webwidget_controlscrubber').slider("value", Math.round(this.position));
			//soundManager._writeDebug('sound '+this.sID+' playing, '+this.position+' of '+this.duration);
		  }

		});
	});

	$(".ron_webwidget_controlpause").live('click', function() {
		$("#ron_webwidget_controlplaypause").removeClass("ron_webwidget_controlpause");
		$("#ron_webwidget_controlplaypause").addClass("ron_webwidget_controlplay");
		soundManager.pause('currentsong');
	});

	$("#ron_webwidget_readcomments_button").live('click', function() {
		ron_webwidget_load_comments($("#ron_webwidget_currentsong").attr('value'));
	});

	$("#ron_webwidget_addcommentbutton").click(function(){
		ron_webwidget_load_commentform();
	});

	$("#ron_webwidget_postcommentbutton").click(function(){
		if(window.ron_webwidget_lockaddcomment != 1)
		{

			window.ron_webwidget_lockaddcomment = 1;
			$.ajax({
				type: "POST",
				url: window.ron_webwidget_path + 'ajax/add_comment.php',
				data: "e="+$("#ron_webwidget_addcomment_email_input").val()+"&p="+$("#ron_webwidget_addcomment_password_input").val()+"&text="+$("#ron_webwidget_addcomment_comment_input").val()+"&tid="+$("#ron_webwidget_currentsong").val(),
				success: function(data) {
					if(data == "TRUE")
					{
						ron_webwidget_newcomment_getcomments($("#ron_webwidget_currentsong").val());
					}else{
						alert(data);
					}
					window.ron_webwidget_lockaddcomment = 0;
				}
			});
		}
	});

	$("#ron_webwidget_flag_lang").click(function(){
		$("#ron_webwidget_flag1wrapper").animate({
			left: '+=300'
		}, 200, function() {});
		$.ajax({
			type: "POST",
			url: window.ron_webwidget_path + 'ajax/add_flag.php',
			data: "s="+$("#ron_webwidget_currentsong").val()+"&r=profanity",
			success: function(data) {alert(data);}
		});

	});

	$("#ron_webwidget_flag_copy").click(function(){
		$("#ron_webwidget_flag1wrapper").animate({
			left: '+=300'
		}, 200, function() {});
		$.ajax({
			type: "POST",
			url: window.ron_webwidget_path + 'ajax/add_flag.php',
			data: "s="+$("#ron_webwidget_currentsong").val()+"&r=copyrighted",
			success: function(data) {alert(data);}
		});

	});

	$("#ron_webwidget_flag_other").click(function(){
		$("#ron_webwidget_flag2wrapper").animate({
			left: '-=300'
		}, 200, function() {});
	});

	$("#ron_webwidget_submitflag").click(function(){
		$("#ron_webwidget_flag1wrapper").animate({
			left: '+=300'
		}, 0, function() {});
		$("#ron_webwidget_flag2wrapper").animate({
			left: '+=300'
		}, 200, function() {});
		$.ajax({
			type: "POST",
			url: window.ron_webwidget_path + 'ajax/add_flag.php',
			data: "s="+$("#ron_webwidget_currentsong").val()+"&r=" + encodeURI($("#ron_webwidget_flag_reason").val()),
			success: function(data) {
				alert(data);
			}
		});

	});

	$("#ron_webwidget_controlscrubber").live('click', function() {
		ron_webwidget_pausesong();
	});

});

function ron_webwidget_disable_buttons()
{
	$("#ron_webwidget_recent").removeClass('ron_webwidget_recenton');
	$("#ron_webwidget_featured").removeClass('ron_webwidget_featuredon');
	$("#ron_webwidget_heat").removeClass('ron_webwidget_heaton');
	$("#ron_webwidget_top").removeClass('ron_webwidget_topon');
	$("#ron_webwidget_fame").removeClass('ron_webwidget_fameon');
}

function ron_webwidget_load_track(id)
{
	$("#ron_webwidget_playwrapper").animate({
		left: '-=300'
	}, 200, function() {
	// Animation complete.
	});

	var f = 1;
	if(window.flashinstalled == 'false')
	{
		f = 0;
	}
	
	$("#ron_webwidget_track").load(window.ron_webwidget_path + 'ajax/get_track.php?tid=' + id + '&f='+f, function(){
		var song_url = $("#ron_webwidget_currentsong_url").val();
		var currentsong = soundManager.createSound({
			id:'currentsong',
			url: song_url,
			autoLoad: true,
			multiShot: false,
			onload: function(){
				$("#ron_webwidget_currentsong_length").attr('value', this.duration);
				$('#ron_webwidget_controlscrubber').slider({ min: 0, max:this.duration, step:10,
					stop: ron_webwidget_setsongposition,
					start: ron_webwidget_pausesong
				});
			},
			onfinish: function(){
				$("#ron_webwidget_controlplaypause").removeClass("ron_webwidget_controlpause");
				$("#ron_webwidget_controlplaypause").addClass("ron_webwidget_controlplay");
			}
		});


	});
}

function ron_webwidget_setsongposition()
{
		soundManager.pause('currentsong');

		//this fixes a soundmanager bug with setposition to 0. 
		if($( "#ron_webwidget_controlscrubber" ).slider( "option", "value" ) == 0) {
			$('#ron_webwidget_controlscrubber').slider("value", 10);
		}

		soundManager.setPosition('currentsong', ($( "#ron_webwidget_controlscrubber" ).slider( "option", "value" )));
		//mysound.setPosition($( "#ron_webwidget_controlscrubber" ).slider( "option", "value" ));
		window.dragging = 0;
		soundManager._writeDebug('position set to' + $( "#ron_webwidget_controlscrubber" ).slider( "option", "value" ));

		if(window.resumeOnScrub == 1)
		{
			soundManager.play('currentsong');
		}
}

function ron_webwidget_pausesong()
{
		var mysound = soundManager.getSoundById('currentsong');
		window.dragging = 1;
		if(mysound.paused == 0)
		{
			window.resumeOnScrub = 1;
			mysound.pause();
		}else{ 
			window.resumeOnScrub = 0;
		}

}

function ron_webwidget_load_comments(id)
{
	$("#ron_webwidget_commentwrapper").animate({
		left: '-=300'
	}, 200, function() {
	// Animation complete.
	});

	$("#ron_webwidget_trackcomments").load(window.ron_webwidget_path + 'ajax/get_comments.php?tid=' + id, function(){

	});
}

function ron_webwidget_newcomment_getcomments(id)
{
	$("#ron_webwidget_addcommentwrapper").animate({
		left: '+=300'
	}, 200, function() {
	// Animation complete.
	});

	$("#ron_webwidget_trackcomments").load(window.ron_webwidget_path + 'ajax/get_comments.php?tid=' + id, function(){

	});
}

function ron_webwidget_load_commentform()
{
	$("#ron_webwidget_addcommentwrapper").animate({
		left: '-=300'
	}, 200, function() {
	// Animation complete.
	});
}
