/*
 *	@titre: dreamers.
 *	@description: javascript dreamers - dream on.
 *	@auteur: neov - http://www.neov.net.
 *	@creation: 20091130.
 *	@modification: -
 *	@required: jQuery 1.3.2
 *
 */

var contHeight = 0;
var limit = 6;
var rowHeight = 0;
var rowLength = 0;
var timeout = null;


// actions sur les dreamers.
$.fn.drmAction = function()
{
	$(this).each(
		function()
		{
			$(this)
				.mouseover(
					function()
					{
						if (!$(this).hasClass('inactive')) {
							$('a', this).hide();
							$('.info', this).css({display: 'block'});
						}
					}
				)
				.mouseout(
					function()
					{
						if (!$(this).hasClass('inactive')) {
							$('a', this).css({display: 'block'});
							$('.info', this).hide();
						}
					}
				)
				.click(
					function()
					{
						if (!$(this).hasClass('inactive')) {
							var count = 0, ccount = 0, paraL = 0;
							rowHeight = $('.dreamers .rows').eq(0).innerHeight();
							rowLength = $('.dreamers .rows').length; 

							$('a', this).css({display: 'block'});
							$('.info', this).hide();

							$('.drm').addClass('inactive');
							$('.inactive').click(function(){
								$('.dreamers .post .fermer').trigger('click');
							});
							$('.drm a').eq(0).css({background: 'black'});
							$('.drm').find('img').css({opacity: 0.2});
							
							$('.separateur span').css({opacity: 0.2});

							// chargement du contenu
							var src = j_basepath + 'ajax.php';
							var iDreamerId = $(this).attr("id");

							$('.dreamers .post').load (
								src,
								{
									module:		'commun',
									action:		'communFo:getZone',
									zone:		'dreamersFo~dreamerInfoFo',
									iDreamerId:	iDreamerId,
									langActive: zlangActive 
								},
								function(res)
								{
									var iLength = $('.post .base').find('img').length;
									var iInit = 0;
									
									$('.post .base').find('img').each(
										function()
										{
											var imagePreload = new Image();
											imagePreload.onload = function()
											{
												imagePreload.onload = null;
												iInit++;
												if(iLength == iInit) {
													contHeight = parseFloat($('.dreamers .post .content').css('min-height'));
													paraL = $('.dreamers .post .content p').length;
				
													var champDreamer = $('<input type="hidden" value="'+iDreamerId+'" id="dreamer_id" name="dreamer_id">');
													$('#commentaireDreamers').append(champDreamer);
				
													$('.dreamers .post').slideDown({queue:false, duration: gtime, easing: 'bounceEaseOut'});
													$('.dreamers .post .content p').eq(paraL-1).addClass('last');
													
													
				
													var topHeight = $('.post .base .top').innerHeight();
													var titHeight = $('.post .base .tit').innerHeight() + 12;
													var botHeight = $('.post .base .bottom').innerHeight();
													var baseHeight = $('.post .base').innerHeight();
													var bcContentHeight = $('.dreamers .bloc-comment .comment-content').height();
													var bcHeight = $('.bloc-comment').height();
													var eHeight = bcHeight - bcContentHeight;
				
													if (rowLength > limit) {
														$('.dreamers .post .content').css({height: 'auto'});
														$('.dreamers .bloc-comment .comment-content').css({height: 'auto'});
				
														for(i=3; i<=rowLength; i++) {
															if (baseHeight > (rowHeight * (i)) - 5) {
																count = i;
															}
														}
				
														if ( $('.dreamers .rows').eq(count).length != 0 ) {
															contHeight = ((rowHeight * (count+1)) - 5) - topHeight - titHeight - botHeight;
														} else {
															contHeight = ((rowHeight * (count)) - 5) - topHeight - titHeight - botHeight;
														}
													}
				
													if (rowLength > limit - 1) {
														$('.dreamers .bloc-comment .comment-content').css({height: 'auto'});
														bcHeight = $('.bloc-comment').height();
														for (i=3; i<=rowLength; i++) {
															if (bcHeight > (rowHeight * (i)) - 5) {
																ccount = i;
															}
														}
				
														if ( $('.dreamers .rows').eq(ccount).length != 0 ) {
															bcContentHeight = ((rowHeight * (ccount+1)) - 5) - eHeight;
														} else {
															bcContentHeight = ((rowHeight * (ccount)) - 5) - eHeight;
														}
													}
				
													$('.dreamers .post .content').height(contHeight);
													$('.bloc-comment .comment-content').height(bcContentHeight);
												}
											}
											
											imagePreload.src = this.src;
										}
									);

									loadAutoClear('.dreamers .post .autoclear');

									//validation formulaire commentaire
									$('#commentaireDreamers [type=submit]').click(
										function ()
										{
											bErreur = tmt_validateForm($("#commentaireDreamers").get(0));
											if (bErreur) {
												var iDreamerId = $("#dreamer_id").val();
												var zTextComment = $("#comment-comment").val();
												var zNomComment = $("#comment-nom").val();
												var zMailComment = $("#comment-mail").val();
												var zSiteWebComment = $("#comment-web").val();
												$('#commentaireDreamers .comments').hide();
												$('#commentaireDreamers .merci').show();												
												$.ajax (
													{
														type:		"POST",
														url:		j_basepath+'ajax.php',
														data:		"module=dreamersFo&action=default:commenterPublication&iDreamerId=" + iDreamerId + "&zTextComment=" + zTextComment + "&zNomComment=" + zNomComment + "&zMailComment=" + zMailComment + "&zSiteWebComment=" + zSiteWebComment,
														async:		false,
														dataType:   "json",
														success:	function(resultat)
																	{
                        												$("#comment-comment").val(resultat.msgcommentaire);
                        												$("#comment-nom").val(resultat.msgnom);
                        												$("#comment-mail").val(resultat.msgmail);
                        												$("#comment-web").val(resultat.msgweb);                                                     					
                                                                        if (timeout) clearTimeout(timeout);
                                    									timeout = setTimeout(function(){
                            												$('#commentaireDreamers .comments').show();
                            												$('#commentaireDreamers .merci').hide();										
                                    										}, 5000);                                    																										
																	}
													}
												);
											}
											return false;
										}
									);
									
									//j'aime / j'aime pas
									$('.aime').click(
										function()
										{
											var iDreamerId = $("#dreamer_id").val();
											$.ajax (
												{
													type:		"POST",
													url:		j_basepath+'ajax.php',
													data:		"module=dreamersFo&action=default:voterPublication&iDreamerId=" + iDreamerId + "&iAimer=1",
													async:		false,
													success:	function(resultat)
																{
																	if (isNaN(resultat))
																		alert(resultat);
																	else
																		$('.aime span').html('('+resultat+')');
																}
												}
											);
										}
									);
									
									$('.aime-pas').click(
										function()
										{
											var iDreamerId = $("#dreamer_id").val();
											$.ajax(
												{
													type:		"POST",
													url:		j_basepath+'ajax.php',
													data:		"module=dreamersFo&action=default:voterPublication&iDreamerId=" + iDreamerId + "&iAimer=0",
													async:		false,
													success:	function(resultat)
																{
																	if (isNaN(resultat))
																		alert(resultat);
																	else
																		$('.aime-pas span').html('('+resultat+')');
																}
												}
											);
										}
									);

									$('.dreamers .base .comment').click(
									function()
									{
										$('.dreamers .bloc-comment').show();
										rowHeight = $('.dreamers .rows').eq(0).innerHeight();
										rowLength = $('.dreamers .rows').length; 

										var bcContentHeight = $('.dreamers .bloc-comment .comment-content').height();
										var bcHeight = $('.bloc-comment').height();
										var count = 0;
										var eHeight = bcHeight - bcContentHeight;

										if (rowLength > limit - 1) {
											$('.dreamers .bloc-comment .comment-content').css({height: 'auto'});
											bcHeight = $('.bloc-comment').height();
											for(i=3; i<=rowLength; i++) {
												if (bcHeight > (rowHeight * (i)) - 5) {
													count = i;
												}
											}

											if ( $('.dreamers .rows').eq(count).length != 0 ) {
												bcContentHeight = ((rowHeight * (count+1)) - 5) - eHeight;
											} else {
												bcContentHeight = ((rowHeight * (count)) - 5) - eHeight;
											}
										}

										$('.bloc-comment .comment-content').height(bcContentHeight);

										return false;
									}
								);

								$('.dreamers .reagir').click(
									function()
									{
										$('.dreamers .post-comment').show();
										return false;
									}
								);

								$('.dreamers .post .fermer').click(
									function()
									{
										$('.dreamers .post-comment').hide();
										$('.dreamers .bloc-comment').hide();
										$('.dreamers .post')
											.empty()
											.hide();
										$('.drm').removeClass('inactive');
										$('.drm a').eq(0).css({background: 'none'});
										$('.drm').find('img').css({opacity: 1});
										$('.separateur span').css({opacity: 1});
										return false;
									}
								);
								}
							);
						}

						return false;
					}
				);
		}
	);

	
	$(document).unbind('click').bind('click', function(e){
		$('.dreamers .post .fermer').trigger('click');
	});	
	 
	$('.post').unbind('click').bind('click', function(e){
		if (!e) var e = window.event;
		e.cancelBubble = true;
		if (e.stopPropagation) e.stopPropagation();
	});	
}

$.fn.loadDreamers = function()
{
	$(this).each(
		function()
		{
			var $img = $('img.thumb', this);
			$img.hide();
			var imagePreloader = new Image();
			imagePreloader.onload = function ()
			{
				imagePreloader.onload = null;
				$img.show();
			};
			
			imagePreloader.src = $img.attr('src');
			
			
			/*$(this).onImagesLoad(
				{
					itemCallback:
					function(a)
					{
						$('img.thumb', a).css({display:'block'});
					}
				}
			);
			*/
		}
	);
}

$.fn.filtrer = function(o)
{
	$.ajax(
		{
			type:		"POST",
			url:		j_basepath+'ajax.php',
			data:		"module=commun&action=communFo:getZone&zone=dreamersFo~dreamerListeFo&triSelection=" + o.tri + "&cacherImage=" + o.image + "&cacherVideo=" + o.video,
			async:		false,
			success:	function (resultat)
						{
							$('.dreamers').empty();
							$('.dreamers').html(resultat);
							$('.dreamers .rows a').loadDreamers();
							$('.drm').drmAction();
						}
		}
	)
}

function displayError(){
	return true;
}

$(
	function()
	{
		// loading dreamers.
		$('.dreamers .rows a').loadDreamers();

		// affichage liste filtre les plus.
		$('.ent .filtre a.selected').click(
			function()
			{
				var $this = $(this);
				if (!$(this).hasClass('running'))
				{
					$(this).addClass('running');
					$('.ent .filtre ul').slideToggle({queue: true, duration: gtime, easing: 'sineEaseOut'});
					$('.ent .filtre ul').queue(
						function()
						{
							$this.removeClass('running');
							$(this).dequeue();
						}
					);
				}

				return false;
			}
		);
		
		// selection filtre les plus.
		$('.ent .filtre li').find('a').click(
			function()
			{	
				if (!$('.ent .filtre ul').hasClass('running'))
				{
					$('.ent .filtre ul').addClass('running');

					var nText = $(this).text(), id = $(this).attr('id').replace('opt_','');

					$('.ent .filtre a.selected')
						.attr({title: nText, id: id})
						.text(nText);

					$('.ent .filtre li.selected').removeClass('selected');

					$(this).parent().addClass('selected');

					//appel zone
					var cacherImage = 0, cacherVideo = 0;

					if ($('.ent .post-image').hasClass('active'))
						cacherImage = 0;
					else
						cacherImage = 1;

					if ($('.ent .post-video').hasClass('active'))
						cacherVideo = 0;
					else
						cacherVideo = 1;

					var triSelection = triSelection = $('.filtre .selected').attr('id');

					$().filtrer({tri: triSelection, image: cacherImage, video: cacherVideo});

					$('.ent .filtre ul').slideUp({queue: true, duration: gtime, easing: 'sineEaseOut'});

					$('.ent .filtre ul').queue(
						function()
						{
							$(this).removeClass('running');
							$(this).dequeue();
						}
					);
				}

				return false;
			}
		);

		$('.ent .post-image').click(
			function()
			{
				$(this).toggleClass('active');

				var txt = null, cacherImage = 0, cacherVideo = 0;

				if ($(this).hasClass('active'))
				{
					//txt = $(this).text().replace('Afficher', 'Masquer');
					cacherImage = 0;
				} else {
					//txt = $(this).text().replace('Masquer', 'Afficher');
					cacherImage = 1;
				}

				if ($('.ent .post-video').hasClass('active'))
					cacherVideo = 0;
				else
					cacherVideo = 1;

				var triSelection = $('.filtre .selected').attr('id');

				$().filtrer({tri: triSelection, image: cacherImage, video: cacherVideo});
				
				$.ajax({
    				type:		"POST",
    				url:		j_basepath+'ajax.php',
    				data:		"module=dreamersFo&action=default:getTextImagePost&iCacher=" + cacherImage,
    				async:		false,
    				success:	function(resultat)
    							{
                                   $('.ent .post-image').text(resultat);     																										
    							}                
                });				

				$(this).text(txt);

				return false;
			}
		);
		
		$('.ent .post-video').click(
			function()
			{
				$(this).toggleClass('active');

				var txt = null, cacherVideo = 0, cacherImage = 0;

				if ($(this).hasClass('active')) {
					//txt = $(this).text().replace('Afficher', 'Masquer');
					cacherVideo = 0;
				} else {
					//txt = $(this).text().replace('Masquer', 'Afficher');
					cacherVideo = 1;
				}

				if ($('.ent .post-image').hasClass('active'))
					cacherImage = 0;
				else
					cacherImage = 1;

				var triSelection = $('.filtre .selected').attr('id');

				$().filtrer({tri: triSelection, image: cacherImage, video: cacherVideo});
				
				$.ajax({
    				type:		"POST",
    				url:		j_basepath+'ajax.php',
    				data:		"module=dreamersFo&action=default:getTextVideoPost&iCacher=" + cacherVideo,
    				async:		false,
    				success:	function(resultat)
    							{
                                   $('.ent .post-video').text(resultat);     																										
    							}                
                });				

				$(this).text(txt);
				return false;
			}
		);
		
		// ajout action aux dreamers.
		$('.drm').drmAction();

		// passage du Id du dreamer en url.
		if (iDreamerId != 0){
            $('#' + iDreamerId).trigger('click');
        }
	
        if (iParAuteur != 0 && $('.filtre > a.selected').attr('id') != 'PAR_AUTEUR'){
			$('.filtre > a.selected')
				.attr({id: 'PAR_AUTEUR'})
				.text($('.ent .filtre li').eq(3).find('a').text());
			$('.ent .filtre li.selected').removeClass('selected');
			$('.ent .filtre li').eq(3).addClass('selected');
			
        }       
        
	}
);
