/*-----------------------------------------
 
 copyright 2009 by mmc
 
 Version: 1.0
 
 Author: Georg Paul, Philipp Bergsmann
 
 Author URI: http://www.mmc-agentur.at
 
 -------------------------------------------*/
$(document).ready(function(){
	//alert($('.course_dates li:first .addr').length);
	if ($('#course_dates li:first .addr').text()!='') {
		initialize();
		showAddress($('#course_dates li:first .addr').text()+', austria');
		showTaxi($('#course_dates li:first .addr').text());
		setAnfahrt($('#course_dates li:first .addr').text());
	}
	//$('#service_arrow').attr('z-index',999999);
	// set the bg-images (then it work's without having JS activated)
	//$("#service_bar #service_arrow").css("background", "url(fileadmin/system/assets/img/background/service_bar_arrow.gif) no-repeat left top");
	
	// active Elements need extra css for their borders
	//setBorderStyles();

	// only show the first course by default
	/*$(".course_dates li .course_facts").css("display", "none");
	$(".course_dates li:first .course_facts").css("display", "block");*/
	
	
	// set the position of the service bar
	//window.setTimeout("setTopMargin()", 1500);
	/*
	setTopMargin();
	
	courseArray = new Array();
	$(".course_dates").find("li").each(function(){
		courseArray.push(this);
		var itemID = $(this).attr("id");
		courseClickFn(itemID);
	});
	*/	
});

/*
function courseClickFn(itemID)
{
	$("#" + itemID + " h2 a").click(function(event){
		
		// don't go to href="#"
		event.preventDefault();
		//alert(itemID);
		// only make slide action for non-active Elements
		if ($("#" + itemID).attr("class") != "active") {
											
				// hide old content										
				$(".course_dates ul li.active .course_facts").slideUp("normal", function(){
								
				//$("#service_bar #service_arrow").css("background",'');		
				$("#service_bar #service_arrow").css('width',0);
						
				// prev active Element is now UP
				$(".course_dates ul li.active").removeClass("active");
				
				// give only the clicked Element the class "active
				$("#" + itemID).addClass("active");
				//alert("stop");
				
				// refresh the styling of the active element
				setBorderStyles();
				
				// refresh the position of the service bar
				setTopMargin();				
				
				// show new content with a little delay
				window.setTimeout("showNewContent('" + itemID + "')", 250);				
				//showNewContent(itemID);
				
			});
		}
		//showAddress($('#'+itemID+'_link .addr').text()+', austria')
		//showTaxi($('#'+itemID+'_link .addr').text());
	});
}


function setBorderStyles()
{	
	$(".course_dates ul li h2").css({
		"border-right": "1px solid #93d438",
		"border-bottom": "1px solid #93d438",
		"border-top": "1px solid #93d438"
	});

	$(".course_dates ul .active h2").css({
		"border-right" : "0",
		"border-bottom" : "0",
		"border-top" : "0"
	});

}
*/
/*
function setTopMargin()
{
	// get offset of the active course
	var activeCourse = $(".course_dates li.active");
	var offset = activeCourse.offset();
	
	$("#header").addClass("offset_" + offset.top);
	
	// set top margin for the service bar
	var serviceBarOffset = offset.top - 252;
	$(".sliding_bar #service_bar").animate({'marginTop': serviceBarOffset},'fast','swing',function() {
		$("#service_bar #service_arrow").css("background", "url(fileadmin/system/assets/img/background/service_bar_arrow.gif) no-repeat left top");
		$("#service_bar #service_arrow").animate({'width':'72px'},'fast');
	});
	
	//$(".sliding_bar #service_bar").css("margin-top", serviceBarOffset);		
}

function showNewContent(itemID)
{
	showAddress($('#'+itemID+'_link .addr').text()+', austria'); // Google Maps
	showTaxi($('#'+itemID+'_link .addr').text()); // Google Taxi
	setAnfahrt($('#'+itemID+'_link .addr').text()); // Anfahrt
	$("#" + itemID + " .course_facts").slideDown("fast");
}
*/
function showTaxi(addr) {
	$('#course_dates li.active .local_taxi').html('suche Taxiunternehmen...');
	addr=addr.split(',');
	var address = addr[1];
	var geocoder = new GClientGeocoder();
	geocoder.getLatLng(address, function (point) {
			var keyword='Taxi';
			var resultSIze='small'; // possible values: small (4), large (8)
			var localPoint=point.toUrlValue();
			var boundingBox='0.065169,0.194149'; // size of the area to search in
			var url = 'http://ajax.googleapis.com/ajax/services/search/local?q='+keyword+
					  '&sll='+localPoint+'&sspn='+boundingBox+'&v=1.0&rsz=large&callback=?';
			$.getJSON(url, function (data) {
					var found = 0;
					if(data.responseData.results && data.responseData.results.length > 0) {
							var resultElement = $('<dl></dl>');
							var results = data.responseData.results;
							for (var i=0; i < results.length; i++) {
									var title = results[i].titleNoFormatting;
									var description = '';
									if(results[i].phoneNumbers != null) description += results[i].phoneNumbers[0].number;

									if(title.toLowerCase().indexOf(keyword.toLowerCase()) == -1) continue;

									// Display each result however you wish
									$(resultElement).append('<dt>'+title+'</dt>');
									$(resultElement).append('<dd>'+description+'</dd>');
									found++;
									if(found == 4) break;
							}
							$('#course_dates li.active .local_taxi').html(resultElement);
					}
					if(found == 0) {
						$('#course_dates li.active .local_taxi').html('<em>kein Taxi-Unternehmern gefunden.</em>');
					}
			});
	});
}


function setAnfahrt(location) {
	href = $('#course_dates li.active .route_car').attr('href');
	$('#course_dates li.active .route_car').attr('href',href.replace(/daddr\=(.*)\,\+austria/,'daddr=' + encodeURI(jQuery.trim(location)) + ',+austria'));
	href = $('#course_dates li.active .route_public').attr('href');
	$('#course_dates li.active .route_public').attr('href',href.replace(/daddr\=(.*)\,\+austria/,'daddr=' + encodeURI(jQuery.trim(location)) + ',+austria'));
}

