/**

 * @author tparisot

 */

(function($){
  /*

   * Vertical centering plugin

   */

  $.fn.verticalCentering = function(settings){

    var settings = typeof settings == 'object' ? settings : {};

    settings = $.extend({

      container:          window,

      handleResize:       true

    }, settings);



    var $container = $(settings.container);

    var window_height = $container.height();

    var $this = this;



    if (settings.handleResize)

    {

      settings.handleResize = false;

      $container.resize(function(){

        $this.verticalCentering(settings);

      });

    }



    return $this.each(function(){

      var $this = $(this);

      var vertical_position = (window_height/2) - ($this.height()/2);

      vertical_position = vertical_position >= 0 ? vertical_position : 0;



      $this.css({top: vertical_position})

    });

  };



  /*

   * On page load

   */

  $(function(){

  /**

   * Menu corporate

   */

  $('#corporate-opener').toggle(

  function(){

    this.blur();

    $(this).addClass('opened');

    $('#corporate-menu')

      .find('> li')

        .css('opacity', 0)

        .end()

      .slideDown('normal', function(){

        /*

         * We animate by callback

         * If opacity is set in the style for IE, rollover menus don't show

         */

        $(this).find('> li')

          .animate({opacity: 1}, function(){

            $(this).css({

              opacity: ''

            });

          })



      });



    var date = new Date();

    date.setTime(date.getTime()+(60*60*24*15*1000));

    document.cookie = ['corporate_menu', '=', 1, '; path=/;  expires=', date.toUTCString()].join('');



    return false;

  },

  function(){

    $(this).removeClass('opened');

    $('#corporate-menu')

      .find('> li')

        .css('opacity', 0)

        .end()

      .slideUp('normal');



    var date = new Date();

    date.setTime(date.getTime()+(60*60*24*15*1000));

    document.cookie = ['corporate_menu', '=', 0, '; path=/; expires=', date.toUTCString()].join('');



    return false;

  });



  $('#corporate-menu > li')

    .bind('mouseenter', function(e){

      $(this)

        .addClass('hover')

        .find('> a')

          .each(function(){

            $(this)

              .clone()

              .addClass('clone')

              .click(function(){ window.location.href = this.href }) //fix IE6 inconsistancy click behavior

              .prependTo($(this).parent())

          })

          .end()

        .find('> ol')

          .show();

    })

    .bind('mouseleave', function(e){

      $(this)

        .removeClass('hover')

          .find('a.clone')

            .remove()

            .end()

        .find('> ol')

          .hide();

    });



  if (document.cookie && document.cookie.match(/corporate_menu=1/))

  {

    $('#corporate-opener').click();

  }



  /*

   * Vertical centering

   */

  $('body.entrydoor #corporate-pre').verticalCentering();





  /**

   * Checkbox + radiobox

   */

  $('#content-wrapper input:radio').checkbox({

    cls:   'radio',

    empty: 'http://www.loremipsum.net/pixelgif/pics/pixel.gif'

  });

  $('#content-wrapper input:checkbox').checkbox({

    cls:   'checkbox',

    empty: 'http://www.loremipsum.net/pixelgif/pics/pixel.gif'

  });



  /**

   * Selectbox

   */

  $('select.dropdown').selectbox();



    /*

     * Fix pour la largeur des selectbox styl?es

     *

     * Le padding augmente la taille;

     * Elle est g?r?e en % donc difficile ? corriger autrement (sans fixer leur largeur en absolu)

     */

    $('input.selectbox').each(function(){

      $this = $(this);

      $this.css('width', $this.width()-10);

    });



    /*

     * Fix pour la position de l'?l?ment, ing?rable en CSS sinon

     */

    $('div.selectbox-wrapper').each(function(){

      if ($.browser.msie && parseInt($.browser.version) < 8)

      {

        $(this)

          .not('#contact-sujet_container')

          .css('width', ($(this).width() - 15)+'px');

      }



      $(this).css({

        'left': $(this).prev().get(0).offsetLeft+'px'

      });

    });



  /**

   * Tab switcher

   */

  $('ul.js-switcher').each(function(){

    $(this).data('el_active', '');



    /*

     * Assignation des ?v?nements

     */

    $('li > a[href^=#]', this).each(function(){

      this.anchor = '#'+this.href.replace(/^(.+)#/g, '');

      $(this.anchor).hide();



      $(this).click(function(){

        var $container = $(this).parents('ul.js-switcher');

        if ($container.data('el_active'))

        {

          $($container.data('el_active')).hide();

        }



        // Load and play media

	//var player = document.getElementById('player');

	//player.sendEvent('LOAD', Utf8.encode($(this).attr('xhref')));



        $container

          .data('el_active', this.anchor)

          .find('a.selected').removeClass('selected');

        $(this).addClass('selected')

        $(this.anchor).show();



	



        return false;

      });

 

    });



    /*

     * Recherche d'un actif

     */

    $actives = $('a.selected', this);



    if (!$actives.length)

    {

      $('a:first', this).click();

    }

    else{

      $actives.filter(':first').click();

    }

  });

  

  $('a.js-loadFirst').click(function() {



    // If media is an image, add a "zoom" button

    var is_image  = new Boolean($(this).attr('xhref').match(new RegExp('^.*.(gif|jp?g|png)$', 'i')));

    var has_slot = new Boolean($('#zoom-slot').length > 0);

    if (is_image != false)

    {

      if (has_slot)

      {

        var zoom_url = $(this).attr('xhref');

        $('#zoom-slot').html('<a class="thickbox" href="' + zoom_url + '">Zoom</a>');

	      tb_init('a.thickbox, area.thickbox, input.thickbox');

	      $('#zoom-slot').show();

      }

    }

    else

    {

      $('#zoom-slot').hide();

    }



    // Load and play media

    var player = document.getElementById('player');

    player.sendEvent('LOAD', Utf8.encode($(this).attr('xhref')));



    return false;

  });



  /**

   * Infobulles ongles

   */

  $('body').append('<span id="selectorTaintTooltip"><span></span></span>');

  $('#selectorTaintTooltip').hide();



  $('#selector-teintes li > a')

    .bind('mouseenter', function(e){

      if (typeof this.titleOrigin == 'undefined')

      {

        this.titleOrigin = this.title;

        this.title = '';

      }



      $('#selectorTaintTooltip')

          .find('span')

            .html(this.titleOrigin)

          .end()

        .show()

        .appendTo(this);

    })

    .bind('mouseleave', function(e){

      $('#selectorTaintTooltip').hide();

    });



  /**

   * Grained selector

   */

  $('#grained-selector')

    .bind('mouseenter', function(e){

      $('li', this).slideDown('fast');

    })

    .bind('mouseleave', function(e){

      $('li', this).not('.selected').slideUp('fast');

    })

    .find('> li.selected').prependTo('#grained-selector');





  /**

   * Fix IE

   */

  if ($.browser.msie)

  {

    /**

     * Fix PNG

     */

    /*$.ifixpng('http://www.loremipsum.net/pixelgif/pics/pixel.gif');



    var ieSelectors = [

      'img.trans',                //transparence g�n�rique

      'div.trans',                //transparence g�n�rique

      'h2.category',              //transparence g�n�rique

      'p.medallion',              //transparence g�n�rique

      '#grained-selector',        //g�n�rique, plusieurs pages

      '#contact',

      '#corporate p.open-close',

      '#heplesfilles-topics-header',

      '#heplesfilles-write #yourmessage',

      '#makeup-listing ul > li',

      '#press-topics table.trans',

      '#press-topic table.trans',

      '#product-medias',

      '#products-list li.trans',

      '#products-news-list > li',

      '#subscription',

      '#trend-summary'

    ];

    $(ieSelectors.join(',')).ifixpng()

    ieSelectors = null;

    $('div.shadow')

      .ifixpng({sizingMethod: 'scale'})

      .css('position', 'static');*/



    /**

     * Applique des classes pour combler les besoins en CSS3

     */

    $('#products-list li.row a')

      .mouseover(function(){ $(this).addClass('hovered')})

      .mouseout(function(){ $(this).removeClass('hovered')});

    $('#faq .categorized-list h2.category + div.content')

      .addClass('category-before-content');



    if (parseInt($.browser.version) < 7)

    {

      $('div.center').wrap('<div style="text-align:center"></div>');

      $('div.shadow-post').each(function(){

        $('> div:first', this).insertBefore(this);

        $(this).css({

          'height': $(this).css('padding-bottom'),

          'padding-bottom': 0

        });

      });

    }

  }



  $('a.js-media').click(function() {



    // If media is an image, add a "zoom" button

    var is_image  = new Boolean($(this).attr('href').match(new RegExp('^.*.(gif|jp?g|png)$', 'i')));

    var has_slot = new Boolean($('#zoom-slot').length > 0);

    if (is_image != false)

    {

      if (has_slot)

      {

        var zoom_url = $(this).attr('x-bj-zoom') ? $(this).attr('x-bj-zoom') : $(this).attr('href');

        $('#zoom-slot').html('<a class="thickbox" href="' + zoom_url + '">Zoom</a>');

	      tb_init('a.thickbox, area.thickbox, input.thickbox');

	      $('#zoom-slot').show();

      }

    }

    else

    {

      $('#zoom-slot').hide();

    }



    // Load and play media

    var player = document.getElementById('player');

    player.sendEvent('LOAD', Utf8.encode($(this).attr('href')));



    return false;

  });

  



  $('a.js-tendance').click(function() {

    var id = '#trend-media-detail-' + $(this).attr('x-bj-trend_media_id');

    if ($(id).length > 0)

    {

      $('.trend-media-detail').hide();

      $(id).show();

    }



  });

  

  $('a.js-moodhtml').click(function() {

  	var attribut = $(this).attr('id');

  	var idA = '#' + attribut;

    var id = '#img-moodhtml-' + attribut;

    if ($(id).length > 0)

    {

      $('.spanmoodhtml').hide();

      $(id).show();

      

      $('.js-moodhtml').removeClass('selected');

      $(idA).addClass('selected');

    }



  });



  });

})(jQuery);



/* deprecated ? */

function display_image(src)

{

  document.getElementById("bj_image_full").style.display="";

  document.getElementById("bj_image_full").src = src;

  document.getElementById("bj_video_full").SetVariable("player:jsStop", "");

  document.getElementById("bj_video_full").style.display="none";

}

function display_video(num)

{

  document.getElementById("bj_image_full").style.display="none";

  document.getElementById("bj_video_full").style.display="";

  document.getElementById("bj_video_full").SetVariable("player:jsPlay", num);

}



/**

 * Provided by Musicovery.

 */

function OuvrirPop(url, nom, haut, Gauche, largeur, hauteur, options)

{

  var monpopup = window.open(url,

                     nom,

                     "top="+haut+"," +

                     "left="+Gauche+"," +

                     "width="+largeur+"," +

                     "height="+hauteur+"," +

                     options);

   monpopup.window.focus();

    

}



function setMoodboosterCookie(hum, def, sty)

{

  $.cookie('HPBJCK_hum', hum);

  $.cookie('HPBJCK_def', def);

  $.cookie('HPBJCK_sty', sty);

}



/**

 *

 *  UTF-8 data encode / decode

 *  http://www.webtoolkit.info/

 *

 **/

var Utf8 = {



    // public method for url encoding

    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;

    },



    // public method for url decoding

    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;

    }



}


