

var Url = {

	// public method for url encoding
	encode : function (string) {
		return escape(this._utf8_encode(string));
	},

	// public method for url decoding
	decode : function (string) {
		return this._utf8_decode(unescape(string));
	},

	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";

		for (var n = 0; n < string.length; n++) {

			var c = string.charCodeAt(n);

			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}

		}

		return utftext;
	},

	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;

		while ( i < utftext.length ) {

			c = utftext.charCodeAt(i);

			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}

		}

		return string;
	}

}




jQuery().ready(function() {

	
	
// Searchadvanced
  jQuery(".searchFilterTrigger").click( function () {
    jQuery("#results").empty().append('<img src="/a/img/search-ajax-loader.gif" />');
    jQuery.get(jQuery(this).attr('href')+'&js=1');
    jQuery("a.searchFilterTrigger").removeClass('filterOn');
    jQuery(this).addClass('filterOn');
    //On affiche le remove filters
   // jQuery('#removeFilters').toggle();
    return false;
  });
  jQuery("#removeFilters").click( function () {
    jQuery("a.searchFilterTrigger").removeClass('filterOn');
 //   jQuery('#removeFilters').toggle(); //Plus de filtres, on l'enleve
  });
  jQuery('#jobsToggle').toggle();  //Cache les jobs en trop
  jQuery('#jobsToggleDo').toggle(); //Affiche le voir +
  jQuery('#jobsToggleDo').click(function() { jQuery('#jobsToggle').toggle(); jQuery('#jobsToggleDo').toggle(); }); //Affiche le span

  jQuery('#otherJobsToggle').toggle();  //Cache les jobs en trop
  jQuery('#otherJobsToggleDo').toggle(); //Affiche le voir +
  jQuery('#otherJobsToggleDo').click(function() { jQuery('#otherJobsToggle').toggle(); jQuery('#otherJobsToggleDo').toggle(); }); //Affiche le span

 // jQuery('#removeFilters').toggle(); //Au chargement de la page, aucun filtre, donc on n'affiche pas le bouton permettant de les enlever

  //Fin searchadvanced
  

  //Dates agenda (JW)
  $(function() {
  if ($('.datePicker').length != 0) {
    $('.datePicker').datePicker({clickInput:true});
    $('#date_start').bind(
    		'dpClosed',
    		function(e, selectedDates)
    		{
    			var d = selectedDates[0];
    			if (d) {
    				d = new Date(d);
    				$('#date_end').dpSetStartDate(d.addDays(0).asString());
    			}
    		}
    	);
    	$('#date_end').bind(
    		'dpClosed',
    		function(e, selectedDates)
    		{
    			var d = selectedDates[0];
    			if (d) {
    				d = new Date(d);
    				$('#date_start').dpSetEndDate(d.addDays(-1).asString());
    			}
    		}
    	);    
  }

  
  });
  
  function agendaCheck(elem) {
	  elem.addClass('activ')
	  elem.find(':first-child').attr('value', 1);
  }

  function agendaUncheck(elem) {
	  elem.removeClass('activ')
	  elem.find(':first-child').attr('value', 0);
  }
  
  jQuery('.dayAgendaToggle').click(function() {
	 if (jQuery(this).hasClass('activ')) {
		 agendaUncheck(jQuery(this));
	 } else {
		 agendaCheck(jQuery(this));
	 }
	  return false;
  });
  
  jQuery('#dayAgendaAllMonday').click(function() {
	  if (!$(this).is(":checked")) {
		  $(this).removeAttr('checked');
		  jQuery('.dayAgendaMonday').each(function() {
			  agendaUncheck(jQuery(this));
		  });		  
	  } else {
		  $(this).attr('checked', 'yes');
		  jQuery('.dayAgendaMonday').each(function() {
			  agendaCheck(jQuery(this));
		  });		  
	  }	  

  });
  
  jQuery('#dayAgendaAllTuesday').click(function() {
	  if (!$(this).is(":checked")) {
		  $(this).removeAttr('checked');
		  jQuery('.dayAgendaTuesday').each(function() {
			  agendaUncheck(jQuery(this));
		  });		  
	  } else {
		  $(this).attr('checked', 'yes');
		  jQuery('.dayAgendaTuesday').each(function() {
			  agendaCheck(jQuery(this));
		  });		  
	  }	  

  });  
  
  jQuery('#dayAgendaAllWednesday').click(function() {
	  if (!$(this).is(":checked")) {
		  $(this).removeAttr('checked');
		  jQuery('.dayAgendaWednesday').each(function() {
			  agendaUncheck(jQuery(this));
		  });		  
	  } else {
		  $(this).attr('checked', 'yes');
		  jQuery('.dayAgendaWednesday').each(function() {
			  agendaCheck(jQuery(this));
		  });		  
	  }
  });    
  
  jQuery('#dayAgendaAllThursday').click(function() {
	  if (!$(this).is(":checked")) {
		  $(this).removeAttr('checked');
		  jQuery('.dayAgendaThursday').each(function() {
			  agendaUncheck(jQuery(this));
		  });		  
	  } else {
		  $(this).attr('checked', 'yes');
		  jQuery('.dayAgendaThursday').each(function() {
			  agendaCheck(jQuery(this));
		  });		  
	  }
  });      
  
  jQuery('#dayAgendaAllFriday').click(function() {
	  if (!$(this).is(":checked")) {
		  $(this).removeAttr('checked');
		  jQuery('.dayAgendaFriday').each(function() {
			  agendaUncheck(jQuery(this));
		  });		  
	  } else {
		  $(this).attr('checked', 'yes');
		  jQuery('.dayAgendaFriday').each(function() {
			  agendaCheck(jQuery(this));
		  });		  
	  }
  });    
  
  jQuery('#dayAgendaAllSaturday').click(function() {
	  if (!$(this).is(":checked")) {
		  $(this).removeAttr('checked');
		  jQuery('.dayAgendaSaturday').each(function() {
			  agendaUncheck(jQuery(this));
		  });		  
	  } else {
		  $(this).attr('checked', 'yes');
		  jQuery('.dayAgendaSaturday').each(function() {
			  agendaCheck(jQuery(this));
		  });		  
	  }
  });   
  
  jQuery('#dayAgendaAllSunday').click(function() {
	  if (!$(this).is(":checked")) {
		  $(this).removeAttr('checked');
		  jQuery('.dayAgendaSunday').each(function() {
			  agendaUncheck(jQuery(this));
		  });		  
	  } else {
		  $(this).attr('checked', 'yes');
		  jQuery('.dayAgendaSunday').each(function() {
			  agendaCheck(jQuery(this));
		  });		  
	  }
  });     
  
  //Fin dates agenda
  
  
  /*
  *//*
  jQuery('#searchForm2').submit()
  {
  $("input").each(function (i) {
  alert(this.attr('value'))
  });


  }
  */
  // ok pour le search form
  /*jQuery("#searchForm").click( function (event) {
  // jQuery(this).attr('id')
  if(jQuery(event.target).is('#category_id_auto_complete ul li'))
  {
  var categoryId = jQuery(event.target).attr('id')
  var categoryNameFull = jQuery(event.target).html();

  var categoryName=categoryNameFull.split("&gt;");
  //alert(categoryId + ' -> ' +categoryName[2]);

  jQuery('#searchForm input#job_id').attr('value', categoryId);
  jQuery('#searchForm input#category_id').attr('value', categoryName[2]);

  return false;
  }
  });*/

  // pour l'édition du profil
  jQuery("#profileJobs").click( function (event) {
    // jQuery(this).attr('id')
    if(jQuery(event.target).is('.auto_complete ul li'))
    {
      var categoryId = jQuery(event.target).attr('id')
      var jobNumber = jQuery(event.target).attr('rel')
      var categoryNameFull = jQuery(event.target).html();

      jQuery("#job_"+jobNumber).after("<input type='hidden' name='job_id["+jobNumber+"]' id='job_id_"+jobNumber+"' value='"+categoryId+"' />");

      var categoryName=categoryNameFull.split("&gt;");

      jQuery("#job_"+jobNumber).attr('value', categoryName[2]);
      jQuery("#id_job_"+jobNumber).attr('value', categoryId);

      return false;
    }
  });


  jQuery("a.hideThis").click(function(){
    jQuery("#"+jQuery(this).attr('rel')).hide();
    return false;
  });

  jQuery("a.external").attr("target","_blank");


  function showModal(url, height, width)
  {
    jQuery('body').append('<div class="jqmWindow" id="modalDialog"><div id="modalCloseButton" style="-moz-border-radius:4px 4px 0 0 ;margin-right:10px;padding-bottom:5px;float:right;background:#4A4A41; color: white;padding:5px"><a href="javascript;" class="closeModal" style="font-weight:bold;color:white">Fermer</a></div><iframe frameborder="0" id="jqmContent" src="" width="100%" height="100%" style="overflow:auto"></iframe></div>');
    var dialog = jQuery('#modalDialog')
    .jqm({
      onShow: function(h) {
        var jQuerymodal = jQuery(h.w);
        var jQuerymodalContent = jQuery("iframe", jQuerymodal);
        jQuerymodalContent.html('').attr('src', url+'?quickadd=true');
        if (height > 0) jQuerymodal.height(height);
        if (width > 0) jQuerymodal.width(width);
        h.w.show();
      }
    }).jqmShow();

    //var ttt = jQuery('iframe#jqmContent').innerHeight()
    //alert(ttt)

  }
  function closeModal()
  {
    jQuery('#modalDialog').jqmHide();
    jQuery('#modalDialog').remove();
  }


  jQuery("a.openDialog").click(function(){
    showModal(jQuery(this).attr('href'), 600, 600);
    return false;
  });

  jQuery("a.videoModal").click(function(){
    showModal(jQuery(this).attr('href'), 400, 450);
    return false;
  });

  jQuery("a.closeModal").click(function(){
    closeModal();
    return false;
  });

  jQuery('div.modalBoxContent a.closeModal').click(function(){
    parent.jQuery('#modalDialog').jqmHide();
    return false;
  });

  jQuery('body').click(function(event) {

    if (jQuery(event.target).is('a.closeModal')) { // <- this is the magic
      closeModal();
      return false;
    }
  });

  jQuery(".videoPlay").click(function(){
    var targetload = jQuery(this).attr('href');
    jQuery("#galleryItem").load(targetload);
    jQuery(this).addClass("active");
    return false;
  });

  jQuery(".content input, .content textarea, .content select").focus(function(){
    jQuery(this).parents(".content").addClass("over");
  }).blur(function(){
    jQuery(this).parents(".content").removeClass("over");
  });

  /* QUICKADD SPECIFIC -- AGENDA -- Uncheck "isFree" checkbox when price input is focused */
  function set_checked(checked) {
    $('input[@name=foo]').attr('checked', checked);
  }

  jQuery("input.eventPrice").click(function(){
    jQuery("input.eventIsFree").attr("checked", false);
    jQuery("input.eventPrice").removeClass("disabled");
  })

  jQuery("input.eventIsFree").click(function(){
    if(jQuery("input.eventIsFree").attr("checked")) {
      jQuery("input.eventPrice").addClass("disabled");
      jQuery("input.eventPrice").attr("value", "0");
      jQuery('#isFreeToggle').hide();
    } else {
      jQuery("input.eventPrice").removeClass("disabled");
      jQuery('#isFreeToggle').show();
    }
  })
  
 jQuery('.srchggllkeclass').focus(function(){
  if(this.valu!='')
  {
    this.value=''
  }
 });
  
 jQuery("#panel").hide();

 var activePanel;
 jQuery(".btn-slide").click(function(){
	   var targetload = jQuery(this).attr('name');
	   var culture = jQuery(this).attr('rel');

   jQuery("#panel").load("/"+culture+"/front/sites #"+targetload);
   if (jQuery("#panel").is(":hidden"))
   {
     jQuery("#panel").slideDown("normal");
     jQuery(this).addClass("active");
   }
   else
   {
     if(activePanel!=targetload)
     {
       jQuery("#panel").slideDown("normal");
       jQuery(this).addClass("active");

     }else{
       jQuery("#panel").slideUp("normal");
       jQuery(this).removeClass("active");
     }
     activePanel = targetload;
     return false;
   }

 });
});