// JavaScript Document
// Manga no haru
// 2010 CopyRIGHT

var MAX_LEFT = 0;

$(function() {
	
	// Scroll
	$(window).scroll(function() {
		if($('div.alerte_box').length > 0) {
			$('div.alerte_box').stop().animate({'margin-top':($(window).scrollTop()+50)+'px'},'slow');
		}
	});

	// Common function
	alerte_box = function(id,contenu,width_,bg_,height,_scrollbar_) {
		// Creation de l'objet << alerte box >>
		var bg_ = (bg_ === undefined) ? 0 : bg_;
		var width_ = (width_ === undefined) ? 300 : parseInt(width_);
		var height = (height === undefined) ? 300 : height;
		// Background
		if(bg_ == 1) {
			$('body').append('<div id="black_bg"></div>');
			$('div#black_bg')
			.css('height',$(document).height())
			.css('width',$(document).width())
			.css('display','block');
		}
		if(contenu === undefined || contenu === null || contenu == '') {
			if($('div#alerte_'+id).length != 0) $('div#alerte_'+id).die().remove();
			if($('div#black_bg').length != 0) $('div#black_bg').fadeOut("slow",function() { $(this).remove(); });
			return;
		} else {
			var more_style = (_scrollbar_ == 0) ? 'overflow:hidden;' : 'overflow:auto; overflow-x: hidden; -ms-overflow-x: hidden;';
			$('body').prepend('<div id="alerte_'+id+'" align="center" class="alerte_box" style="width:'+width_+'px; margin-top:'+($(window).scrollTop()+10)+'px; margin-left:'+(($(document).width()-width_)/2)+'px; height:'+height+'px; '+more_style+'"><div align="right" style="padding:6px; padding-bottom:0px;"><a href="javascript:;" onclick="alerte_box(\''+id+'\')">[ Fermer ]<\/a><\/div>'+contenu+'<br />&nbsp;</div>');
			return;
		}
	};
	
	// Champ supplémentaire dans une recherche avancée.
	if($('input[name=type][type=radio]').length > 0) {
		$('input[name=type][type=radio]').click(function() {
			if($(this).val() == 'anime') {
				$('div#porte_ost').css('display','none').children('input').attr('disabled','disabled').attr('checked', false);
				$('div#porte_anime').css('display','block').children('input').removeAattr('disabled');
			} else {
				$('div#porte_anime').css('display','none').children('input').attr('disabled','disabled').attr('checked', false);
				$('div#porte_ost').css('display','block').children('input').removeAattr('disabled');
			}
		});
		
		if($('input.porte').length > 0) {
			$('input.porte').click(function() {
				if($(this).is(':checked')) {
					$('div#recherche_form').append('<div style="margin-bottom:3px;"><b>'+$(this).parent('div').text()+' :</b> <input name="'+$(this).attr('name').replace('c_','')+'" type="text" /></div>');
				} else {
					$('input[name='+$(this).attr('name').replace('c_','')+']').parent('div').remove();
				}
			});
		
			$('form#recherche_avance').submit(function() {
				if($('input.porte:checked').length <= 0) {
					alert('Une case doit être coché au moins dans "Sur quoi porte la recherche ?"');
					return false;
				} else if($('input[type=radio][name=type]:checked').length != 1) {
					alert('Une case doit être coché dans "Sur quel type la recherche porte ?"');
					return false;
				} else {
					/*var vide = 0;
					$('input[type=text]').each(function() {
						if($.trim($(this).text()) == '') vide += 1;
					});
					if(vide == $('input[type=text]').length) {
						alert('Tous les champs de recherche sont vide !');
						return false;
					}*/
				}
			});
		}
	}
	
	// Trier les résultats
	if($('input.tri_recherche').length > 0) {
		$('input.tri_recherche').click(function() {
			if($('input.tri_recherche:checked').length <= 0) {
				$('div#resultat_de_recherche div').css('display','block');
			} else {
				$('div#resultat_de_recherche div').css('display','none');
				$('input.tri_recherche:checked').each(function() {
					$('div#resultat_de_recherche div.'+$(this).attr('name')).css('display','block');
				});
			}
		});
	}
	
	$('div#header').data('num', 0);
	// Slide, code par Jean Clément
	// Calcul max left (for slider)
	$('div#slider > div > div').each(function(i) {
		if(i%2 == 0 && i > 0) {
			MAX_LEFT += 471;
		}
	});
	
	// Slider
	$('a#next').click(function() {
		if(parseInt($('div#slider > div:eq(0)').css('left'))-471 < (MAX_LEFT)*(-1)) {
			$('div#slider > div:eq(0)')
			.animate({'left':'-=100px'},"slow", function() {
				$('div#slider > div:eq(0)')
				.animate({'left':'0px'},"slow");
			});
		} else {
			$('div#slider > div:eq(0)')
			.animate({'left':'-=471px'},"slow");
		}
	});
	
	$('a#prev').click(function() {
		if(parseInt($('div#slider > div:eq(0)').css('left')) == 0) {
			$('div#slider > div:eq(0)')
			.animate({'left':'+=100px'},"slow", function() {
				$('div#slider > div:eq(0)')
				.animate({'left':'-'+MAX_LEFT+'px'},"slow");
			});
		} else {
			$('div#slider > div:eq(0)')
			.animate({'left':'+=471px'},"slow");
		}
	});

	// Menu
	$('ul#main_menu li').hover(
		function() {
			$(this)
			.children('ul.child')
			.not(":animated")
			.stop().animate({ height: 'show', opacity: 'show' }, 'slow');
		},
		function() {
			$(this)
			.children('ul.child')
			.delay(500).animate({opacity: 'hide', height: 'hide'}, 1);
		}
	);

	// Boite qui s'ouvre/fermer
	$('a.boite_toggle').click(function() {
		if($(this).parent('div').parent('div').children('div.milieu').css('display') != 'none') {
			$(this).css('background', 'url(http://img.manga-no-haru.net/design/v4/lien_toggle_hide.png) no-repeat');
			$(this)
			.parent('div').parent('div').children('div.milieu')
			.slideUp();
		} else {
			$(this).css('background', 'url(http://img.manga-no-haru.net/design/v4/lien_toggle_show.png) no-repeat');
			$(this)
			.parent('div').parent('div').children('div.milieu')
			.slideDown();
		}
	});

	// Headeur qui change
	//var index = (isNaN(parseInt($.cookie('head_site_num')))) ? 0 : parseInt($.cookie('head_site_num'));
	//$('div#header').css('background','url(http://img.manga-no-haru.net/headeurs/headeur_'+index+'.jpg) no-repeat center');
	$('a[title="Headers"]:eq(0)').click(function() {
		// get index
		$.ajax({
			url:'http://www.manga-no-haru.net/images/headeurs/headeur_'+index+'.jpg',
			complete: function(jqXHR, textStatus) {
				if(textStatus == "error") {
					index = 0;
				} else {
					index = index+1;
				}
				$.cookie('head_site_num', 'headeur_fall11.png', { expires: 360 });
					
				$('div#header').fadeOut("slow", function() {
					$('div#header').css('background','url(http://img.manga-no-haru.net/headeurs/headeur_'+index+'.jpg) no-repeat center');
					$('div#header').fadeIn("slow");
				});
			}
		});
	});
	
	// Signaler un lien mort
	var _ALL_SELECTED_ = false;
	var _SLIDE_DOWN_ = false;
	$('input[type=button][name=all_liens]').click(function() {
		if(_ALL_SELECTED_) {
			$(this).parent('div').children('div').children('input[type=checkbox]').attr('checked', false);
			_ALL_SELECTED_ = false;
			$(this).val('Selectionner tout les liens');
		} else {
			$(this).parent('div').children('div').children('input[type=checkbox]').attr('checked', true);
			_ALL_SELECTED_ = true;
			$(this).val('Déselectionner tout les liens');
		}
	});
	$('a#lien_mort').click(function() {
		if(_SLIDE_DOWN_) {
			$('div#lien_mort_status').slideUp("slow");
			_SLIDE_DOWN_ = false;
		} else {
			$('div#lien_mort_status').slideDown("slow");
			_SLIDE_DOWN_ = true;
		}
		/*if(confirm('Etes vous sûr de vouloir signlaer un (ou plusieurs) lien mort sur cette page ?')) {
			$.post(
				$(location).attr('href')+'?lien_mort',
				{'lien_mort':'1'},
				function(data) {
					alert('Merci ! Ces liens seront corrigé dans les plus BREF délais !');
				}
			);
		}*/
	});
	$('div#lien_mort_status > div:not(#result_lien_mort)').hover(
		function() {
			$(this).css({'cursor':'pointer', 'background':'#C43300', 'color':'#FFFFFF', 'font-weight':'bold'});
		},
		function() {
			$(this).css({'background':'none', 'color':'#656565', 'font-weight':'normal'});
		}
	);
	$('div#lien_mort_status > div').click(function() {
		if($(this).children('input').is(':checked')) {
			$(this).children('input').attr('checked', false);
		} else {
			$(this).children('input').attr('checked', true);
		}
	});
	$('input[type=button][name=valider_liens_mort]').click(function() {
		
		var post_data = {};
		var checked = $(this).parent('div').children('div').children('input[type=checkbox]:checked');
		if(checked.length <= 0) {
			alert('Vous devez selectionné au moins 1 lien.');
			return false;
		} else {
			$(this).attr('disabled', true).val('Chargement en cours...');
			$('div#result_lien_mort').css({margin:'10px'}).html('<img src="http://img.manga-no-haru.net/divers/ajaxloader.gif" /><b>Etape 1/4 :</b> Vérification des liens... (s\'il sont bien mort !)');
			
			$.each(checked, function(i, v) {
				var name = $(this).attr('name');
				post_data.name = 1;
			});
		}		
		$.post(
			$(location).attr('href')+'?lien_mort',
			post_data,
			function(data) {
				
			}
		);
	});
});
