var showProject;

$(document).ready(function() {

   showProject = function(prjnr) {
		// $('#Projekt').show();
		$('#Projekt').load('projekt.php?nr='+prjnr);
		// var api = jQuery("ul#MenuTabs").data("tabs"); 
		// api.click('#Projekte'); 
		// var api = jQuery("ul#MenuProjekteTabs").data("tabs"); 
		// api.click('#ShowProjekt'); 
		// $('#t43').replaceWith('<a id=\'t43\' href=\'#ShowProjekt\'>'+prjname+'</a>');
	};

	$("ul.tabs").tabs("> .pane", {effect: 'fade', fadeOutSpeed: 200, history: true });
	$("ul.submenutabs").tabs("> .submenupane", {effect: 'fade', fadeOutSpeed: 200});
	
	$(".hiddentab").hide();
	
	
	$("#t42").bind('click', function() {
		$('#ProjekteAufKarte').load('projekt_karte.php');
	});
	
	
	$("#myform").validate({
		messages: {	vorname: "Dieses Feld ist ein Pflichtfeld.",
					nachname: "Dieses Feld ist ein Pflichtfeld.",
					email: "Ungültige Email- Adresse."
					},
		submitHandler: function(form) {
				// do other stuff for a valid form
				$.post('process.php', $("#myform").serialize(), function(data) {
					$('#results').html(data);
				});
			}
		});
		

/*
	$(".HomePageImage img[rel]").overlay({effect: 'apple'});

	$("a.PrjLink").bind('click', function() {
		$('#t43.hiddentab').show();
	});
	
*/
	
});


/* Datatables */
/* ******************************************************************************************* */

(function($) {
/*
 * Function: fnGetColumnData
 * Purpose:  Return an array of table values from a particular column.
 * Returns:  array string: 1d data array 
 * Inputs:   object:oSettings - dataTable settings object. This is always the last argument past to the function
 *           int:iColumn - the id of the column to extract the data from
 *           bool:bUnique - optional - if set to false duplicated values are not filtered out
 *           bool:bFiltered - optional - if set to false all the table data is used (not only the filtered)
 *           bool:bIgnoreEmpty - optional - if set to false empty values are not filtered from the result array
 * Author:   Benedikt Forchhammer <b.forchhammer /AT\ mind2.de>
 */
$.fn.dataTableExt.oApi.fnGetColumnData = function ( oSettings, iColumn, bUnique, bFiltered, bIgnoreEmpty ) {
	// check that we have a column id
	if ( typeof iColumn == "undefined" ) return new Array();
	
	// by default we only wany unique data
	if ( typeof bUnique == "undefined" ) bUnique = true;
	
	// by default we do want to only look at filtered data
	if ( typeof bFiltered == "undefined" ) bFiltered = true;
	
	// by default we do not wany to include empty values
	if ( typeof bIgnoreEmpty == "undefined" ) bIgnoreEmpty = true;
	
	// list of rows which we're going to loop through
	var aiRows;
	
	// use only filtered rows
	if (bFiltered == true) aiRows = oSettings.aiDisplay; 
	// use all rows
	else aiRows = oSettings.aiDisplayMaster; // all row numbers

	// set up data array	
	var asResultData = new Array();
	
	for (var i=0,c=aiRows.length; i<c; i++) {
		iRow = aiRows[i];
		var aData = this.fnGetData(iRow);
		var sValue = aData[iColumn];
		
		// ignore empty values?
		if (bIgnoreEmpty == true && sValue.length == 0) continue;

		// ignore unique values?
		else if (bUnique == true && jQuery.inArray(sValue, asResultData) > -1) continue;
		
		// else push the value onto the result data array
		else asResultData.push(sValue);
	}
	
	return asResultData;
}}(jQuery));

function fnCreateSelect( aData )
{
	var r='<select><option value=""></option>', i, iLen=aData.length;
	for ( i=0 ; i<iLen ; i++ )
	{
		r += '<option value="'+aData[i]+'">'+aData[i]+'</option>';
	}
	return r+'</select>';
}


var asInitVals = new Array();

$(document).ready(function() {

	/* Initialise the DataTable */
	var oTable = $('#projects').dataTable( {
		"oLanguage": {
			"sSearch": "Search all columns:",
			"sUrl": "/inc/DataTables/de_DE.txt"
		},
		"aaSorting": [[5,'desc'], [1,'asc']],
		"bAutoWidth": false,
		"bSortCellsTop": true,
		"bSortClasses": false,
		"sPaginationType": "full_numbers",
		"aoColumnDefs": [ 
			{ "bSortable": false, "aTargets": [ 0, 4 ] },
			{ "bVisible": false, "aTargets": [ 5 ] } ],
		"sDom": '<"top"li>rt<"bottom"lp>',
		"bStateSave": true,
		"fnInitComplete": function() {
				var oSettings = this.fnSettings();
				for ( var i=0 ; i<oSettings.aoPreSearchCols.length ; i++ ){
					if(oSettings.aoPreSearchCols[i].sSearch.length>0){
						$("thead input")[i-1].value = oSettings.aoPreSearchCols[i].sSearch;
						$("thead input")[i-1].className = "";
					}
				}
			}

		
	} );


	$("thead input").keyup( function () {
		/* Filter on the column (the index) of this element */
		oTable.fnFilter( this.value, $("thead input").index(this)+1 );
	} );

	$("thead input").each( function (i) {
		asInitVals[i] = this.value;
	} );
	
	$("thead input").focus( function () {
		if ( this.className == "search_init" )
		{
			this.className = "";
			this.value = "";
		}
	} );
	
	$("thead input").blur( function (i) {
		if ( this.value == "" )
		{
			this.className = "search_init";
			this.value = asInitVals[$("thead input").index(this)];
		}
	} );


	/* Add a select menu in the table header where id = dataselect */
	$("thead td").each( function ( i ) {
	  		if($(this).is('.dataselect'))
	  		{
				this.innerHTML = fnCreateSelect( oTable.fnGetColumnData(i) );
				$('select', this).change( function () {
				oTable.fnFilter( $(this).val(), i );
				} );
	  		}
	
	});
	

} );


// JavaScript Document
