var google_map = null;
var global_marker_list = [];

$(document).ready(function() {
	
	// remove text from main menu items
	$('#menu li a').text('');
	
	// enable datepicker
	$(".datepicker").datepicker({dateFormat: 'dd.mm.yy', onClose: function(dateText, inst) { $(this).removeClass('tipped'); } });
	
	// remove first submenu part
	$('#submenu-list').each(function() {
		var i = $('ul:first li:first ul', this).html();
		$('#submenu-list ul:first').html(i);
	});
	
	// we need hover state for meta menu locations
	$('#meta li a#menu-standorte').each(function() {
		$(this).parent().mouseover(function() {
			$(this).addClass('hover');
		});
		$(this).parent().mouseout(function() {
			$(this).removeClass('hover');
		});
	});
	
	// enable formtips
	$('form').formtips({
        class_name: 'tipped'
    });

	// wrap helper divs around location dropdown
	$('#meta ul ul').wrap('<div class="h"></div>');
	$('#meta ul ul').wrap('<div class="h-inner"></div>');
	$('#meta ul ul').wrap('<div class="h-content"></div>');
	$('#meta ul ul').css('display', 'block');

    // handle quickjump toggle
    var quickjump_cookie_options = { path: '/', expires: 365 };
	$('#footer-sitemap').each(function() {
		var container = $(this);
		$('h5', container).click(function() {
			if($(container).is('.closed')) {
				$.cookie('quickjump_toogle_state', 'visible', quickjump_cookie_options);
				$(container).removeClass('closed');
				$(container).animate({height: '250px'}, 800);
			} else {
				$.cookie('quickjump_toogle_state', 'hidden', quickjump_cookie_options);
				$(container).addClass('closed');
				$(container).animate({height: '35px'}, 800);
			}
		});
		if($.cookie('quickjump_toogle_state') == 'hidden') {
			$(container).height(35).addClass('closed'); // inital state
		}
	});

	// handle external links (should open in new window)
	var initExternalLinks = function() {
		$('a.external').click(function() {
			var url = $(this).attr('href');
			window.open(url, 'external');
			return false;
		});
	}
	initExternalLinks();
	
	// add hover state for form buttons
	$('form button').hover(function() {
		$(this).addClass('hover');
	}, function() {
		$(this).removeClass('hover');
	});
	
	// handle print links
	$('.print-page').click(function() {
		window.print();
		return false;
	});
	
	// zebra styles
	$('.zebra').each(function() {
		$(this).find('li:odd').addClass('row-odd');
		$(this).find('li:even').addClass('row-even');
	});
	
});



// handle submenu
var closePanel = function(selector) {
	$(selector).each(function() {
		$(this).slideUp('fast', function() {
			$(this).empty();
		});
	});
};

var initLocationSubmenu = function() {
	
	$('#submenu-list a.location').each(function() {
		var link = $(this);
		$(link).parent().addClass('closed').append('<div class="location-wrapper"></div>').find('.location-wrapper').hide();
		$(link).click(function() {
			if($(link).parent().is('.closed')) {
				$(link).parent().find('.location-wrapper').addClass('loading');
				// close all opened panels
				$('#submenu-list ul li').each(function() {
					$(this).find('div.location-wrapper').each(function() {
						closePanel(this);
						$(this).parent().addClass('closed').removeClass('active');
					});
				});
				$(link).parent().removeClass('closed').addClass('active');
				$(link).parent().find('.location-wrapper').slideDown('fast');
				// show details
				var url = $(link).attr('href') + '?format=html';
				$.get(url, {}, function(data) {
					// inject data
					$(link).parent().find('.location-wrapper').append(data);
					$(link).parent().find('.location-wrapper').removeClass('loading');
				});
				// pan overview map
				$('#overview-map').each(function() {
					var map = google_map;
					if(global_marker_list != null && global_marker_list.length > 0) {
						for(var a = 0; a < global_marker_list.length; a++) {
							var marker = global_marker_list[a];
							var marker_id = marker.location_id;
							var html_id = $(link).attr('id');
							var result = html_id.match(/expandedmenu-([0-9]+)/);
							var location_id = result[1];
							if(location_id == marker_id) {
								marker.show_info_window();
							}
						}
					}
				});
			} else {
				// close details
				$(link).parent().addClass('closed').removeClass('active');
				closePanel($(link).parent().find('.location-wrapper'));
			}
			return false;
		});
	});
	
};


