/**
	returns value of url param
	@author: faeb187
*/
function param( k ){

	k = k.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    
    var reg = new RegExp( "[\\?&]" + k + "=([^&#]*)" ),
    	res = reg.exec( window.location.href );

	return res ? res[ 1 ] : null;
}

/**
	returns a random number
	@author: faeb187
*/
Math.rand = function( min, max ){
	
	return Math.floor( Math.random() * ( max - min + 1 ) + min );
}

/**
	changes first letter to upper case
	@author: faeb187
*/
String.prototype.ucFirst = function(){

	return this.charAt( 0 ).toUpperCase() + this.slice( 1 );
}

/**
	changes first letter to lower case
	@author: faeb187
*/
String.prototype.lcFirst = function(){

	return this.charAt( 0 ).toLowerCase() + this.slice( 1 );
}

/**
	returns true if string
	starts with prefix
	@author: faeb187
*/
String.prototype.startsWith = function( prefix ){

	return this.indexOf( prefix ) === 0;
}

/**
	returns day count
	for a month
	@author: faeb187
*/
Date.prototype.daysInMonth = function( month, year ){

	return 32 - new Date( year, month, 32 ).getDate();
}

/**
	returns formatted date
	support for: ch (default), en, us
	@author: faeb187
*/
Date.prototype.format = function( code ){
	
	var self = this,
		time = [ self.getDate(), self.getMonth(), self.getFullYear(), self.getHours(), self.getMinutes() ];
		
	for( var i = 0, len = time.length; i < len; ++i )
		if( time[ i ] < 10 ) time[ i ] = "0" + time[ i ];
		
	switch( code ){
		
		//england, usa
		case "en":
		case "us": return time[ 2 ] + "/" + time[ 1 ] + "/" + time[ 0 ] + " " + time[ 3 ] + ":" + time[ 4 ]; break;
		
		//germany, austria, switzerland
		default: return time[ 0 ] + "." + time[ 1 ] + "." + time[ 2 ] + " " + time[ 3 ] + ":" + time[ 4 ]; break;
	}
}

/**
	image preloader
	@author: faeb187
*/
Array.prototype.preload = function( opt ){

	opt = opt || {};

	var toLoad = this,
		l = toLoad.length,
		images = [];

	//no images to load
	if( !l ){
	
		if( opt.cb ) opt.cb();
		return;
	}
	
	//start loading
	for( var i = 0; i < l; ++i ){
		
		var image = new Image();
		image.src = toLoad[ i ];
		images.push( image );
	}
	
	//callback when all images loaded
	var loading = setInterval( function(){
				
		var loaded = 0;
					
		for( var i = 0, l = images.length; i < l; ++i )
					
			if( images[ i ].complete ) loaded++;
			
		//all images loaded		
		if( loaded == toLoad.length ){
		
			if( opt.dom ) $( opt.dom ).remove();
			
			clearInterval( loading );
			if( opt.cb ) opt.cb();
		}
		
		//show loader
		else if( opt.dom && opt.loader ){
		
			//only add loader once
			if( !$( 'img', opt.dom ).length )
			
				opt.dom.innerHTML = "<img src='" + opt.loader + "'/>";
		}
		
		//show percentage
		else if( opt.dom ) opt.dom.innerHTML = parseInt( 100 / images.length * loaded ) + "%";
				
	}, 100 );
}

/**
	random array order
	@author: faeb187
*/
Array.prototype.shuffle = function(){

	var s = [];
	
	while ( this.length ) s.push( this.splice( Math.random() * this.length, 1 ) );
	while ( s.length ) this.push( s.pop()[ 0 ]);
	
	return this;
}

/**
	let object blink
	@author: faeb187
*/
Node.prototype.blink = function( bg, c ){

	var node = this,
		i = 0;

	bg = [ bg, $( this ).css( "background-color" )];
	c = [ c, $( this ).css( "color" )];

	setInterval( function(){
	
		$( node ).css({
		
			'background-color': bg[ i ],
			color: c[ i ]
		});
		
		i = i ? 0 : 1;
		
	}, 500 );
}
