// 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';
		}
	}
}

debug = function(msg) {
	if (window.console && window.console.log)
		window.console.log(msg);
}

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(text){
	if(text=="") text = 'Wollen Sie diese Daten wirklich loeschen?';
    return confirm(text);
}

var isOpen = false;
var isOpenKal = false;
var isOpenPN = false;	
var isOpenWK = false;	
var isOpenMK = false;	

$(document).ready(function(){
	
		j = new Date();
		Jahr = j.getYear();
		if ((Jahr > 99) && (Jahr < 1900)) Jahr += 1900;
		$.datepicker.regional['de'] = {
			closeText: 'schlie&szlig;en',
			prevText: '&lt; zur&uuml;ck',
			nextText: 'Vor &gt;',
			currentText: 'heute',
			monthNames: ['Januar','Februar','M&auml;rz','April','Mai','Juni','Juli','August','September','Oktober','November','Dezember'],
			monthNamesShort: ['Jan','Feb','M&auml;r','Apr','Mai','Jun','Jul','Aug','Sep','Okt','Nov','Dez'],
			dayNames: ['Sonntag','Montag','Dienstag','Mittwoch','Donnerstag','Freitag','Samstag'],
			dayNamesShort: ['So','Mo','Di','Mi','Do','Fr','Sa'],
			dayNamesMin: ['So','Mo','Di','Mi','Do','Fr','Sa'],
			weekHeader: 'KW',
			dateFormat: 'dd.mm.yy',
			firstDay: 1,
			isRTL: false,
			showMonthAfterYear: false,
			yearSuffix: '',
			showOn:'focus',
			showStatus: true, 
			showWeek: true, 
			highlightWeek: true,  
			numberOfMonths: 1, 		
			changeYear: true,
			changeMonth: true,
			showOtherMonths: true,
			yearRange: '2004:'+(Jahr+1),
			//showAnim: "scale"
			};
		$.datepicker.setDefaults($.datepicker.regional['de']);
		
		$('.datepicker').live('click', function() {
			$(this).datepicker().focus();
		});
						   
	$(".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;});						   
	
	$.nyroModalSettings({
		endShowContent: function(elts) {
		  $('input:text:first', elts.content).focus();
		},
		endRemove: function(elts, settings) {
			if ($(settings.from).attr('rev')){
			if ($(settings.from).attr('rev').match(/reload/)){
				tempUrl = $(settings.from).attr('rev').split(':');
				//console.log(tempUrl[1]);
         			 if(tempUrl.length>1){
					 top.window.location.href = "http://" + top.window.location.hostname + "/" + tempUrl[1];
					}else{
					 top.window.location.href = top.window.location;
					}
			}
			}
		}
	});
	
					   
	$("#open_pn").click(function (e) {
		if(isOpen){ 
			isOpen.hide();
			isOpenWK = false;isOpenMK = false; isOpenKal = false;
		}
		if(isOpenPN){  // Nachrichten Fenster ist eingeblendet
			isOpenPN = false;
			isOpen = false;
			$("#nachrichten").hide();
			$(this).removeClass("fix_link_aktiv");
		}else{ // Nachrichten Fenster ist ausgeblendet (Standard)
			isOpenPN = true;
			isOpen = $("#nachrichten");	
			$(".fix_link").removeClass("fix_link_aktiv"); 
			$(this).addClass("fix_link_aktiv");
			$("#nachrichten").show();
			$.get(HTTP_SERVER + "ajaxcontent/mini_pn.php", function(data){
				$("#nachrichten").html(data);
			});
		}
		e.stopPropagation(); 
	});
	
	
	$("#open_wk").click(function (e) {
		if(isOpen){ 
			isOpen.hide();
			isOpenPN = false; isOpenMK = false; isOpenKal = false;
		}
		if(isOpenWK){ 
			isOpenWK = false;
			isOpen = false;
			$("#warenkorb").hide();
			$(this).removeClass("fix_link_aktiv");
		}else{ 
			isOpenWK = true;	
			isOpen = $("#warenkorb");	
			$(".fix_link").removeClass("fix_link_aktiv"); 
			$(this).addClass("fix_link_aktiv");
			$("#warenkorb").show();
			$.get(HTTP_SERVER + "ajaxcontent/mini_wk.php", function(data){
				$("#warenkorb").html(data);
			});
		}
		e.stopPropagation(); 
	});
	
	
	$("#open_mk").click(function (e) {
		if(isOpen){ 
			isOpen.hide();
			isOpenPN = false; isOpenWK = false; isOpenKal = false;
		}
		if(isOpenMK){ 
			isOpenMK = false;
			isOpen = false;
			$("#mein_konto").hide();
			$(this).removeClass("fix_link_aktiv");
		}else{ 
			isOpenMK = true;	
			isOpen = $("#mein_konto");	
			$(".fix_link").removeClass("fix_link_aktiv"); 
			$(this).addClass("fix_link_aktiv");
			$("#mein_konto").show();
		}
		e.stopPropagation();
	});
	
	$("#open_kal").click(function (e) { 
		if(isOpen){ 
			isOpen.hide();
			isOpenPN = false; isOpenWK = false; isOpenMK = false;
		}
		if(isOpenKal){ 
			isOpenKal = false;
			isOpen = false;
			$("#kalenderview").hide();
			$(this).removeClass("fix_link_aktiv");
		}else{ 
			isOpenKal = true;	
			isOpen = $("#kalenderview");	
			$(".fix_link").removeClass("fix_link_aktiv"); 
			$(this).addClass("fix_link_aktiv");
			$("#kalenderview").show();
			getKalender();
		}
		e.stopPropagation();
	});
	
	$("#kalenderview, #mein_konto, #warenkorb, #nachrichten").click(function (e) { 
		e.stopPropagation();
	});
	
	$(document.body).click(function(){     
		if(isOpen){ 
			isOpen.hide();
			$(".fix_link").removeClass("fix_link_aktiv"); 
			isOpenPN = false; isOpenWK = false; isOpenMK = false; isOpenKal = false;
		}
	});
	
	var WK_Act_url = HTTP_SERVER + "includes/act_wk.php";
	ActWK = function() {
		$.get(WK_Act_url, function(data){
			$("#wk_anz1").html(data);
		});
	}
	
	var Act_MR_url = HTTP_SERVER + "includes/act_module_rechts.php";
	ActMR = function(typ,action) {
		$.get(Act_MR_url, {module: ""+action+"",typ: ""+typ+""}, function(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("");
    }
  );


	$(".select_alternate_24, .select_alternate").live('click', function () {
		$(this).next().toggle();
	});

	$("#select_suchkatid").blur(function () {
		$('#suchkatid_content').fadeOut();
	});
	
	function LiveSuche() {
	var inputString = $("#search").attr("value");
		if(inputString.length <= 1) {
			$('#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(function(){
	  	StartSuche();
	  });
	  
	var timer = null;
	  function StartSuche(){
		if(timer) clearTimeout(timer);
		timer = setTimeout(LiveSuche, 150);
	  }
	  
	
	var get_amazon_url = HTTP_SERVER + "includes/getAmazon.php"; // The server-side script 
	getAmazon = function(Keywords,SearchIndex, ItemPage, div, NumberOfItems) {
		$.get(get_amazon_url, { Keywords: ""+Keywords+"", SearchIndex: ""+SearchIndex+"", ItemPage: ""+ItemPage+"", div: ""+div+"", NumberOfItems: ""+NumberOfItems+"" },
		  function(data){
			$('#'+div).html(data);
		  });
	}
  
});
