﻿/*
Script: Foresite.Framework.js
	Foresites core framework scripts, consists of root namespace generation, 
	validation and core library components

Remarks:
	This core lib is indepenant. Certain objects that require libraries such as 
	mootools can be found in additional JS files i.e. Foresite.Framework.MT, Foresite.Framework.JQ

Authors:
	Ryan Marsh <ryan@fsite.com>
	
Copyright:
	Copyright (c) 2006-2009 Foresite Business Solutions.
*/

var Foresite = {
	Version: '1.0',
	Framework: new Object(),

	RegNS: function(ns) {
		var nsParts = ns.split(".");
		var root = window;
		for (var i = 0; i < nsParts.length; i++) {
			if (typeof root[nsParts[i]] == "undefined")
				root[nsParts[i]] = new Object();
			root = root[nsParts[i]];
		}
	}
};

/*
Object: Number
	Additional methods added to Number JavaScript object
	
Example:
	> 2.isEven();
	> 5.isOdd();
	
Author:
	Ryan Marsh <ryan@fsite.com
*/

Number.prototype.isEven = function() {
	return this % 2 == 0;
};

Number.prototype.isOdd = function() {
	return this % 2 != 0;
};

/*
Object: Element
	Extension methods for mootools Element class
	
Usage:
	> $('element').hide();
	> $('element').toggle();
	> $('element').show();

Authors:
	Ryan Marsh <ryan@fsite.com>
*/

Element.implement({
	previousToggleState: null,

	visible: function() {
		return this.style.display != 'none';
	},

	toggle: function() {
		if (this.visible())
			this.hide();
		else
			this.show();

		return this;
	},

	hide: function() {
		this.previousToggleState = this.style.display;
		this.style.display = 'none';

		return this;
	},

	show: function() {
		if (null != this.previousToggleState)
			this.style.display = this.previousToggleState;
		else
			this.style.display = (this.nodeName == 'div' ? 'block' : '');

		return this;
	}
});

/*
Class: PopWindow
	Pop up window helper class
	
Usage:
	> new PopWindow( url, { title: 'hello', width: 500, height: 500 } );

Authors:
	Ryan Marsh <ryan@fsite.com>
*/

var PopWindow = new Class({
	Implements: [Options],
	
	url: null,

	options: {
		title: null,
		width: 500,
		height: 500,
		features: null
	},

	initialize: function(url, options) {
		this.url = url;
		this.setOptions(options);

		if (this.options.width == 0) this.options.width = screen.width;
		if (this.options.height == 0) this.options.height = screen.height;

		this.Left = (screen.width - this.options.width) / 2;
		this.Top = (screen.height - this.options.height) / 2;

		this.Features = (this.options.features + "").length > 0 ? this.options.features + ',' : '';
		this.Features += ', left=' + this.Left + ', top=' + this.Top;
		this.Features += ', status = 1';
	},

	Open: function() {
		this.Window = window.open(
			this.url,
			this.options.title,
			this.Features + ((this.Features != '') ? ',' : '') + 'width=' + this.options.width + ',height=' + this.options.height
			);

		this.Window.focus();
	},

	Close: function() {
		this.Window.close();
	}
});

/*
Class: QueryString
	QueryString formated text helper class - if no string is passed in as an argument it will use the Request.QueryString
	
Usage:
	> new QueryString().PageID;

Authors:
	Ryan Marsh <ryan@fsite.com>
*/

var QueryString = new Class({
	initialize: function(qs) {
		this.params = new Object();

		if (qs == null)
			qs = location.search.substring(1, location.search.length);

		if (qs.length == 0) return

		qs = qs.replace(/\+/g, ' ');
		var args = qs.split('&');

		for (var i = 0; i < args.length; i++) {
			var value;
			var pair = args[i].split('=');
			var name = unescape(pair[0]);

			if (pair.length == 2)
				value = unescape(pair[1]);
			else
				value = name

			this.params[name] = value
		}
	},

	Get: function(key, d) {
		var value = this.params[key];

		if (null == value)
			value = d;

		return value
	}
});


/*
Class: SubmitOnEnter
	Used to ensure the correct submit button is selected when pressing enter on the specified input field.
*/
Foresite.RegNS("Foresite.Framework.Forms");

Foresite.Framework.Forms.SubmitOnEnter = new Class({
	input: null,
	button: null,

	initialize: function(input, button) {
		this.input = $(input);
		this.button = $(button);

		if (!this.input)
			throw "Foresite.Framework.Forms.SubmitOnEnter: Could not find $(" + input + ")";

		if (!this.button)
			throw "Foresite.Framework.Forms.SubmitOnEnter: Could not find $(" + button + ")";
			
		this.input.addEvent('keypress', this.Eval.bind(this));
	},

	Eval: function(e) {
		var e = new Event(e);
		if (e.key == 'enter') {
			try {
				//e.preventDefault();
				this.button.click();
				return false;
			}
			catch (e) { }
		}
	}
});