var textCounter = function (element, id, maxlength) {
	$(id).val(maxlength - parseInt($(element).val().length));
	if(parseInt($(id).val()) < 1) {
		$(element).val($(element).val().substring(0, maxlength));
		$(id).val(0);
	}
}

$(function(){

	$(".cloudItem").click(function () {
		if ($(this).is('.active')) {
			$(this).removeClass('active');

			$(".visibleBlock").show();
		} else {
			$(".cloudItem").removeClass('active');
			$(this).addClass('active');
			
			$(".visibleBlock").hide();
			$("." + $(this).attr("id")).show();
		}

		return false;
	});
	
	
	
	/* -- FROM: http://www.west-wind.com/WebLog/posts/282495.aspx -- */

	String.repeat = function(chr,count)
	{
	    var str = ""; 
	    for(var x=0;x<count;x++) {str += chr}; 
	    return str;
	}

	String.prototype.padL = function(width,pad)
	{
	    if (!width ||width<1)
	        return this;
	 
	    if (!pad) pad=" ";        
	    var length = width - this.length
	    if (length < 1) return this.substr(0,width);

	    return (String.repeat(pad,length) + this).substr(0,width);    
	}    

	String.prototype.padR = function(width,pad)
	{
	    if (!width || width<1)
	        return this;        

	    if (!pad) pad=" ";
	    var length = width - this.length
	    if (length < 1) this.substr(0,width);

	    return (this + String.repeat(pad,length)).substr(0,width);
	} 

	Date.prototype.formatDate = function(format)
	{
	    var date = this;
	    if (!format)
	      format="MM/dd/yyyy";               
	    var month = date.getMonth() + 1;
	    var year = date.getFullYear();    
	    format = format.replace("MM",month.toString().padL(2,"0"));        

	    if (format.indexOf("yyyy") > -1)
	        format = format.replace("yyyy",year.toString());
	    else if (format.indexOf("yy") > -1)
	        format = format.replace("yy",year.toString().substr(2,2));

	    format = format.replace("dd",date.getDate().toString().padL(2,"0"));

	    var hours = date.getHours();       

	    if (format.indexOf("t") > -1)
	    {
	       if (hours > 11)
	        format = format.replace("t","pm")
	       else
	        format = format.replace("t","am")
	    }

	    if (format.indexOf("HH") > -1)
	        format = format.replace("HH",hours.toString().padL(2,"0"));
	    if (format.indexOf("hh") > -1) {
	        if (hours > 12) hours - 12;
	        if (hours == 0) hours = 12;
	        format = format.replace("hh",hours.toString().padL(2,"0"));        
	    }

	    if (format.indexOf("mm") > -1)
	       format = format.replace("mm",date.getMinutes().toString().padL(2,"0"));
	    if (format.indexOf("ss") > -1)
	       format = format.replace("ss",date.getSeconds().toString().padL(2,"0"));

	    return format;
	}
	
	Date.dayNames = ['Sonntag', 'Montag', 'Dienstag', 'Mittwoch', 'Donnerstag', 'Freitag', 'Samstag'];
	Date.abbrDayNames = ['So', 'Mo', 'Di', 'Mi', 'Do', 'Fr', 'Sa'];
	Date.monthNames = ['Januar', 'Februar', 'März', 'April', 'Mai', 'Juni', 'Juli', 'August', 'September', 'Oktober', 'November', 'Dezember'];
	Date.abbrMonthNames = ['Jan', 'Feb', 'Mrz', 'Apr', 'Mai', 'Jun', 'Jul', 'Aug', 'Sep', 'Okt', 'Nov', 'Dez'];

	Date.firstDayOfWeek = 1;
	Date.format = 'yyyy-mm-dd';

	$('.date-pick')
		.datePicker({createButton:false})
		.bind(
			'click',
			function()
			{
				$(this).dpDisplay();
				this.blur();
				return false;
			}
		)
		.bind(
			'dateSelected',
			function(e, selectedDate, td)
			{
				$('#dateDE').html(selectedDate.formatDate("dd.MM.yyyy"));
				$('#dateEN').val(selectedDate.formatDate("yyyy-MM-dd"));

				var outerForm = $(this).parents("form:first");
				outerForm.submit();
			}
		);
	
	$('.turn-me-into-datepicker').datePicker({inline:true});
	if(typeof($('#dateEN').val()) != "undefined") { $('.turn-me-into-datepicker').dpSetSelected($('#dateEN').val()) }
	$('.turn-me-into-datepicker').bind(
			'dateSelected',
			function(e, selectedDate, td)
			{
				window.location = '/kalender/datum/' + selectedDate.formatDate("yyyy-MM-dd");
			}
		);

});
