/*-----------------------------------------
 
 copyright 2009 by mmc
 
 Version: 1.0
 
 Author: Georg Paul
 
 Author URI: http://www.mmc-agentur.at
 
 -------------------------------------------*/



$(document).ready(function(){
	
	/* ************************ */
	// KURSTERMINE 
	/* ************************ */	
		
	// Defaultmäßig nur den ersten Kurs aufklappen
		$(".course_facts").css("display", "none");
		$('.course_dates .active .course_facts').show();
		$("#course_dates li:first .course_facts").css("display", "block");
	
	// "active"-Klasse vergeben
		$("#course_dates li:first").addClass("active");
	
	// "last"-Klasse vergeben
		$("#course_dates li:last").addClass("last");
	
	
	// Click Function für jeden Kurslink setzen
		$("#course_dates").find("li").each(function(){
			var itemID = $(this).attr("id");
			courseClickFn(itemID);
		});		


	/* ************************ */
	// SONSTIGES 
	/* ************************ */
	
	// alle Input Values bei Fokus leeren und bei blur wieder reseten
		/*resetInputValue($("#search_field").attr("id"));
		resetInputValue($("#fullname").attr("id"));
		resetInputValue($("#tel").attr("id"));*/
		
		formDefaults();
		
		
	// 1. Teaser die Klasse "first" geben
	$(".teaserwrap:first").addClass("first");
});


function formDefaults() {
	$('input, textarea').each(function() {
		if ($(this).val()=='') {
			$(this).val($(this).attr('title'));
		}
	});
	$('input, textarea').bind('focus', function() {
		if ($(this).val() == $(this).attr('title')) {
			$(this).val('');
		}
	});
 
	$('input, textarea').bind('blur', function() {
		if ($(this).val() == '') {
			$(this).val($(this).attr('title'));
		}
	});
}



/* ************************ */
// FUNCTIONS - KURSTERMINE 
/* ************************ */

function courseClickFn(itemID)
{
		$("#" + itemID + " .location a").click(function(event){
		
		// don't go to href="#"
		event.preventDefault();
		
		// only make slide action for non-active Elements
		if ($("#" + itemID).attr("class") != "active") {
											
			// hide old content										
			$("#course_dates .active .course_facts").slideUp("normal", function(){
								
				// prev active Element is now UP
				$("#course_dates .active").removeClass("active");
				
				// give only the clicked Element the class "active
				$("#" + itemID).addClass("active");
				
				// refresh the styling of the active element
				refreshStyles();	
				
				// show new content with a little delay
				window.setTimeout("showNewContent('" + itemID + "')", 250);				
			
			});
		}
	});
}

function refreshStyles()
{	

}

function showNewContent(itemID)
{
	//alert(itemID);
	showAddress($('#'+itemID+' h2 .addr').text()+', austria'); // Google Maps
	showTaxi($('#'+itemID+' h2 .addr').text()); // Google Taxi
	setAnfahrt($('#'+itemID+' h2 .addr').text()); // Anfahrt
	$("#" + itemID + " .course_facts").slideDown("fast");
}




/* ************************ */
// OTHER FUNCTIONS 
/* ************************ */

function resetInputValue(inputID) {
	
	var defaultValue = $("#" + inputID).val();
	
	// reset
	$("#" + inputID).bind("focus", function(){
		$(this).val("");
	})
	
	// wiederherstellen
	$("#" + inputID).bind("blur", function(){
		$(this).val(defaultValue);
	})
	
}

/**
 * FSW FUNKTIONEN
 */


$(document).ready(function(){
 	// set the input values blank and fill it again on blur
	for (var i = 0; i < field_id_array.length; i++) {
		setInputValues(field_id_array[i]["field_id"], field_id_array[i]["field_value"]);
	}
	
	// hide submit buttons for js-fallback
	$('#geo_content_wrap .fallback').css('display','none');
	

	// apply ajax-functions to geo search
	// Nachschulungskurse

	// jQuery select boxen
	fixSelects()

	JSForm();
	changeSelects();
});
 //  #'+this.attr('rel')+'alert($(this).attr('rel'));
 
 function changeSelects() {
	$(".tx-mmcfsw-pi3").find("form").each(function()
	{
		var formID = $(this).attr("id");
		MMCsetFormAction(formID);
		$('.tx-mmcfsw-pi3 select').bind('change',function() {
				//alert($(this).parents().find('form').attr('id'));
				MMCsetFormAction($(this).parents().find('form').attr('id'));
		});
	});
	return false;
 }
 
 function JSForm() {
 	$('.tx-mmcfsw-pi3 form').bind('submit',function() {
		var formID = $(this).attr('id');
		window.location.href= window.baseURL+$('#' + formID).attr('action');
		return false;
	});
 }
 
 function MMCsetFormAction(formID) {
 	var part = new Array(3);
	part[0] = convertToRealURL($('#' + formID + ' #land option:selected').text());
	part[1] = convertToRealURL($('#' + formID + ' #city').val());
	//part[2] = convertToRealURL($('#' + formID + ' .jquery-selectbox-currentItem:last').text());
	preVar = ($('#geo_content_1').hasClass('inactive')) ? 'Verkehrspsychologische-Untersuchungen' : 'Nachschulungskurse';
	//MyLocation = preVar + '/Kurstermine/' + part[0] + '/' + part[1] + '/' + part[2];
	MyLocation = 'Kurstermine/' + part[0] + '/' + part[1];
	//alert(MyLocation);
	//alert(MyLocation);
	$('#' + formID).attr('action',MyLocation);
 }
 
 function convertToRealURL(text) {
	//return text.replace(/\ /g,'-');
	return encodeURIComponent(text);
 }
 
 function fixSelects() {
	//alert(window.location.hostname);
	if ($('#geo_content_1').hasClass('inactive')) {
		var kursType = 'VPU';
	} else {
		var kursType = 'NSD';
	}
 	$('.tx-mmcfsw-pi3 #land').unbind('change');
	$('.tx-mmcfsw-pi3 #land').bind('change', 
		function() {
			$('.tx-mmcfsw-pi3 #city').load(
				'http://'+window.location.hostname+'/typo3conf/ext/mmc_fsw/pi3/res/ajax.php',
				{
					'bundesland':$('.tx-mmcfsw-pi3 #land').attr('value'),
					'rand':Math.random(),
					'type':kursType
				},
				function () {
					restoreSelects();
					MMCsetFormAction($(this).parents().find('form').attr('id'));
				}
			);
		}
	);
	restoreSelects();
 }
 
 function restoreSelects() {
	$("#geo_content_wrap select").parents('.jquery-selectbox').unselectbox();
	$("#geo_content_wrap select").selectbox();
 }
 
 /* sollten Inputfeldern dazukommen, muss dieses Array hier ergänzt werden */
var field_id_array = new Array();

field_id_array[0] = new Array();
field_id_array[0]["field_id"] = "#fullname";
field_id_array[0]["field_value"] = "Ihr Name";

field_id_array[1] = new Array();
field_id_array[1]["field_id"] = "#tel";
field_id_array[1]["field_value"] = "Telefonnummer";

function setInputValues(field_id, field_value)
{
	$(field_id).click(function(){
		this.value = "";
	});
	$(field_id).blur(function(){
		if (this.value == "") {
			this.value = field_value;
		}
	});
}

function bookmark_site(die_bookmark_url, der_bookmark_text)  {
	if (navigator.appName == 'Microsoft Internet Explorer' && parseInt(navigator.appVersion) >= 4) {
		window.external.addFavorite(die_bookmark_url, der_bookmark_text)
	}
	else {
		alert("Diese Funktion ist leider nur mit Microsoft Internet Explorer möglich!");
	}
}

