var netvHelperClass = function() {
	var self = this;
	
	this.init = function(initAddDots,initPlaceholder){
		initAddDots = (typeof initAddDots == 'undefined') ? 1 : initAddDots;
		initPlaceholder = (typeof initPlaceholder == 'undefined') ? 1 : initPlaceholder;
		
		if(initAddDots){
			self.initAddDotsWithJS();
		}
		if(initPlaceholder){
			$('form').each(function(){
				self.initPlaceholders($(this));
			});
		}
		
		//bugfixing "image floated from text" if image-height greater then text-height!
		$('.content-text-image').each(function(){
			var $item = $(this);
			if(!$item.parent().hasClass('content-textlist-image')){
				if($('#container_wide').is(':hidden')){
					$('#container_wide').show();
					if($item.height() < $item.children('.content-image').height()){
						$item.css('padding-bottom','14px');
					}
					$('#container_wide').hide();
				}
				else{
					if($item.height() < $item.children('.content-image').height()){
						$item.css('padding-bottom','14px');
					}
				}
			}
		});
	};
	
	this.initAddDotsWithJS = function(){
		$('.add-dots-with-js').each(function(){
			var innerObj = $(this).find('.add-dots-with-js-inner');
			if(innerObj.length > 0){
				//additional hack for --BAUWERK--
				if($('#container_wide').is(':hidden')){
					$('#container_wide').show();
					self.addDotsWithJS($(this), innerObj);
					$('#container_wide').hide();
				}
				else{
					self.addDotsWithJS($(this), innerObj);
				}
			}
		});
	};
	
	this.trimIt = function(str){
		str.replace(/[\t]/g, '');
		return jQuery.trim(str);
	};

	this.addDotsWithJS = function(outerObj, innerObj){
		var htmltext = self.trimIt(innerObj.html());
		var text = self.trimIt(innerObj.text());
		var splittext = text.split(" ");
		// check if the height of innerObj exceeds outerObj.
		// if the inner object is too tall remove the last word 
		// add three dots and check again.
		while (innerObj.height() > outerObj.height() && splittext.length > 3){
			splittext = text.split(" ");
			splittext.pop();
			text=splittext.join(" ");
			text = jQuery.trim(text); 


			if (text.substr(text.length-1,1) == "."){
				// there is already a dot at the end. 2 more are needed.
				text = text + '..';
			}
			else {
				text = text + '...';
			}
			
			html = text.replace(/(\r\n|\n\r|\r|\n)/gm, '<br />');
			innerObj.html(html);
		}
		
		//add first html-span in the text-block
		if(htmltext.substr(0,6).toLowerCase() == "<span "){
			var splithtmltext = htmltext.split("</span>");
			//IE8 BUGFIX
			if(splithtmltext.length == 1){
				var splithtmltext = htmltext.split("</SPAN>");
			}
			if(splithtmltext.length == 2){
				var comparetext = splithtmltext[0] += '</span>';
				if(comparetext.length-26 < text.length){ //<span class="lead"></span> => "-26"
					var replacetext = $(comparetext).text();
					text = text.replace(replacetext, comparetext);
					html = text.replace(/(\r\n|\n\r|\r|\n)/gm, '<br />');
					innerObj.html(html);
				}
				else{
					text = text.replace(/(\r\n|\n\r|\r|\n)/gm, '<br />');
					html = '<span class="lead">'+text+'</span>';
					innerObj.html(html);
				}
			}
		}
	};

	/*!
	 * Placeholder functionality for older browsers.
	 */
	this.initPlaceholders = function(frm) {
		if(!frm)return false;
		frm.find('[placeholder]').focus(function() {
			var input = $(this);
			if (input.val() == input.attr('placeholder')) {
				if(input.hasClass('password')){
					input.attr('type','password');
				}
				input.val('');
				input.removeClass('placeholder');
			}
		}).blur(function() {
			var input = $(this);
			if (input.val() == '' || input.val() == input.attr('placeholder')) {
				if(input.hasClass('password')){
					input.attr('type','text');
				}
				input.addClass('placeholder');
				input.val(input.attr('placeholder'));
				
			}
		}).blur();
		frm.submit(function() {
			frm.find('[placeholder]').each(function() {
				var input = $(this);
				if (input.val() == input.attr('placeholder')) {
					input.val('');
				}
			});
		});
	};
	
	this.showElements = function(arr) {
		$.each(arr, function(index, objName) { 
			$(objName).show();
		});
	};
	this.hideElements = function(arr) {
		$.each(arr, function(index, objName) { 
			$(objName).hide();
		});
	};
	this.toggleElements = function(arr) {
		$.each(arr, function(index, objName) { 
			$(objName).toggle();
		});
	};
};

// Create a class instance for use in the scripts
var netvHelper = new netvHelperClass();
