
/* Copyright (c) 2011 der|Design */

/* $ = null; */

(function($) {

	var init_functions = [];

	var init_args = [];

	this.log = function(msg) {

		try {

			console.log(msg);

		} catch(e) {}

	}

	this.queue_ready_fn = function(callback, args) {

		args = ( args ) ? args : null;

		if (is_array(callback)) {

			for (var i in callback) {

				init_functions.push(callback[i]);

			}

		} else {

			init_functions.push(callback);

		}

		init_args.push(args);
	}

	this.initialize = function() {

		for (var i in init_functions) {

			var cb = init_functions[i];

			var args = init_args[i];

			(args) ? cb(args) : cb();
		}
	}

	this.is_array = function(input) {

		return typeof(input) == 'object' && (input instanceof Array);

	}

	this.isset = function(variable) {

		return typeof variable != 'undefined';

	}

	this.decode_option = function(code) {

		return eval('('+$.base64Decode(code)+')');

	}

	/* 
	 * cycle()
	 * @increment: Integer, use 1 (forward) or -1 (backward)
	 * @current: Current index, starts from zero
	 * @length: Total number of items */

	this.cycle = function(increment, current, total) {

		var next = parseInt(current) + parseInt(increment);
		
		if ( next >= 0 && next <= total-1 ) { return next; }
		
		else if ( next < 0 ) { return total - 1; }
		
		else if ( next >= total ) { return 0; }

	}

	this.global_coords = function(element) { // http://bit.ly/gTyFbq

		var top = 0, left = 0;

		do {

			top += element.offsetTop  || 0;

			left += element.offsetLeft || 0;

			element = element.offsetParent;

		} while(element);

		return {
			top: top,
			left: left
		};
	}

	this.object_copy = function(obj) {

		var newob = {};

		for (var i in obj) {newob[i] = obj[i];}

		return newob;

	}
	
	/* Initialization (onload) */

	$(document).ready(initialize);
	
})(jQuery);
