/* 
 * Fonctions de click
 *
 */
$(document).ready(function() {
	// Titre uniquement
	/*actionScope($("#Scope").attr('checked'));
	$("INPUT.titre_uniquement").change( function() {
		actionScope($(this).attr('checked'));
	});*/
	// Rubriques d'un produit
	$("INPUT.produit.action_rubriques").live('click', function(e) {
		return actionOneProduit();
	});
	// Trier les résultats
	$("SELECT.re_trier").change( function() {
		var url = $(this).attr('rel');
		var order = $(this).val();
		document.location = url + order;
	});
	//Ajout panier
	$("A.ajouter_panier").live('click', function(e) {
		e.preventDefault();
		var url = $(this).attr('href');
		if(url!='' && url!='undefined' && url!=undefined){
			var id_doc = $(this).attr('rel');
			var parent = $(this).parent();
			$.ajax({ url: url + '?AjouterPanier=' + id_doc + '&Ajax', success: function(data) {
				parent.find('.ajouter_panier').hide();
				parent.find('.supprimer_panier').show();
				$('.toolTip-text').hide();
				$('#MaSelectionDeDocuments').html(data);
			}});
		}
	});
	//Supprimer panier
	$("A.supprimer_panier").live('click', function(e) {
		e.preventDefault();
		var url = $(this).attr('href');
		if(url!='' && url!='undefined' && url!=undefined){
			var id_doc = $(this).attr('rel');
			var parent = $(this).parent();
			$.ajax({ url: url + '?SupprimerPanier=' + id_doc + '&Ajax', success: function(data) {
				parent.find('.supprimer_panier').hide();
				parent.find('.ajouter_panier').show();
				$('#basket_'+id_doc).hide();
				$('.toolTip-text').hide();
				$('#MaSelectionDeDocuments').html(data);
				$.ajax({ url: url + '?TotalPanier&Ajax', success: function(data) {
					$('#basket_count').html(data);
				}});
			}});
		}
	});
	//Augmenter police
	$("A.augmenter_police").click(function(e) {
		e.preventDefault();
		increaseFontSize();
	});
	//Diminuer police
	$("A.diminuer_police").click(function(e) {
		e.preventDefault();
		decreaseFontSize();
	});
	//Envoyer par email
	$("A.envoyer_par_email").click(function(e) {
		e.preventDefault();
		var url = $(this).attr('href');
		$('#AjaxContainer').hide();
		$.ajax({ url: url + 'Ajax', success: function(data) {
			$('#AjaxContainer').html(data);
			$('#AjaxContainer').show();
			//$('#AjaxEnvoyerParEmailForm').css('left',($(window).width() - $('#AjaxEnvoyerParEmailForm').width()) / 2);
			$('#EmailForm').attr('action',url);
		}});
	});
	$("INPUT.envoyer_par_email").live('click', function(e) {
		e.preventDefault();
		$('#AjaxEnvoyerParEmailForm').find('span.error').hide();
		if($('#EmailClient').attr('value')!=''){
			if($('#EmailDestinataire').attr('value')!=''){
				if($('#EmailMessageClient').attr('value')!=''){
					var form_url = $('#EmailForm').attr('action');
					var form_datas = 'EmailExpediteur=' + $('#EmailExpediteur').attr('value');
					form_datas += '&EmailExpediteurNom=' + $('#EmailExpediteurNom').attr('value');
					form_datas += '&EmailSujet=' + $('#EmailSujet').attr('value');
					form_datas += '&EmailMessage=' + $('#EmailMessage').attr('value');
					if($('#EmailMessageFin').attr('value')!=undefined) form_datas += '&EmailMessageFin=' + $('#EmailMessageFin').attr('value');
					form_datas += '&EmailClient=' + $('#EmailClient').attr('value');
					form_datas += '&EmailDestinataire=' + $('#EmailDestinataire').attr('value');
					form_datas += '&EmailMessageClient=' + $('#EmailMessageClient').attr('value');
					$.ajax({ url: '/Recherche/Waiting/Ajax', success: function(data) {
						$('#AjaxContainer').hide();
						$('#AjaxContainer').html(data);
						$('#AjaxContainer').show();
						//$('#AjaxWaiting').css('left',($(window).width() - $('#AjaxWaiting').width()) / 2);
						$.ajax({ type: "POST", url: form_url, data: form_datas, success: function(data) {
							$('#AjaxContainer').html(data);
							//$('#AjaxEnvoyerParEmailResultat').css('left',($(window).width() - $('#AjaxEnvoyerParEmailResultat').width()) / 2);
						}});
					}});
				}else $('#EmailMessageClient').parent().find('label').append('<br /><span class="error">' + $('#EmailMessageClient').attr('rel') + '</span>');
			}else $('#EmailDestinataire').parent().find('label').append('<br /><span class="error">' + $('#EmailDestinataire').attr('rel') + '</span>');
		}else $('#EmailClient').parent().find('label').append('<br /><span class="error">' + $('#EmailClient').attr('rel') + '</span>');
	});
	// Fermer le calque ajax
	$(".fermer_ajax").live('click', function(e) {
		$('#AjaxContainer').hide();
	});
	// Imprimer
	$("A.imprimer").click(function(e) {
		e.preventDefault();
		var url = $(this).attr('href');
		var popup_print = window.open(url,'popup_print','left=10,top=10,width=680,height=500,scrollbars=1,menubar=1,status=0');
	});
	// Afficher en pdf
	/*$("A.afficher_pdf").click(function(e) {
		e.preventDefault();
		var url = $(this).attr('href');
		$.ajax({ url: '/Recherche/Waiting/Ajax', success: function(data) {
			$('#AjaxContainer').hide();
			$('#AjaxContainer').html(data);
			$('#AjaxContainer').show();
			//$('#AjaxWaiting').css('left',($(window).width() - $('#AjaxWaiting').width()) / 2);
			setTimeout('Exporter("'+url+'")',200);
		}});
	});
	//Exporter
	$("A.exporter").click(function(e) {
		e.preventDefault();
		var url = $(this).attr('href');
		$.ajax({ url: '/Recherche/Waiting/Ajax', success: function(data) {
			$('#AjaxContainer').hide();
			$('#AjaxContainer').html(data);
			$('#AjaxContainer').show();
			//$('#AjaxWaiting').css('left',($(window).width() - $('#AjaxWaiting').width()) / 2);
			setTimeout('Exporter("'+url+'")',200);
		}});
	});*/
	// Fermer la fenêtre
	$("A.fermer_fenetre").click(function(e) {
		e.preventDefault();
		window.close();
	});
	$("#Document A").click(function(e) {
		if($(this).attr('className')=='' && ($(this).attr('href')!='' && $(this).attr('href').substring(0,1)!='#' && $(this).attr('href').substring(0,11)!='javascript:')){
			$(this).attr('target','_blank');
		}
	});
	// Clic sur les images
	$("#Document IMG.thumbnail").click(function(e) {
		e.preventDefault();
		var img_id = $(this).attr('id').replace('tn_','');
		$("#"+img_id).addClass('bigger').show();
		//$(this).hide();
	});
	$("#Document IMG.bigger").live('click', function(e) {
		e.preventDefault();
		var tnb_id = 'tn_' + $(this).attr('id');
		$(this).removeClass('bigger').hide();
		//$("#"+tnb_id).show();
	});
	//Tooltips
	$(".tooltips").toolTips();
});
/* 
 * Fonctions
 *
 */
function showDateControls(value){
	if (value == 1){
		$('#Date1').value = $('#Date2').value = '';
		$('#1date').show();
		$('#2date').hide();
	}else if(value == 2){
		$('#Date').value = '';
		$('#1date').hide();
		$('#2date').show();
	}else{
		$('#Date').value = $('#Date1').value = $('#Date2').value = '';
		$('#1date').hide();
		$('#2date').hide();
	}
}
function showConvcollControls(value){
	if(value == 1){
		$('#TypeConvcoll_1').attr('checked',true);
		$('#TypeConvcoll_Products').attr('disabled',true);
		$('#TypeConvcoll_IdConvcoll').attr('disabled',false);
		$('#TypeConvcoll_IdCc').attr('disabled',true);
	}else if(value == 2){
		$('#TypeConvcoll_2').attr('checked',true);
		$('#TypeConvcoll_Products').attr('disabled',true);
		$('#TypeConvcoll_IdConvcoll').attr('disabled',true);
		$('#TypeConvcoll_IdCc').attr('disabled',false);
	}else{
		$('#TypeConvcoll_0').attr('checked',true);
		$('#TypeConvcoll_Products').attr('disabled',false);
		$('#TypeConvcoll_IdConvcoll').attr('disabled',true);
		$('#TypeConvcoll_IdCc').attr('disabled',true);
	}
}
function actionOneProduit(){
	$('LI.precisions_rubriques').remove();
	$('LI.precisions_sequences').remove();
	if($.find('INPUT.produit.action_rubriques:checked').length==1){
		var checkbox = $.find('INPUT.produit.action_rubriques:checked');
		var parent = $(checkbox).parent();
		while(!$(parent).hasClass('produits')) parent = $(parent).parent();
		var url = $(parent).attr('rel');
		var produit = $(checkbox).attr('value');
		$.ajax({ url: url + produit + '/Ajax', success: function(data) {
			$(parent).after(data);
		}});
	}
}
function actionScope(checked){
	if(checked==true) hideScope();
	else showScope();
}
function showScope(){
	$('#ExactMatch').attr('disabled',false);
	$('#SomeWords').attr('disabled',false);
	$('#NotWords').attr('disabled',false);
}
function hideScope(){
	//$('#ExactMatch').attr('disabled',true);
	//$('#SomeWords').attr('disabled',true);
	//$('#NotWords').attr('disabled',true);
}
function search_bd(id_doc){
}
function openModele(modele){
}
function openURL(url){
	window.open(url,'popurl','left=10,top=10,width=680,height=500,scrollbars=1,menubar=1,status=1');
}
function OpenForm(modele,mode){
	document.location = '/modeles/modeles_detail.php?titre=&categorie=-1&page=&num=' + modele;
}
function Redirection(id_doc){
	var popup = window.open('/Recherche/Document/'+id_doc+'/Popup','popup','left=10,top=10,width=680,height=500,scrollbars=1,menubar=1,status=0');
}
function Exporter(url){
	var export_popup = window.open(url,'export_popup','left=1,top=1,width=1,height=1,scrollbars=0,menubar=0,status=0');
	window.focus();
	$('#AjaxContainer').hide();
}

/*
 * Show a calendar
 * => Works if (and only if) calendar.js + calendar-[lang].js has been loaded
 *
 * @param	obj			The object where to display calendar (id or object itself)
 * @param	date		The date used to initialize calendar (or null to use obj.value)
 *
 */
var _calendar = null;
var _calendarDateTimeId = null;
var _calendarCallback = null;
function onCalendar_Close(){
	_calendar.hide();
	if (_calendarCallback) calendarCallback(null);
}

function onCalendar_Selection(calendar, date){
	calendar.sel.value = date;
	// DateTime mode ?
	if (_calendarDateTimeId) updateCalendarDateTime(_calendarDateTimeId);
	_calendar.hide();
	if (_calendarCallback) calendarCallback(date);
}

function showCalendar(idOrObject, date, callback, calendarDateTimeId){
	// Get selection objet (usually an input box)
	var obj = null;
	if (typeof idOrObject == "string") obj = document.getElementById(idOrObject);
	else obj = idOrObject;
	if (_calendar != null){
		// we already have one created, so just update it
		_calendar.hide();
	}else{
	    // Remember datetime id (for "datetime" mode only)
	    _calendarDateTimeId = calendarDateTimeId;
	    // Remember callback
		if (callback) _calendarCallback = callback;
		// first-time call, create the calendar
		_calendar = new Calendar(true, null, onCalendar_Selection, onCalendar_Close);
		_calendar.setRange(1970, 2070);
		_calendar.create();
	}
	if (date) _calendar.parseDate(date);
	else _calendar.parseDate(obj.value);
	_calendar.sel = obj;
	_calendar.showAtElement(obj);
	return false;
}

/*
 * Update a calendar picked-up value when it's in "datetime" mode
 * (i.e. with hour,minute,seconds fields)
 */
function updateCalendarDateTime(id){
    var date = $(id + '_date');
    var time = $(id + '_time');
    var datetime = $(id);
    if (datetime){
        if (date){
            datetime.value = date.value + ' ';
            if (time) datetime.value += time.value;
            else datetime.value += '00:00:00';
        }else{
            if (time) datetime.value = time.value;
        }
    }
}
/*
 * increase/decrease FontSize
 *
 */
var min=8;
var max=18;
increaseFontSize = function() {
	var p = document.getElementById('Document').getElementsByTagName('p');
	for(i=0;i<p.length;i++) {
		if(p[i].style.fontSize) {
			var s = parseInt(p[i].style.fontSize.replace("px",""));
		} else {
			var s = 12;
		}
		if(s!=max) {
			s += 1;
		}
		p[i].style.fontSize = s+"px"
	}
	var span = document.getElementById('Document').getElementsByTagName('span');
	for(j=0;j<span.length;j++) {
		if(span[j].style.fontSize) {
			var t = parseInt(span[j].style.fontSize.replace("px",""));
		} else {
			var t = 12;
		}
		if(t!=max) {
			t += 1;
		}
		span[j].style.fontSize = t+"px"
	}
	var div = document.getElementById('Document').getElementsByTagName('div');
	for(j=0;j<div.length;j++) {
		if(div[j].style.fontSize) {
			var t = parseInt(div[j].style.fontSize.replace("px",""));
		} else {
			var t = 12;
		}
		if(t!=max) {
			t += 1;
		}
		div[j].style.fontSize = t+"px"
	}
	var li = document.getElementById('Document').getElementsByTagName('li');
	for(j=0;j<li.length;j++) {
		if(li[j].style.fontSize) {
			var t = parseInt(li[j].style.fontSize.replace("px",""));
		} else {
			var t = 12;
		}
		if(t!=max) {
			t += 1;
		}
		li[j].style.fontSize = t+"px"
	}
	var td = document.getElementById('Document').getElementsByTagName('td');
	for(j=0;j<td.length;j++) {
		if(td[j].style.fontSize) {
			var t = parseInt(td[j].style.fontSize.replace("px",""));
		} else {
			var t = 12;
		}
		if(t!=max) {
			t += 1;
		}
		td[j].style.fontSize = t+"px"
	}
	var td = document.getElementById('Document').getElementsByTagName('i');
	for(j=0;j<td.length;j++) {
		if(td[j].style.fontSize) {
			var t = parseInt(td[j].style.fontSize.replace("px",""));
		} else {
			var t = 12;
		}
		if(t!=max) {
			t += 1;
		}
		td[j].style.fontSize = t+"px"
	}
	var td = document.getElementById('Document').getElementsByTagName('b');
	for(j=0;j<td.length;j++) {
		if(td[j].style.fontSize) {
			var t = parseInt(td[j].style.fontSize.replace("px",""));
		} else {
			var t = 12;
		}
		if(t!=max) {
			t += 1;
		}
		td[j].style.fontSize = t+"px"
	}
	var td = document.getElementById('Document').getElementsByTagName('a');
	for(j=0;j<td.length;j++) {
		if(td[j].style.fontSize) {
			var t = parseInt(td[j].style.fontSize.replace("px",""));
		} else {
			var t = 12;
		}
		if(t!=max) {
			t += 1;
		}
		td[j].style.fontSize = t+"px"
	}
}
decreaseFontSize = function() {
	var p = document.getElementById('Document').getElementsByTagName('p');
	for(i=0;i<p.length;i++) {
		if(p[i].style.fontSize) {
			var s = parseInt(p[i].style.fontSize.replace("px",""));
		} else {
			var s = 12;
		}
		if(s!=min) {
			s -= 1;
		}
		p[i].style.fontSize = s+"px"
	}
	var span = document.getElementById('Document').getElementsByTagName('span');
	for(i=0;i<span.length;i++) {
		if(span[i].style.fontSize) {
			var s = parseInt(span[i].style.fontSize.replace("px",""));
		} else {
			var s = 12;
		}
		if(s!=min) {
			s -= 1;
		}
		span[i].style.fontSize = s+"px"
	}
	var div = document.getElementById('Document').getElementsByTagName('div');
	for(i=0;i<div.length;i++) {
		if(div[i].style.fontSize) {
			var s = parseInt(div[i].style.fontSize.replace("px",""));
		} else {
			var s = 12;
		}
		if(s!=min) {
			s -= 1;
		}
		div[i].style.fontSize = s+"px"
	}
	var li = document.getElementById('Document').getElementsByTagName('li');
	for(i=0;i<li.length;i++) {
		if(li[i].style.fontSize) {
			var s = parseInt(li[i].style.fontSize.replace("px",""));
		} else {
			var s = 12;
		}
		if(s!=min) {
			s -= 1;
		}
		li[i].style.fontSize = s+"px"
	}
	var td = document.getElementById('Document').getElementsByTagName('td');
	for(j=0;j<td.length;j++) {
		if(td[j].style.fontSize) {
			var t = parseInt(td[j].style.fontSize.replace("px",""));
		} else {
			var t = 12;
		}
		if(t!=min) {
			t -= 1;
		}
		td[j].style.fontSize = t+"px"
	}
	var td = document.getElementById('Document').getElementsByTagName('i');
	for(j=0;j<td.length;j++) {
		if(td[j].style.fontSize) {
			var t = parseInt(td[j].style.fontSize.replace("px",""));
		} else {
			var t = 12;
		}
		if(t!=min) {
			t -= 1;
		}
		td[j].style.fontSize = t+"px"
	}
	var td = document.getElementById('Document').getElementsByTagName('b');
	for(j=0;j<td.length;j++) {
		if(td[j].style.fontSize) {
			var t = parseInt(td[j].style.fontSize.replace("px",""));
		} else {
			var t = 12;
		}
		if(t!=min) {
			t -= 1;
		}
		td[j].style.fontSize = t+"px"
	}
	var td = document.getElementById('Document').getElementsByTagName('a');
	for(j=0;j<td.length;j++) {
		if(td[j].style.fontSize) {
			var t = parseInt(td[j].style.fontSize.replace("px",""));
		} else {
			var t = 12;
		}
		if(t!=min) {
			t -= 1;
		}
		td[j].style.fontSize = t+"px"
	}
}
