// ########## GET ELEMENTS BY CLASS NAME ##########
document.getElementsByClassName = function(n) {
  var list = document.getElementsByTagName("*");
  var r = new Array();
  var i = 0;
  var j = 0;
  var theClass = " "+n+" ";
  for( i=0 ; i < list.length ; i++ ) {
    if( (" "+list[i].className+" ").indexOf(theClass) != -1 )
      r[j++] = list[i];
  }
  return r;
}

// ########## HIGHSLIDE addSlideshow ##########

hs.addSlideshow({
  slideshowGroup: 'group1',
  interval: 5000,
  repeat: false,
  useControls: true,
  fixedControls: true,
  overlayOptions: {
    opacity: .6,
    position: 'top center',
    hideOnMouseOut: true
  }
});

// ########## HIGHSLIDE onSetClickEvent ##########

hs.onSetClickEvent = function ( sender, e ) {
  switch(e.type){
    case undefined :
      // Case thumbnail
      var slideshow = e.element.className.match(/slideshow/);
      var inlineContent = e.element.className.match(/inlineContent/);
      if (slideshow) {
        // Case slideshow
        e.element.onclick = function () {
          return hs.expand(this, { outlineType: 'rounded-white', slideshowGroup: 'group1', align: 'center', dimmingOpacity: '0.75', transitions: ['expand', 'crossfade'] });
        }
      }
      else if (inlineContent) {
        // Case html content without iframe
        e.element.onclick = function () {
          return hs.htmlExpand(this, { outlineType: 'rounded-white', contentId: 'highslide_html', height: '600', width: '1200', align: 'center', dimmingOpacity: '0.75' });
        }
        hs.onActivate = function() {
           var moveTo = document.getElementById("container");
           if (moveTo) moveTo.appendChild(hs.container);
        }
      }
      else{
        // Case greybox like
        e.element.onclick = function () {
          return hs.expand(this, { outlineType: 'rounded-white' });
        }
        hs.registerOverlay({
          thumbnailId: null,
          overlayId: 'controlbar',
          position: 'top right',
          hideOnMouseOut: false
        });
      }
    break;
    case 'iframe' :
      // Case iframe
      hs.enableKeyListener = false;
      var hs_big = e.element.className.match(/hs_big/);
      var hs_newsletter = e.element.className.match(/hs_newsletter/);
      var hs_media = e.element.className.match(/hs_media/);
      var hs_cgv = e.element.className.match(/hs_cgv/);
      var hs_page = e.element.className.match(/hs_page/);
      var hs_shipping = e.element.className.match(/hs_shipping/);
      var hs_loyalty = e.element.className.match(/hs_loyalty/);
      if (hs_newsletter) {
        e.element.onclick = function () {
          return hs.htmlExpand(this, { objectType: 'iframe', outlineType: 'rounded-white', width: '450', align: 'center', dimmingOpacity: '0.75' });
        }
      }
      else if (hs_big) {
        e.element.onclick = function () {
          return hs.htmlExpand(this, { objectType: 'iframe', outlineType: 'rounded-white', height: '480', align: 'center', dimmingOpacity :'0.75' });
        }
      }
      else if (hs_media) {
        e.element.onclick = function () {
          return hs.htmlExpand(this, { objectType: 'iframe', outlineType: 'rounded-white', width: '800', height: '600' });
        }
      }
      else if (hs_cgv) {
        e.element.onclick = function () {
          return hs.htmlExpand(this, { objectType: 'iframe', outlineType: 'rounded-white', width: '800', height: '600', align: 'center', dimmingOpacity :'0.75' });
        }
      }
      else if (hs_page) {
        //Iframe for address_book add
        e.element.onclick = function () {
          return hs.htmlExpand(this, { objectType: 'iframe', outlineType: 'rounded-white', width: '600', height: '445', align:'center', dimmingOpacity:'0.75' });
        }
      }
      else if (hs_shipping) {
        //Iframe for shipping
        e.element.onclick = function () {
          return hs.htmlExpand(this, { objectType: 'iframe', outlineType: 'rounded-white', width: '620', height: '500', align:'center', dimmingOpacity:'0.75' });
        }
      }
      else if (hs_loyalty) {
        //Iframe for loyalty points
        e.element.onclick = function () {
          return hs.htmlExpand(this, { objectType: 'iframe', outlineType: 'rounded-white', width: '620', height: '700', align:'center', dimmingOpacity:'0.75' });
        }
      }
      else{
        e.element.onclick = function () {
          return hs.htmlExpand(this, { objectType: 'iframe', outlineType: 'rounded-white', width: '450', height: '75', anchor: 'top left', align:'center', dimmingOpacity:'0.75'  });
        }
      }
    break;
  }

  // return false to prevent the onclick being set once again
  return false;
}

// ##### FONCTION DE PAGINATION ###### //
function showPage(n) {
  window.location.href = window.location.pathname+window.location.search+'#'+n;
  var pages = document.getElementsByClassName('page_block');
  var nbPages = pages.length;
  for(i = 0; i < pages.length; i++) {
    pages[i].style.display = 'none';
  }
  
  var nav_page = document.getElementById('nav_page');
  //Shoot des liens "active"
  for(i = 0; i < nav_page.childNodes.length; i++) {
    if(nav_page.childNodes[i].nodeType != 3){
      nav_page.childNodes[i].setAttribute('class','');
      nav_page.childNodes[i].setAttribute('className','');
    }
  }
  
  document.getElementById('page1').style.display = 'none';
  document.getElementById('page'+n).style.display = 'block';
  document.getElementById('nav_page'+n).setAttribute('class', 'active');
  document.getElementById('nav_page'+n).setAttribute('className', 'active');
  
  if(n>1) {
    document.getElementById('prev_page').href = "#"+parseInt(n-1);
    document.getElementById('prev_page').onclick = function() {
      showPage(parseInt(n-1));
      return false;
    };
  }
  if(n <= nbPages) {
    document.getElementById('next_page').href = "#"+parseInt(n+1);
    document.getElementById('next_page').onclick = function() {
      showPage(parseInt(n+1));
      return false;
    };
  }
}
function showAllPages() {
  document.getElementById('page1').style.display = 'block';
  var pages = document.getElementsByClassName('page_block');
  for(i = 0; i < pages.length; i++) {
    pages[i].style.display = 'block';
  }
  
  nav = document.getElementById('nav_page').childNodes;
  for(i = 0; i < nav.length; i++) {
    if(nav[i].tagName == 'A'){
      nav[i].setAttribute('class', '');
      nav[i].setAttribute('className', '');
    }
  }
  document.getElementById('show_all').setAttribute('class', 'active');
  document.getElementById('show_all').setAttribute('className', 'active');
}
function loadPage(){
  if(window.location.hash){
    showPage(window.location.hash.substr(1));
  }
}
function pageFromUrl(){
  var url = window.location;
  var reg = new RegExp(/\#([\d]+)$/);
  var res = reg.exec(url);
  if(res && res[1]>0)
    showPage(parseInt(res[1]));
}

function updateSale(e) {
  var keynum;
  if(window.event) 
  // IE
  {
    keynum = e.keyCode;
  }
  else if(e.which)
  // Netscape/Firefox/Opera
  {
    keynum = e.which;
  }
  if(keynum == 13) {
    alert(document.forms[0]);
    document.forms[0].onsubmit = function() {
      return false;
    }
  }
}


function conf(message) {
  var agree=confirm(message);
  if (agree){
	  return true;
  }
  else {
	  return false;
  }
}
