;(function($) {

$.fn.divsurround = function(options) {
  return this.each(function() {
    var opts = $.extend({ imagePrefix: 'img', sides: 25, corners: {}, deflate: 0, exclude: {}, substitute: {}, extend: {} }, $.fn.divsurround.defaults, options);
    // sides
    opts.sides = opts.sides.toString().replace(/[^0-9\. ]/g, "").split(" ");
    for (var i=0; i<4; i++)
      opts.sides[i] = !opts.sides[i] && opts.sides[i] !== 0 ? opts.sides[Math.max(i-2, 0)] : parseInt(opts.sides[i]);
    // deflate
    opts.deflate = opts.deflate.toString().replace(/[^0-9\. ]/g, "").split(" ");
    for (var i=0; i<4; i++) {
      opts.deflate[i] = !opts.deflate[i] && opts.deflate[i] !== 0 ? opts.deflate[Math.max(i-2, 0)] : parseInt(opts.deflate[i]);
    }

    // exclude
 //   opts.exclude = opts.exclude.replace(/\s/g,"").split(",");

    // substitute  ???

    var a = ['top', 'right', 'bottom', 'left'];
    var b = ['left', 'right'];
    var w = [];

    for (s in a) {
      var imgPrefix = (typeof(opts.substitute[a[s]]) == 'undefined' ? opts.imagePrefix : opts.substitute[a[s]]);
      var v = $('<div class="dsItem ds'+a[s]+'">'
 //         + ($.inArray(a[s], opts.exclude) == -1 ?
   //       '<img src="' + opts.imgPath + '/' + imgPrefix + '-' + a[s] + '.' + opts.imgType + '" />' :
  //        "")
         + "</div>");

      var extend = (typeof(opts.extend[a[s]]) == 'undefined' ? opts.sides[s] : opts.extend[a[s]]);
      if (s == 0 || s == 2) {
        // top or bottom
        v.css('position', 'absolute')
         .css('width', '100%')
         .css('height', opts.sides[s] + 'px')
         .css(a[s], -extend + 'px')
         .css('left', '0px')
         .css('background', ($.inArray(a[s], opts.exclude) == '-1' ? 'url(' + opts.imgPath + '/' + imgPrefix + '-' + a[s] + '.' + opts.imgType + ') repeat-x' : 'none'));
        for (c in b) {
          // add corners
          var imgPrefixc = (typeof(opts.substitute[a[s]+b[c]]) == 'undefined' ? opts.imagePrefix : opts.substitute[a[s]+b[c]]);
          var vc = $('<div class="dsItem ds'+a[s]+'-'+b[c]+'">'
              + ($.inArray(a[s]+b[c], opts.exclude) == -1 ?
              '<img src="' + opts.imgPath + '/' + imgPrefixc + '-' + a[s] + '-' + b[c] + '.' + opts.imgType + '" style="position:absolute;" />' :
              "")
              + "</div>");
          var extendx = (typeof(opts.extend[b[c]]) == 'undefined' ? opts.sides[c==0?3:1] : opts.extend[b[c]]);
          var cwidth = (typeof(opts.corners[a[s]+b[c]]) == 'undefined' ? opts.sides[s==0?3:1] : opts.corners[a[s]+b[c]][0]);
          var cheight = (typeof(opts.corners[a[s]+b[c]]) == 'undefined' ? opts.sides[s] : opts.corners[a[s]+b[c]][1]);
          vc.css('position', 'absolute')
            .css('width', cwidth + 'px')
            .css('height', cheight + 'px')
            .css(b[c], -extendx + 'px')
            .css(a[s], -extend + 'px');

          w.push(vc);
        }
      } else {
        // left or right
        v.css('position', 'absolute')
         .css('height', '100%')
         .css('width', opts.sides[s] + 'px')
         .css(a[s], -extend + 'px')
         .css('top', '0px')
         .css('background', ($.inArray(a[s], opts.exclude) == -1 ? 'url(' + opts.imgPath + '/' + imgPrefix + '-' + a[s] + '.' + opts.imgType + ') repeat-y' : 'none'));
      }

      w.push(v);
    }

    for(i in w) $(this).append(w[i]);

    // adjust container
    for (s in a) {
      if (opts.deflate[s] != 0) {
        $(this).css('margin-' + a[s], function(index, value) {
          val = parseInt(value);
          return (isInt(val) ? val : 0) + opts.deflate[s] + 'px';
        });
      }
    }


  });
};

// define base to add to
$.divsurround = {};

// static functions
$.divsurround.setImagePath = function(imgPath) {
  $.fn.divsurround.defaults.imgPath = imgPath;
}

// divsurround default options
$.fn.divsurround.defaults = {
  imgPath: '',
  imgType: 'png'
};



})(jQuery);

function isArray(obj) {
   return  (obj.constructor.toString().indexOf("Array") != -1)
}

function isInt(value){
  if((parseFloat(value) == parseInt(value)) && !isNaN(parseInt(value))){
      return true;
 } else {
      return false;
 }
}

