var map;

$(document).ready(function() {
	$('.tt_entry:has(a)').mouseover(function() {
		old_color = $(this).css('background');
		$(this).css('background','#ff3');
		
		$(this).mouseout(function() {
			$(this).css('background',old_color);
		});
		
		$(this).click(function() {	
			document.location.href=$("a",this).attr('href');		
		});
	});
	
	$(".uc_first_src").mousedown(function(e){ 
		switch(e.which) {
			case 2:
			case 3: 			
				multi_entry_click_handler(this);
			break;			
		}
	});



	$("#datepicker").datepicker({
		dateFormat: 'dd.mm.yy',			
		altField: '#TimetableEntryStartDate'
	});
	
	$(".multi_date").datepicker({
		dateFormat: 'dd.mm.yy',		
	});
	
	if ($("#session_flash")) {
		$("#session_flash").show('slow');
	}
	$("#session_flash").click(function(e) {
		
		$("#session_flash").hide('slow');
	});
	
});

function remove_import_item(item_index)
{
	var count = $('#count').attr('value');
	var items_to_shift = count - item_index;
	$('#entry_' + item_index).remove();
	$('#count').attr('value',(count - 1));
	for (var x = 0; x < items_to_shift; x++) {
		var shift_ident = (item_index + x + 1);
		$('#ImportDate'+shift_ident).attr('id','ImportDate'+(shift_ident - 1)).attr('name','data[Import][date'+(shift_ident - 1)+']');
		$('#ImportTime'+shift_ident).attr('id','ImportTime'+(shift_ident - 1)).attr('name','data[Import][time'+(shift_ident - 1)+']');
		$('#ImportActName'+shift_ident).attr('id','ImportActName'+(shift_ident - 1)).attr('name','data[Import][act_name'+(shift_ident - 1)+']');
		$('#remove_link'+shift_ident).attr('href','javascript:remove_import_item('+(shift_ident - 1)+')');
		$('#remove_link'+shift_ident).attr('id','remove_link'+(shift_ident - 1));
		$('#entry_' + shift_ident).attr('id','entry_' + (shift_ident - 1));		
	}
}

function multi_entry_click_handler(obj) {	
	var pos = $(obj).position();
	
	if($("#uc_menu")) {
		$("#uc_menu").remove();
	}
	target = $("input",obj);
	
	$(obj).append('<div id="uc_menu"><span id="uc_convert_entry" target="'+target.attr('id')+'">convert to uc-first</span></div>');
	$("#uc_menu").css('position', 'fixed');
	$("#uc_menu").css('top', pos.top);
	$("#uc_menu").css('left', pos.left);	
	$("#uc_convert_entry").click(function() {
		src = $("#"+$(obj).attr('target')).attr('value');			
		elems = src.split(" ");
		res = '';
		for (x = 0; x < elems.length; x++) {
			res += elems[x].substr(0,1).toUpperCase() + elems[x].substr(1,elems[x].length).toLowerCase() + " ";				
		}
		$("#"+$(obj).attr('target')).attr('value',res);
	});
	$("#uc_convert_entry").mouseout(function() {
		$("#uc_menu").remove();
	});
}


function showLoc(Lat, Lng, name) {
			
	if (GBrowserIsCompatible()) {	
			map = new GMap2(document.getElementById("google_maps_div_location_add"),
				  {size: new GSize(600,400)}
			);
		
		mylocation = new GLatLng(Lat,Lng);
		var marker = new GMarker(mylocation, {draggable: false});
		marker.openInfoWindowHtml('<h2>'+name+'</h2>')

		map.addOverlay(marker);
		
		map.setCenter(mylocation, 13);
		map.setUIToDefault();
	}
}

function initMap(address) {	
	var geocoder = new GClientGeocoder();
	geocoder.setBaseCountryCode('DE'); 
	geocoder.getLatLng(address, function(c) {
		
		if (GBrowserIsCompatible()) {
			if ($('#google_maps_div_location_add')) {		
				map = new GMap2(document.getElementById("google_maps_div_location_add"));
				
			}
		}
		if (c) {
			
			var marker = new GMarker(c, {draggable: true});
			GEvent.addListener(marker, "dragstart", function() {
			  map.closeInfoWindow();
			});

			GEvent.addListener(marker, "dragend", function() {
				p = marker.getLatLng();
				$('#TimetableRequestCoords').attr('value',p);						
			});

			map.addOverlay(marker);
			
			map.setCenter(c, 14);
			map.setUIToDefault();

		}
	});
	
	
	
}

function add_event_entry() {	
	var count = $('.tt_div_startdate').length;
		
	$('div.tt_entry_add fieldset').append('<div class="input text required uc_first_src" style="float: left; width: 200px;" id="event_'+count+'"><label for="TimetableEntryActName">Name / Titel</label><input name="data[TimetableEntry][act_name'+count+']" maxlength="255" value="" id="TimetableEntryActName'+count+'" type="text"></div><div class="input text required" style="float: left; clear: none;"><label for="TimetableEntryStartDate">Datum</label><input name="data[TimetableEntry][start_date'+count+']" class="multi_date tt_div_startdate" value="" id="TimetableEntryStartDate'+count+'" type="text"></div>');
	$('#event_'+count).click(multi_entry_click_handler);
	$("#TimetableEntryStartDate"+count).datepicker();
	$('#event_count').val(++count);
}

function toggle_display(elem_id) {
	if ($('#'+elem_id).css('display') == 'none') {
		$('#'+elem_id).css('display', 'block');
	} else {
		$('#'+elem_id).css('display', 'none');
	}
}

