$(document).ready(function(){
	setTimeout(function(){
		if(!$('#iframed_banner').hasClass('closed')){
			hide_rsr_banner();
		}
	}, 4000);		
	
	$('#banner_rsr_button').click(function(){ 
		if($('#iframed_banner').hasClass('closed')){
			show_rsr_banner();
		}else{
			hide_rsr_banner();
		}
	});
	if(getCookie('banner_status') != 'true'){
		var expdate = new Date ();
		DateExpiration (expdate);
		expdate.setTime (expdate.getTime() + (24 * 60 * 60 * 1000)); 
		FixCookie('banner_status', 'true', expdate);	
	}
	ajax_serv = "http://"+document.domain+"/";
   	$('.add_article_playlist').click(function () { 
   		$(this).hide();
 	   	string = ""+location;
   		hashes = string.split('#');
   		hash = '#'+hashes[1];
   		hashContent = hash.split('/');
   		add_item_to_playlist($(this).attr('id'), 'transfer', '', 'playlist_index');
		$(this).parent().parent().fadeOut('medium', function(){
			window.location.reload();	
		});
	});
	$('.small_croppable').each(function(){
		$(this).crop({x:15, y:48, height:57, width:80});
	});
	$('.medium_croppable').each(function(){
		url = $(this).attr('src');
		$(this).crop({x:4, y:60, height:110, width:154});
	});
	$("#auth_form").fancybox({
		'zoomSpeedIn':	145, 
		'zoomSpeedOut':	0, 
		'frameWidth' : 300,
		'frameHeight' : 260,
		'overlayShow':	true
	});
		$("a#super_admin_zone_anchor").fancybox({
			'zoomSpeedIn':	0, 
			'zoomSpeedOut':	0,
			'frameWidth' : 800,
			'frameHeight' : 600
		});
	$('#onglet_logout').click(function(){
		disconnect_user();
	});
	$("a#show_manual").fancybox({
		'zoomSpeedIn':	0, 
		'zoomSpeedOut':	0,
		'frameWidth' : 1020,
		'frameHeight' : 355
	});
	$("a#show_selection_bookmark").fancybox({
		'zoomSpeedIn':	0, 
		'zoomSpeedOut':	0,
		'frameWidth' : 300,
		'frameHeight' : 300
	});
	$("a#show_rss_options").fancybox({
		'zoomSpeedIn':	0, 
		'zoomSpeedOut':	0,
		'frameWidth' : 300,
		'frameHeight' : 300
	});
	$('.read_article_lien').each(function(){
		var href = $(this).attr('href');
		$(this).parent().parent().children('.article_p2').click(function(){
			document.location.href = href;
		});
		$(this).parent().parent().children('.article_p1').children('.read').click(function(){
			document.location.href = href;
		});
	});
	$("#search").bind("click", load_search_zone);
	set_topic_scrollable();
}); 	
function auth_user(){
	login = "";
	password = "";
	$('#fancy_div').children('div:eq(0)').children('div:eq(0)').children().each(function(){
		if($(this).attr('id')=='login_input'){
			login = $(this).val();
		}else if($(this).attr('id')=='password_input'){
			password = $(this).val();
			if(password != ""){
			password = password.md5();
			}
		}
	});
	if((login != "") && (password != "")){
		data_string = 'login='+login+'&password='+password;
		$.ajax({
		   type: 'POST',
		   url: ajax_serv+'auth_user.php',
		   data: data_string,
		   success: function(msg){
		   	if(msg.substr(-8) == "not_auth"){
		   		show_error_field();
		   		$('#fancy_div').children('div:eq(0)').children('#login_info').append("<div id='auth_error'><br /></div>");
		   		$('#fancy_div').children('div:eq(0)').children('#login_info').children('#auth_error').fadeIn('slow').append("Mauvais login et/ou password");
		   	}else if(msg == "reload"){
		   		window.location.reload();
		   	}
		   }
		 }); 		
	}else if((login == "") && (password == "")){ 
		show_error_field();
		$('#fancy_div').children('div:eq(0)').children('#login_info').append("<div id='auth_error' style='display:none'><br />Champs Login vide!<br />Champs Password vide!</div>");
		$('#fancy_div').children('div:eq(0)').children('#login_info').children('#auth_error').fadeIn('slow');
	}else{
		show_error_field();
		if(login == ""){
			$('#fancy_div').children('div:eq(0)').children('#login_info').append("<div id='auth_error' style='display:none'><br />Champs Login vide!</div>");
		}else if(password == ""){
			$('#fancy_div').children('div:eq(0)').children('#login_info').append("<div id='auth_error' style='display:none'><br />Champs Password Vide</div>");
		}
		$('#fancy_div').children('div:eq(0)').children('#login_info').children('#auth_error').fadeIn('slow');
	}
}
function disconnect_user(){
	data_string = '';
	$.ajax({
	   type: 'POST',
	   url: ajax_serv+'disconnect_user.php',
	   data: data_string,
	   success: function(msg){
   		window.location.reload();
	   }
	 }); 
}
function show_error_field(){
	if($('#auth_error').text()==""){
		$('#fancy_outer').animate({"height": "+=40px"}, 'slow');
	}else{
		$('#auth_error').remove();
	}
}
function handle_pwd_form(){
	if(!$('#fancy_div').children('div:eq(0)').children('#pwd').children('#pwd_loosed').hasClass('shown')){
		$('#fancy_div').children('div:eq(0)').children('#pwd').children('#pwd_loosed').addClass('shown');
		$('#fancy_outer').animate({"height": "+=50px"}, 'fast', function(){
			$('#fancy_div').children('div:eq(0)').children('#pwd').children('#pwd_form').fadeIn('fast');
		});
	}else{
		$('#fancy_div').children('div:eq(0)').children('#pwd').children('#pwd_loosed').removeClass('shown');
		$('#fancy_div').children('div:eq(0)').children('#pwd').children('#pwd_form').fadeOut('fast', function(){
			$('#fancy_outer').animate({"height": "-=50px"}, 'fast');
		});
	}
}
function regenerate_password(){
	email = $('#fancy_div').children('div:eq(0)').children('#pwd').children('#pwd_form').children('#pwd_email_input').val();
	if(echeck(email)){
		data_string = 'email='+email;
		$.ajax({
		   type: 'POST',
		   url: ajax_serv+'regenerate_password.php',
		   data: data_string,
		   success: function(msg){
			$('#fancy_div').children('div:eq(0)').children('#pwd').children('#pwd_form').children('#pwd_email_input').removeClass('account_form_error');
			$('#fancy_div').children('div:eq(0)').children('#pwd').children('#pwd_form').children('#pwd_reg_msg').remove();
			$('#fancy_div').children('div:eq(0)').children('#pwd').children('#pwd_form').append("<div id='pwd_reg_msg' style='display:none'><br />"+msg+"</div>");
			$('#fancy_div').children('div:eq(0)').children('#pwd').children('#pwd_form').children('#pwd_reg_msg').fadeIn('slow');
		   }
		 }); 
	 }else{
	 	$('#fancy_div').children('div:eq(0)').children('#pwd').children('#pwd_form').children('#pwd_email_input').addClass('account_form_error');
	 }
}
function handle_account_form(){
	if(!$('#fancy_div').children('div:eq(0)').children('#account').children('#create_account').hasClass('shown')){
		$('#fancy_div').children('div:eq(0)').children('#account').children('#create_account').addClass('shown');
		$('#fancy_outer').animate({"height": "+=120px"}, 'fast', function(){
			$('#fancy_div').children('div:eq(0)').children('#account').children('#account_form').fadeIn('fast');
		});
	}else{
		$('#fancy_div').children('div:eq(0)').children('#account').children('#create_account').removeClass('shown');
		$('#fancy_div').children('div:eq(0)').children('#account').children('#account_form').fadeOut('fast', function(){
			$('#fancy_outer').animate({"height": "-=120px"}, 'fast');
		});
	}
}
function create_new_account(){
	errors = false;
	login = "",
	password = "";
	email = "";
	$('#fancy_div').children('div:eq(0)').children('#account').children('#account_form').children().each(function(){
		verify_field($(this).attr('id'));
	});
	$('#fancy_div').children('div:eq(0)').children('#account').children('#account_form').children().each(function(){
		if($(this).hasClass('account_form_error')){
			errors = true;
		}else{
			if($(this).attr('id')=="account_login_input"){
				login = $(this).val();
			}else if($(this).attr('id')=="account_password_input"){
				password = $(this).val();
			}else if($(this).attr('id')=="account_email_input"){
				email = $(this).val();
			}
		}
	});
	if(errors == true){
	}else{
		data_string = 'login='+login+'&password='+password+'&email='+email;
		$.ajax({
		   type: 'POST',
		   url: ajax_serv+'add_new_user.php',
		   data: data_string,
		   success: function(msg){
			if(msg == "login used"){
				element = $('#fancy_div').children('div:eq(0)').children('#account').children('#account_form').children('#account_login_input');
				element.addClass('account_form_error');
			}else if(msg == "mail adress used"){
				element = $('#fancy_div').children('div:eq(0)').children('#account').children('#account_form').children('#account_email_input');
				element.addClass('account_form_error');
			}else if(msg == "login and mail adress used"){
				element = $('#fancy_div').children('div:eq(0)').children('#account').children('#account_form').children('#account_login_input');
				element.addClass('account_form_error');
				element = $('#fancy_div').children('div:eq(0)').children('#account').children('#account_form').children('#account_email_input');
				element.addClass('account_form_error');
			}else{
				element = $('#fancy_div').children('div:eq(0)').children('#account').children('#account_form');
				element.empty();
				element.append("<div id='create_account_success'>Inscription effectuée avec succès! Pour terminer votre inscription, cliquez sur le lien envoyé par mail!</div>");
			}
		   }
		 }); 					
	}
}
function verify_field(element_name){
	element = $('#fancy_div').children('div:eq(0)').children('#account').children('#account_form').children('#'+element_name);
	if(element_name == 'account_login_input'){
		if((element.val() =="") || (element.val().length < 2)){
			element.addClass('account_form_error');
		}else{
			element.removeClass('account_form_error');
		}
	}
	if(element_name == 'account_password_input'){
		if((element.val() =="") || (element.val().length < 4)){
			element.addClass('account_form_error');
		}else{
			element.removeClass('account_form_error');
		}		
	}
	if(element_name == 'account_password_input_bis'){
		if((element.val() =="") || (element.val().length < 4) ||((element.val())!=(element.parent().children('#account_password_input').val()))){
			element.addClass('account_form_error');
		}else{
			element.removeClass('account_form_error');
		}		
	}
	if(element_name == 'account_email_input'){
		if(!echeck(element.val())){
			element.addClass('account_form_error');
		}else{
			element.removeClass('account_form_error');
		}		
	}
}
function set_topic_scrollable(){	
	$("#scrollable").scrollable({size:4,prev:'.prev_topic', next:'.next_topic', clickable: false});
	if($('.topic_actif').length > 0){			
		tokens = $('.topic_actif').attr('id').split('_');
		
		pos = tokens[3] *1;
		
		page = Math.floor((pos)/4);
		if($("#scrollable").scrollable().getStatus().page != page){

		}
	}
	$('#next_topic').click(function(){
		$('#prev_topic').addClass('prev_appear');
		//$('#prev_topic').show();	
		$("#scrollable").scrollable().next();
		if(($("#scrollable").scrollable().getStatus().size+$("#scrollable").scrollable().getStatus().index)==$("#scrollable").scrollable().getStatus().total){
			$('#next_topic').removeClass('next_appear');
			//$('#next_topic').hide();
		}
	});
	$('#prev_topic').click(function(){
		$('#next_topic').addClass('next_appear');
		//$('#next_topic').show();
		$("#scrollable").scrollable().prev();
		if($("#scrollable").scrollable().getStatus().index==0){
			$('#prev_topic').removeClass('prev_appear');
			//$('#prev_topic').hide();
		}
	});
}
function add_item_to_playlist(element, animation, stringToPrepend, location){	
	isRep = false;
	if(animation == 'transfer'){
		itemID = "";
		divID = "";
		text = "";
		if((((element+'').substr(0, 12))=='article_list')||(((element+'').substr(0, 12))=='reperto_list')){
			
			if($('#'+element).hasClass('rep')){
				isRep = true;
			}
			itemID = (element+'').substr(12, (element+'').length);
			itemIDSTR = itemID+"";
			if(itemID.substr(0, 3)=='rep'){
				itemID = (itemID+'').substr(3, (itemID+'').length);
			}
			if(isRep){
				divID = "repid"+itemID;
			}else{
				divID = "itemid"+itemID;
			}	
			if($('#'+element).hasClass('front')){
				item_title = $('#'+divID+'').contents('#titre_news').text();
			}else{
				item_title = $('#'+divID+'').contents('.article_p1').contents('.titre_milieu').text();
			}
			if(isRep){
				clickStr = 'remove_from_playlist("rep_'+itemID+'")';	
			}else{
				clickStr = 'remove_from_playlist('+itemID+')';				
			}
			if(isRep){
				text = '<div class="item_in_pl rep" id="in_plrep_'+itemID+'"><div class="remove_from_pl" onclick='+clickStr+'><img src="img/cross.gif"/></div>'+cutIflong(item_title)+'</div>';
				itemID = 'rep_'+itemID;
			}else{
				text = '<div class="item_in_pl" id="in_pl'+itemID+'"><div class="remove_from_pl" onclick='+clickStr+'><img src="img/cross.gif"/></div>'+cutIflong(item_title)+'</div>';
			}	
		}else if(((element+'').substr(0, 15))=='current_article'){ 
			itemID = (element+'').substr(15, (element+'').length);
			divID = "titre_milieu_large";		
			clickStr = 'remove_from_playlist('+itemID+')';	
			text = '<div class="item_in_pl" id="in_pl'+itemID+'"><div class="remove_from_pl" onclick='+clickStr+'><img src="img/cross.gif"/></div>'+cutIflong($('#'+divID+'').text())+'</div>';		
		}
	}else{		
		itemID = element;
		text = stringToPrepend;
	}
	data_string = 'item_id='+itemID;
	if(isRep){
		if(itemID.length > 4){
			if(itemID.substr(0, 3)=='rep'){
				itemID_tokens = itemID.split('_');
				itemID = itemID_tokens[1]; 
			}
		}	
	}
	$.ajax({
	   type: 'POST',
	   url: ajax_serv+'add_item_to_playlist.php',
	   data: data_string,
	   success: function(msg){
         	if(msg == "success"){
			if(itemID.length > 4){
				if(itemID.substr(0, 3)=='rep'){
					itemID_tokens = itemID.split('_');
					itemID = itemID_tokens[1]; 
					isRep = true;
				}
			}
			if($('#text_playlist')!=null)
			{
				$('#text_playlist').remove();
			}
			$('#carre_playlist').prepend(text);
			if(location == "thema_index"){
				$('#article_list'+itemID).children('.ajouter_article_icon').hide();
				$('#article_list'+itemID).children('.ajouter_article').hide();
				$('#article_list'+itemID).children('.supprimer_article_icon').show();
				$('#article_list'+itemID).children('.supprimer_article').show();
				if($('#current_article'+itemID).length > 0){
					$('#current_article'+itemID).children('.ajouter_article_icon').hide();
					$('#current_article'+itemID).children('.ajouter_article').hide();
					$('#current_article'+itemID).children('.supprimer_article_icon').show();
					$('#current_article'+itemID).children('.supprimer_article').show();				
				}
			}else if(location == "article_index"){
				$('#current_article'+itemID).children('.ajouter_article_icon').hide();
				$('#current_article'+itemID).children('.ajouter_article').hide();
				$('#current_article'+itemID).children('.supprimer_article_icon').show();
				$('#current_article'+itemID).children('.supprimer_article').show();
				if($('#article_list'+itemID).length > 0){
					$('#article_list'+itemID).children('.ajouter_article_icon').hide();
					$('#article_list'+itemID).children('.ajouter_article').hide();
					$('#article_list'+itemID).children('.supprimer_article_icon').show();
					$('#article_list'+itemID).children('.supprimer_article').show();				
				}
			}
			if(animation=='transfer'){
				$('#'+divID+'').effect("transfer", { to: "div.carre", className: "transferring" }, 1000);
				if(isRep){
					$('#repid'+itemID).draggable( "disable" );
				}else{
					$('#itemid'+itemID).draggable( "disable" );
				}				
			}else{
				if(isRep){
					$('#repid'+itemID).draggable( "disable" );
				}else{
					$('#itemid'+itemID).draggable( "disable" );
				}
			}
		}else{
			if(itemID.length > 4){
				if(itemID.substr(0, 3)=='rep'){
					itemID_tokens = itemID.split('_');
					itemID = itemID_tokens[1]; 
					isRep = true;
				}
			}	
			if(location == "thema_index"){
				$('#article_list'+itemID).children('.supprimer_article_icon').hide();
				$('#article_list'+itemID).children('.supprimer_article').hide();	
				$('#article_list'+itemID).children('.ajouter_article_icon').show();
				$('#article_list'+itemID).children('.ajouter_article').show();	
					if(isRep){
						$('#repid'+itemID).draggable( "enable" );
					}else{
						$('#itemid'+itemID).draggable( "enable" );
					}
				if($('#current_article'+itemID).length > 0){
					$('#current_article'+itemID).children('.supprimer_article_icon').hide();
					$('#current_article'+itemID).children('.supprimer_article').hide();
					$('#current_article'+itemID).children('.ajouter_article_icon').show();
					$('#current_article'+itemID).children('.ajouter_article').show();				
				}
			}else if(location == "article_index"){
				$('#current_article'+itemID).children('.supprimer_article_icon').hide();
				$('#current_article'+itemID).children('.supprimer_article').hide();
				$('#current_article'+itemID).children('.ajouter_article_icon').show();
				$('#current_article'+itemID).children('.ajouter_article').show();
				if($('#article_list'+itemID).length > 0){
					$('#article_list'+itemID).children('.supprimer_article_icon').hide();
					$('#article_list'+itemID).children('.supprimer_article').hide();	
					$('#article_list'+itemID).children('.ajouter_article_icon').show();
					$('#article_list'+itemID).children('.ajouter_article').show();				
				}
			}	
			if(isRep){
				$('#in_plrep_'+itemID).empty();
				$('#in_plrep_'+itemID).remove();
			}else{
				$('#in_pl'+itemID).empty();
				$('#in_pl'+itemID).remove();
			}
			if($('.item_in_pl').length==0){
				$('#carre_playlist').append('<div id="text_playlist">Glisser ici les articles que vous souhaitez ajouter à la playlist</div>');
			}
		}
         }
	 });
}
function send_to_friend(){
	emails = $('#share_to_friends').val();
	tokens = emails.split(',');
	errors = false;
	for(var i in tokens){
		if(!echeck(tokens[i])){
			errors = true;
			//alert('Le données saisies contiennent des erreurs!');
			$('#share_message').empty().append('<b>Le données saisies contiennent des erreurs!</b>');
			$('#form_share').children('input').hide();
			$('#share_message').fadeIn('fast');
			setTimeout(function(){
				$('#share_message').fadeOut('fast', function(){
					$('#form_share').children('input').show();
				});
                  }, 2000);
		}
	}
	if(!errors){
		data_string = 'emails='+emails;
		$.ajax({
		   type: 'POST',
		   url: ajax_serv+'send_to_friend.php',
		   data: data_string,
		   success: function(msg){
			$('#share_message').empty().append('<b>'+msg+'</b>');
			$('#form_share').children('input').hide();
			$('#share_message').fadeIn('fast');
			setTimeout(function(){
				$('#share_message').fadeOut('fast', function(){
					$('#form_share').children('input').show();
				});
                  }, 2000);
		   }
		 }); 		
	}
}
function load_search_zone(){
	$("#search").unbind("click", load_search_zone);
	data_string = '';
	$.ajax({
	   type: 'POST',
	   url: ajax_serv+'load_search_zone.php',
	   data: data_string,
	   success: function(msg){
	   	$('#searching_forms').children('form').remove();
	   	//$('#searching_forms').css('z-index', 100);
	  	$('#searching_forms').prepend(msg);
	  	show_search_zone();
	   }
	 });
}
function show_search_zone(){
	$('#search_form_container').height(0);
	$('#super_admin_zone_anchor').fadeOut('fast');
	$('#onglet_mode_emplois').fadeOut('fast');
	$('#accueil_zone_anchor').fadeOut('fast');
	$('#banner_rsr_button').fadeOut('fast');
   	$('#search_form_container').animate({"height": "+=80px"}, 'fast', function(){
   		$('#search_form').fadeIn('medium');
		$("#search").bind("click", hide_search_zone);
   	}); 	
}
function hide_search_zone(){
	$("#search").unbind("click", hide_search_zone);
	$('#search_form').fadeOut('medium', function(){
   		$('#search_form_container').animate({"height": "-=80px"}, 'fast', function(){
   			//$('#searching_forms').css('z-index', 89);
   			$('#super_admin_zone_anchor').fadeIn('fast');
   			$('#onglet_mode_emplois').fadeIn('fast');
   			$('#accueil_zone_anchor').fadeIn('fast');
   			$('#banner_rsr_button').fadeIn('fast');
   		});
		$("#search").bind("click", load_search_zone);
   	}); 	
}
function echeck(str) {
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	if (str.indexOf(at)==-1){
		return false;
	}
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		return false;
	}
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		return false;
	}
	if (str.indexOf(at,(lat+1))!=-1){
		return false;
	}
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		return false;
	}
	if (str.indexOf(dot,(lat+2))==-1){
		return false;
	}
	if (str.indexOf(" ")!=-1){
		return false;
	}
	return true;					
}
function cutIflong(str){
	if(str.length > 40){
		str = str.substr(0, 40);
		str += " ...";
	}
	return str;
}
function show_rsr_banner(){
		$('#super_admin_zone_anchor').fadeOut('fast');
		$('#onglet_mode_emplois').fadeOut('fast');
		$('#accueil_zone_anchor').fadeOut('fast');
		$('#search').fadeOut('fast');	
	$('#iframed_banner').slideDown('slow', function(){
		$('#iframed_banner').removeClass('closed');
		button_src = $('#banner_rsr_button').children('img').attr('src');
		new_button_src = button_src.replace("close", "open");
		$('#banner_rsr_button').children('img').attr('src', new_button_src);
	});
}
function hide_rsr_banner(){
	$('#iframed_banner').slideUp('slow', function(){
		$('#iframed_banner').addClass('closed');
		button_src = $('#banner_rsr_button').children('img').attr('src');
		new_button_src = button_src.replace("open", "close");
		$('#banner_rsr_button').children('img').attr('src', new_button_src);
		$('#super_admin_zone_anchor').fadeIn('fast');
		$('#onglet_mode_emplois').fadeIn('fast');
		$('#accueil_zone_anchor').fadeIn('fast');
		$('#search').fadeIn('fast');	
	});
}
function getCookie(nom) {
	deb = document.cookie.indexOf(nom + "=");
	if (deb >= 0) {
		deb += nom.length + 1;
		fin = document.cookie.indexOf(";",deb);
		if (fin < 0) fin = document.cookie.length;
		return unescape(document.cookie.substring(deb,fin));
	}
	return "";
}
function FixCookie(nom,valeur,expire,path,domaine,securise) {
      document.cookie = nom + "=" + escape (valeur) + ((expire) ? "; expires=" + expire.toGMTString() : "") + ((path) ? "; path=" + path : "") + ((domaine) ? "; domain=" + domaine : "") + ((securise) ? "; secure" : "");
}
function DateExpiration (DateExp) {

      var aujourdhui = new Date(0);
      var aujourdhuidate = aujourdhui.getTime();
      if (aujourdhuidate > 0)
            DateExp.setTime (DateExp.getTime() - aujourdhuidate);

}

