/** MISC FUNCTIONS **/
function getObjectLength (o) {
	var length = 0;

	for (var i in o) {
	    if (Object.prototype.hasOwnProperty.call(o, i)){
	      length++;
	    }
	}
	return length;
}

/** FUNC: TRUNCATE STRING **/
function truncate (str, len) {
	if (typeof(str) !== 'undefined' && str != null && str != '') {
		if (str.length < len) {
			return str;
		}
		else {
			return str.substring(0, len) + '...';
		}
	}
}

/** URLEncode **/
$.extend({URLEncode:function(c){var o='';var x=0;c=c.toString();var r=/(^[a-zA-Z0-9_.]*)/;
  while(x<c.length){var m=r.exec(c.substr(x));
    if(m!=null && m.length>1 && m[1]!=''){o+=m[1];x+=m[1].length;
    }else{if(c[x]==' ')o+='+';else{var d=c.charCodeAt(x);var h=d.toString(16);
    o+='%'+(h.length<2?'0':'')+h.toUpperCase();}x++;}}return o;},
URLDecode:function(s){var o=s;var binVal,t;var r=/(%[^%]{2})/;
  while((m=r.exec(o))!=null && m.length>1 && m[1]!=''){b=parseInt(m[1].substr(1),16);
  t=String.fromCharCode(b);o=o.replace(m[1],t);}return o;}
});

$(document).ready(function() {
	
	/** FOOTER FEEDBACK FORM: **/
	$('#rootFeedback').click(function() {
		$('#textFeedback').focus();
		return false;
	})
	$('#textFeedback').focus(function() {
		if ($(this).val() == $(this).attr('rel')) {
			$(this).css('color', '#333');
			$("#emailFeedbackBox").animate({"opacity": "show"}, 800);
			$('html, body').animate({scrollTop: $(document).height()}, 800); //scroll down
		}
		$(this).css('height', '60px');
		$('.footer').css('height', '25em');
	});
	$('#textFeedback').blur(function() {
		if ($(this).val() == '') {
			$(this).css('color', '#9C9B98');
			$(this).css('height', '16px');
			$('.footer').css('height', '20em');
			$('#emailFeedbackBox').css('display', 'none');
		}
	});

	$('#emailFeedback').focus(function() {
		if ($(this).val() == $(this).attr('rel')) {
			$(this).css('color', '#333');
		}
	});
	$('#emailFeedback').blur(function() {
		if ($(this).val() == '') {
			$(this).css('color', '#9C9B98');
		}
	});

	$('#feedbackForm').submit(function(e) {
		var message = $("#textFeedback").val();
		var email = $("#emailFeedback").val();
		if (message == 'Sende uns Verbesserungen, Fehler, Ideen ...') message = '';
		if (message != '') {
			$.post("/feedback", { message: message, email: email, uri: document.URL },
				function(data) {
					if (data == 'OK') {
						$('#feedback').hide();
						$('#feedback_confirm').show();
					}
				}
			);
		}
		return false;
	});
	
	
	/** PREFILLED INPUTFIELDS: **/
	var prefilledText = '';

	$('.prefilled').each(function() {
		if ($(this).val() == '') {
			$(this).css('color', '#9C9B98').css('font-style', 'italic');
			$(this).val($(this).attr('rel')); //write default text
		} else {
			$(this).css('color', '#333').css('font-style', 'normal');
		}

		/* clear default inputfields: */
		$(this).parents('form').first().submit(function() {
			prefilledInputs = $(this).find('.prefilled');
			
			if (prefilledInputs) {
				prefilledInputs.each(function(i) {
					prefilledText = $(this).attr('rel');
					if ($(this).val() == prefilledText) {
						$(this).val('');
					}
				});
			}
		})
	});
	$('.prefilled').focus(function() {
		prefilledText = $(this).attr('rel');
		if ($(this).val() == prefilledText) {
			$(this).val('');
			$(this).css('font-style', 'normal');
			$(this).css('color', '#333');
		}
	});
	$('.prefilled').blur(function() {
		if ($(this).val() == '') {
			$(this).css('color', '#9C9B98').css('font-style', 'italic');
			$(this).val(prefilledText);
		}
	});

});
