/** Limits the characters of an input field
  * @param elemid id of the inputfield to check
  * @param countfield the label, where the current amount of available chars are displayed
  * @param maxchars number of chars to limit
  */
var limitInputChars = function (elemid, countfield, maxchars) {
	var elem = document.getElementById(elemid);
	var val = elem.value;
	
	if (typeof val != undefined) {
		var charsToNeglect = val.replace(/[^\r\n]/g, "").length;
		maxchars += charsToNeglect;
		
		if (val.length > maxchars){
			elem.value = val.substring(0, maxchars);
		
		}else { 
			if (typeof countfield == "string"){
				document.getElementById(countfield).firstChild.data = maxchars - val.length;
			}else {
				alert ("The function limitInputChars() doesn't handle other 'countfield' params than typeof string!");
			}
		}
	}
}

jQuery(document).ready(function () {
	var errorFields = jQuery('input.alert, checkbox.alert, textarea.alert');
	
	var focus = undefined;
	if (errorFields.length) {
		focus = errorFields.eq(0);
	} else {
		var contentFields = jQuery('input[type="text",value=""], textarea[value=""]').eq(0);
		
		if (contentFields.length) {
			focus = contentFields.eq(0);
/*
		} else {
			focus = jQuery('input#input_search').eq(0);
*/			
		}
	}
	if (typeof focus != 'undefined') {
		focus.focus();
	}
	
	// listener for language selector
	jQuery(".coolSelect .selectContainer").toggle(function(){
		jQuery(this).addClass("open");
		jQuery(".coolSelect .optionsDiv").show();
	},function(){
		jQuery(this).removeClass("open");
		jQuery(".coolSelect .optionsDiv").hide();
	});
	
	// listener for default values
	$("input.defaultvalue").focus(function(){
		if ($(this).val() == $(this).attr("defaultvalue")){
			$(this).val("");
			$(this).select();
		}
	}).blur(function(){
		if ($(this).val() == "")
			$(this).val($(this).attr("defaultvalue"));
	});
	

	jQuery(".validation:input").keypress(function(e) {
		if (e.keyCode == 13) {
			// validation
			if (doValidation($(this))) {
				// set button
				jQuery(this.form).attr("action", this.form.action + "&" + jQuery("input.quicksubmit", this.form).attr("name") + "=submit");

				// submit form
				jQuery(this.form).submit();
			}
			return false;
		}
	});
	
	jQuery(".validation[type='submit']").click(function(e) {
		return (doValidation($(".validation:input:first", this.form)));
	});
	
	jQuery.nyroModalSettings({
		debug: false,		// Show the debug in the background
		padding: 0,			// padding for the max modal size
		minWidth: 960,
		minHeight: 600,
		type: 'form'		// nyroModal type (form, formData, iframe, image, etc...)
	});
	
});

function doValidation(elem) {
	var retvalue = true;
	if (elem.hasClass("defaultvalue")) {
		if (elem.val() == elem.attr("defaultvalue")) {
			elem.val("");
			elem.select();
			elem.focus();
			retvalue = false;
		} else if (elem.val() == "") {
			retvalue = false;
		}
	}
	return retvalue;
}
/*
var loadContactPage = function() {
	// override lightBox default settings
	jQuery(function() {
		jQuery.nyroModalSettings({
			debug: false,		// Show the debug in the background
			width: 980,
			minWidth: 980,		// Minimum width
			minHeight: 600,		// Minimum height
			padding: 0,			// padding for the max modal size
			type: 'form'		// nyroModal type (form, formData, iframe, image, etc...)
		});
	});
}
*/

var contactInitiator = "#loadContactInitiator";
var loadContactForm = function (){
	jQuery(contactInitiator).click();
}

/* Kickoff lightbox */
jQuery(function() {
	jQuery('.mainContainer a[rel=lightbox]').lightBox({
		fixedNavigation:true,
		imageLoading: '/GenticsApp/images/layout/lightbox-ico-loading.gif',
		imageBtnClose: '/GenticsApp/images/layout/lightbox-btn-close.gif',
		imageBlank: '/GenticsApp/images/layout/lightbox-blank.gif',
		containerResizeSpeed: 350
	});
});

function humane_date(date_obj){
	var time_formats = [
		[60, 'Just now'],
		[90, '1 minute'], // 60*1.5
		[3600, 'minutes', 60], // 60*60, 60
		[5400, '1 hour'], // 60*60*1.5
		[86400, 'hours', 3600], // 60*60*24, 60*60
		[129600, '1 day'], // 60*60*24*1.5
		[604800, 'days', 86400], // 60*60*24*7, 60*60*24
		[907200, '1 week'], // 60*60*24*7*1.5
		[2628000, 'weeks', 604800], // 60*60*24*(365/12), 60*60*24*7
		[3942000, '1 month'], // 60*60*24*(365/12)*1.5
		[31536000, 'months', 2628000], // 60*60*24*365, 60*60*24*(365/12)
		[47304000, '1 year'], // 60*60*24*365*1.5
		[3153600000, 'years', 31536000], // 60*60*24*365*100, 60*60*24*365
		[4730400000, '1 century'], // 60*60*24*365*100*1.5
	];

	var dt = new Date,
		seconds = ((dt - date_obj) / 1000),
		token = ' ago',
		i = 0,
		format;

	if (seconds < 0) {
		seconds = Math.abs(seconds);
		token = '';
	}

	while (format = time_formats[i++]) {
		if (seconds < format[0]) {
			if (format.length == 2) {
				return format[1] + (i > 1 ? token : ''); // Conditional so we don't return Just Now Ago
			} else {
				return Math.round(seconds / format[2]) + ' ' + format[1] + (i > 1 ? token : '');
			}
		}
	}

	// overflow for centuries
	if(seconds > 4730400000)
		return Math.round(seconds / 4730400000) + ' centuries' + token;

	return date_str;
};
