// JavaScript Document
var HTTP_SERVER = "http://www.fit20.de/"; 

function ViewStyle(ziel,newStyle)
{
	document.getElementById(ziel).className = newStyle;
	var viewChangeButton = document.getElementById('nahrungUebersicht').className;
	if(viewChangeButton == 'contentBox floatLeft listView')
	{
		document.getElementById('viewChanger').style.backgroundPosition = 'bottom';	
	}
	else
	{
		document.getElementById('viewChanger').style.backgroundPosition = 'top';	
	}
}

function three(position, dukind) {
	for(var x = 1; x < 6; x++){
		if(dukind==x) {
		document.getElementById(position+'top5p'+dukind).style.display = 'block';
		document.getElementById(position+'top'+dukind).className = 'activeli'+dukind;
		} else {
		document.getElementById(position+'top'+x).className = '';
		document.getElementById(position+'top5p'+x).style.display = 'none';
		}
	}
}

function Delay() {
	return true;	
}

function switch_del(el_id, pic_id,del_id)  {
	document.getElementById(el_id).href = "trainingsplan.php?del_id="+del_id;
	document.getElementById(pic_id).src = "images/end_loeschen.gif";
}

function setbg(wo,wie)  {
	document.getElementById(wo).style.backgroundColor = wie;
}

function openWindow(url,width,height ){
window.open(url, "Fitness-Übungen", "toolbar=no, location=no, menubar=no, scrollbars=yes, width=" + width + ", height=" + height + ", resizeable=yes, status=no");
}

function toggle(control,type)
{
	var elem = document.getElementById(control);
	if(elem.style.display == "none"){
		elem.style.display = type;
	}else{
		elem.style.display = "none";
	}
}

function toggle_checkbox(control)
{
	var cb = document.getElementById(control);
	if(cb.checked == true){
		cb.checked = false;
	}else{
		cb.checked = true;
	}
}

function toggle_tr(control)
{
var elem = document.getElementById(control);
	if(elem.style.display == "none"){
		elem.style.display = "";
	}else{
		elem.style.display = "none";
	}
}

function cbt(name,id){
	var alttext = '';
	switch(id){
	case 'bookmark':
		var text = '... diese Seite bei <b>'+name+'</b> bookmarken';
		var alttext = '...';
		break;
	case 'optionen':
		if(name=='frage'){
			var text = 'neue Frage vorschlagen';
		}else{
			var text = 'diesen Artikel '+name;
		}
		var alttext = 'diesen Artikel ...';
		break;
	default:
		var text = name;
		var alttext = '';
		break;
	}
	if(name != ''){
	document.getElementById(id).innerHTML = text;
	} else {
	document.getElementById(id).innerHTML = alttext;
	}
}

function bm(seite) {
	bm_url=encodeURIComponent(location.href);
	bm_title=encodeURIComponent(document.title);
	switch(seite) {		
		case 'delicious':
			window.open('http://del.icio.us/post?url='+bm_url+'&title='+bm_title);
			break;
		case 'wong':
			window.open('http://www.mister-wong.de/index.php?action=addurl&bm_url='+bm_url+'&bm_description='+bm_title);
			break;
		case 'blinkList':
			window.open('http://www.blinklist.com/index.php?Action=Blink/addblink.php&Description=&Url='+bm_url+'&Title='+bm_title);
			break;
		case 'yahoo':
			window.open('http://myweb2.search.yahoo.com/myresults/bookmarklet?u='+bm_url+'&t='+bm_title);
			break;
		case 'yigg':
			window.open('http://yigg.de/neu?exturl='+bm_url+'&exttitle='+bm_title);
			break;	
		case 'furl':
			window.open('http://www.furl.net/storeIt.jsp?u='+bm_url+'&t='+bm_title);
			break;
		case 'oneview':
			window.open('http://www.oneview.de/quickadd/neu/addBookmark.jsf?URL='+bm_url+'&title='+bm_title);			
			break;
		case 'folkd':
			window.open('http://www.folkd.com/submit/page/'+bm_url);
			break;
		case 'linkarena':	
			window.open('http://linkarena.com/bookmarks/addlink/?url='+bm_url+'&title='+bm_title+'&desc=&tags=');
			break;
		case 'google': 		
			window.open('http://www.google.com/bookmarks/mark?op=add&hl=de&bkmk='+bm_url+'&title='+bm_title);
			break;
		case 'webnews': 		
			window.open('http://www.webnews.de/einstellen?url='+bm_url+'&title='+bm_title);
			break;
		case 'tausendreporter': 		
			window.open('http://tausendreporter.stern.de/submit.php?url='+bm_url);
			break;
	}
}

function textCounter(field, countfield, maxlimit) {
  if (field.value.length > maxlimit)
      {field.value = field.value.substring(0, maxlimit);
	  countfield.style.color= 'red';
	  countfield.style.borderColor= 'red';
	  }
      else
      {countfield.value = maxlimit - field.value.length;
	  countfield.style.color= '';
	  countfield.style.borderColor= '';
	  }
}	  

function AddText(wrap, wrap2, where) {
	var editBody = document.getElementById(where);
	if (document.selection) { // for IE 
		var str = document.selection.createRange().text;
		editBody.focus();
		var sel = document.selection.createRange();
		sel.text = "[" + wrap + "]" + str + "[" + wrap2 + "]";
		return;
	} else if ((typeof editBody.selectionStart) != 'undefined') { // for Mozilla
		var txtarea = editBody;
		var selLength = txtarea.textLength;
		var selStart = txtarea.selectionStart;
		var selEnd = txtarea.selectionEnd;
		var oldScrollTop = txtarea.scrollTop;
		//if (selEnd == 1 || selEnd == 2)
		//selEnd = selLength;
		var s1 = (txtarea.value).substring(0,selStart);
		var s2 = (txtarea.value).substring(selStart, selEnd)
		var s3 = (txtarea.value).substring(selEnd, selLength);
		txtarea.value = s1 + "[" + wrap + "]" + s2 + "[" + wrap2 + "]" + s3;
		txtarea.selectionStart = s1.length;
		txtarea.selectionEnd = s1.length + s2.length + wrap.length + wrap2.length + 4;
		txtarea.scrollTop = oldScrollTop;
		txtarea.focus();
		return;
	} else {
		insertText(wrap + wrap2,where);
	}
}

function insertText(what,where) {
	var editBody = document.getElementById(where);
	if (editBody.createTextRange) {
		editBody.focus();
		document.selection.createRange().duplicate().text = what;
	} else if ((typeof editBody.selectionStart) != 'undefined') { // for Mozilla
		var tarea = editBody;
		var selEnd = tarea.selectionEnd;
		var txtLen = tarea.value.length;
		var txtbefore = tarea.value.substring(0,selEnd);
		var txtafter =  tarea.value.substring(selEnd, txtLen);
		var oldScrollTop = tarea.scrollTop;
		tarea.value = txtbefore + what + txtafter;
		tarea.selectionStart = txtbefore.length + what.length;
		tarea.selectionEnd = txtbefore.length + what.length;
		tarea.scrollTop = oldScrollTop;
		tarea.focus();
	} else {
		editBody.value += what;
		editBody.focus();
	}
}


function changeUserTab(tabid) {
	var bereich = document.getElementById('usert');
	var getLI = bereich.getElementsByTagName('li');
	for(var i = 0; i < getLI.length; i++){
		if(getLI[i].id == tabid){
			//richtigen Tab auswählen
			document.getElementById(getLI[i].id).className = 'selected';
			//Alle Tabinhalte ausblenden
			for(var z = 0; z < getLI.length; z++){
				document.getElementById(getLI[z].id+'_active').style.display = 'none';
			}
			//richtigen Tabinhalt einblenden
			document.getElementById(tabid+'_active').style.display = 'block';	
		} else {
			getLI[i].className = '';
		}
	}
}

function ask(){
    return confirm('Wollen Sie diese Daten wirklich loeschen?');
}


$(document).ready(function(){
						   
	$(".tooltip").dblclick($.tooltip.block);
	$(".tooltip").tooltip({	bodyHandler: function() { return $(this).next(".tt").html(); },	track: true, delay: 0, showURL: false });
	$("#tooltip > h3").click(function(){$.tooltip.blocked=false;$('#tooltip').fadeOut(100);$.tooltip.handle;});						   
						   
		$("#usermenuetabs > *").mouseover(function () {
		var index = $("#usermenuetabs > *").index(this);
		for(i=0;i<=3;i++) {
			if(i==index) {
				$("div#usermenuetabs" + i).show(0);
				//$("#utp" + i).addClass("usermenuetabspfeilselected");
			} else {
				$("div#usermenuetabs" + i).hide(0);
				$("#usermenuetabs > *").removeClass("selected");
			}
			$(this).addClass("selected");		
		}
    });

	$("a.usermenuetabspfeil").click(function () {
	var klicker = $("a.usermenuetabspfeil").index(this);
    $("#usertabLoading" + klicker).show();
	  $(this).addClass("usermenuetabspfeilselectedopen");
		if(klicker == 1) {
			$.get(HTTP_SERVER + "ajaxcontent/mini_pn.php", function(data){
  				$("#usertabLoading" + klicker).html(data);
			});			
		} else if (klicker == 2) {
			$.get(HTTP_SERVER + "ajaxcontent/mini_wk.php", function(data){
  				$("#usertabLoading" + klicker).html(data);
			});
		}
	});
	
 $(".cbt").hover(
	function () {
      var id = $(this).attr("id").split("_"); 
	  var titleString = $(this).attr("title");
      $("#"+id[1]).text(titleString);
    },  
    function () {
      var id = $(this).attr("id").split("_"); 
	  $("#"+id[1]).text("");
    }
  );


    $(".usertabLoading").hover(
      function () {
        $(this).show();
      }, 
      function () {
        $(this).hide();
		    $("a.usermenuetabspfeil").removeClass("usermenuetabspfeilselectedopen");
      }
    );
	
	$("#select_suchkatid").click(function () {
		$('#suchkatid_content').toggle();
	});
	
	$("#select_suchkatid").blur(function () {
		$('#suchkatid_content').fadeOut();
	});
	
	function LiveSuche() {
	var inputString = $("#search").attr("value");
		if(inputString.length <= 2) {
			$('#livesuche').fadeOut(); // Hide the result box
		} else {
			var suchkat = $("#suchkat").attr("value");
			$.get(HTTP_SERVER + "ajaxcontent/live_suche.php", {suchkat: ""+suchkat+"",search_name: ""+inputString+""}, function(data) { // Do an AJAX call
				$('#livesuche').fadeIn(); // Show the result box
				$('#livesuche').html(data); // Fill the result box
			});
		}
	}
	
	$("#suchkatid_content>li").click(function () {
		$('#suchkatid_content').hide();
		$('#select_suchkatid').attr("value",$(this).attr("title"));
		var id = $(this).attr('id').split("_");
		$('#suchkat').attr("value",id[1]);
		$("#suchkatid_content>li").removeClass("selected");
		$(this).addClass("selected");
		LiveSuche();
	});
  
  $("#search").keyup(LiveSuche);
  
});