$(document).ready(function() 
{  
	ulcolors();
	playlist_mouseover();
	repeat_button();
	sortable_init();
	
	delsong();
	
	
	var film = getFlashMovie('prima_tv');
	//alert(film);
	//alert(film.width);
	//alert(film.PercentLoaded());
	if(film.PercentLoaded() != 100)
	{
		$("#writer").everyTime(100, function () 
		{
			if(film.PercentLoaded() == 100)
			{
				
				$("#writer").stopTime();
				start_movie();
				
			}
		});
	}
	else
	{
		start_movie();
	}
});


function getFlashMovie(movieName) 
{
  var isIE = navigator.appName.indexOf("Microsoft") != -1;
  return (isIE) ? window[movieName] : document[movieName];
 }

function load_movie(film, stream)
{
	film.decryptAndLoad(stream, 1);
}
	



/* FUNCTII */

function sortable_init()
{
	$(".play-ul").sortable(
	{
		stop: function () 
		{
			
        	ulcolors();
        	
        	//stabilim ordinea dupa sortare si salvam
        	var order = "";
        	var count = 0;
        	
        	$(".playlist_order").each(function () {
        		if(count == 0)
        			order = $(this).val();
        		else
        			order += ","+$(this).val();
        		
        		count = 1;	
        	});
        	
        	
        	$.ajax(
	 		 {
				   type: "GET",
				   url: "http://www.mediadirect.ro/primatv/ajax/ordine.php?ord="+order,
				   success: function (msg) { }
	 		});
	 		//end stabilire ordine
	 		
	 		
	 		var nextsong = $(".nowplaying").next().children(".play-right").children(".playlistmel").attr("href");
	 		$("#nextsong").val(nextsong);
	 		
		}
		,
		opacity: 0.7,
		axis: 'y',
		containment: $("#playlist_content")
	});
}
 
function repeat_button()
{
	$("#repeat-butt").click(
	function () 
	{
		var self = $(this);
		
		if($("#repeat-val").val() == 0)
		{
			$.ajax(
	 		 {
				   type: "GET",
				   url: "http://www.mediadirect.ro/primatv/repeat.php?state=on",
				   success: function(msg)
				   {
				   	
						self.attr('src', 'http://www.mediadirect.ro/primatv/img/repeat.png');
						$("#repeat-val").val(1);
						$("#repeta-text").text("Repeta lista de redare").css("color", "green");
				   }
	 		});
		}
		else
		{
			$.ajax(
	 		 {
				   type: "GET",
				   url: "http://www.mediadirect.ro/primatv/repeat.php?state=off",
				   success: function(msg)
				   {
						self.attr('src', 'http://www.mediadirect.ro/primatv/img/repeat_no.png');
						$("#repeat-val").val(0);
						$("#repeta-text").text("Nu repeta lista de redare").css("color", "#333333");
				   }
	 		});
		}
	});
}

function playlist_mouseover()
{
	$("#playlist ul li").mouseover(function () 
	{
		var pyr = $(this).children(".play-right");
		pyr.children(".manuta").css("display", "block");
		
		$(this).addClass("playme");

	}).mouseout(function () 
	{
		var pyr = $(this).children(".play-right");
		pyr.children(".manuta").css("display", "none");
		
		$(this).removeClass("playme");
	
	});
}
 
function ulcolors()
{
	var i = 0;
	$("#playlist ul li").each(function () 
	{
		if($(this).hasClass("nowplaying"))
		{
			
		}
		else
		{
			$(this).removeClass("playlist-even playlist-odd");
			if(i%2 == 0)
				$(this).addClass("playlist-even");
			else
				$(this).addClass("playlist-odd");
		}	
		i++;
	});
}

function delsong()
{
	$(".hisdels").click(function (event) 
	{
		var id = $(this).parent("div").parent(".play-left").children(".playlist_order").val();
		var self = $(this).parent("div").parent(".play-left").parent("li");
		self.fadeOut("slow", function () 
		{ 
			var de_sters = $(this);
			$.ajax(
	 		 {
				   type: "GET",
				   url: "http://www.mediadirect.ro/primatv/delsong.php?song="+id,
				   success: function (msg) 
				   {
				   		de_sters.remove();
						ulcolors();
						
						var nextsong = $(".nowplaying").next().children(".play-right").children(".playlistmel").attr("href");
				 		$("#nextsong").val(nextsong);
				   }
	 		});
			
		});
		
		
	});
}
