
function slideshow() {
  var $alt = $('#diastaple img.toplevel');

  if ($alt.next().length)
    var $neu = $alt.next();
  else
    var $neu = $('#diastaple img:first');

  $alt.addClass('inactive');
  $alt.removeClass('toplevel');
  $neu.css({opacity: 0.0});
  $neu.addClass('toplevel');

  $neu.animate({opacity: 1.0}, 6000, function() {
    $alt.removeClass('inactive');
  });
}

var oTable;
var asInitVals = new Array();

$(document).ready(function() {
	if ($('.contenttable.display')) {
		oTable = $('.contenttable.display').dataTable(
			{
				"oLanguage": {
					"sSearch": "Alle Publikationen durchsuchen:",
					"sInfo": "Publikationen _START_ bis _END_ von insgesamt _TOTAL_ werden angezeigt.",
					"sZeroRecords": "Keine entsprechenden Einträge gefunden",
					"sInfoEmpty": "Einträge 0 bis 0 von 0 Einträgen",
					"sInfoFiltered": "(Gefiltert von insgesamt _MAX_ Einträgen)",
					"oPaginate": {
						"sFirst":    "Erste",
						"sPrevious": "Vorherige",
						"sNext":     "Nächste",
						"sLast":     "Letzte"
					}
				},
				"aaSorting": [[ 3, "desc" ], [ 0, "asc" ]],
				"aoColumns": [ null, null, null, null, null ],
				"bLengthChange": false,
		        "bAutoWidth": false
			}
		);
	}
	if ($('.display_courses')) {
		oTable = $('.display_courses').dataTable(
			{
				"oLanguage": {
					"sSearch": "Alle Kurse durchsuchen:",
					"sInfo": "Kurse _START_ bis _END_ von insgesamt _TOTAL_ werden angezeigt.",
					"sZeroRecords": "Keine entsprechenden Einträge gefunden",
					"sInfoEmpty": "Einträge 0 bis 0 von 0 Einträgen",
					"sInfoFiltered": "(Gefiltert von insgesamt _MAX_ Einträgen)",
					"oPaginate": {
						"sFirst":    "Erste",
						"sPrevious": "Vorherige",
						"sNext":     "Nächste",
						"sLast":     "Letzte"
					}
				},
				"aaSorting": [],
				"aoColumns": [ null, null, null, null, null, null ],
				"bLengthChange": false,
		        "bAutoWidth": false
			}
		);
	}
	if ($('.display_courseslist')) {
		oTable = $('.display_courseslist').dataTable(
			{
				"oLanguage": {
					"sSearch": "Alle Kurse durchsuchen:",
					"sInfo": "Kurse _START_ bis _END_ von insgesamt _TOTAL_ werden angezeigt.",
					"sZeroRecords": "Keine entsprechenden Einträge gefunden",
					"sInfoEmpty": "Einträge 0 bis 0 von 0 Einträgen",
					"sInfoFiltered": "(Gefiltert von insgesamt _MAX_ Einträgen)",
					"oPaginate": {
						"sFirst":    "Erste",
						"sPrevious": "Vorherige",
						"sNext":     "Nächste",
						"sLast":     "Letzte"
					}
				},
				"aaSorting": [],
				"aoColumns": [ null, null, null, null, null ],
				"bLengthChange": false,
		        "bAutoWidth": false
			}
		);
	}
	if ($('.contenttable.display_doclist')) {
		oTable = $('.contenttable.display_doclist').dataTable(
			{
				"oLanguage": {
					"sSearch": "Alle Einträge durchsuchen:",
					"sInfo": "Einträge _START_ bis _END_ von insgesamt _TOTAL_ werden angezeigt.",
					"sZeroRecords": "Keine entsprechenden Einträge gefunden",
					"sInfoEmpty": "Einträge 0 bis 0 von 0 Einträgen",
					"sInfoFiltered": "(Gefiltert von insgesamt _MAX_ Einträgen)",
					"oPaginate": {
						"sFirst":    "Erste",
						"sPrevious": "Vorherige",
						"sNext":     "Nächste",
						"sLast":     "Letzte"
					}
				},
				"aaSorting": [[ 0, "asc" ]],
				"aoColumns": [
					{ "sWidth": "50px" },
					{ "sWidth": "120px" },
					null
				],
				"bLengthChange": false,
				"bAutoWidth": false
			}
		);
	}

	if ($('.contenttable.display_plzlist')) {
		oTable = $('.contenttable.display_plzlist').dataTable(
			{
				"oLanguage": {
					"sSearch": "Alle Einträge durchsuchen:",
					"sInfo": "Einträge _START_ bis _END_ von insgesamt _TOTAL_ werden angezeigt.",
					"sZeroRecords": "Keine entsprechenden Einträge gefunden",
					"sInfoEmpty": "Einträge 0 bis 0 von 0 Einträgen",
					"sInfoFiltered": "(Gefiltert von insgesamt _MAX_ Einträgen)",
					"oPaginate": {
						"sFirst":    "Erste",
						"sPrevious": "Vorherige",
						"sNext":     "Nächste",
						"sLast":     "Letzte"
					}
				},
				"aaSorting": [[ 0, "asc" ]],
				"aoColumns": [
					{ "sWidth": "30px", "sClass": "center" },
					{ "sWidth": "70px", "sClass": "center" },
					null
				],
				"bLengthChange": false,
				"bAutoWidth": false
			}
		);
	}
	
	if ($("tfoot input")) {
		$("tfoot input").keyup( function () {
			//Filter on the column (the index) of this element
			oTable.fnFilter( this.value, $("tfoot input").index(this) );
		} );
		
	
		$("tfoot input").each( function (i) {
			asInitVals[i] = this.value;
		} );
	
		$("tfoot input").focus( function () {
			if ( this.className == "search_init" )
			{
				this.className = "";
				this.value = "";
			}
		} );
	
		$("tfoot input").blur( function (i) {
			if ( this.value == "" )
			{
				this.className = "search_init";
				this.value = asInitVals[$("tfoot input").index(this)];
			}
		} );
	}
		
		
	setInterval( "slideshow()", 10000 );
	   
	$(function() {
		function getCourseDate(year, month) {
            $.getJSON('fileadmin/php/kurstermine.php?year='+year+'&month='+month, function(data) {
                $.each(data.data, function(i,item) {
                	if (typeof item != "undefined") {
                		
                		$('.ui-datepicker-calendar td a:exactly('+item['d']+')')
                			.after('<div id="hoverpopupcal'+item['id']+'" style="visibility:hidden; position:absolute; color: #ffffff; background-color: #3399CC; padding: 10px 20px; width: 250px;"><p>'+data.data[i]['kurs']+' - '+data.data[i]['rubrik']+'</p><p>'+data.data[i]['beschreibung']+'</p></div>')
		                    .attr('href',item['link'])
		                    .attr('id', 'hoverover'+item['id'])
		                    .attr('class','ui-state-active')
		                    .mouseover(function() { ShowCalPopup(this, item['id']); } )
	                    	.mouseout(function() { HideCalPopup(item['id']) } );
                		}
                	
                });
                /*
                var i = 0;
                var calid;
                for (i = 0; i < data.data.length; i++)
                {
                	if (typeof data.data[i] != "undefined") {
                		calid = data.data[i]['id'];
                		$('.ui-datepicker-calendar td a:exactly('+data.data[i]['d']+')')
                			.after('<div id="hoverpopupcal'+data.data[i]['id']+'" style="visibility:hidden; position:absolute; color: #ffffff; background-color: #3399CC; padding: 10px 20px; width: 250px;"><p>'+data.data[i]['kurs']+' - '+data.data[i]['rubrik']+'</p><p>'+data.data[i]['beschreibung']+'</p></div>')
                			
		                    .attr('href',data.data[i]['link'])
		                    .attr('id', 'hoverover'+data.data[i]['id'])
		                    .attr('class','ui-state-active')
		                    .mouseover(function() { ShowCalPopup(this, calid); } )
	                    	.mouseout(function() { HideCalPopup(calid) } );
                		/*
	                    $('.ui-datepicker-calendar td a:exactly('+data.data[i]['d']+')')
		                    //.css({color: '#f00'})
		                    .attr('href',data.data[i]['link'])
		                    .attr('id', 'hoverover'+data.data[i]['id'])
		                    
		                    //.attr('onMouseOver', 'ShowCalPopup(this, '+data.data[i]['id']+')')
		                    .attr('onMouseOut', 'HideCalPopup('+data.data[i]['id']+')')
		                    
		                    
		                    
		                    //.onmouseout(HideCalPopup(data.data[i]['id']))
		                    
		                    .attr('class','ui-state-active')
//		                    .parent().append('<div id="hoverpopupcal'+data.data[i]['id']+'" style="visibility:hidden; position:absolute; color: #ffffff; background-color: #3399CC; padding: 10px 20px; width: 250px;"><p>'+data.data[i]['kurs']+' - '+data.data[i]['rubrik']+'</p><p>'+data.data[i]['beschreibung']+'</p></div>')
		                    .parent().attr('onclick','')
		                    .mouseover(ShowCalPopup(this, data.data[i]['id']));
		                    
	                    //$('.ui-datepicker-calendar td a:exactly('+data.data[i]['d']+')')
	                    	//.onmouseover(ShowCalPopup(this, data.data[i]['id']));
                	}
                }
				*/
            });
		}
		/*
		function getJsonDate(year, month) {
            $.getJSON('dates.php?year='+year+'&month='+month, function(data) {
                var i = 0;
                for (i = 0; i < data.data.length; i++)
                {
                    $('.ui-datepicker-calendar td a:exactly('+data.data[i]['d']+')')
                    .css({color: '#f00'})
                    .attr('href',data.data[i]['link'])
                    .parent().attr('onclick','');
                }
            });
        }
        */
        $.expr[":"].exactly = function(el, i, m) {
            var s = m[3];
            if (!s) return false;
            return eval("/^" + s + "$/i").test($(el).text());
        };
		
		$("#datepicker").datepicker({
			firstDay: 1,
			inline: true,
            onSelect: function(dateText, inst) {
                 Date.prototype.toString = function () {return isNaN (this) ? 'NaN' : [this.getDate(), this.getMonth(), this.getFullYear()].join('/')}
                 d = new Date(dateText);
                 getCourseDate(d.getFullYear(), d.getMonth()+1);
             },
             onChangeMonthYear: function(year, month, inst) {
                 //alert(year);
                 //alert(month);
            	 getCourseDate(year, month);
             }
			});
	});
	  
	if ($("#dialog")) {
		$("#dialog").dialog({
			bgiframe: true,
			height: 240,
			width: 340,
			modal: true
		});
	}
   
});

function ShowPopup(hoveritem, hoverid) {
	hp = document.getElementById("hoverpopup"+hoverid);
	hp.style.visibility = "Visible";
}

function HidePopup(hoverid) {
	hp = document.getElementById("hoverpopup"+hoverid);
	hp.style.visibility = "Hidden";
}


function ShowRightPopup(hoveritem, hoverid) {
	hp = document.getElementById("hoverpopupright"+hoverid);
	hp.style.visibility = "Visible";
}

function HideRightPopup(hoverid) {
	hp = document.getElementById("hoverpopupright"+hoverid);
	hp.style.visibility = "Hidden";
}

function ShowCalPopup(hoveritem, hoverid) {
	var id = "hoverpopupcal"+hoverid;
	var hp = document.getElementById(id);
	if (hp) {
		hp.style.visibility = "Visible";
	}
}

function HideCalPopup(hoverid) {
	var id = "hoverpopupcal"+hoverid;
	var hp = document.getElementById(id);
	if (hp) {
		hp.style.visibility = "Hidden";
	}	
}
