Functions= {};
var visible ={
	'visibility' : 'visible',
	'display' : 'block'
}
var invisible = {
	'visibility' : 'visible',
	'display' : 'none'			
}

/* ---------------------------*/
/* Util -------------*/
/* ---------------------------*/
Util= {
  decryptMailLinks: function() {
    $('a.encrypted_mail').each(function () {
      $(this).attr('href', Util.rot13($(this).attr('href')));
	  $(this).html($(this).attr('href').replace(/mailto:/, ''));
    });
  },
  rot13: function(text) {
    var keycode = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    var text = new String(text);
    var rot13 = new String();
    for(var i=0;i<text.length;i++)
    {
      var codechar = text.substring(i, i + 1);
      var pos = keycode.indexOf(codechar.toUpperCase())

      if(pos >= 0)
      {
        pos = (pos + keycode.length / 2) % keycode.length;
        codechar = (codechar == codechar.toUpperCase()) ?
          keycode.substring(pos, pos + 1) :
          keycode.substring(pos, pos + 1).toLowerCase();
      }
      rot13 = rot13 + codechar;
    }
    return rot13;
  },
  validateForm: function(id) {
		var result= true;
		$("#"+id + " input, #"+id + " textarea").each(function() {
			if($(this).attr("class") && $(this).attr("class").match("required")) {
			  $(this).val($(this).val().replace(/^\s+|\s+$/g, ''));
				var itemresult= Util.validate($(this).attr("id"));
				result= result && itemresult;
			}
		});
		if(result) {
			$("#form_error").removeClass("error");
		} else {
			$("#form_error").addClass("error");
		}
		return result;
	},
	validate: function(id) {
		var result= true;
		var obj= $("#"+id);
		var commands= $("#"+id).attr("class").split(" ");
		for(var i=0; i<commands.length; i++) {
			var command= commands[i].replace(/[0-9]/g, "");
			switch(command) {
				case("minlength"):
					var length= parseInt(commands[i].replace(/minlength/g, ""));
					result= result && (obj.val().length>=length);
					break;
				case("maxlength"):
					var length= parseInt(commands[i]);
					result= result && (obj.val().length<=length);
					break;
				case("email"):
					result = result && ( obj.get(0).value.match(/\S@\S.\S{2,}/)!=null )
					break;
				default:
					result= result && (obj.val().length!=0);
					break;
			}
		}
		if(!result) {
			obj.addClass("error");
		} else {
			obj.removeClass("error");
		}
		return result;
	}
}

/* ---------------------------*/
/* ready -------------*/
/* ---------------------------*/
$(document).ready(function () {
	 
	 //externe Links erstellen
	 Functions.externalLink();
	
	//decrypt mails
	Util.decryptMailLinks();
	 
	 //TOOLTIP
	 tooltip();
	 		
	//-----------------//	
	//akkordeon -------//
	//-----------------//
	$(".accordion").each(function() {
	   var id = '#' + $(this).attr('id');
       var minAccordionLength = 400;
       var maxShortlength = 150;
       var shorttext = Functions.trim($(id + ' .accordion_open .accordion_text').text().substring(0, maxShortlength));
	   var contentLength = $(id + ' .accordion_open .accordion_text').text().length;
		if( contentLength <= minAccordionLength ){
			$(id + ' .accordion_closed').css('display', 'none');
			$(id + ' .accordion_open').css('display', 'block').css('visibility', 'visible');
			$(id).removeClass('accordion');
		
		}else{
			if (maxShortlength == shorttext.length ) {
			  shorttext +=' ...';
			}
			$(id + ' .accordion_closed .accordion_text').html(shorttext);
			
		}	
		
		});		
			
		$(".accordion").click(function() {
			if ($(this).hasClass('accordion_active')) {
				//geklickte Box wird geschlossen
				$(this).children('.accordion_content').animate({
					height: '70px'
				}, 300, 'swing', function() {
					$(this).children('.accordion_open').fadeOut(500);					
					$(this).children('.accordion_open').css('display', 'none');
					$(this).children('.accordion_closed').fadeIn(1000);
		      
				});
				$(this).removeClass('accordion_active');
			} else {
				
				//neue Akkordeon-Box öffnen				 
				 $(this).children('.accordion_content').children('.accordion_open').css('display', 'block');
				 var textheight =  $(this).children('.accordion_content').css('height', 'auto').height();				
				 $(this).children('.accordion_content').children('.accordion_open').css('display', 'none');
				
				if (textheight > 600) {
				  textheight += 20;
				}
				
				$(this).children('.accordion_content').animate(
					{
						height: (textheight-70)+'px'
					},
					500,
					'swing',
					function(){
				    		$(this).children('.accordion_closed').fadeOut(500);
				    		//$(this).css('height', 'auto');
				    		$(this).children('.accordion_open').fadeIn(500);
				    		$(this).parent().addClass('accordion_active');
				    }
				);				
				
			}
		});


		
		
				
		
	
});

/* ---------------------------*/
/* Functions -------------*/
/* ---------------------------*/
Functions= {
	init: function(){
		//alert("init");
	},
	doFormCheck: function() {	
		var result = true;		
		//alert('doFormCheck');
		
		jQuery.each($('.required'),function() {
			//alert('required');
			if($(this).val() < 2 ){
				$(this).addClass("invalid");
				result=false;
			}else if($(this).hasClass("invalid")){$(this).removeClass("invalid");}		
		});
		
		return result;
	},
	trim: function(s) {
		var l=0; var r=s.length -1;
		while(l < s.length && s[l] == ' ') {
			l++;
		}
		while(r > l && s[r] == ' ') {
			r-=1;
		}
		return s.substring(l, r+1);
	},
	externalLink: function () {
		 if (!document.getElementsByTagName) {
		 	return;
		 }
		 var anchors = document.getElementsByTagName("a");
		 for (var i=0; i<anchors.length; i++) {
			 var anchor = anchors[i];
			 if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") {
			 anchor.target = "_blank";
		 }
	 }
}
	
};


/* ---------------------------*/
/* tooltip -------------*/
/* ---------------------------*/
this.tooltip = function(){	
	/* CONFIG */		
		xOffset = 40;
		yOffset = 0;		
		// these 2 variable determine popup's distance from the cursor
		// you might want to adjust to get the right result		
	/* END CONFIG */		
	$(".tooltip").hover(function(e){											  
		this.t = this.title;
		this.title = "";									  
		$("body").append("<div id='tooltip'>"+ this.t +"</div>");
		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.css("z-index","100")
			.fadeIn("fast");		
    },
	function(){
		this.title = this.t;		
		$("#tooltip").remove();
    });	
	$(".tooltip").mousemove(function(e){
		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});			
};
