$(document).ready(function() {
    
    // reset button action, reset style and current max, min z-index
    $("#btreset").click(function() {
		$(".box").removeAttr("style");
		$(".box").css("position", "relative");
		maxZindex = 1;
		minZindex = 1;
		return false;
	});
});

/* this function from:
 * http://www.admixweb.com/2010/08/24/javascript-tip-get-a-random-number-between-two-integers/
 */
function randomFromTo(from, to){
    return Math.floor(Math.random() * (to - from + 1) + from);
}

function scramble() {
    var children = $('#container').children();
    var child = $('#container div:first-child');
    for (i=0; i<children.length; i++) {
        $("#"+child.attr('id')).css("position", "absolute");
        moveRandom(child.attr('id'));
        child = child.next();
    }
}

function moveRandom(id) {
    /* get container position and size
     * -- access method : cPos.top and cPos.left */
    var cPos = $('#container').offset();
    var cHeight = $('#container').height();
    var cWidth = $('#container').width();

    // get box padding (assume all padding have same value)
    var pad = parseInt($('#container').css('padding-top').replace('px', ''));

    // get movable box size
    var bHeight = $('#'+id).height();
    var bWidth = $('#'+id).width();

    // set maximum position
    maxY = cPos.top + cHeight - bHeight - pad;
    maxX = cPos.left + cWidth - bWidth - pad;

    // set minimum position
    minY = cPos.top + pad;
    minX = cPos.left + pad;

    // set new position			
    newY = randomFromTo(minY, maxY);
    newX = randomFromTo(minX, maxX);

    $('#'+id).css({
        top: newY,
        left: newX
        }, function() {
    });

	$("a.group").fancybox();
}

