/* VIDEO */     
nosend = 0;  
player_ready = false; 
function create_player(target,player_id,width,height,file,image,duration,autostart)
{   
	var so = new SWFObject('/script/player-licensed.swf', player_id, width, height, '9', '#000000');
	so.addParam('allowfullscreen','true');
	so.addParam('allowscriptaccess','always');
	so.addVariable('controlbar',  'over'); 
	so.addVariable('stretching','fill'); 
	so.addVariable("file",file);  
	if(duration)
	so.addVariable('duration',duration);
	if(image)  
	so.addVariable("image",image);  
	if(autostart)
	so.addVariable('autostart',  'true'); 
	so.write(target);  
}

/* INSTANTIATION DU PLAYER */
function playerReady(obj)
{
	var id = obj['id'];
	var version = obj['version'];
	var client = obj['client'];
	player = document.getElementById(id); 
	if(player.id == 'mpl'|| player.id == 'hdmpl')
	{
		player.addModelListener('STATE', 'statem');
		player_ready = true;
	}  
};

/* ARRÊTE LE PLAYER */
function stop_player()
{   
	if(player_ready==true)
	player.sendEvent("STOP","true");
}

/* CHANGEMENT D'ÉTAT DU PLAYER */
function statem(obj)
{   
   if((obj.newstate == "PAUSED"||obj.newstate == "COMPLETED"))
	{
		$('#body').animate({backgroundColor: "#f7f7f7", color: "#000"}, 500);
		$('#container').animate({backgroundColor: "#f7f7f7"}, "slow");
		$('#footer').show(); 

		if(perm)
		{
			var duration = (Math.floor(player.getConfig().duration));
			$( '#jquery' ).load( '/page/video.php', { action: "duration_update", v: video_id, d: duration } ); 
		} 
	}
	if(obj.newstate == "BUFFERING")
	{
		$('#body').animate({backgroundColor: "#000", color: "#fff"}, 500);
		$('#container').animate({backgroundColor: "#000"});
		$('#footer').hide();
	}
	if(obj.newstate == "PLAYING")
	{	    			
		$('#body').animate({backgroundColor: "#000", color: "#fff"}, 500);
		$('#container').animate({backgroundColor: "#000"});
		$('#footer').hide();
		if(nosend == 0)
		{
			nosend = 1;
			$( '#total_view' ).load( '/page/video.php', { action: "add_view", v: video_id } );
		}
	}
}


/* FUNCTION SHOW THUMB */
function show_thumb(element)
{
	if(!element.data("init"))       
	{
	var top = Math.round((69-element.children("a").height())/2); 
	element.children("a").css("top",top+"px");  
	element.data("init",1);   
	}
	element.children("img").show();    
	element.children("a").show();
}
             

$( document ).ready( function () {

	/* COLORBOX */
	$("a[rel='colorbox']").colorbox({ opacity: 0.8, rel: "nofollow" });
	$("a[rel='colorbox_follow']").colorbox({ opacity: 0.8, current: "{current} sur {total}" });
	$("a[rel='video']").colorbox({ width: "682px", height: "475px", opacity: 0.8, rel: "nofollow" });
	$("a[rel='photo']").colorbox({ opacity: 0.8 });
	$("a[rel='comment']").colorbox({ width: "550px", height: "450px", opacity: 0.8, rel: "nofollow" });
	$("a[rel='video'],a[rel='colorbox_follow'],a[rel='comment'],a[rel='colorbox']").click(function() {
		$(".video_element").hide();
		stop_player();
	});
	$(document).bind('cbox_closed', function(){
		$(".video_element").show();
	});

	/* COMMUNICATION */   
	if(neg)
	$(".neg_com").html(neg).show();      
	if(pos)   
	$(".pos_com").html(pos).show();
	$( '.pos_com,.neg_com' ).click( function () { $( this ).hide(); } );
	$(".pos_com,.neg_com").delay(5000).slideUp();

    
	/* VIDEO THUMB */
	$( '.video_thumb:not(.selected)' ).live('mouseover', function() {   
		show_thumb($(this));
	});

	$( '.video_thumb:not(.selected)' ).live('mouseout', function() {
		$(this).children("img").hide();   
		$(this).children("a").hide(); 
	});
	if(video_selected != '')
	{  
	$(".video_thumb").eq(video_selected).addClass("selected");
    show_thumb($(".video_thumb").eq(video_selected));
	}
	
	
	/* SUGGEST */
	var timeout = null; 
	$("#search_input").live('keyup',function () {
		if (timeout)
		clearTimeout (timeout);  
		if( $("#search_input").val().length > 3)
		{
			timeout = setTimeout ( function(){ $('#search_selector').text("").load("/manager.php", { action: "search", q: $("#search_input").val() }).show(); }, 500);   
		}
	});
	$('body').click(function() {
		$('#search_selector').hide();
	}); 


	/* ENVOIE COMMENT */
	$( '.toggle_comment' ).click(function() {
		$( '#comment_form' ).slideToggle();
	});
	$( '#send_comment' ).live('click', 
	function () { 
		if($( "#comment_value" ).val()&&$( "#name_value" ).val())
		{
			$( "#comment_count" ).load( "/page/video.php", { action: "add_comment", video: video_id, comment: $( "#comment_value" ).val(), name: $( "#name_value" ).val() } );
			$( '#comment_form' ).slideUp();
			$( '.toggle_comment' ).text("Merci pour le commentaire!").removeClass("oc cursor").unbind('click');
		}
		else
		alert("Formulaire incomplet");
	} );


	/* TOGGLE HD */
	$("#toggle_hd").toggle(function() {
		$(".video_element").hide(); 
		$("#hd_container").parent().show();
		$("#hd_container").show();
		$("#sd_container").parent().hide();   
		$("#toggle_hd").text("Visionner en SD");
	}, function() {
		$(".video_element").hide();
		$("#sd_container").parent().show();  
		$("#sd_container").show();
		$("#hd_container").parent().hide();
		$("#toggle_hd").text("Visionner en HD"); 
	});


	/* NO LEFT CLICK */
	$( '.no_left_click' ).click(function() {
		alert("Cliquez avec l'autre bouton de la souris");
		return false;
	});


	/* FORGET NAME */
	$( '#forget_name' ).click(function() {
		$(this).prev().val("");
		$(this).hide();
		$( '#jquery' ).load( '/page/video.php', { action: "logout" } );
	});


	/* STAR VOTE */
	$( '.star_vote' ).hover( 
		function() { 
			$( this  ).prevAll('.star_vote').removeClass("star_vote_frame").addClass("star_vote_fill");
			$( this  ).removeClass("star_vote_frame").addClass("star_vote_fill");
		},
		function() {  
			$( '.star_vote:not(.star_vote_selected)'  ).removeClass("star_vote_fill").addClass("star_vote_frame"); 
		}
	);
	$( '.star_vote' ).click( function(){ 
		$( '.star_vote'  ).nextAll('.star_vote').removeClass("star_vote_selected").addClass("star_vote_frame");
		$( this  ).prevAll('.star_vote').removeClass("star_vote_frame").addClass("star_vote_selected");
		$( this  ).removeClass("star_vote_frame").addClass("star_vote_selected");
		var votevalue = $( this ).attr("votevalue");
		var videovalue = $( this ).parent().attr("videovalue");
		$( '#vote_ratio' ).load( '/page/video.php', { o: videovalue, vote: votevalue } );
	} );


	/* CAROUSSEL */   
	if(video_id)
	{   
		video_caroussel = (video_caroussel)? video_caroussel:0;
		$('#video_caroussel').jcarousel({
			start: video_caroussel,
			scroll: 6
		});  
	}

	/* CLICK OPEN */
	$( '.clickopen' ).click( function(){ 
		if( $( this ).next().is(':hidden') )
		$( this ).next().slideDown();
		else
		$( this ).next().slideUp();
	});


	/* MODIFICATION */  
	if(perm)
	{
		$(".cm").live('dblclick', function() {
			if($(this).find(".cm_send").length == 0)
			{
				$(this).html("<textarea class='cm_send' style='width: "+$(this).width()+"px; height: "+$(this).height()+"px;'>"+$(this).html().replace(/(<br>)/ig,"")+"</textarea>");
			}
		});

		$(".cm_send").live('focusout', function(event) {
			$('#jquery').load("/manager.php", { action: "modify", value: $(this).val(), id: $(this).closest(".cm").attr("cm_id"), field: $(this).closest(".cm").attr("cm_f"), table: $(this).closest(".cm").attr("cm_tb")});
			$(this).parent(".cm").html($(this).val());
		});

		$( ".delete_ajax" ).live( 'click', function () { 
			if(confirm("Effacer?"))
			$( this  ).closest(".content").load( "/manager.php", { action: "delete", table: $(this).attr('delete_table'), id: $(this).attr('delete_id') });
		});   
	}

});
