/*
// Site : http://www.gmc.com
// Date : 2010-10-19
*/

// Setup : jQuery No Conflict Mode
jQuery.noConflict();

jQuery(document).ready(function()
{
  /*
  // Section : Carrousel
  */

  // Setup : Carrousel : Vertical : History
  jQuery('.history_carroussel_buttons .date').click(function(e)
  {
    e.preventDefault();
    jQuery(this).siblings().removeClass('current');
    jQuery(this).addClass('current');
  });

  jQuery(".history_carroussel_slides").jCarouselLite({
      visible   : 1,
      start     : 0,
      btnGo     : ['.history_carroussel_buttons .buttons_1', '.history_carroussel_buttons .buttons_2', '.history_carroussel_buttons .buttons_3', '.history_carroussel_buttons .buttons_4', '.history_carroussel_buttons .buttons_5', '.history_carroussel_buttons .buttons_6', '.history_carroussel_buttons .buttons_7', '.history_carroussel_buttons .buttons_8', '.history_carroussel_buttons .buttons_9', '.history_carroussel_buttons .buttons_10', '.history_carroussel_buttons .buttons_11', '.history_carroussel_buttons .buttons_12'],
      easing    : 'jswing',
      speed     : 600,
      vertical  : true
  });

  /*
  // Section : Homepage : Blurb
  */

  //
  // Setup : Carrousel : Vertical : Blurb
  var blurb_container = jQuery('.blurbs_container');
  blurb_container.addClass('is_loading');

  var blurbs_images   = blurb_container.children('.blurbs');
  blurbs_images.css('opacity', '0');

  var blurbs_buttons  = blurb_container.children('.blurb_buttons');
  blurbs_buttons.css('opacity', '0');


  //
  // Blurb : Image : Loader
  jQuery(function ()
  {
    // Draw the Blurb Images
    var images          = blurb_container.find('img');
    var images_count    = 0;

    // Load Each Images
    images.each(function(index, element)
    {
      var image = new Image();
      // console.info('Loading : ' + element.src);

      jQuery(image).load(function()
      {
        // console.info('Loaded : ' + image);
        images_count++;

        // If all images loaded, remove Spinner with animate
        images_count === images.length ? setup_blurb() : null;

      }).error(function()
      {
        // Notify the user that the image could not be loaded, but with what...
      }).attr('src', element.src);
    });

  });


  //
  // Blurb : Image : Setup
  function setup_blurb()
  {
    // Remove Spinner with animate

    // Reveal : Blurbs images & buttons
    blurbs_images.animate({
      opacity: 1
    }, 1000);

    blurbs_buttons.animate({
      opacity: 1
    }, 1000);

    blurb_container.removeClass('is_loading');

    // Setup : Buttons
    jQuery('.blurb_buttons a').click(function(e)
    {
      e.preventDefault();
    });

    jQuery('.blurb_buttons a').hover(
      function(e)
      {
        jQuery(this).siblings().removeClass('current');
        jQuery(this).siblings().children('.button_on').animate({
          opacity: 0
        })

        jQuery(this).children('.button_on').animate({
          opacity: 1
        });

        jQuery('.blurbs').scrollTo(jQuery(this).attr('rel'), 600);
        jQuery(this).addClass('current');
      }
    );

    // Setup : Blurb : 1
    jQuery("#blurb_1").jCarouselLite({
        visible   : 1,
        start     : 0,
        auto      : 3500,
        easing    : 'jswing',
        speed     : 600
    });
  }


  /*
  // Section : Dossiers : Javascript Reformating
  */
  jQuery('div.dossiers-liste > ul > li > a').append('<span class="button view position_absolute top_0_px right_10_px"><span class="view"></span><span class="label">En savoir +</span></span>');
  jQuery('div.dossiers-liste > ul > li').html(function(index, html)
  {
   return '<div class="boxes box_710 plain gradient bottom white"><div class="header"></div><div class="container"><div class="content">' + html + '</div></div><div class="footer"></div></div>';
  });


  /*
  // Section : Dossiers : Articles : Javascript Reformating (Green Block)
  */
  jQuery('.article').html(function(index, html)
  {
    var article = html.split('<p>-----</p>');
    if(article.length < 2) article = html.split('<P>-----</P>');

	// NK - 20101022 : si aucun conseil
    if (article.length < 2)
    	return article[0];

    return article[0] + '<div class="boxes box_710 plain gradient top green margin_top_20_px"><div class="header"></div><div class="container"><div class="content dossiers_article_solution">' + article[1] + '</div></div><div class="footer no_margin"></div></div>';
  });


  /*
  // Section : Footer : Group's Websites
  */
  jQuery('.footer_other_websites_trigger').hover(
    function(e)
    {
      console.log('Hover');
      jQuery('.footer_other_websites').slideDown('slow');
    },
    function(e)
    {
      console.log('Out');
      jQuery('.footer_other_websites').slideUp('slow');
    }
  );


  /*
  // Section : Fontes Size : Switcher
  */
  var ind_value = 1;
  var max_value = 14;
  var min_value = 10;

  jQuery('.btn_font_plus').click(function(e)
  {
    jQuery('.zoomable').each(function(index, element)
    {
      jQuery(this).css(
      {
        fontSize: function(index, value)
        {
          return (parseFloat(value) + ind_value) < max_value ? parseFloat(value) + ind_value : max_value;
        }
      });
    })
  });

  jQuery('.btn_font_moins').click(function(e)
  {
    jQuery('.zoomable').each(function(index, element)
    {
      jQuery(this).css(
      {
        fontSize: function(index, value)
        {
          return (parseFloat(value) - ind_value) > min_value ? parseFloat(value) - ind_value : min_value;
        }
      });
    })
  });


	// ----------
	// NK - 20101024 : pour la gestion des liens par ancre
	function labelizeText (inStr)
	{
		
		var outStr = inStr.toLowerCase();
		var outStr = outStr.replace (/ /g, '-');
		var outStr = outStr.replace (/’/g, '-');
		var outStr = outStr.replace (/'/g, '-');
		var outStr = outStr.replace (/é/g, 'e');
		var outStr = outStr.replace (/è/g, 'e');
		var outStr = outStr.replace (/à/g, 'a');
		var outStr = outStr.replace (/ç/g, 'c');
		var outStr = outStr.replace (/û/g, 'u');
// more transcoding here !!!!!
//  all punctuations & all accents

		return outStr;
	}


	// ----------
	// NK - 20101026 : appelé pour tous les sauts vers une "ancre"
	function jumpToAnchor (targetText)
	{
		var headers = jQuery("h2", ".article");
		headers.each(function(index, element)
		{
			var labelized = labelizeText(jQuery(element).text());
			if (labelized.indexOf(targetText) >= 0)
			{
				jQuery.scrollTo(element, 400);
				return false;
			}
		});
	}


	function addBirthYearOptions (selectID)
	{
		var today = new Date();
		var optionStr = '';
		var tooOld = today.getFullYear() - 81;
		optionStr += '<option value="-1">Sélectionnez…</option>';
		optionStr += '<option value="0">Avant ' +tooOld+ '</option>';

		for (var i = tooOld; i < today.getFullYear() - 39; i++)
		{
			optionStr += '<option value="' +i+ '">' +i+ '</option>';
		}
		optionStr += '<option value="0">Après ' +(today.getFullYear() - 40)+ '</option>';

		jQuery("#"+selectID).html(optionStr);
	}

	// sur la homepage, générer le menu des années de naissance
	addBirthYearOptions("birth_year");


  jQuery("#simul_starter").click(function(e)
  {
    var selectedYear = jQuery("#birth_year :selected").val();

    switch(parseInt(selectedYear))
    {
      case 0 :
        popinManager(
        {
          mode: 'alert',
          title: '',
          subtitle: '',
          content: "Pour obtenir un devis, nous vous remercions de contacter GMC Europe, Département La Garantie Obsèques au <b>01.49.70.27.89</b>"
        });
        return false;
      break;

      case -1 :
        popinManager(
        {
          mode: 'alert',
          title: 'Information',
          subtitle: '',
          content: "Veuillez sélectionner une année de naissance."
        });
        return false;
      break;

      default:
        jQuery('#simul_caller').submit();
      break;
    }
  });


	// les liens de navigation gauche
	jQuery("a", ".dossiers-navigation").click(function(e)
 	{
		var linktxt = jQuery(this).attr('href').split('#');
		if (linktxt.length > 1)
		{
			var anchorText = labelizeText(linktxt[1]);
		//	e.preventDefault();
			jumpToAnchor(anchorText);
		}

		return true;
 	});


	// le lien vers le conseil
	jQuery("#jump2conseil").click(function(e)
 	{
		var anchorText = labelizeText('Le conseil de la Garantie Obsèques');
		//e.preventDefault();
		jumpToAnchor(anchorText);
	});


	// quand on arrive sur la page d'article, si il  y a une ancre, y aller
	var currentURL = new String(document.location);
	var urlBits = currentURL.split('#');
	if (urlBits.length > 1)
	{
		jumpToAnchor(urlBits[1]);
	}


	//
	//
	// Popin : Close
  jQuery('.popin-close').live('click', function(e){
    jQuery.fancybox.close();
  });


  // Popin : Open (Alert || Prompt || Whatever)
  if(jQuery('.popin-open').length)
  {
    jQuery('.popin-open').fancybox(
    {
      'scrolling' : 'no',
      'titleShow' : false,
      autoDimensions : false,
      width		: '465',
      height    	: 'auto',
      modal       : true
    });
  }
  
  //Entete : On baisse le menu si le pave de connexion est grand
	var userHeight = parseFloat(jQuery('.informations').height());
	if(Number(userHeight))
	{
		jQuery('body').addClass('log');
		if(userHeight > 35 && userHeight < 80  ){
			jQuery('body').addClass('longName');		
		}
		else if(userHeight >= 80)
		{
			jQuery('body').addClass('longError');
		}
	}
});


//
// Basic Popin Configuration
function popinManager(popin_properties)
{

  // Configure (alert || prompt)
  if(popin_properties.mode == 'alert'){
    jQuery('#popin-mode-prompt').attr('style', 'display: none;');
    jQuery('#popin-mode-alert').attr('style', 'display: block;');
  }else{
    jQuery('#popin-mode-prompt').attr('style', 'display: block;');
    jQuery('#popin-mode-alert').attr('style', 'display: none;');
  }

  // Title
  popin_properties.title ? jQuery('#popin-title').html(popin_properties.title) : jQuery('#popin-title').html('');

  // SubTitle
  popin_properties.subtitle ? jQuery('#popin-subtitle').html(popin_properties.subtitle) : jQuery('#popin-subtitle').html('');

  // Content
  popin_properties.content ? jQuery('#popin-content').html(popin_properties.content) : jQuery('#popin-content').html('');

  // Trigger the popin
  jQuery('.popin-open').click();
};

