/**
 * Cookie plugin
 *
 * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};




	var verifdate = function() {
		var date_limite = $.datepicker._determineDate("-1y", new Date()).getTime();
		
		if ($(this).hasClass("ref_index")) {
			switsh_index (this, $(this).datepicker( 'getDate' ).getTime(), date_limite) ;
		}
		
		$(this).parents(".ref_index_periode").each( function () {
			var dates = new Array () ;
			var index_date = 0 ;
			var somm_date = 0 ;
			var moyenne_date = 0;
			
			$("input",this).each( function () {
				somm_date += $(this).datepicker( 'getDate' ).getTime();
				index_date ++ ;
			})
			moyenne_date = somm_date/index_date ;

			switsh_index (this, moyenne_date, date_limite) ;
		}) ;
		
		function switsh_index (input_obj, date, date_limite) {
			if (date <= date_limite){
				//$(input_obj).parents("form").each( function () { $(".type_index",this).html("Type d'index : archives"); }) ;
				attr_action = $(input_obj).parents("form").attr("action");
				attr_action = attr_action.replace("/gesca/","/archive-gesca/");
				$(input_obj).parents("form").attr("action", attr_action);
			}else{
				//$(input_obj).parents("form").each( function () { $(".type_index",this).html("Type d'index : base"); }) ;
				attr_action = $(input_obj).parents("form").attr("action");
				attr_action = attr_action.replace("/archive-gesca/","/gesca/");
				$(input_obj).parents("form").attr("action", attr_action);
			}
		}
	};


// Fonction jquery
var base_id_cookie = 100 ;
$.fn.extend({
	input_value_default :  function () {
		// pour que cette fonction marche corectement ne pas oublire attr 'value_default' dans l'input
		// ex. : <input type="text" value_default="ecrivez un mot" value="" name="name" id="un_mt_cles"/> ee
		$(this).each ( function (){
			$(this)
			.focus (function(){
				if($(this).attr ("value") == $(this).attr ("value_default"))
					// activer l'input en lui atribuan le name
					$(this).val("").turnAttr("name","on");
			})
			.blur (function () {
				if($(this).attr ("value") == "")
					// desactiver l'input si celui ci est vide 
					$(this).val($(this).attr ("value_default")).turnAttr("name","off");
			});
			// si l'input est vide, lui atribuer la valeur par defaut
			if ($(this).attr ("value") == "")
				$(this).attr ({"value":$(this).attr("value_default")}).turnAttr("name","off") ;
		})
	},
	click_ongle : function (num_result,bt_open,obg_ongles) {
		$.cookie(num_result,"true");
		var linke_ongl = $(this).attr("ongl");
		//alert(obg_ongles.html());
		obg_ongles.each(function () {
			// - definir le cookie à false pour désactiver l'ongle
			$.cookie( $("."+bt_open+"_on",this).attr("num_bt"), "false" );
			// - desactiver l'ongle dejà actif
			$("."+bt_open+"_on",this).addClass(bt_open+"_off").removeClass(bt_open+"_on");
			// - masquer le contenu dejà actif 
			$("."+bt_open+"_body_on", this).removeClass(bt_open+"_body_on");
			// + afficher le nouveau contenu
			$("."+linke_ongl,this).addClass(bt_open+"_body_on").removeClass(bt_open+"_body_off");
		})
		// + activer le nouveau ongle
		$(this).addClass(bt_open+"_on").removeClass(bt_open+"_off");
	},
	obj_ongle : function (bt_open,body) {
		var obg_ongles = $(this);
		obg_ongles.each(function (num_boite) {
			// creation d'une reference unique pour le cookie
			var title = $(this).attr("tt") ? $(this).attr("tt")+"_"+num_boite : "id_"+num_boite;
			// boucle sur les boutons
			$("."+bt_open,this).each( function (num) {
				//si le cookie correspond à l'omgles
				if($.cookie("num_"+title+"_"+num)=="true"){
					$(this).click_ongle("num_"+title+"_"+num,bt_open,obg_ongles);
				} else {
					$.cookie("num_"+title+"_"+num,"false");
				}
				$(this).attr("num_bt","num_"+title+"_"+num) // attribut pour lier bt / cookie
				.click(function () {
					$(this).click_ongle("num_"+title+"_"+num,bt_open,obg_ongles);
					return false;
				});
			})
		}).addClass("ongle_js_act");
	},
	folding_simple : function (item,bt_open,body,mode_open,animate) {
		var root = $(this);
		root.each ( function () {
			base_id_cookie += 100 ;
			if ($("."+item+"_on", root).html() || $("."+item+"_off", root).html()) { var cooki_on = false ; }else { var cooki_on = true ; }
			$("."+item, this).each ( function (num) {
				var id_cookie = base_id_cookie+num ;
				var obg_body = $("."+body,this) ;
				$(this).attr("cookie",base_id_cookie+num);
				$("."+bt_open,this).each ( function () {
					function switsh_folding (obg_this, on_off_dep,on_off_fin) {
						var cookie = $(obg_this).parents("."+item).attr("cookie");
						$(obg_this).parents("."+item).removeClass(item+"_"+on_off_dep).addClass(item+"_"+on_off_fin);
						if ($("."+bt_open+"_txt",obg_this).html()) {
							$("."+bt_open+"_txt",obg_this).each ( function () {
								var attr_off_on = "txt_"+on_off_dep ;
								if ($(this).attr("txt_"+on_off_fin)) $(this).html($(this).attr("txt_"+on_off_fin));
							})
						}
						$.cookie(item+cookie,on_off_fin);
					}
					if (cooki_on){
						if ($.cookie(item+id_cookie)!="on") {
							switsh_folding (this, 'on','off');
						} else {
							switsh_folding (this,'off','on');
						}
					}
					$(this).click (function () {
						var obg_bt_open = this ;
						if ($(this).parents("."+item+"_on").html()){
							if (mode_open!="single_always") obg_body.slideUp("slow",function () {switsh_folding (obg_bt_open,'on','off');});
						}else{
							function test_obg (obg) { if (obg.html()) { return true;  } else { return false; } }
							if (mode_open=="single" || mode_open=="single_always"){
								root.each ( function () {
									$("."+item+"_on", this).each ( function () {
										var obg_bt_close = $("."+bt_open,this);
										if ($("."+body,this).html()) {
											$("."+body,this).slideUp("slow",function () {switsh_folding (obg_bt_close,'on','off');});
										} else {
											switsh_folding (obg_bt_close,'on','off');
										}
									})
								});
								if (obg_body.html()) {									
									obg_body.slideDown("slow",function () {switsh_folding (obg_bt_open,'off','on');});
								} else { switsh_folding (obg_bt_open,'off','on'); }
							} else {
								obg_body.slideDown("slow",function () {switsh_folding (obg_bt_open,'off','on');});
							}
						}
					});
				});
			})	
		}).addClass(item+"_js");
	},
	fenetre_flottante : function (parent_obj,class_off, block_window, titre_actif) {
		$('body').click(function() {
			$("."+parent_obj).addClass(class_off);
		});
		$(this).each (function () {
			var titre = $(this).text();
			$(this).parents("."+parent_obj).each ( function () {
				if (titre_actif) {
					$("."+block_window,this).prepend("<h3><a href='#_' class='bt_ferm'>Fermer</a>"+titre+"</h3>");
					$(".bt_ferm",this).click ( function () {
						$(this).parents("."+parent_obj).addClass(class_off);	
						return false ;
					});
				}
				$(".lien_actif a",this).click ( function () {
					window.location.replace($(this).attr("href")); return false;				 
				})
			}).click ( function () { return false ;});
			$(this).click ( function () {
				if($(this).parents("."+class_off).html()) {
					$("."+parent_obj).addClass(class_off);
					$(this).parents("."+parent_obj).removeClass(class_off);
				}else {
					$(this).parents("."+parent_obj).addClass(class_off);		
				}
				return false ;
			});
		})
	},
	clone_obj : function (item_elment,txt_add,txt_remove,balise_obj,class_obj,limite,function_callback) {
		var parent_root = $(this);
		$(item_elment,parent_root).each ( function () {
			var obj = $(this) ;
			obj.wrap("<"+balise_obj+" class='"+class_obj+"'></"+balise_obj+">");
			obj.after("<a href='#_' class='"+class_obj+"_add'>"+txt_add+"</a>");
			obj.after("<a href='#_' class='"+class_obj+"_remove'>"+txt_remove+"</a>");
			obj.parent().clone_event (parent_root,function_callback,class_obj,limite);
		});
	},
	clone_event : function (parent_root,function_callback,class_obj,limite) {
		var num_items = $("."+class_obj,parent_root).length ;
		$("."+class_obj+"_add",this).click (function (){
			$(this).parent().clone().appendTo(parent_root).each( function_callback ).clone_event (parent_root,function_callback,class_obj,limite);
			return false
		}) ;
		$("."+class_obj+"_remove",this).click (function (){
			$(this).parent().remove();
			num_items = $("."+class_obj,parent_root).length ;
			return_num_obj ();
			return false
		}) ;
		function return_num_obj () {
			if (num_items>1) {
				$("."+class_obj,parent_root).each ( function (num) {
					if (num==0) $(this).addClass ("prem_item"); else $(this).removeClass ("prem_item");
					if (num_items==num+1) $(this).addClass ("der_item"); else $(this).removeClass ("der_item");
					if (num_items==limite) $(this).addClass("end_item"); else $(this).removeClass ("end_item");
				});
				$(parent_root).removeClass("seul")
			}
			else  $(parent_root).addClass("seul");
		}
		return_num_obj ();
	},
	slide_image_click : function (cyble,sense,text) {
		$(this).click( function () {
			on = cyble+"_on";
			off = cyble+"_off"
			active_img = $("."+cyble+" ."+on) ;
			suiv_img = active_img.prev_next(sense) ;
			if (active_img.prev_next(sense).html()){
				load_img_obj = suiv_img.prev_next(sense);
				load_img_src = load_img_obj.attr("src_img");
				if (load_img_src) {
					load_img_html = load_img_obj.html() ;
					load_img_obj
					.addClass(off)
					.removeAttr("src_img")
					.html("<img src='"+load_img_src+"' />"+load_img_html);
				}
				active_img.removeClass(on).addClass(off);
				suiv_img.removeClass(off).addClass(on);
				txt_suiv_img_r = suiv_img.attr("bloc_blanc_droit");
				txt_suiv_img_l = suiv_img.attr("bloc_blanc_gauche");
				if (!txt_suiv_img_r) txt_suiv_img_r = $(".bloc_blanc_droit",suiv_img).html();
				if (!txt_suiv_img_l) txt_suiv_img_l = $(".bloc_blanc_gauche",suiv_img).html();
				$("."+text+" .bloc_blanc_gauche p").html(txt_suiv_img_l);
				$("."+text+" .bloc_blanc_droit p").html(txt_suiv_img_r);
			}
		})
	},
	prev_next : function (sense) {
		if (sense=="moins"){ return $(this).prev()}
		else { return $(this).next()}
	}
})



