$(document).ready(function(){
	
	//sous-menus	
	$('#dropdown2').nmcDropDown({submenu_selector: 'ul',
		show: {opacity: 'show', top: '-=10px'},
		show_speed: 100,
		hide: {opacity: 'hide', top: '+=10px'},
		hide_speed: 300
	});
	//var max = Math.max($('#pagecontent').height(), Math.max($('#home_content3').height()));
	//$('#pagecontent').height(max+15);
	//$('#home_content3').height(max);
	$('.bigboxlien a strong').vAlign();
	$('.box_labelfull_link a.box_labelfull_link_txt strong').vAlign();
	$('.faq_titre_repeat h2.trigger a').vAlign();
	
	// Logotype moptip
	$('.logo_moptip').mopTip({'w':95,'style':"overOut",'get':"#moptip_logo"});
	
	//toogle faq
	$(".toggle_container").hide();
	$("h2.trigger").toggle(function(){
		$(this).addClass("active"); 
		}, function () {
			$(this).removeClass("active");
		});
		$("h2.trigger").click(function(){
			$(this).next(".toggle_container").slideToggle("slow,");
		});
	
	//slide annonce	
	jQuery('.annonce_slide').toogslider();
		
	// galerie
	jQuery('.gallery_cio_unstyled').addClass('gallery_cio'); // adds new class name to maintain degradability
	jQuery('ul.gallery_cio').galleria({
		history   : true, // activates the history object for bookmarking, back-button etc.
		clickNext : true, // helper for making the image clickable
		insert    : '#main_image', // the containing selector for our main image
		onImage   : function(image,caption,thumb) { // let's add some image effects for demonstration purposes
			image.css('display','none').fadeIn(1000);
			caption.css('display','none').fadeIn(1000);
			var _li = thumb.parents('li');
			_li.siblings().children('img.selected').fadeTo(500,0.3);
			thumb.fadeTo('fast',1).addClass('selected');
			image.attr('title','Image suivante >>');
		},
		onThumb : function(thumb) { // thumbnail effects goes here
			var _li = thumb.parents('li');
			var _fadeTo = _li.is('.active') ? '1' : '0.3';
			thumb.css({display:'none',opacity:_fadeTo}).fadeIn(1500);
			thumb.hover(
				function() { thumb.fadeTo('fast',1); },
				function() { _li.not('.active').children('img').fadeTo('fast',0.3); } // don't fade out if the parent is active
			)
		}
	});
	
	//filter annonces
	var mobilhomefilter = function(){
	  $('.bigboxcol').show();
	  var filter = $('#tarifsfilter').val().split('-');
	  if (filter) {
	    $('.tarifs').each(function() {
	      var v = parseInt($(this).html().replace(' ', ''));
	      if (v<filter[0] || v>filter[1]) $(this).parents('.bigboxcol').hide();
	    });    
	  }
	  var filter = parseInt($('#chambres').val());
	  if (filter) { $('.chambres').each(function() { if (parseInt($(this).html()) != filter) $(this).parents('.bigboxcol').hide(); }); }
	  var filter = $('#type').val();
	  if (filter) { $('.type').each(function() { if ($(this).html() != filter) $(this).parents('.bigboxcol').hide(); }); }
	
	}
	
	$(document).ready(function(){	
	  $('#tarifsfilter, #chambres, #type').change(mobilhomefilter);
	});	
	
	// concours
  jQuery('#tg_concours form').submit(function(e){
    e.preventDefault();
    var emailRegexp = /[a-z0-9\-\.]+@[a-z0-9\-\.]+\.[a-z\.]{2,}/i;
    
    var form_email = jQuery(this).find('input:text[name=email]').val();
    var form_name = jQuery(this).find('input:text[name=name]').val();
    var subscribe = jQuery(this).find('input:checkbox[name=subscribe]').attr('checked');
    
    var url = '/index.php?module=newsletter&action=subscribe&email=:email&sitename=:sitename';
    url = url.replace(':email', form_email);
    url = url.replace(':sitename', 'cio_portail');
    
    // verif email valide
    if(form_email != '' && form_name != '' && !form_email.match(emailRegexp)) {
      jQuery('#tg_concours form').find('input:text[name=email]').after('<label class="error" for="email" generated="true">'+contest_error_format_mail+'</label>');
      return false;
    } else if(form_email != '' && form_name != '') {
      
      // enregistrement formulaire en base
      $.getJSON(jQuery('#tg_concours form').attr('action'), jQuery('#tg_concours form').serialize(), function(data) {
        
        if(data == 'OK') {
          jQuery('#tg_concours form').html(contest_thank_you);
          
          // enregistrement newsletter
          if(subscribe) {
            $.getJSON(url + '&callback=?', {}, function(data2) { });
          }
          
          return true;
        } else if(data == 'KO') {
          jQuery('#tg_concours form').find('input:text[name=email]').after('<label class="error" for="email" generated="true">'+contest_mail_already_exists+'</label>');
          return false;
        }
      });
      
    }
  });
});	
