/*
 NB :: this build of FMCorners works on the assumption left and right borders are the same width.
 */

function initFMCorners(className, imgSrc, w, h, $extra) {
  var all = document.getElementsByTagName("*");
  for (ii in all) {
    if (all[ii].className == undefined) continue;
    if (all[ii].className.lastIndexOf(className) == -1) continue;
    applyFMCorners(all[ii], imgSrc, w, h, $extra);
  }
}

function intiFM9Slice(className, imgSrcCorner, w, h, imgSrcVert, bw, imgSrcHoriz, bh, $extra) {
  var all = document.getElementsByTagName("*");
  for (ii in all) {
    if (all[ii].className == undefined) continue;
    if (all[ii].className.lastIndexOf(className) == -1) continue;
    applyFMCorners(all[ii], imgSrcCorner, w, h, $extra);
    applyFMBorders(all[ii], imgSrcVert, bw, imgSrcHoriz, bh, $extra);
  }
}

function applyFMCorners(elm, imgSrc, w, h, $extra) {
  $extra = ($extra == undefined) ? '' : $extra;

  // marginOffset can be use to padd the absolute position of the corners.
  var marginOffset = ($extra['marginOffset'] != undefined) ? 1*$extra['marginOffset'] : 0;
  var extraClass = ($extra['extraClass'] != undefined) ? $extra['extraClass'] : 'FMCORNER-';

  if ($extra['dontPosition'] != undefined && !$extra['dontPosition']) {
    elm.style.position = (elm.style.position == '') ? 'relative' : elm.style.position;
  }
  
  var $ = {
    name: ['tl', 'tr', 'bl', 'br'],
    hPos: ['left', 'right', 'left', 'right'],
    vPos: ['top', 'top', 'bottom', 'bottom']
  };

  var div,img;
  for (var ii=0; ii<4; ii++) {
  
    div = document.createElement("div");
    div.style.overflow = 'hidden';
    div.style['position'] = 'absolute';
    div.className = extraClass+$.name[ii];
    
    if ($.hPos[ii] == 'right' && false) {
      div.style['left'] = (0-marginOffset)+(elm.offsetWidth-w/2)-(elm.clientLeft*2)+'px';
    } else {
      div.style[$.hPos[ii]] = (0+marginOffset)+'px';
    }
    div.style[$.vPos[ii]] = (0+marginOffset)+'px';
    div.style.width = w/2+'px';
    div.style.height = h/2+'px';
    
    img = document.createElement("img");
    img.setAttribute("src", imgSrc);
    img.style.position = "absolute";
    img.style[$.hPos[ii]] = w/-2+'px';
    img.style[$.vPos[ii]] = h/-2+'px';
    
    div.appendChild(img);
    elm.appendChild(div);
  
  }

}

function applyFMBorders(elm, imgSrcV, w,  imgSrcH, h, $extra) {
  $extra = ($extra == undefined) ? '' : $extra;
  
  // marginOffset can be use to padd the absolute position of the corners.
  var marginOffset = ($extra['marginOffset'] != undefined) ? 1*$extra['marginOffset'] : 0;
  
  if ($extra['dontPosition'] != undefined && !$extra['dontPosition']) {
    elm.style.position = (elm.style.position == '') ? 'relative' : elm.style.position;
  }
  
  var $ = {
    name: ['t', 'b', 'l', 'r'],
    hPos: ['left', 'left', 'left', 'right'],
    vPos: ['top', 'bottom', 'top', 'top']
  };
  
  var div,div2,bg;
  for (var ii=0; ii<4; ii++) {
    
    div = document.createElement("div");
    div.style.overflow = 'hidden';
    div.style['position'] = 'absolute';

    div.style[$.hPos[ii]] = (0+marginOffset)+'px';
    div.style[$.vPos[ii]] = (0+marginOffset)+'px';
    
  if ($.name[ii] == 't' || $.name[ii] == 'b') {
    div.style.width = "100%";
    div.style.paddingLeft = div.style.paddingRight = (0-marginOffset)+'px';
    div.style.height = h/2+'px';
    div.style.marginLeft = "-"+w/2+"px";
    bg = "url('"+imgSrcH+"') no-repeat "+w+"px "+(($.name[ii] == 't') ? '-'+h/2 : '0')+'px';
    div.style.background = bg;
  } else {
    div.style.width = w/2+'px';
    div.style.height = "100%";
    div.style.paddingTop = div.style.paddingBottom = (0-marginOffset)+'px';
    div.style.marginTop = "-"+h/2+"px";
    bg = "url('"+imgSrcV+"') no-repeat "+(($.name[ii] == 'l') ? '-'+w/2 : '0')+'px '+h+"px";
    div.style.background = bg;
  }

    elm.appendChild(div);
  }

}

