/* Element ein/ausblenden */

function toggleMe(a){
  var e=document.getElementById(a);
  if(!e)return true;
  if(e.style.display=="none"){
    e.style.display="block"
  } else {
    e.style.display="none"
  }
  return true;
}

/* Checkboxen komplett (de)aktivieren */

function SetAllCheckBoxes(FormName, FieldName, mthis)
{
  CheckValue = true;

  if (mthis.checked) CheckValue=true;
  else CheckValue=false;

  if(!document.forms[FormName])
    return;
  var objCheckBoxes = document.forms[FormName].elements[FieldName];
  if(!objCheckBoxes) return;

  var countCheckBoxes = objCheckBoxes.length;
  if(!countCheckBoxes) objCheckBoxes.checked = CheckValue;
  else
    for(var i = 0; i < countCheckBoxes; i++)
      objCheckBoxes[i].checked = CheckValue;
}

/* Link abfragen und bestätigen */
	
 function ask_first(link, question)
{
	if (typeof(question) == 'undefined')
		question = 'Bitte erneut bestätigen'
	return window.confirm(question);
}

/* BB-CODE REPLACER */
function insert(aTag, eTag) {
  var input = document.getElementById('bb');
  input.focus();
  /* für Internet Explorer */
  if(typeof document.selection != 'undefined') {
    /* Einfügen des Formatierungscodes */
    var range = document.selection.createRange();
    var insText = range.text;
    range.text = aTag + insText + eTag;
    /* Anpassen der Cursorposition */
    range = document.selection.createRange();
    if (insText.length == 0) {
      range.move('character', -eTag.length);
    } else {
      range.moveStart('character', aTag.length + insText.length + eTag.length);      
    }
    range.select();
  }
  /* für neuere auf Gecko basierende Browser */
  else if(typeof input.selectionStart != 'undefined')
  {
    /* Einfügen des Formatierungscodes */
    var start = input.selectionStart;
    var end = input.selectionEnd;
    var insText = input.value.substring(start, end);
    input.value = input.value.substr(0, start) + aTag + insText + eTag + input.value.substr(end);
    /* Anpassen der Cursorposition */
    var pos;
    if (insText.length == 0) {
      pos = start + aTag.length;
    } else {
      pos = start + aTag.length + insText.length + eTag.length;
    }
    input.selectionStart = pos;
    input.selectionEnd = pos;
  }
  /* für die übrigen Browser */
  else
  {
    /* Abfrage der Einfügeposition */
    var pos;
    var re = new RegExp('^[0-9]{0,3}$');
    while(!re.test(pos)) {
      pos = prompt("Einfügen an Position (0.." + input.value.length + "):", "0");
    }
    if(pos > input.value.length) {
      pos = input.value.length;
    }
    /* Einfügen des Formatierungscodes */
    var insText = prompt("Bitte geben Sie den zu formatierenden Text ein:");
    input.value = input.value.substr(0, pos) + aTag + insText + eTag + input.value.substr(pos);
  }
}


/* SEO Wortdichte bestimmen */
	
var matchPhrases = null;
var matchExpressions = null;
	   
function wordcount(src,evt) 
{
	if (window.event) evt = window.event;
	if (!evt.charCode) evt.charCode = evt.keyCode;
	if ( (evt.charCode == 10) || (evt.charCode == 13) || (evt.charCode == 32) ) {
	   var wordCount = 0;
		var tmp = src.value.replace(/(^\s+|\s+$)/,'').replace(/[.;-?=!, \r\n\t]+/gi,' ');
		wordCount = tmp.split(' ').length;
		document.getElementById('wordCount').innerHTML = wordCount;
		if (matchPhrases) {
		   var strResult = '';
		   var pct;
		   var count;
			tmp = ' '+tmp+' ';
			for(var i=0;i<matchPhrases.length;i++) {
			   var found = tmp.match(matchExpressions[i]);
				if (found) 
				{
					count = found.length;
				} else 
				{
					count = 0;
				}
				pct = (count*100)/wordCount;
				if ((pct==0) || (pct>6)) 
				{
					color = '#ff0000';
				}
				else if ((pct >= 3) && (pct <= 6))
				{
					color = 'green';
				}
				else if (pct > 0)
				{
					color = '#ff9900';
				}						
				strResult += '<span style="color:'+color+';">'+matchPhrases[i]+': <strong>'+count+'</strong> ('+pct.toFixed(2)+'%)</span><br>';
			}
			document.getElementById('phraseCount').innerHTML = strResult;
		}
	}
}
// Trennzeichen
function prepare(src) {
	matchPhrases = src.value.replace(/\r\n/g,'\n').split(",");
	matchExpressions = new Array(matchPhrases.length);
	for(var i=0;i<matchPhrases.length;i++) {
		matchExpressions[i] = new RegExp('\\\s'+matchPhrases[i]+'\\\s','gi');
	}
}

// Tabs
$(function () {
    var tabContainers = $('div.tabs > div');
    
    $('div.tabs ul.tabNavigation li a').mouseover(function () {
        tabContainers.stop().hide().css("opacity","1").filter(this.hash).fadeIn(300);
        
        $('div.tabs ul.tabNavigation a').removeClass('selected');
        $(this).addClass('selected');
        
        return false;
    }).filter(':first').mouseover();
});

// Navigation

var timeout    = 500;
var closetimer = 0;
var ddmenuitem = 0;

function jsddm_open()
{  jsddm_canceltimer();
   jsddm_close();
   ddmenuitem = $(this).find('ul').css('visibility', 'visible');}

function jsddm_close()
{  if(ddmenuitem) ddmenuitem.css('visibility', 'hidden');}

function jsddm_timer()
{  closetimer = window.setTimeout(jsddm_close, timeout);}

function jsddm_canceltimer()
{  if(closetimer)
   {  window.clearTimeout(closetimer);
      closetimer = null;}}

$(document).ready(function()
{  $('#jsddm > li').bind('mouseover', jsddm_open)
   $('#jsddm > li').bind('mouseout',  jsddm_timer)});

document.onclick = jsddm_close;

// Transparent PNG superslight

jQuery.fn.supersleight = function(settings) {
	settings = jQuery.extend({
		imgs: true,
		backgrounds: true,
		shim: 'x.gif',
		apply_positioning: true
	}, settings);
	
	return this.each(function(){
		if (jQuery.browser.msie && parseInt(jQuery.browser.version, 10) < 7 && parseInt(jQuery.browser.version, 10) > 4) {
			jQuery(this).find('*').andSelf().each(function(i,obj) {
				var self = jQuery(obj);
				// background pngs
				if (settings.backgrounds && self.css('background-image').match(/\.png/i) !== null) {
					var bg = self.css('background-image');
					var src = bg.substring(5,bg.length-2);
					var mode = (self.css('background-repeat') == 'no-repeat' ? 'crop' : 'scale');
					var styles = {
						'filter': "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='" + mode + "')",
						'background-image': 'url('+settings.shim+')'
					};
					self.css(styles);
				};
				// image elements
				if (settings.imgs && self.is('img[src$=png]')){
					var styles = {
						'width': self.width() + 'px',
						'height': self.height() + 'px',
						'filter': "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + self.attr('src') + "', sizingMethod='scale')"
					};
					self.css(styles).attr('src', settings.shim);
				};
				// apply position to 'active' elements
				if (settings.apply_positioning && self.is('a, input') && (self.css('position') === '' || self.css('position') == 'static')){
					self.css('position', 'relative');
				};
			});
		};
	});
};

/*
 * Image preview script 
 * powered by jQuery (http://www.jquery.com)
 * 
 * written by Alen Grakalic (http://cssglobe.com)
 * 
 * for more info visit http://cssglobe.com/post/1695/easiest-tooltip-and-image-preview-using-jquery
 *
 */
 
this.imagePreview = function(){	
	/* CONFIG */
		
		xOffset = 150;
		yOffset = 20;
		
		// these 2 variable determine popup's distance from the cursor
		// you might want to adjust to get the right result
		
	/* END CONFIG */
	$("a.preview").hover(function(e){
		this.t = this.title;
		this.title = "";	
		var c = (this.t != "") ? "<br/>" + this.t : "";
		$("body").append("<p id='preview'><img src='"+ this.rel +"' alt='Image preview' />"+ c +"</p>");								 
		$("#preview")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("100");						
    },
	function(){
		this.title = this.t;	
		$("#preview").remove();
    });	
	$("a.preview").mousemove(function(e){
		$("#preview")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});			
};

// starting the script on page load
$(document).ready(function(){
	imagePreview();
});

/*
 * Tooltip script 
 * powered by jQuery (http://www.jquery.com)
 * 
 * written by Alen Grakalic (http://cssglobe.com)
 * 
 * for more info visit http://cssglobe.com/post/1695/easiest-tooltip-and-image-preview-using-jquery
 *
 */

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

// starting the script on page load
$(document).ready(function(){
	tooltip();
});


// Lightbox
$(function() {
	$('a.lightbox').lightBox({
	overlayBgColor: '#000',
	overlayOpacity: 0.6,
	containerResizeSpeed: 350,
	txtImage: 'Bild',
	txtOf: 'von'
   });
});

// Limiter
function limitChars(textarea, limit, infodiv)
 {
	 var text = textarea.value; 
	 var textlength = text.length;
	 var info = document.getElementById(infodiv);
	  
	 if(textlength > limit)
	 {
	 info.innerHTML = 'Mehr als '+limit+' Zeichen sind nicht möglich!';
	  textarea.value = text.substr(0,limit);
	  return false;
	  }
	  else
	  {
	  info.innerHTML = 'Sie können noch '+ (limit - textlength) +' Zeichen schreiben';
	  return true;
	  }
}
