/*  Images resizing support */

/* ************************
   GENERAL RESIZE FUNCTIONS
   ************************ */

/* Resize an image so as to fit in the rectangle whose dimensions are passed in
   arguments.
   Sets the width and height CSS properties of the image. */

function resizeImage(img,parBlockWidth,parBlockHeight) {

  imgW = img.clientWidth;
  newH = imgH = img.clientHeight;

  if(imgW>parBlockWidth || imgH>parBlockHeight) {
    wM=hM=1;
    if(imgW>parBlockWidth) wM = imgW/parBlockWidth;
    if(imgH>parBlockHeight) hM = imgH/parBlockHeight;

    m = wM>hM?wM:hM;

    newW = Math.floor(imgW/m);
    newH = Math.floor(imgH/m);


    newW = Math.floor(imgW/m);
    newH = Math.floor(imgH/m);

    img.style.width = newW+"px";
  img.style.height = newH+"px";


  } // if

    return newH;
}

/* Crop and resize an image so as to fit in the rectangle whose dimensions are
   given in arguments.
   Sets the width, height, top and left CSS properties of the image. */

function resizeAndCropImage(img,parBlockWidth,parBlockHeight) {
  imgW = img.clientWidth;
  newH = imgH = img.clientHeight;

  if(imgW>parBlockWidth || imgH>parBlockHeight) {
    wM=hM=1;

    if(imgW>parBlockWidth) wM = imgW/parBlockWidth;
    if(imgH>parBlockHeight) hM = imgH/parBlockHeight;

    m = wM<hM?wM:hM;
    
    newW = Math.floor(imgW/m);
    newH = Math.floor(imgH/m);

    newW = (parBlockWidth-newW)==1 ? parBlockWidth : newW;
    newH = (parBlockWidth-newH)==1 ? parBlockHeight : newH;
      
    img.style.width = newW+"px";
    img.style.height = newH+"px";
      
    hh = parBlockHeight-newH;
    if(hh!=0) img.style.top = Math.floor(hh/2)+"px";

    ww = parBlockWidth-newW;
    if(ww<0) img.style.left = Math.floor(ww/2)+"px";
  }
}

/* ***************************
   RESIZE FUNCTIONS FOR ARTIST
   *************************** */

/* 
  Single product image preloading by changing the path from thumb paths to big image paths
  The function starts when we click on a particular thumb and the message "Chargement en cours..." appearing.
*/

function preloadSingleProductPicture(thumbBox, image_src) {

  var thumbs = thumbBox.getElementsByTagName("img");
  var thumbSrc = thumbs[0].src;

  var imgs = document.getElementById('product_details').getElementsByTagName("img");

  for (var i = 0; i < imgs.length; i++) { 

  if(imgs[i].src!= thumbSrc) continue;

  //var src = imgs[i].src.replace(/thumbnail_/,'product_');

  var imageParentNode = imgs[i].parentNode;

// Adding new image tag - foro big product image

  imageParentNode.removeChild(imgs[i]);    // removing temporary product image tag

  var newImage = document.createElement("img");

  newImage.onload = showImage;        // add onload event before (!) src definition

  imageParentNode.appendChild(newImage);    // adding real product image tag

  newImage.style.display = "none";      // IE needs this way to 
  newImage.style.marginTop = "0";
  newImage.className = "pr";
  newImage.src = image_src;

// Adding "load in progress" message node

  var loading = document.createElement("div");

  imageParentNode.parentNode.appendChild(loading);

  loading.className = "loadingImageMessage";
  loading.innerHTML = '<p>Chargement en cours...</p>';
  
    break;    // go out as we prepeared the selected image

  } // for 

// Let's remove no longer nesessary image preloading from thumb's onclick event

    thumbBox.onclick = function() {
      showCloseBtn(); 
      show_product(this);
      resizeSingleProductPicture();
    }

}


// On load big product image action
//
function showImage() {

// Remove "load in progress" message node

  var loadingMessage = this.parentNode.parentNode.getElementsByTagName('div');
  loadingMessage[0].style.display = "none";
  this.parentNode.parentNode.removeChild(loadingMessage[0]);

//  Show the product image

  this.style.display='block';
  
  resizeSingleProductPicture();    // resizing the active product image

}


// Resizing the only selected (active) product image when clicking on it's thumbnail
//
function resizeSingleProductPicture() {

  parBlockWidth = 428;
  parBlockHeight = 425;  // we use less height value to add some gaps on the top/bottom

  i = window.document.getElementById('active_product').getElementsByTagName('img');

  newH = resizeImage(i[0],parBlockWidth,parBlockHeight);

  if(newH==0) marginTop = 0;                  // fixing the case when rarely the image height can't be read
    else marginTop = Math.floor((parBlockHeight-newH)/2);

  if(marginTop>0) 
    i[0].style.marginTop = marginTop + "px";
  else
    i[0].style.marginTop = "0";

}


/* Resize and crop the images with 'img' class to fit in a square image of
   dim*dim. */
function resizeThumbs(blockID, dim) {
  var box = window.document.getElementById(blockID);
  if(box==null) return;    // return if no any product on the page

  var imgs = box.getElementsByTagName("img"); 
    parBlockWidth = dim;
    parBlockHeight = dim;
  for (var i = 0; i < imgs.length; i++) { 
    resizeAndCropImage(imgs[i],parBlockWidth,parBlockHeight);
  }
}

/* Welcome image resizing */
function resizeWelcomeImage() {
  if(!checkIEversion()) return; // if not IE6/5

  var block = window.document.getElementById('welcomeImage');
  var img = block.getElementsByTagName('img');

  if(img.length==0) return;
  if(img[0].clientWidth > block.clientWidth) img[0].style.width = (block.clientWidth - 24)+"px";
}

/*  Hover border around prod icons  - for IE<=6 only
*/
function startList() {
if(!checkIEversion()) return;
var box = document.getElementById("prod_list");
if(box==null) return;
  var navRoot = box.getElementsByTagName("li");
  for (i=0; i<navRoot.length; i++) {
    navRoot[i].onmouseover=function() { this.className+=" over"; }
  navRoot[i].onmouseout=function() { this.className=this.className.replace(" over", ""); }
  } // for
}

/* ****************
   COMMON FUNCTIONS
   **************** */

/* Return true if IE lte v.6 */
function checkIEversion() {
  var agt=navigator.userAgent.toLowerCase();

  var ind = agt.indexOf('msie');
  if(ind==-1) return false;

  var ieVer = parseInt(agt.substr(ind+4,2));
  if(ieVer>6) return false;
  
  return true;
}

