/*
* Author:      Marco Kuiper (http://www.marcofolio.net/)
*/
google.load("jqueryui", "1.7.0");
google.setOnLoadCallback(function()
{
	// Set the Z-Index (used to display images on top while dragging)
	var zindexnr = 17;	

	// When everything has loaded, place all works on a random position	
	$(".dragdiv").each(function (i) {
		var tempVal = Math.round(Math.random());
		if(tempVal == 1) {
			var rotDegrees = randomXToY(0, 0); // rotate left
		} else {
			var rotDegrees = randomXToY(0, 0); // rotate right
		}
		
		// Internet Explorer doesn't have the "window.innerWidth" and "window.innerHeight" properties
		if(window.innerWidth == undefined) { 
			var wiw = 1020;
			var wih = 3000;
		} else {
			var wiw = window.innerWidth;
			var wih = 3000;
			// var wih = window.innerHeight;	
		}
		
		var cssObj = { 'left' : Math.random()*(wiw-600),
			'top' : Math.random()*(wih-600),
			'margin-left' : '160px'}; // added in case CSS3 is standard
		$(this).css(cssObj);
	});
	

	
	// boolean to check if the user is dragging
	var dragging = false;
	
	// Show the work on top when clicked on
	$(".dragdiv").mouseup(function(e){
		if(!dragging) {
			// Bring work to the foreground
			zindexnr++;
			var cssObj = { 'z-index' : zindexnr }; 
			$(this).css(cssObj);
		}
	});
		$(".contact").mouseup(function(e){
			// Bring work to the foreground
			zindexnr++;
			var cssObj = { 'z-index' : zindexnr }; 
			$(this).css(cssObj);
	});
	// Make the work draggable & display a shadow when dragging
	$(".dragdiv").draggable({
		cursor: 'move',
		start: function(event, ui) {
			dragging = true;
			zindexnr++;
			var cssObj = { 'margin-left' : '-10px',
				'margin-top' : '-10px',
				'z-index' : zindexnr };
			$(this).css(cssObj);
		},
		stop: function(event, ui) {
			var tempVal = Math.round(Math.random());
			if(tempVal == 1) {
				var rotDegrees = randomXToY(0, 0); // rotate left
			} else {
				var rotDegrees = randomXToY(0, 0); // rotate right
			}
			var cssObj = { 'margin-left' : '0px',
				'margin-top' : '0px' };
			$(this).css(cssObj);
			dragging = false;
		}
	});
	
	// Function to get random number upto m
	// http://roshanbh.com.np/2008/09/get-random-number-range-two-numbers-javascript.html
	function randomXToY(minVal,maxVal,floatVal) {
		var randVal = minVal+(Math.random()*(maxVal-minVal));
		return typeof floatVal=='undefined'?Math.round(randVal):randVal.toFixed(floatVal);
	}
	
});