/*******************************************************************************
 * sort of var_dump() for JS requires JQuery
 ******************************************************************************/
var log = function(elem) {
	if (window.console && window.console.debug) {
		console.debug(elem);
	}
} // Ende debug

/*******************************************************************************
 * all the things that have to be done on document load
 * 
 * @return void
 ******************************************************************************/

// Table Overview
if(typeof($) == 'function')
$(function() {
	if($.browser.msie && $.browser.version == 6.0) {
		is_ie6 = true;
	} else {
		is_ie6 = false;
	}
	$('a.flowplayer').html('').click(function() {
		
		return false;
	});
	init();
});

function init(){
	$('a.extern').click(function() {
		var jthis = $(this);
		var href = jthis.attr('href');
		var title = jthis.attr('title');
		window.open(href, title);
		return false;
	});
	if(typeof(init_flowplayer) == 'function') {
		init_flowplayer();
	}
	init_searchfield('suche');
	init_searchfield('ansp_search');
}

//function init_mediathek(node) {
//	if(!node) {
//		log('Kein Element übergeben.');
//		return false;
//	}
//	jnode = $(node);
//	if(jnode.length <= 0) {
//		log('Element nicht gefunden: '+node);
//		return false;
//	}
//	if(typeof(jnode.iframeResize) != 'function') {
//		log('IframeResize PLugin nicht gefunden');
//		return false;
//	}
//	var options = {
//	        width: "fill",          //width options are :
//	                                //'fill' (default, equivalent to 100%), 
//	                                //any valid CSS unit : '90%', '230px', '12em'
//	        height: "auto",         //height options are :
//	                                //'auto' (default, will adapt iframe height to its content, the more content, the higher)
//	                                //any valid CSS unit : '90%', '230px', '12em'
//	        autoUpdate : true       // 'true' (default, automatically update iframe height on content changes in the future) or 'false'
//	};
//	jnode.iframeResize(options);
//
//}

function init_searchfield(id) {
	if(id && id.length <= 0) {
		log('ID ist nicht gesetzt.');
		return false;
	}
	if(jQuery && jQuery.browser && jQuery.browser == 'msie' && jQuery.browser.version == '6.0') {
		return false;
	}
	var input = $('#'+id);
	var label = $('label[for="'+id+'"]');
	input.parent().addClass('autohide');
	
	if(input.length == 1 && label.length == 1) {
		var text = input.val();
		if(text.length > 0) {
			input.css('z-index', '3');
		}
	} else {
		log('Input oder Label nicht gefunden. id war: "'+id+'".');
		return false;
	}
	var f = function(ev) {
		var jthis = $(this);
		var value = jthis.val();
		var index = 0;
		
		if(ev.type == 'focus') {
			if(value.length == 0) {
				index = 3;
			}
		} else if(ev.type == 'blur') {
			if(value.length == 0) {
				index = 1;
			}
		} else if(ev.type == 'keypress') {
			if(value.length == 0) {
				index = 3;
			}
		}
		if(index > 0) {
			jthis.css('z-index', index);
		}
	};
	input.addClass('autohide').addClass('js').blur(f).focus(f).keypress(f);
	label.addClass('autohide').addClass('js');
	return true;
}

function loadMap(mapId, address){
    var myOptions = {
        zoom: 12,
        center: new google.maps.LatLng(48.14364, 7.6688),
        mapTypeId: google.maps.MapTypeId.HYBRID
    };
    var map = new google.maps.Map(document.getElementById("gMap-" + mapId), myOptions);
    var c = null;
    var gCoder = new google.maps.Geocoder();
    gCoder.geocode({
        "address": address
    }, function(Result, Status){
        if (Status == google.maps.GeocoderStatus.OK)
        {
        	var latitude = Result[0].geometry.location.lat();
            var longitude = Result[0].geometry.location.lng();
            
            c = new google.maps.LatLng(latitude, longitude);
            map.setCenter(c);
            var marker = new google.maps.Marker({
                position: c,
                map: map,
                title: address
            });
        }
    });
    try {
        var directionsDisplay = new google.maps.DirectionsRenderer();
        directionsDisplay.setPanel(document.getElementById("GDirectionsPanel-" + mapId));
        directionsDisplay.setMap(map);
        $DirectionsForm = $("#GDirectionsForm-" + mapId);
        $DirectionAddress = $("#GDirectionsAddress-" + mapId);
        $DirectionsForm.live("submit", function(){
            var direction = new google.maps.DirectionsService();
            direction.route({
                origin: $DirectionAddress.val(),
                destination: address,
                travelMode: google.maps.DirectionsTravelMode.DRIVING
            }, function(Result, Status){
                if (Status == google.maps.GeocoderStatus.OK) {
                    directionsDisplay.setDirections(Result);
                }
            });
            return false;
        });
    } 
    catch (E) {
        log(E);
    }
}
