/*

	License information: 	
	
		Copyright © by  A n d r é  A  h  n  e r
		For contact and further information visit: www.xitroMEDIA.com
		
		All rights reserved by copyright owner. Not any license given to anybody.	
	
		This script/software code is protected by German and international law.
		You are not allowed to copy, modify or use this script/software code wholly or partly in 
		any way without explicit and in written authorization for that by the owner of the Copyright !
	
		File name: "xitroBASE.js"	
		Module name: "xitroBASE"
		Description: JS-library with commonly used methods
		File version: 1.0 build 14
		File release date: 2011/10/20
	
	Technical remarks:
	
		uses:
		
			jQuery >=1.6.4 (required)

*/

var d=window.document;

// create and init global hash if not yet done
if (typeof(xitroBASE)=='undefined') {

	xitroBASE={};
}


// extend xitroBASE.h-hash with parameters
xitroBASE.h=jQuery.extend(

	{
		// DO NOT modify below

		// collects elements; keys=IDs, values= a hash with any data
		'els_h': {}
	},
	typeof(xitroBASE.h) != 'undefined'

		? xitroBASE.h
		: null
);


// extend xitroBASE-hash with methods
xitroBASE=jQuery.extend(

	xitroBASE,
	{


_ajax_req: function(_h) {
/**************************

Does an ajax-request using jQuery

Input: 

	hash={
	
	}

Returns:

	true on success, else false

*/

jQuery(d).ready(

	function() {

		jQuery.ajax({

			'type': typeof(_h.type) != 'undefined'

				? _h.type
				: 'GET',

			'url': typeof(_h.uri) != 'undefined'

				? _h.uri
				: '',

			'data': typeof(_h.params_h) != 'undefined'

				? _h.params_h
				: null,
			
			'success': typeof(_h.succ_f) != 'undefined'

				? _h.succ_f
				: null,

			'error': function(jqXHR,textStatus,errorThrown) {
			
				window.alert("Error [xitroBASE.js: 001]:\nEs ist ein Fehler aufgetreten. # " + textStatus);
			},
			'dataType': typeof(_h.dataType) != 'undefined'

				? _h.dataType
				: 'json'
		});
	}
);
},


_hash_from_array: function(_h) {
/**************************

	Copys values of an array to a hash as keys and sets the values of the keys to null
	
	Input: 
	
		hash={

			h: the array will be applied to, else starts with an empty one
			a: array those values will be used as keys
		}
	
	Returns:
	
		the renewed hash

*/

var o={};	

// use incoming hash or create new one
_h.h=typeof(_h.h)=='object'
	? _h.h
	: {};

// we need an array
if (typeof(_h.a)=='object') {

	for (o.i=0, o.j=_h.a.length-1; o.i<=o.j; o.i++) {
	
		_h.h[_h.a[o.i]]=null;
	}
}
return _h.h;
},


_array_from_hashkeys: function(_h) {
/**************************

	Takes a hash and puts its keys to an array
	
	Input: 
	
		hash={

			h: hash whose keys will be added to the array
			a: array to push the keys to, else starts with an empty one
		}
	
	Returns:
	
		array

*/

var o={};

// use incoming hash or create new one
_h.h=typeof(_h.h)=='object'
	? _h.h
	: {};

// use incoming array or create new one
_h.a=typeof(_h.a)=='object'
	? _h.a
	: [];

for (o.k in _h.h) {

	_h.a.push(o.k);
}
return _h.a;
},


_html_select_mod: function(_h) {
/**************************

	Modifies a select-menu/dropdown
	
	Input: 
	
		hash={

			'this_o': select-element object or ID-string
			opts_h: hash of menu-options
		}
	
	Returns:
	
		object if existing or null

*/

var o={

	i: 0
}
if (! (o.this_o=this._get_el(_h.this_o))) {

	window.alert("Error [xitroBASE.js: 004]:\nEs ist ein Fehler aufgetreten.");
	return false;
}

// clear all options but keep the first option if wanted 
o.this_o.options.length=0;

// create new options
for (o.k in _h.opts_h) {

	o.this_o.options[o.i]=new Option(

		// text
		_h.opts_h[o.k],

		// value
		o.k	
	);	
	o.i++;
}
return true;
},


_msg_old_browser_print: function() {
/*******************************

checks the browser for an outdated version; currently only checks for IE<7 because of its unusable :hover support

Input: 

	hash={
	}

*/

// use jQuery´s browser parameters
if (jQuery.browser.msie && parseFloat(jQuery.browser.version)<7) {

	var o={

		// create a DIV-fragment with message and append to body; CSS rules must be defined for id='browser_msg_div'
		'div_o': d.createDocumentFragment().createElement('DIV')
	};
	o.div_o.id='browser_msg_div';

	// pay attention: no multiline support in JS
	o.div_o.innerHTML='<p>WICHTIGER HINWEIS: Ihr Internet-Browser ist nicht geeignet diese Webseiten wie gewünscht anzuzeigen. Um diese Webseiten vollumfänglich nutzen zu können, benötigen Sie einen moderneren Browser.</p><br><p>IMPORTANT NOTICE: Your internet-browser is outdatet and not able to display these websites properly.<br>For being able to use completely all functions of these websites you need a more modern browser.</p>';

	d.body.insertBefore(o.div_o,d.body.firstChild);
}
},


_is_array: function(o) {
/*******************************

Checks whether a value is an array or not

Input: 

	value to check

Returns:

true if value is an array, else false

*/

return 

	typeof(o)=='object' 
	&& typeof(o.length)=='number';
},


_rand_get: function(_h) {
/*******************************
build 1

Returns a random combination of numbers or characters and numbers within 
a specific range or with a  specific length.

Input:

	hash={
	
		min: (optional) minimum number or length (if 'mixed' is true); 0 by default
		max: (optional) maximum number or length (if 'mixed' is true); 10 by default
		mixed: (optional) characters and numbers mixed; only numbers by default
	}

Returns: 

	random number/chars or false on error


*****************************************/

var o={

	'min': typeof(_h.min)=='number'

		? _h.min
		: 0,

	'max': typeof(_h.max)=='number'

		? _h.max
		: 10
};

// min must be smaller than max
if (o.min<=o.max) {

	// generate random number between min and max
	o.num=o.min+parseInt(Math.random()*(o.max-o.min+1));

	// mixed 
	if (typeof(_h.mixed) != 'undefined') {

		o.chars='u0G9ABC4pqD6EF2HIJ1OfP8RSTUW5rXKwQmLMTZabVcdsegxhi3klnNotvy7z';
		o.rand='';
	
		for (o.i=0; o.i<=o.num; o.i++) {
		
			o.j=Math.floor(Math.random()*61);
			o.rand += o.chars.charAt(o.j);
		}
		return o.rand;
	}
	// number
	else {
		return o.num;
	}
}
return false;
},


_get_el: function(el) {
/**************************
build: 2

Returns an object which will either be the same as incoming or gotten from DOM.

Input: 

	el-object or ID-string of the element

Returns:

	object if existing or null

*/

var o={

	'el_o': null
};

// if we already  have an object
if ((o.type=typeof(el))=='object') {

	// get object 
	o.el_o=el;	

	// add el to cache
	xitroBASE._cache_els_add(el);
}
// if we only have an ID-string
else if (o.type=='string') {

	// get element from cache if existing
	if (typeof(this.h.els_h[el])=='object') {

		o.el_o=this.h.els_h[el].o;
	}
	//  else get it from DOM and add to cache
	else {

		xitroBASE._cache_els_add(
	
			o.el_o=d.getElementById(el)
		);
	}
}
// return el-object that is null if not found
return o.el_o;
},


_cache_els_add: function(el) {
/**************************
build 3

Adds an element to the els_h-hash if not yet done

Input: 

	el:  element-object or  or ID-string of element to add to global 'els_h'; must have an ID

Returns:

	true on success, false on error

*/

var o={
};

// we need an el´s ID, else create and add random
if (typeof(el.id)=='undefined') {

	el.id=xitroBASE._rand_get({

		'min': 5,
		'mixed': true
	});
}

// add el if it does not exist in els_h-cache
if (typeof(this.h.els_h[el.id]) != 'object') {

	this.h.els_h[el.id]={

		'o': el
	};
}

return true;
}


// end jQuery extend
	}
);


/*****************************

	Following some unmodified  JS-scripts from third party vendors.
	The copyrights keep untouched as they are given by these vendors.  

*****************************/



