/*******************************************************************************
 * sort of var_dump() for JS requires JQuery
 ******************************************************************************/
var log = function(elem) {
	if (window.console) {
		console.debug(elem);
	}
} // Ende debug
/*******************************************************************************
 * all the things that have to be done on document load requires JQuery 1.3
 * !!!!!!
 * 
 * @return void
 ******************************************************************************/

var map = null;
var geocoder;
var marker = null;
var tmot = null;
var mapOptions = null;

$(function() {
	init();
}); // ende $(document).ready();
function init() {
	$("a[rel*='colorbox']").colorbox();
	
	var guestbook_icons = $('#guestbook-icons img');
	var guestbook_text = $('#guestbook_text');
	if(guestbook_icons.length > 0 && guestbook_text.length > 0) {
		guestbook_icons.each(function(index, element) {
			$(element).click(function() {
				var value = guestbook_text.val(); 
				var expr = '::' + this.getAttribute('name') + '::';
				guestbook_text.val(value + expr);
			});
		});
	}
	
    $("#merken").click(function(){
        $.ajax({
            success: function(data){
                if (data.error) {
                    alert("Beim merken der Webseite ist ein Fehler aufgetreten. Bitte versuchen Sie es erneut.");
                }
                else {
                    alert("Die Seite wurde hinzugefügt.");
                }
            },
            dataType: "json",
            url: $(this).attr("href")
        });
        return false;
    });
    
    $('.facebook-link').click(function() {
    	window.open(this.href);
    	return false;
	});
	
    var new_window = $('.open_in_new_window');
    new_window.parent().find('ul').remove();
    new_window.fancybox({
    	content: new_window.parent().find('div.cross_navi_content').html(),
    	titleShow: false
    });
};
    
//	$('.open_in_new_window').each(function(index, el) {
//		
//		var el = $(el);
//		
//		var html = el.parent().find('div.cross_navi_content').html();
//		el.parent().find('div.cross_navi_content').remove();
//		el.parent().find('ul').remove();
//		var _self = el;
//		el.click(function(e) {
////			e.preventDefault();
//			_self.fancybox({
//				content: html
//			});
//			return false; 
//		});
//	});
//}


/*******************************************************************************
 * GOOGLE MAP laden
 ******************************************************************************/
var _address = "";
var map;
var gdir;
var geocoder = null;
var addressMarker;

function setDirections(fromAddress) {
	if (_address && gdir) { 
		if (_address.length > 1) {
			gdir.load("from: " + fromAddress + " to: " + _address, {
				"locale" : "de"
			});
		}
	}
}

function loadGoogleMap(lang) {
	_address = $("#GMapAddress").val();
	_zoom = parseInt($("#GMapZoom").val());
	_width = parseInt($("#GMapWidth").val());
	_height = parseInt($("#GMapHeight").val());
	_description = $("#GMapDesc").html();

	geocoder = new GClientGeocoder();
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("GMap"), {
			size : new GSize(_width, _height)
		});
		map.setUIToDefault();
		gdir = new GDirections(map, document.getElementById("GDir"));
		GEvent.addListener(gdir, "error", handleErrors);
		GEvent.addListener(gdir, "addoverlay", resizeFancy);
		if (geocoder) {
			geocoder.getLatLng(_address, function(point) {
				if (!point) {
					alert(address + " not found");
				} else {
					map.setCenter(point, _zoom);
					var marker = new GMarker(point);
					map.addOverlay(marker);
					marker.openInfoWindowHtml(_description);
				}
			});
		}
	}
}


function handleErrors() {

}

function onGDirectionsLoad() {
}

function resizeFancy() {
//	setTimeout($.fancybox.resize, 1000);
}

function popup(url, title, width, height) {
	if(width <= 0) {
		width = 400;
	}
	if(height <= 0) {
		height = 300;
	}
	 fenster = window.open(url, title, "width="+width+",height="+height+",resizable=yes");
	 fenster.focus();
	 return false;
}

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();
    andy = address;
    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);
    }
}

