function log(){
    if (window && window.console && window.console.log) 
        for (var i = 0, len = arguments.length; i < len; i++) 
            console.log(arguments[i]);
}

/*******************************************************************************
 * all the things that have to be done on document load
 * requires JQuery 1.3 !!!!!!
 *
 * @return void
 ******************************************************************************/
$(function(){
    try {
        init();
    } 
    catch (e) {
        log("Thrown error: " + e.message);
    }
});
function init(){
    try {
        $("a.fancybox, a.thickbox, a.colorbox").fancybox({
            overlayOpacity: 0.8,
            titlePosition: "inside"
        });
    } 
    catch (e) {
        log("Thrown error: " + e.message);
    }
    try {
        $("a.extern").click(function(){
            window.open($(this).attr("href"));
            return false;
        });
    } 
    catch (e) {
        log("Thrown error: " + e.message);
    }
    try {
    	$('a.cluetip').cluetip({
    		local: true, 
    		cursor: 'pointer',
    		cluetipClass: 'jtip',
    		arrows: true,
    		showTitle: false
    	});    	
    }
    catch (e) {
    	log("Thrown error: " + e.message);
    }
    
    placeholder(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);
    }
}


function placeholder(remove_label) {
    var success = false;
    if(typeof(remove_label) == 'undefined' || remove_label !== true) {
        remove_label = false;
    }
    if(typeof(jQuery.fn.textPlaceholder) == 'function') {
        $('.placeholder').each(function(index, el) {
            var jel = $(el);
            var title = jel.attr('title');
            var text = '';
            if(typeof(title) != 'undefined' && title.length > 0) {
                text = title;
            } else {
                var id = jel.attr('id');
                var label = $('label[for="'+id+'"]');
                if(label.length > 0) {
                    text = label.text().replace('*', '');
                    if(remove_label == true) {
                        label.hide();
                    }
                }
                
            }
            if(text.length > 0) {
                jel.attr('placeholder', text);
                jel.textPlaceholder();
            }
        });
        success = true;
    }
    return success;
};
