Cufon.replace('h2');

function resizeFrame()
{
    var wh = $(window).height();
    var ww = $(window).width();
    var dh = $(document).height();
    var dw = $(document).width();
    //$("#raster").width(ww).height(wh);
    $("#content").width(ww-20).height(wh-40);
}

jQuery.event.add(window, "load", resizeFrame);
jQuery.event.add(window, "resize", resizeFrame);

// Following functions are customized from a script by Marco Kuiper (http://www.marcofolio.net)

function randomPositions()
{
    // When everything has loaded, places all content boxes on random positions
    var boxes = $('.box');
    boxes.each(function(i){
        var ww = $("#content").width();
        var wh = $("#content").height();
        var prevBox = i>0?$(boxes[i-1]):false;
        var yPos = randomXToY(40,(wh-$(this).innerHeight()));
        if (prevBox) {
            var prevOffset = $(prevBox).offset();
            var offset = $(this).offset();
            var xPos = Math.round(($(prevBox).innerWidth()+prevOffset.left)-(ww*0.05));
            if ((xPos+$(this).innerWidth()) >= ww) {
                var xPos = Math.round((prevOffset.left-$(this).innerWidth())+(ww*0.05));
            }
        } else {
            if (ww <= 820) {
                if (yPos >= 124) {
                    var xPos = randomXToY(10,(ww-$(this).innerWidth()));
                } else {
                    var xPos = randomXToY(160,(ww-$(this).innerWidth()));
                }            
            } else {
                if (yPos >= 324) {
                    var xPos = randomXToY(10,((ww*0.68)-$(this).innerWidth()));
                } else {
                    var xPos = randomXToY(360,((ww*0.68)-$(this).innerWidth()));
                }
            }
        }
        $(this).css({
            'top': yPos,
    	    'left': xPos
    	});
    });
    
    function randomXToY(minVal,maxVal,floatVal){
        var randVal = minVal+(Math.random()*(maxVal-minVal));
        return typeof floatVal=='undefined'?Math.round(randVal):randVal.toFixed(floatVal);
    }
}

jQuery.event.add(window, "load", randomPositions);

$(function(){
    // Set the z-index (used to display the dragged box on top)
    var zindexnr = 10;
    
    // Boolean to check if the user is dragging
    var dragging = false;
    
    // Show the box on top when hovered
	$(".hover").mouseover(function(e){
	    self.boxes = $('.hover');
	    if(!$(this).hasClass('top')) {
	        self.boxes.removeClass('top');
	        $(this).addClass('top');
	    }
		if(!dragging) {
			// Bring the box to foreground
			zindexnr++;
			$(this).css({
			    'z-index': zindexnr
			});
		}
	});
	$(".drag").draggable({
		containment: 'parent',
		cursor: 'move',
		//handle: 'h2',
		start: function(event, ui) {
			dragging = true;
			zindexnr++;
			$(this).css({
			    'z-index': zindexnr
			});
		},
		stop: function(event, ui) {
			dragging = false;
		}
	});
});
