$(function(){
	
	$("a.vote_up").click(function(){
	//get the id
	the_id = $(this).attr('rel');
	
	//fadeout the vote-count 
	$("span#votes_count"+the_id).fadeOut("fast");
	
	//the main ajax request
		$.ajax({
			type: "POST",
			data: "action=vote_up&id="+$(this).attr("rel"),
			url: "../../content/facts-and-fun/votes.php",
			success: function(msg)
			{				
				$("span#votes_count"+the_id).html(msg);
				//fadein the vote count
				$("span#votes_count"+the_id).fadeIn();
				
				//remove the rejected option
				$("#sucks_vote_"+the_id).fadeOut("fast");
				
				// resort list by net votes
				$("#songs>tbody>tr").tsort("span.votes_count",{order:"desc"});
			}
		});
	return false;
	});
	
	$("a.vote_down").click(function(){
	//get the id
	the_id = $(this).attr('rel');
	
	// show the spinner
	// $(this).parent().html("<img src='images/spinner.gif'/>");
	
	//the main ajax request
		$.ajax({
			type: "POST",
			data: "action=vote_down&id="+$(this).attr("rel"),
			url: "../../content/facts-and-fun/votes.php",
			success: function(msg)
			{
				$("span#votes_count"+the_id).fadeOut();
				$("span#votes_count"+the_id).html(msg);
				$("span#votes_count"+the_id).fadeIn();
				
				$("#nice_vote_"+the_id).fadeOut("fast");
				// $("#nice_vote_"+the_id).addClass("hide_contents");
				$("#songs>tbody>tr").tsort("span.votes_count",{order:"desc"});
			}
		});
	return false;
	});
	
	
// imeem integration
$("a.play_song").click(function(){

	// fadein a cover for the imeem player
	
	
	// stop ie caching content
	var stop_ie_cache = Math.random();
	
	// ajax request for imeem code
	// $("#imeem_player").load("../../content/facts-and-fun/imeem_generate_2.php", { stop_ie_cache: stop_ie_cache, imeem_code: $(this).attr("rel")}, function(){ $("div#imeem_player_hide").fadeTo(3000, 1).fadeOut("slow") });
		
	var linkrel = $(this).attr("rel");
	var imeem_code = linkrel.replace(" nofollow", "");
  	
   	// swfobject
	var flashvars = {};
	flashvars.backColor = "000000";
	flashvars.primaryColor = "999999";
	flashvars.secondaryColor = "4d4d4d";
	flashvars.linkColor = "666666";
	var params = {};
	params.wmode = "transparent";
	
	var attributes = {};
	$("div#imeem_player_hide").fadeIn(1000, function() { swfobject.embedSWF("http://media.imeem.com/m/"+imeem_code, "imeem_player", "300", "110", "9.0.0", "/flash/expressInstall.swf", flashvars, params, attributes)});
	
	$("div#imeem_player_hide").fadeTo(3000, 1).fadeOut("slow");
	
	return false;
	});
});	

/**
* Default input text
* Based on code by Rob Schmitt (webdeveloper.beforeseven.com)
*/

var active_color = '#666'; // Colour of user provided text
var inactive_color = '#999'; // Colour of default text

$(document).ready(function() {
  $("input.default-value").css("color", inactive_color);
  var default_values = new Array();
  $("input.default-value").focus(function() {
    if (!default_values[this.id]) {
      default_values[this.id] = this.value;
    }
    if (this.value == default_values[this.id]) {
      this.value = '';
      this.style.color = active_color;
    }
    $(this).blur(function() {
      if (this.value == '') {
        this.style.color = inactive_color;
        this.value = default_values[this.id];
      }
    });
  });
});

$(document).ready(function() {
	// alert($("#song_name_inputs").val());
	// submit song validation
	$("form#submit_song_form").submit(function(){
	 	var song_name = $("input.text").val();
		var song_artist = $("input.song_artist_input").val();
		
		if(song_name == "" || song_name == "song") {
			
			
			$("#failed_validation").fadeIn();
			$("#song_name_error").show();
			
			var failed_validation = 1;
		}
		
		if(song_artist == "" || song_artist == "artist") {
			
			$("#failed_validation").fadeIn();
			$("#artist_name_error").show();
			var failed_validation = 1;
		}
		
		if(failed_validation == 1) {
			return false;
		}
		else {
			$("#failed_validation").fadeOut('fast');
			$("#submit_song_form").fadeOut('fast');
			$("#submit_song_form").load("../../content/facts-and-fun/submit_songs.php?song_title="+song_name+"&song_artist="+song_artist+"&ajax=y");
			$("#submit_song_form").fadeIn();
			return false;
		}
		
	});
	
	// fix ie flash embed
	objects = document.getElementsByTagName("object");
	for (var i = 0; i < objects.length; i++)
	{
	    objects[i].outerHTML = objects[i].outerHTML;
	}

});
