//
// ** jQuery Layout Functions
//
// ** Provided by www.pumkin.co.uk
//
// ** BETA 2nd March 2011 (Not tested in IE)
//
// ** Re-use as desired but please retain this credit
//
// ** Requires jQuery 1.4+
//
//
//
//
// 
// Resizes an element to fit or fill its container
// option params - sendWid, sendHei - override the auto wid/hei detection 
// (useful for images which haven't loaded yet, if you already know the size)
//
function fitArea(target, fillorfit, centreItem, sendWid, sendHei) {
    //
    var originalWid = sendWid > 0 ? sendWid : target.width();
    var originalHei = sendHei > 0 ? sendHei : target.height();
    var wid = target.parent().width();
    var hei = target.parent().height();
    target.width(wid);
    var scaleRatio = wid/originalWid;
    target.height(Math.round(originalHei*scaleRatio));
    if (fillorfit == 'fit') {
      if (target.height() > hei) {
        scaleRatio = hei/originalHei;
        target.height(hei);
        target.width(Math.round(originalWid*scaleRatio));
      }
    } else {
        if (target.height() < hei) {
        scaleRatio = hei/originalHei;
        target.height(hei);
        target.width(Math.round(originalWid*scaleRatio));
      }
    }
    if (centreItem == true) {
        position(target);   
    }
}


//
// Positions content within its container
// can send margins as an array of 4 numbers (top, right, bottom, left)
// and align as an array of 2 items, eg ["top","left"]
//
function position(target, margins, align) {
    //
    if (!margins || margins.length<4) {
        margins = new Array(0,0,0,0);
    }
    var container = target.parent();
    var areaWidth = container.width();
    var areaHeight = container.height();
    var wid = target.width();
    var hei = target.height();
    //
    var xOffset = margins[3]-margins[1];
    var yOffset = margins[0]-margins[2];
    //  
    var targetX = null;
    var targetY = null;
    //
    // if margins and align parameters sent, align to one side, centre on the other axis
    if (align && align.length<2) {
        align.push(""); 
    }
    if (align) {
        switch (align[0]) {
            case "right" :
                var targetX = areaWidth - (margins[1] + wid);
                break;
            case "bottom" :
                var targetY = areaHeight - (margins[2] + hei);
                break;
            case "left" :
                var targetX = margins[3];
                break;
            case "top" :
                var targetY = margins[0];
                break;
            // otherwise align centre   
            default : 
                var targetX = ((areaWidth + xOffset) / 2) - (wid / 2);
                var targetY = ((areaHeight + yOffset) / 2) - (hei / 2);
                break;
        }
        switch (align[1]) {
            case "right" :
                var targetX = areaWidth - (margins[1] + wid);
                break;
            case "bottom" :
                var targetY = areaHeight - (margins[2] + hei);
                break;
            case "left" :
                var targetX = margins[3];
                break;
            case "top" :
                var targetY = margins[0];
                break;  
            // otherwise align centre   
            default : 
                if (!targetX && targetX !== 0) { var targetX = ((areaWidth + xOffset) / 2) - (wid / 2); }
                if (!targetY && targetY !== 0) { var targetY = ((areaHeight + yOffset) / 2) - (hei / 2); }
                break;
        }
    } 
    //otherwise centre within margins
    else {
        var targetX = ((areaWidth + xOffset) / 2) - (wid / 2);
        var targetY = ((areaHeight + yOffset) / 2) - (hei / 2);     
    }
    //
    target.css("margin-left", Math.round(targetX));
    target.css("margin-top", Math.round(targetY));
}

