// validate register form on submit
function registerFormValidation()
{
  if ($('#mail').attr('value') != $('#mail_confirm').attr('value'))
  {
    alert("Les 2 adresses mails saisies ne sont pas identiques.");
    return false;
  }
  if ($('#condition').attr('checked') != true)
  {
    alert("Veuillez accepter les conditions generales d'utilisation'.");
    return false;
  }
  return true;
}

// text zoom on article page
function zoom(p)
{
  var MAX = 22;
  var MIN = 9;
  $article_text = $('#left_column .block_article p');
  var currentSize = parseFloat($article_text.css("font-size").slice(0,-2));
  if (p == "in")
  {
    if (currentSize < MAX)
      $article_text.css("font-size", (currentSize+1)+"px");
  }
  else if (p == "out")
  {
    if (currentSize > MIN)
      $article_text.css("font-size", (currentSize-1)+"px");
  }
}

/* News slider on homepage with caption */
function newsslider(prev)
{
	//Get the current selected item (with selected class), if none was found, get the first item
	var current_image = $('#gallery li.selected').length ? $('#gallery li.selected') : $('#gallery li:first');
	var current_excerpt = $('#excerpt li.selected').length ? $('#excerpt li.selected') : $('#excerpt li:first');

	//if prev is set to 1 (previous item)
	if (prev) {
		//Get previous sibling
		var next_image = (current_image.prev().length) ? current_image.prev() : $('#gallery li:last');
		var next_excerpt = (current_excerpt.prev().length) ? current_excerpt.prev() : $('#excerpt li:last');

	//if prev is set to 0 (next item)
	} else {
		//Get next sibling
		var next_image = (current_image.next().length) ? current_image.next() : $('#gallery li:first');
		var next_excerpt = (current_excerpt.next().length) ? current_excerpt.next() : $('#excerpt li:first');
	}

	//clear the selected class
	$('#excerpt li, #gallery li').removeClass('selected');

	//reassign the selected class to current items
	next_image.addClass('selected');
	next_excerpt.addClass('selected');

	//Scroll the items
	$('#mask-gallery').scrollTo(next_image, 800);
	$('#mask-excerpt').scrollTo(next_excerpt, 800);
}


// slider function
function mySlider(eltId)
{
	var elt = '#'+eltId;

	// scroll article one by one
	var $panels = $(elt + ' .scrollContainer li');
	var $container = $(elt + ' .scrollContainer');

	// collect the scroll object, at the same time apply the hidden overflow
	// to remove the default scrollbars that will appear
	var $scroll = $(elt + ' .scroll').css('overflow', 'hidden');

	// if false, we'll float all the panels left and fix the width of the container
	var horizontal = true;

	// float the panels left if we're going horizontal
	if (horizontal)
	{
		$panels.css({
			'float' : 'left',
			'position' : 'relative' // IE fix to ensure overflow is hidden
		});
		// calculate a new width for the container (so it holds all panels)
		$container.css('width', $panels[0].offsetWidth * $panels.length);
	};

	// add an id to navigation element
	var nextId = eltId + 'next';
	$(elt + ' .next').attr('id', eltId + 'next');
	var prevId = eltId + 'prev';
	$(elt + ' .prev').attr('id', eltId + 'prev');

	// offset is used to move to *exactly* the right place, since I'm using
	// padding on my example, I need to subtract the amount of padding to the offset.
	var offset = parseInt((horizontal ? 
		$container.css('paddingTop') : 
		$container.css('paddingLeft')) || 0) * -1;

	var scrollOptions = {
		target: $scroll, // the element that has the overflow

		items: $panels, // can be a selector which will be relative to the target

		// selectors are NOT relative to document, i.e. make sure they're unique
		prev: '#'+prevId, 
		next: '#'+nextId,

		axis: 'x', // allow the scroll effect to run both directions

		offset: offset,

		duration: 500, // duration of the sliding effect

		easing: 'swing' // easing - can be used with the easing plugin
	};

	// apply serialScroll to the slider - the indexed next and previous
	// scroll along with hooking in to our navigation.
	$(elt).serialScroll(scrollOptions);

	// now apply localScroll to hook any other arbitrary links to trigger the effect
	$.localScroll(scrollOptions);

	// finally, if the URL has a hash, move the slider in to position,
	// setting the duration to 1 because I don't want it to scroll in the
	// very first page load.  We don't always need this, but it ensures
	// the positioning is absolutely spot on when the pages loads.
	scrollOptions.duration = 1;
	$.localScroll.hash(scrollOptions);

}


$(function() {

  // ie png fix
  $("img[src$='png'], form .search_ok, #captionslider_buttons a, span.bulle, div.article_pointer, span.commentaire_lien , div#mask-excerpt").ifixpng();

	// dropdown menu in header
	$('#more_rubrique_header').hover(
		function ()
     { /* over */
			//$('#more_rubrique_header ul').slideDown("fast");
			$('#more_rubrique_header ul').show();
		//	$('#more_rubrique_header ul').dropdown();
			//$('#more_rubrique_header ul li').visible();
			$('div#more_rubrique_header').addClass('rubrique_hover');
		}, 
		function () 
    { /* out */
			//$('#more_rubrique_header ul').slideUp("slow");
			$('#more_rubrique_header ul').hide();
			$('div#more_rubrique_header').removeClass('rubrique_hover');
		}
	);
	$('#more_rubrique_header .dropdown').click(function () { return false });
	
  
  
  // dropdown article 
  
  $('#more_article').hover(
		function ()
     { /* over */
      $('#more_article ul').show();
       $('#more_article a').removeClass('border_article');
     }, 
		function () 
    { /* out */
      $('#more_article ul').hide();
      $('#more_article a').addClass('border_article');
    }
	);
     	$('#more_article .dropdown').click(function () { return false });
	/*$('#more_article a').click(
		function ()
     { 
			if($(this).hasClass('down'))
      {
        $(this).removeClass('down');
        $('#more_article ul').slideUp('slow');
      }
      else{
        $(this).addClass('down');
        $('#more_article ul').slideDown('slow');
      }
		}
	);*/
	
	
	//mouseover
	
	$('.next').hover(function(){
      $(this).addClass('next_hover');
    }, function(){
      $(this).removeClass('next_hover');
    });
	
	
		$('.prev').hover(function(){
      $(this).addClass('prev_hover');
    }, function(){
      $(this).removeClass('prev_hover');
    });
	
	
	//mouseover
	
	$('.up').hover(function(){
      $(this).addClass('up_hover');
    }, function(){
      $(this).removeClass('up_hover');
    });

	    
	// add scroll event on footer link
	$('#footer_action_left').click(function(){
		$.scrollTo(0, { duration:1000 });
		return false;
	});

	// modal box link
	$(".fancybox_image").fancybox();
	$(".fancybox_page").fancybox({
		hideOnContentClick: false,
		frameWidth: 730,
		frameHeight: 650
	});
	$(".fancybox_page_custom").each(function() {
	  width = parseInt($(this).attr('class').match(/width[0-9]*/)[0].replace(/width/,''));
	  height = parseInt($(this).attr('class').match(/height[0-9]*/)[0].replace(/height/,''));
	  $(this).fancybox({
		  hideOnContentClick: false,
		  frameWidth: width,
		  frameHeight: height
	  })
	});

	$(".fancybox_iframe").addClass("iframe").attr('href', function() {
		return $(this).attr('href').replace(baseDir, baseDir+'iframe/');
	}).fancybox({
		hideOnContentClick: false,
		frameWidth: 765,
		frameHeight: 640
	});

	// caption slider on homepage
	$('#mask-gallery, #gallery li').width($('#captionslider').width());
	$('#gallery').width($('#captionslider').width() * $('#gallery li').length);
	$('#mask-gallery, #gallery li, #mask-excerpt, #excerpt li').height($('#captionslider').height());

	$('#gallery li:first, #excerpt li:first').addClass('selected');

	//Next Slide by calling the function
	$('#btn-next').click(function () {
		newsslider(0);
		return false;
	});

	//Previous slide by passing prev=1
	$('#btn-prev').click(function () {
		newsslider(1);
		return false;
	});

});

