/******************************************************************************
-------------------------------------------------------------------------------
  PROJECT: 		OpenCode
  AUTHOR:		mud(tm)
  DATE:			07/25/2006
  COPYRIGHT:	(c) 2006, PLW.
  REQUIRES:		prototype.js, effects.js
-------------------------------------------------------------------------------
  utilities.js
-------------------------------------------------------------------------------
******************************************************************************/

///////////////////////////////////////////////////////////////////////////////
// GLOBALS

var images_over = new Array();
var images_off = new Array();

///////////////////////////////////////////////////////////////////////////////
// FUNCTIONS

function toggle_code(element) {
	new Effect.toggle(element, 'blind', {duration: 0.4});
}

/* ------------------------- Image Functions ------------------------------- */

function images_load(imgs) {
	if(imgs.length > 0) {
		for (var i = 0; i < imgs.length; i++) {
			if ($(imgs[i].name)) {
				images_off[imgs[i].name] = new Image();
				images_off[imgs[i].name].src = imgs[i].off;
				images_over[imgs[i].name] = new Image();
				images_over[imgs[i].name].src = imgs[i].over;
				Event.observe(imgs[i].name, 'mouseover', image_over, false);
				Event.observe(imgs[i].name, 'mouseout', image_off, false);
				Event.observe(imgs[i].name, 'click', image_off, false);
			}
		}
	}
}

function image_over(evt) {
	var img = Event.element(evt);
	img.src = images_over[img.id].src;
}

function image_off(evt) {
	var img = Event.element(evt);
	img.src = images_off[img.id].src;
}

///////////////////////////////////////////////////////////////////////////////
// FUNCTIONS: Applet

function view_applet(id) {
	new Ajax.Request('/users/applet/' + id, {
		asynchronous: true,
		evalScripts: true,
		onComplete: function(request) {
			code_success(request.responseText);
		}
	});
}

function code_success(app_tag) {
	if ($('code_text_area')) $('code_text_area').style.overflow = 'hidden';
	new Effect.BlindUp('page_content', {
		duration: 0.5,
		afterFinish: function() {
			$('applet').innerHTML = app_tag;
			show_applet();
		}
	});
}

/*
void compile_complete()

Shows applet once compiled.
*/
function show_applet() {
	set_applet_background();
	Element.show('applet-wrapper');
}

/*
void set_applet_background()

Sets the dimensions of applet background. Should be called on load and resize.
*/
function set_applet_background() {
	var content_dimensions = getWindowDimensions();
	Element.setStyle('applet-wrapper', {
		width: content_dimensions.width + "px",
		height: content_dimensions.height + "px"
	});
}

/*
void applet_clear()

Closes applet and strips out the innerHTML to prevent Firefox from using cache.
*/
function applet_clear() {
	$('applet').innerHTML = "";
	Element.hide('applet-wrapper');
	new Effect.BlindDown('page_content', {
		duration: 0.5,
		afterFinish: function() {
			if ($('code_text_area')) $('code_text_area').style.overflow = 'auto';
		}
	});
}

/*
void toggle_tag(id)

Toggles add tag field.
*/
function toggle_tag(id) {
	new Effect.toggle(id, 'blind', {duration: 0.4})
}

/*
void delete_opencode(id)

Deletes opencode with id
*/
function delete_opencode(id) {
	var confirm = window.confirm("Are you sure you want to delete this opencode?");
	if (confirm) {
		new Ajax.Updater('opencode-'+id, '/tool/open_code/destroy/'+id, {
			method: 'post',
			onComplete: function(request) {
				window.setTimeout("new Effect.BlindUp('opencode-"+id+"', {duration: 0.4})", 2000);
			},
			onFailure: function(request) {
				alert('Sorry, could not delete opencode.');
			}
		});
	}
}

///////////////////////////////////////////////////////////////////////////////
// FUNCTIONS: Events

function init() {
	// load all images
	var images_to_load = new Array(
		{name: 'run', off: '/tool/open_code/images/run.png', over: '/tool/open_code/images/run_over.png'},
		{name: 'close', off: '/images/close.png', over: '/images/close_over.png'}
	);
	images_load(images_to_load);
}

///////////////////////////////////////////////////////////////////////////////
// EVENTS

Event.observe(window, 'load', init, false);


//////////////////////////////////////////////////////////////////////////////
// EXTENDING Prototype.js

/*
String Array.to_s(separator = ","), String Array.to_string(separator = ",")

Extends Array object. Returns string with elements of array delimited by separator.
*/
Object.extend(Array.prototype, {
	to_s: function(separator) {
		separator = (separator) ? separator : ",";
		var out = "";
		var last = this.last();
		this.each(function(elem) {
			if (elem == last) {
				out += elem;
			} else {
				out += elem + separator
			}
		});
		return out;
	},
	to_string: function(separator) {
		return this.to_s(separator);
	}
});


//////////////////////////////////////////////////////////////////////////////
// UTILITIES

/*
{width, height} getWindowDimensions()

Returns size of window content area.
*/
function getWindowDimensions() {
	var dimensions = {width: 0, height: 0};
	if (typeof(window.innerWidth) == 'number') {
		//Non-IE
		dimensions.width = window.innerWidth;
		dimensions.height = window.innerHeight;
	}
	else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
		//IE 6+ in 'standards compliant mode'
		dimensions.width = document.documentElement.clientWidth;
		dimensions.height = document.documentElement.clientHeight;
	}
	else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		dimensions.width = document.body.clientWidth;
		dimensions.height = document.body.clientHeight;
	}
	return dimensions;
}
