/*
	misc.js: Miscellaneous functions for JCE.
	Copyright (c) 2006-2009 Kalle Raisanen.

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU Affero General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU Affero General Public License for more details.

	You should have received a copy of the GNU Affero General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/


function get_query_var(variable)
{
	var query = window.location.search.substring(1);
	var vars  = query.split('&');

	for(var i = 0; i < vars.length; i++) {
		var pair = vars[i].split('=');
		if(pair[0] == variable) {
			return pair[1];
		}
	}

	return null;
}

var COOKIE = {
	set: function (name, value, days) {
		var expires = '';
		if(days){
			var date = new Date();
			date.setTime(date.getTime() + (days*24*60*60*1000));
			expires = '; expires=' + date.toGMTString();
		}
		document.cookie = name + "=" + value + expires + "; path=/";
	}

	,get: function(name) {
		var name_eq = name + "=";
		var ca      = document.cookie.split(';');

		for(var i = 0; i < ca.length; i++) {
			var c = ca[i];
			while (c.charAt(0) == ' ')
				c = c.substring(1, c.length);
			if(c.indexOf(name_eq) == 0)
				return c.substring(name_eq.length, c.length);
		}
		return null;
	}

	,unset: function(name) {
		COOKIES.set(name, "", -1);
	}
};

function map(arr, callback)
{
	var len = arr.length;
	var out = new Array(len);

	for(var i = 0; i < len; i++)
		out[i] = callback(arr[i]);

	return out;
}
