/*-----------------------------------------
 
 copyright 2009 by mmc
 
 Version: 1.0
 
 Author: Georg Paul, Philipp Bergsmann
 
 Author URI: http://www.mmc-agentur.at
 
 -------------------------------------------*/

/*
try {
	document.execCommand("BackgroundImageCache", false, true);
} catch(err) {}
*/


 $(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();
	
	// set bookmark links
	$('#sbm_delicious').attr('href', 'http://del.icio.us/post?url='+escape(location.href));
	$('#sbm_twitter').attr('href', 'http://twitter.com/home?status='+encodeURIComponent('führerscheinweg.at: Nachschulung, Verkehrspsychologische Untersuchung, Führerscheinentzug '+location.href));
	$('#sbm_facebook').attr('href', 'http://www.facebook.com/sharer.php?u='+escape(location.href));
	$('#sbm_stumbleupon').attr('href', 'http://www.stumbleupon.com/submit?url='+escape(location.href));
});
 //  #'+this.attr('rel')+'alert($(this).attr('rel'));
 
 function changeSelects() {
	$("#geo_content_wrap").find("form").each(function()
	{
		formID = $(this).attr("id");
		MMCsetFormAction(formID);
		$('#geo_content_wrap select').bind('change',function() {
				//alert($(this).parents().find('form').attr('id'));
				MMCsetFormAction($(this).parents().find('form').attr('id'));
		});
	});
	return false;
 }
 
 function JSForm() {
 	$('#geo_content_wrap form').bind('submit',function() {
		var formID = $(this).attr('id');
		window.baseURL = window.baseURL.replace(/at\/\//,'at/');
		window.location.href= window.baseURL+$('#' + formID).attr('action');
		return false;
	});
 }
 
 function MMCsetFormAction(formID) {
 	var part = new Array(3);
	part[0] = convertToRealURL($('#' + formID + ' .jquery-selectbox-currentItem:first').text());
	part[1] = convertToRealURL($('#' + formID + ' .step_2_select').val());
	part[2] = convertToRealURL($('#' + formID + ' .jquery-selectbox-currentItem:last').text());
	//part[2] = convertToRealURL($('#' + formID + ' .step_3_select').val());
	
	preVar = ($('#geo_content_1').hasClass('inactive')) ? 'Verkehrspsychologische-Untersuchungen' : 'Nachschulungskurse';
	MyLocation = preVar + '/Kurstermine/' + part[0] + '/' + part[1] + '/' + part[2];
	//alert(MyLocation);
	$('#' + formID).attr('action',MyLocation);
 }
 
 function convertToRealURL(text) {
	return text.replace(/\ /g,'-');
 }
 
 function fixSelects() {
	//alert(window.location.hostname);
	if ($('#geo_content_1').hasClass('inactive')) {
		var kursType = 'VPU';
	} else {
		var kursType = 'NSD';
	}
 	$('#geo_content_wrap .step_1_select').unbind('change');
	$('#geo_content_wrap .step_1_select').bind('change', 
		function() {
			$('#geo_content_wrap .step_2_select').load(
				'http://'+window.location.hostname+'/typo3conf/ext/mmc_fsw/pi3/res/ajax.php',
				{
					'bundesland':$('#geo_content_wrap #'+$(this).attr('rel')+ ' .step_1_select').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!");
	}
}

var addthis_config =
{
  services_exclude: 'facebook,twitter,delicious,email,print,stumbleupon',
  ui_click: true
}