var commentReplyId;

$(document).ready(function(){
	add_reply_action();
	add_copy_action();
	add_other_link_action();
	
	add_vote_positif_action();
	add_vote_negatif_action();
	add_seesmic_action();
	
	add_accordeon_action();

	$('#yoururlbox').hide();

	//$('#comments-submit').attr('value','');
	/*
	if(typeof sIFR == "function"){
	    sIFR();
	};

	if(typeof sIFR == "function"){
    	sIFR.replaceElement("div#main-content h1", named({sFlashSrc: "/public/sifr/futuraheavy.swf", sColor: "#ffffff", sCase: "upper", sBgColor: "#000000", sWmode:"transparent", sLinkColor: "#ffffff"}));
	}*/
});

function add_accordeon_action(){
	$('div.accordeon div.content').hide();
	$('div.accordeon:first div.content').show();
	//$('div.accordeon:not(:first) div.content').hide();
	$('div.accordeon:first').find('h3').addClass('on');
	$('div.accordeon h3').bind('click',function(){
	    if($(this).hasClass('on')){
	        return false;
	    }
	    $(this).closest('div.accordeon').siblings().find('h3').removeClass('on');
	    $(this).addClass('on');

		if($.browser.msie == true && $.browser.version == 6 ){
		    $('div.accordeon div.content').hide();
		    $(this).parent().children('div.content').show();
		    
		}else{
		    $('div.accordeon div.content').slideUp('slow');
		    $(this).parent().children('div.content').slideDown('slow');
		    
		}
		
		return false;
	}).bind('mouseover', function(){
	    $(this).css('cursor', 'pointer');
	}).bind('mouseout', function(){
    	$(this).css('cursor', 'default');
    });
}

function add_copy_action(){
	$('#copy-link').click(function(){
		$(this)[0].focus();
		$(this)[0].select();	
	});
}

function add_other_link_action(){
	$('div#partager ul.plusoutils').hide();
	$('div#partager li a.outils').click(function(){
		$(this).toggleClass('plus');
		$(this).toggleClass('moins');
		$('div#partager ul.plusoutils').slideToggle('slow');
		return false;
	})
}

function add_reply_action(){
	$('a.reply').click(function(){
		$(this).parent().parent().children('form').remove();
		
		//on ajoute le formulaire
		html = $('#add-comments').html();
		
		id = $(this).parent().parent().attr('id').split('-')[1];
		
		html = '<form action="#add-comments" method="post">'+html+'<p class="button-cancel"><input type="button" class="cancel" value="Annuler" /><input type="hidden" name="cite" value="'+id+'" /></p></form>';
		
		$(this).parent().parent().append(html);
		$('#see_buttons_div:first').remove();
		$('#block-comment-video:first').remove();
		$('#block-comment-text:first').attr('id','nothingtodo')
		add_seesmic_action()
		
		
		$(this).parent().addClass('hide');
		
		$('input.cancel').click(function(){
			$(this).parent().parent().parent().find('p.hide').removeClass('hide');
			$(this).parent().parent().remove();
			
		});
		
		$(this).parent().parent().children('form').submit(function(){
			//on efface les messages d'erreurs
			$(this).find('.error').remove();
			
			msgError = new Array();
			//on valide le formulaire
			if($(this).find('#comments-name').val()==''){
				msgError.push('Le champ Votre nom est requis.');
			}
			if($(this).find('#comments-email').val()==''){
				msgError.push('Le champ Votre courriel est requis.');
			}else{
				if (!echeck($(this).find('#comments-email').val())) {
					msgError.push('Le champ Votre courriel doit contenir une adresse email valide.');
				}
			}
			if($(this).find('#comments-texte').val()==''){
				msgError.push('Le champ Votre commentaire est requis.');
			}
			
			if(msgError.length>0){
				$(this).prepend('<ul class="error" style="margin-top:10px;"><ul>');
				for(x=0;x<msgError.length;x++){
					$(this).find('ul.error').append('<li>'+msgError[x]+'</li>');
				}
				return false;
			}
			$(this)[0].submit();
		})
		
		return false;
	})
}

function add_seesmic_action(){
	commentReplyId=0; 
	$('#block-comment-video').hide();
	
	setInterval("checkCommentReplyChange()",500);
	
	$('a.see_buttons_add').click(function(){
		see_set_comment_mode('videoRec','comment');
		
		return false;
	})
	$('a#see_back_button').click(function(){
		see_set_comment_mode('text','comment')
		return false;
	})
	
	$('span.seePlayOverlay').click(function(){
		see_play_video($(this).attr("rel"),true);
	})
}

function checkCommentReplyChange() {
	replyIdInput = $("#comment_reply_ID");
	if(replyIdInput) {
		if(replyIdInput.val()!=commentReplyId) {
			commentReplyId=replyIdInput.val();
			see_set_comment_mode('text','comment');
		}
	}
}

function see_set_comment_mode(_mode, _recState) {
	if(_mode=="videoRec"){
		inreplyto = "";			
		if(commentReplyId==0) {
			postVideouri = $("#see_post_videouri");
			if(postVideouri.val()) {
				inreplyto=postVideouri.val();
			}else{
				commentsList = $("#see_comments_list");
				if(commentsList.val()) {
					commentsSplit=(commentsList.val()+"").split(";");
					inreplyto = commentsSplit[0].split("=")[1];
				}
			}
		}else{
			commentsList  = $("#see_comments_list");
			commentsSplit = (commentsList.val()+"").split(";");
			commentsArray = new Array();
			for (var comm in commentsSplit){
				valuesSplit=(commentsSplit[comm]+"").split("=");
				commentsArray[valuesSplit[0]]=valuesSplit[1];
			}
			if(commentsArray[commentReplyId]){
				inreplyto = commentsArray[commentReplyId];
			}
		}
		// resize flash content container
		$('#flashcontent:last').css("width","300px");
 		$('#flashcontent:last').css('height',"270px");
		
		// push flash widget
		var so = new SWFObject("http://seesmic.com/embeds/wrapper.swf?v=" + Math.floor(Math.random()*10000001), "sobject", "300", "270", "9", "#000000");
		so.addVariable("version","wprecorder");
		so.addVariable("title", "");
		so.addVariable("recState", _recState);
		so.addVariable("inreplyto", inreplyto);
		so.addVariable("blog_id", $('#see_wp_home').val());
		so.addVariable("allowAnonymous", $("#see_allow_anonymous").val());
		so.addVariable("blog_name", $('#see_wp_name').val());
		so.addParam("allowScriptAccess","always");
		so.useExpressInstall('swfobject/expressinstall.swf');
		so.write("flashcontent");
		
		$('#block-comment-text:last').hide();
		$('#block-comment-video:last').show();
		
	}else{
		// remove widget
 		if ($('#flashcontent:last')) {
	 		$('#flashcontent:last').html("");
			$('#flashcontent:last').css('width',"10px");
	 		$('#flashcontent:last').css('height','10px');
 		}
 		
 		$('#block-comment-text:last').show();
		$('#block-comment-video:last').hide();
	}
}
function see_play_video(_videoUri, _add) {
	if(_add) {
		// push flash widget
		var so = new SWFObject("http://seesmic.com/embeds/wrapper.swf?v=" + Math.floor(Math.random()*10000001), "sotester", "300", "270", "9", "#000000");
		so.addVariable("video", _videoUri);
		so.addVariable("version","wpplayer");
		so.addParam("allowFullScreen", "true");
		so.addParam("allowScriptAccess","always");
		so.useExpressInstall('swfobject/expressinstall.swf');
		so.write("see_" + _videoUri + "_content");
		
		$('#see_'+_videoUri+'_content').show();	
		$("#see_" + _videoUri + "_preview").hide();
	} else {
		$("#see_" + _videoUri + "_preview").show();
		$('#see_'+_videoUri+'_content').hide();
	}

}
function videoFromRecorder(videoUri, title, url_thumbnail, recState)
{
	if(recState=="comment") {		
		newVideo = '{seesmic_video:{';
			newVideo += '"url_thumbnail":{"value":"' + url_thumbnail + '"}';
			newVideo += '"title":{"value":"'+title+'&nbsp;"}';
			newVideo += '"videoUri":{"value":"' + videoUri + '"}';
		newVideo += '}}';
	
		$("#comments-texte").val(newVideo);
		$("#add-comments").submit();
	}

	return 'posted'
}

