$(document).ready(function(){
	var player;
    if($("#flowplayer").length > 0){
    	$f("flowplayer", $("#player_url").val(), { 
    	    clip: { 
	    		provider: 'rtmp',
    			autoplay: false,
    			autoBuffering: false,
				url: $("#video_name").val(),
				bufferLength: 5
    	    },
    	    plugins: {
    	        rtmp: { 
	    	    	url: 'flowplayer.rtmp-3.1.3.swf',
	    	    	netConnectionUrl: $("#video_server").val()
    	        },
	        	myContent: {
    	        	url: 'flowplayer.content-3.1.0.swf',
    	        	width: "100%",
    	        	height: "100%",
    	        	borderRadius: 0,
    	        	left: 10,
    	        	backgroundColor: 'transparent', 
    	        	backgroundGradient: 'none', 
    	        	border: 0, 
    	        	html: ($("#video_fly").length > 0)?'<img src="' + $("#video_fly").val() + '"/>':''
    	        }
    	    } 
    	});
    	player = $f(0);
    }
    
    if($("#stream_flowplayer").length > 0){

    	$f("stream_flowplayer", $("#player_url").val(), { 
    	    clip: { 
	    		provider: 'rtmp',
    			autoBuffering: false,
			url: $("#video_name").val(),
			bufferLength: 3,
			autoPlay: true
    	    },
	    onLoad: function(){
	            var fplayer = $f("stream_flowplayer");
		    window.setTimeout(function(){
          	        fplayer.toggle();
		    },5000);
            },

    	    plugins: {
    	        rtmp: { 
	    	    	url: 'flowplayer.rtmp-3.1.3.swf',
	    	    	netConnectionUrl: $("#video_server").val(),
	    	    	live: true
    	        },
	        	myContent: {
    	        	url: 'flowplayer.content-3.1.0.swf',
    	        	width: "100%",
    	        	height: "100%",
    	        	borderRadius: 0,
    	        	left: 10,
    	        	backgroundColor: 'transparent', 
    	        	backgroundGradient: 'none', 
    	        	border: 0, 
    	        	html: ($("#video_fly").length > 0)?'<img src="' + $("#video_fly").val() + '"/>':''
    	        }
    	    } 
    	});
    	player = $f(0);
    }
	
    /*
     * FIXME: Los streamNames hardcodeados no molan un cagao
     */
	if($("#publisher_url").length > 0){
	    flashembed("publisher", {
			// src: $("#publisher_url").val(),
			src: 'publisher.swf',
			width: "100%",
			height: "100%",
		},{
		    streamName: $("#streamName").val(),
		    viewStreamName: $("#viewStreamName").val(),
		    publishStreamName: $("#publishStreamName").val()
		});
	}

	$(".preview_image").preload({
		find: ".png",
		replace: ".gif"
		
	});
	$('.preview_image').hover(function(){
		this.src = this.src.replace('.png','.gif');
	},function(){
		this.src = this.src.replace('.gif','.png');
	});
	$('#remove_file').click(function(){
		var dialog_el = $("<div />");
		dialog_el.html(lang_strings["DELETE FILE TEXT"]);
		dialog_el.dialog({
			width: 400,
			close: function(){
				$(this).dialog("destroy")
			},
			buttons:{
				"OK": function(){
					$.getJSON($("#base").val()+"ajax/remove_file/"+$("#remove_file_id").val(),{},function(data){
						alert(data.txt);
						if(data.errorcode == 0){
							$("#remove_file").parent().remove();
						}
					});
					$(this).dialog("close");
				},
				"Cancel": function(){
					$(this).dialog("close");
				}
			}
			
		});
		return false;
	});

	if($("#fhora_inicio").length > 0){
		$("#ffecha_inicio").datepicker({
			showOn: "both",
			buttonImage: $("#calendar_img").val(),
			buttonImageOnly: true,
			dateFormat: 'dd/mm/yy',
			minDate: new Date()
		});
	}

	if($("#fhora_inicio").length > 0){
		$("#fhora_inicio").timeEntry({
			show24Hours: true, 
			showSeconds: false,
			spinnerImage: $("#timer_img").val()
		});
	}
	
	if(typeof(player) != 'undefined'){
		if($("#embedTab").length){
			$("#embedTab").click(function(){
				var txt = '<div class="embed_code"><textarea>' + player.embed().getEmbedCode() + '</textarea></div>';
				$(txt).dialog({
					width: 600,
				    height: 350,
				    title: 'Code'
				});
				
				$(".embed_code textarea").click(function(){
					$(this).select();
				})
			});
		}
	}
});

