var check = false;
var pagination = false;
var docs_page = false;
var hide = new Array();

$(function()
{ 
	$('#switchblog').change(function(){
		document.location = this.form.action+(this.form.action.indexOf('?') == -1 ? '?' : '')+'&switchblog='+this.value;
	});
	
	$('#log_user_id').click(function(){
		this.value = '';
		this.form.user_pwd.value = '';
	});
	
	$('#search-form input[name="searchaction"]').click(function() {
		document.location = '/recherche/'+$('#q').val()+'.html';
		return false;
	});
	
	$('.error').effect('pulsate', { times: 3, duration: 50 });
	$('.confirm').effect('pulsate', { times: 3, duration: 50 });
	$('.warning').effect('pulsate', { times: 3, duration: 50 });
	
	$('.comment-new').effect('highlight', { }, 5000);
	
	$('#message_hide').click(function(){
		$('#message_box').fadeOut('slow');
	});
	
	$('#control h3').click(function() 
	{
		var menu = this.id.substring(8,this.id.length);
		var _Exp = new Date();
		
		if(hide[menu] == true) 
		{
			hide[menu] = false;
			var __Exp = "Fri, 02 Jan 1970 00:00:00 GMT";
		} 
		else 
		{
			hide[menu] = true;
			_Exp.setTime(_Exp.getTime() + (365*24*60*60*1000));
			var __Exp = _Exp.toGMTString();
		}
		$('#'+menu+' ul').slideToggle();
		
		document.cookie = "control"+menu+blog+"=1; expires=" + __Exp;
	});
	
	$('#control .content div').each(function() 
	{
		if(typeof blog != 'undefined') {
			var menu = this.id;
			
			_HM = (document.cookie.indexOf('control'+menu+blog+'=1') != -1 ? 1 : 0);
			if(!_HM) {
				hide[menu] = false;
				return;
			}
			else {
				hide[menu] = true;		
				$('#'+menu+' ul').hide();
			}
		}
	});
	
	$('.footer-show').click(function() 
	{
		$('#footer').slideToggle();
	});
	
	$('form').each(function() 
	{
		$(this).submit(function()
		{
			var submitButton = $(this).find("input[type='submit']");
			$(submitButton).attr('value','Patientez...');
			$(submitButton).attr('disabled','true');
			$(submitButton).attr('style','color:#000;background:#eee;cursor:wait;border-color:#ccc');
		});
  });
  
  $('#form-pagination input[name="btn-pagination"]').click(function() 
  {  	
  	if(parseInt($('#page-query').val()) > 0 && parseInt($('#page-query').val()) <= parseInt($('#page-max').val())) {
  		document.location = $('#page-url').val()+$('#page-query').val()+($('#page-rewrite').val() == 1 ? '.html' : '');
  	}
  	else 
  	{  		
  		if(pagination == false) 
  		{
  			$('#page-query').before('<span class="red">Entrez un nombre entre 1 et '+$("#page-max").val()+'</span>&nbsp;&nbsp');
  			pagination = true;
  		}
  	}
  	return false;
  });
  
  $('.tooltip').ToolTipDemo('#fff');
  
  $('#q').keyup(function()
	{
		if($(this).val() == '') {
			return;
		}
		
		$.post('/search.php',{ autosuggest:$(this).val() } ,function(data)
		{
	 		if(data != 'none')
	 		{
	 			var suggest = '';
	 			var resp = data.split('&');
	 			
	 			for(i=0; i < (resp.length - 1); i++) {
	 				suggest += '<li>'+resp[i]+'</li>';
	 			}
	 			
	 			$('#search-autosuggest div').html('<ul>'+suggest+'</ul>');
	 			$('#search-autosuggest').show();
	 			
	 			$('body').click(function()
				{
					$('#search-autosuggest').hide();
				});
				
				$('#search-autosuggest li').click(function()
				{
					$('#q').val($(this).text());
					$('#search-autosuggest').hide();
					document.location = '/recherche/'+$(this).text()+'.html';
				});
	  	}
	  	else {
	  		$('#search-autosuggest').hide();
	  	}
		});
	});
	
	$('#q').attr('autocomplete','off');	
	$('#q').css({ width:'195px' });
  
  var _online_img = new Image(1,1);
  var _www = 'http://www.cowblog.fr/online.php?';
  _online_img.src = _www+Math.round(Math.random()*1000000000);
});

function bottomToBottom()
{
	if($(window).height() > $('#document').height()) {
		$('#page').height($(window).height());
		$('#manage').height($(window).height());
	}
	setTimeout('bottomToBottom()',1000);
}

function checkBoxes(elem)
{
	$("#check-all").click(function(){
		$(elem).find("input[type='checkbox']").each(function(){
			if(check == false) {
				if(this.disabled != true) {
					this.checked = true;
				}
			} else {
				this.checked = false;
			}
		});
		if(check == false) {
			check = true;
		} else {
			check = false;
		}
	});
}

function uncheckBoxes(elem)
{
	$(elem).find("input[type='checkbox']").each(function(){
		this.checked = false;
	});
}

function prependMsg(type,msg)
{
	switch(type)
	{
		case 'error': var title = 'Erreur'; break;
		case 'warning': var title = 'Avertissement'; break;
		case 'confirm': var title = 'Action réussie'; break;
	}
	
	var message_box = document.getElementById('message_box');
	
	var content = '<p id="message_hide"><a href="javascript:void(0);">'+
		'<img src="img/site/blank.gif" width="14" height="14"></a></p><strong>'+title+
		' :</strong><ul><li>'+msg+'</li></ul>';
	
	if(message_box)
	{
		message_box.className = type;
		message_box.innerHTML = content;
	}
	else
	{	
		$('#container').prepend('<div id="message_box" class="'+type+'">'+content+'</div>');
	}
	
	// IE debug [début]
	document.getElementById('bottom').style.position = 'absolute';
	document.getElementById('bottom').style.bottom = 0;
	// IE debug [fin]
	
	$.scrollTo(0,800);
	
	$('.'+type).effect("pulsate", { times: 3, duration: 50 });
	
	$('#message_hide').click(function()
	{
		$('#message_box').fadeOut('slow');
	});
}

function displayWait()
{
	$.blockUI({ 
		css: 
		{ 
			'z-index':1,
			width:'200px', 
			border:0, 
			background:'transparent', 
			'text-align':'center'
		},
		overlayCSS: 
		{  
			backgroundColor: '#000',  
      opacity: '0'  
    },
		message: '<img src="http://www.cowblog.fr/img/site/loading.gif" alt="Patientez..." />' 
	});
}

function hideWait()
{
	$.unblockUI();
}

function is_numeric(num)
{
	var exp = new RegExp("^[0-9-.]*$","g");
	return exp.test(num);
}

function dialog(d_id,d_title,d_content,d_width,d_height)
{
	if($('#dialog-'+d_id).is('div')) {
		$('#dialog-'+d_id).remove();
	}
	
	$('#container').prepend('<div id="dialog-'+d_id+'"></div>');
	$('#dialog-'+d_id).html(d_content);	
	
	$('#pub728x90').hide();
	
	$('#dialog-'+d_id).dialog(
	{
    width: d_width,
    height: d_height,
		modal: true, 
    resizable: false,
    title: d_title,
    show: 'slow'
	});
	
	/*
	if($.browser.msie && parseInt($.browser.version) <= 6) {
		var dialog_resize = '#dialog-'+d_id;
	}
	else {
		var dialog_resize = '#dialog-'+d_id+' .ui-dialog-content';
	}	
	
	$(dialog_resize).css({
		height: parseInt(d_height - 50),
		width: parseInt(d_width - 15)
	});
	*/
}

function dialog_close(d_id)
{
	$('#pub728x90').show();
	$('#dialog-'+d_id).dialog('close');
}

function confirm(msg, callback)
{
	var content = '<div style="width:390px; text-align:center">'+msg+'<br /><br />'+
	'<input type="submit" value="Annuler" class="cancel-button">&nbsp;'+
	'<input type="submit" value="Confirmer" class="confirm-button"></div>';
	
	dialog('confirm','Demande de confirmation',content,420,(75+(msg.length/60)*25));
	
	$('#dialog-confirm').find('.confirm-button').click(function() {
		callback.apply();
		$('#dialog-confirm').dialog('close');
		return false;
	});
	
	$('#dialog-confirm').find('.cancel-button').attr("style", "color:#000;background:#eee;border-color:#ccc");
	
	$('#dialog-confirm').find('.cancel-button').click(function() {
		dialog_close('confirm');
		return false;
	});
}

function alert(msg)
{
	var content = '<div style="width:390px; text-align:center">'+msg+'<br /><br />'+
	'<input type="submit" value="Fermer cette fenêtre" class="cancel-button"></div>';
	
	dialog('alert','Avertissement',content,410,(75+(msg.length/60)*20));
	
	$('#dialog-alert').find('.cancel-button').click(function() {
		dialog_close('alert');
		return false;
	});
}

function strip_tags(str, allowed_tags) 
{
	var key = '', tag = '', allowed = false;
	var matches = allowed_array = [];
	var allowed_keys = {};
	
	var replacer = function(search, replace, str) {
		return str.split(search).join(replace);
	};
	
	// Build allowes tags associative array
	if(allowed_tags) {
		allowed_array = allowed_tags.match(/([a-zA-Z]+)/gi);
	}
	
	str += '';
	
	// Match tags
	matches = str.match(/(<\/?[^>]+>)/gi);
 
	// Go through all HTML tags
	for(key in matches) {
		if(isNaN(key)) {
			// IE7 Hack
			continue;
		}
 
		// Save HTML tag
		html = matches[key].toString();
 
		// Is tag not in allowed list? Remove from str!
		allowed = false;
 
		// Go through all allowed tags
		for(k in allowed_array) {
			// Init
			allowed_tag = allowed_array[k];
			i = -1;
 
			if (i != 0) { i = html.toLowerCase().indexOf('<'+allowed_tag+'>');}
			if (i != 0) { i = html.toLowerCase().indexOf('<'+allowed_tag+' ');}
			if (i != 0) { i = html.toLowerCase().indexOf('</'+allowed_tag)   ;}
 
			// Determine
			if(i == 0) {
				allowed = true;
				break;
			}
		}
 
		if(!allowed) {
			str = replacer(html, "", str); // Custom replace. No regexing
		}
	}
	return str;
}

$.fn.extend({ reset: function() {
	return this.each(function() { $(this).is('form') && this.reset(); });
	} 
});