// global variable holding the machine window
var program_window;
var program_windows = new Array();

function openShockwave(name, filename, width, height, stretch) {
	
	var url = 'shockwave.php?name=' + name + '&filename=' + filename + '&stretch=' + stretch + '&width=' + width + '&height=' + height + '';
	
	var x = randomX(width);
	var y = randomY(height);
	var stretch_yes_or_no;
	
	if (stretch) {
		stretch_yes_or_no = 'yes';
		height += 18;
	} else {
		stretch_yes_or_no = 'no';
	}
	
	features = 'width=' + width + ', height=' + height + ', location=no, menubar=no, scrollbars=no, resizable=' + stretch_yes_or_no + ', status=' + stretch_yes_or_no + ', toolbar=no, top=' + x + ', left=' + y + ', screenX=' + x + ', screenY=' + y + '';
	
	
	// if the window is already open, close it (easier to control window size)
	if (program_window != null) {
		// make sure the user hasn't already closed it himself (in which case the following lines wouldn't run!)
		if (program_window.closed == false) program_window.close();
	}
	
	// open up a new window with the features we want
	program_window = window.open(url, 'machine', features);
	
}


function openProcessing(name, filename, width, height) {
	
	var url = 'processing.php?name=' + name + '&filename=' + filename + '&width=' + width + '&height=' + height + '';
	
	var x = randomX(width);
	var y = randomY(height);
	
	features = 'width=' + width + ', height=' + height + ', location=no, menubar=no, scrollbars=no, resizable=no, status=no, toolbar=no, top=' + x + ', left=' + y + ', screenX=' + x + ', screenY=' + y + '';
	
	// if the window is already open, close it (easier to control window size)
	if (program_window != null) {
		// make sure the user hasn't already closed it himself (in which case the following lines wouldn't run!)
		if (program_window.closed == false) program_window.close();
	}
	
	// open up a new window with the features we want
	program_window = window.open(url, 'machine', features);

}


function openLink( url, width, height, scrollbars ) {
	
	var x = randomX(width);
	var y = randomY(height);
	
	if (scrollbars) {
		scrollbars = 'yes';
	} else {
		scrollbars = 'no';
	}
	
	
	var features = 'width=' + width + ', height=' + height + ', location=no, menubar=no, scrollbars=' + scrollbars + ', resizable=yes, toolbar=no, top=' + x + ', left=' + y + ', screenX=' + x + ', screenY=' + y + '';
	
	// if the window is already open, close it (easier to control window size)
	if (program_window != null) {
		// make sure the user hasn't already closed it himself (in which case the following lines wouldn't run!)
		//if (program_window.closed == false) program_window.close();
	}
	
	// open up a new window with the features we want
	//program_window = window.open(url, 'machine', features);
	program_window = window.open(url, url, features);

}


function openPopUp( url, width, height, scrollbars ) {
	
	var x = randomX(width);
	var y = randomY(height);
	
	if (scrollbars) {
		scrollbars = 'yes';
	} else {
		scrollbars = 'no';
	}
	
	
	var features = 'width=' + width + ', height=' + height + ', location=no, menubar=no, scrollbars=' + scrollbars + ', resizable=yes, toolbar=no, top=' + x + ', left=' + y + ', screenX=' + x + ', screenY=' + y + '';
	
	// go through previous popup windows and bring them to the front
	if (program_windows.length > 0) {
	    var i;
	    for(i=0; i<program_windows.length; i++) {
	        if (program_windows[i].closed == false) program_windows[i].focus();
	    }
	}
	
	// open up a new window with the features we want
	program_windows.push(window.open(url, url, features));

}

// Array Remove - By John Resig (MIT Licensed)
Array.remove = function(array, from, to) {
  var rest = array.slice((to || from) + 1 || array.length);
  array.length = from < 0 ? array.length + from : from;
  return array.push.apply(array, rest);
};

function randomX(width) {
	var offset = 20, screenW = screen.availWidth;
	return ( Math.round( Math.random() * (screenW - (width+offset) ) ) );
}


function randomY(height) {
	var offset = 20, screenH = screen.availHeight;
	return( Math.round( Math.random() * (screenH - (height+offset) ) ) );
}