// JavaScript Document
var HTTP_SERVER = "http://www.blue-time.de/hs-woche";

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

function ask(asktext){
    return confirm(asktext);
}
function update_ns() {
	$.get("ajax/ns_count_unread.php", function(data){
		darr = data.split("_");
		if ( darr[1] > 0 ) {
			$( "#dialog" ).dialog( "open" );
		}
		$("#ns_mail_notify").html(darr[0]);
		$("#ns_eingang_count").html("("+darr[0]+"/"+darr[2]+")");
		$("#ns_gesendet_count").html("("+darr[3]+")");
	})
}

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

var NyroOptions = { // NyromodalOptionen
	galleryLoop: true,
	// 2.0
	//debug: true,
	//header: '<div class="nyroModalHeader"></div>',
	callbacks: {
		beforeShowCont: function(nm) {
			if(nm.store.title){
				if(nm.loadFilter == "image"){ 
					//nm.elts.cont.after('<div class="nyroModalHeader"></div>');
					//debug(nm.store.title.html());
					//var nyroTitle = nm.store.title.html();
					//$(".nyroModalHeader").html(nyroTitle).addClass("hiker");
				}else{
					nm.store.title.remove();
				}
			}
		},
		afterShowCont: function(nm) {
			$('input:text:first', nm.elts.cont).focus();
			//console.log(nm.opener.attr('rev'));
			/*$('.resizeLink', nm.elts.cont).click(function(e) {
				e.preventDefault();
				nm.sizes.initW = Math.random()*1000+200;
				nm.sizes.initH = Math.random()*1000+200;
				nm.resize();
			});*/
		},
		beforeShowBg: function(nm){ 
			//$('#nyroModalWrapper').draggable({ handle: '.h2_weiss, .handle, .drag' });
			$.tooltip.blocked=false; $('#tooltip').fadeOut(100); $.tooltip.handle;
			if(nm.opener.hasClass("fileUpload")) $(".nyroModalLoad").addClass("fileUpload"); else  $(".nyroModalLoad").removeClass("fileUpload");
		},
		afterClose: function(nm) {
			
			nm_md = nm.opener.metadata();
			
			if(nm.opener.attr('rev') && $(".nyroModalStack").length == 0 ){
				if(nm.opener.attr('rev').match(/reload/)){
					var tempUrl = nm.opener.attr('rev').split(':');
					if(tempUrl.length>1){
						top.window.location.href = "http://" + top.window.location.hostname + "/" + tempUrl[1];
					}else{
						top.window.location.href = top.window.location;
					}
				}						
			}
			
			$(".stackreload").each(function(index, element) {
                var md = $(element).metadata();
				if ( !md.ajaxdata ) {
					md.ajaxdata = {};
				}
				if ( md.ajaxurl ) {
					$.post(''+md.ajaxurl+'', md.ajaxdata,  function( data ) {
						$(element).html(data);
					});
				}
            });
			
		}
	}
};

$(document).ready(
	function () {
		
	$(".loeschenLink").click(function(e) {
		/* INFO:
			Verwendung von Metadata auf Klasse .loeschenLink
			msgHeader: Dies steht im Dialog als Überschrift
			msgCont: Inhalt des Dialogs
			ajax: kompletter pfad zur Ajaxdatei
			id: zu löschender datensatz
			berID: Bereich ID ( für Table )
			del: Löschen oder Update 1 = löschen
			delElement: Die eventuell abgeänderte Id deszu löschenden Elements ( immer delElement_ID )
		*/
		var t = $(this);
		var md = t.metadata();
		
		var d;
		if ( md.del ) {
			d = md.del;
		} else {
			d = "0";
		}
		
		var berID;
		if ( md.del ) {
			berID = md.berID;
		} else {
			berID = "0";
		}
		
		var msgHeader;
		if ( md.msgHeader ) {
			msgHeader = md.msgHeader;
		} else {
			msgHeader = "";
		}

		if ( md.msgCont ) {
			$("#dialog-confirm").html(md.msgCont);
		}
			
		$( "#dialog-confirm" ).dialog({
			resizable: false,
			height:140,
			modal: true,
			autoOpen: false,
			title: msgHeader,
			dialogClass: 'dialog_delete',  
			close: function(event,ui) {
				$("#dialog-confirm").html("");
			},
			buttons: {
				"Ja": function() {
					$( this ).dialog( "close" );
	
					if ( md.ajax && md.id && !isNaN(md.id) && md.id > 0 ) {
						$.post(''+md.ajax+'', { id: ''+md.id+'', bid: ''+berID+'', del: ''+d+'' }, function(data) {
							if ( data == "0" ) {
									var delObjId;
									if ( md.delElement ) {
										delObjId = md.delElement+"_"+md.id;
									} else {
										delObjId = "delElement_"+md.id;
									}
									debug( delObjId );
									$("#"+delObjId).fadeOut( function() {
										$(this).remove();
									});
							}
						});
					}
				},
				"Nein": function() {
					$(this).dialog( "close" );
				}
			}
		});
		$("#dialog-confirm" ).dialog( "open" );
		return false;
	});		
		
		$(".tooltip").dblclick($.tooltip.block);
		$(".tooltip").tooltip({	bodyHandler: function() {  return $(this).next().html();},track: true, 	delay: 0, showURL: false	});
		$(".tt_header").click(function(){$.tooltip.blocked=false;$('#tooltip').fadeOut(100);$.tooltip.handle;});
		
		$.tablesorter.addParser({         
			id: 'datum',         
			is: function(s) { return false; },   // return false so this parser is not auto detected                   
			format: function(s) { return s.replace(new RegExp(/(\d{1,2})[\.](\d{1,2})[\.](\d{4})/), "$3$2$1"); },         
			type: 'numeric' // set type, either numeric or text           
		});
		$.tablesorter.addParser({         
			id: 'title_text',         
			is: function(s) { 	return false; },    
			format: function(value, table, cell) { 	return $(cell).attr('title'); 	},         
			type: 'text' 
		});
		$.tablesorter.addParser({         
			id: 'title_num',         
			is: function(s) { 	return false; },    
			format: function(value, table, cell) { 	return $(cell).attr('title'); 	},         
			type: 'numeric' 
		});
		$.tablesorter.addParser({         
			id: 'id_num',         
			is: function(s) { return false; },         
			format: function(value, table, cell) { var id = $(cell).attr('id').split("_");	return id[1]; },         
			type: 'numeric' 
		});
		$.tablesorter.addParser({         
			id: 'id_text',         
			is: function(s) { return false; },         
			format: function(value, table, cell) { var id = $(cell).attr('id').split("_");	return id[1]; },         
			type: 'text' 
		});
		$.tablesorter.addWidget({
			// give the widget a id
			id: "fixHeader",
			// format is called when the on init and when a sorting has finished
			format: function(table) { 
				var t = $(table);
				if(!t.hasClass("NoFixHeader")){
					var header;
					$(document).bind("ready resize", function() {
						 $("th",table).each(function(index, element) {
						   $(element).css("width",$(element).width());
						 });
						 
						 var thead = $('thead', table).html();
						 if($('#fix_div').length == 0) $('<div id="fix_div"><\/div>').appendTo("body").hide();
						 
						 header = '<table style="'+t.attr("style")+'" class="sortable"><thead>'+thead+'<\/thead><\/table>';
						 $('#fix_div').html(header);					
					});
					$(window).scroll(function(){
						var table_top = t.offset();
						var window_top = $(window).scrollTop();
						if (table_top.top <= window_top) {
							$('#fix_div').css({position:'fixed', top:'-10px', left: (table_top.left-10)+'px'}).html(header).show();
						} else {
							$('#fix_div').hide();
						}
					});
				}
			}
		});		
		$(".sortable").tablesorter({
			widgets: ['fixHeader']
		});	
		
		/*$.nyroModalSettings({
			bgColor: "#990000",
			endRemove: function(elts, settings) {
				//debug($(settings.from));
				if ($(settings.from).attr('rev')){
					if ($(settings.from).attr('rev').match(/reload/)){
						var tempUrl = $(settings.from).attr('rev').split(':');
						if(tempUrl.length>1){
							top.window.location.href = "http://" + top.window.location.hostname + "/" + tempUrl[1];
						}else{
							top.window.location.href = top.window.location;
						}
					}
				}	
				if ($(settings.from).hasClass('nyroReload')){
					if ($(settings.from).attr('rev').match(/reload/)){
						top.window.location.href = top.window.location;
					}
				}					
			}
		});*/
		
		$('.nyroModal').nyroModal(
			NyroOptions
		);
		//$.nmInternal({debug: true});
		
		$("#clear_suche").click(function () {
			$('#suchergebnis').fadeOut();
			$('.suchtext').attr("value","");
		});
		
        function LiveSuche() {
            var inputString = $(".suchtext").attr("value");
            if(inputString.length <= 1) {
                $('#suchergebnis').fadeOut(); // Hide the result box
            } else {
                var suchkat = $("#suchkat").attr("value");
                $.get(HTTP_SERVER+"/includes/suche.php", {suchkat: ""+suchkat+"",suche: ""+inputString+""}, function(data) { // Do an AJAX call
                $('#suchergebnis').fadeIn(); // Show the result box
                $('#suchergebnis').html(data); // Fill the result box
                });
            }
        }
		
        $(".select_alternate_24").click(function () {
            $(this).next().toggle();
        });
        
        $(".select_alternate_24").blur(function () {
			$(this).next().fadeOut();
        });
        
        $("#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();
        });
    
      $(".suchtext").keyup(LiveSuche);
	   
	   var Strassen_url = "http://"+ window.location.hostname +"/hs-woche/includes/strassen.php";
		hole_strassen = function (stadt, strasse) {
		  
			$('#strassen').html("<img src='http://"+ window.location.hostname +"/hs-woche/images/loading.gif' alt='wird geladen' />");
			
			$.get(Strassen_url, {stadt: ""+stadt+"", strasse: ""+strasse+""}, function(data){
				$('#strassen').html(data);
				setTimeout("setFocus()", 200);
			});
		}
		
		setFocus = function () {
			$('#strasse').focus();	
		} 
	
});	
