// BGFG Mini-Karte Hamburg Stuff
function newImage(arg) {
	if (document.images) {
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}

function changeImages() {
	if (document.images && (preloadFlag == true)) {
		for (var i=0; i<changeImages.arguments.length; i+=2) {
			document[changeImages.arguments[i]].src = changeImages.arguments[i+1];
		}
	}
}

var preloadFlag = false;
function preloadImages() {
	if (document.images) {
		bfgf_karte_1_01_bergedorf_over = newImage("fileadmin/bgfg/minikarte/bfgf_karte_1_01-bergedorf_o.gif");
		bfgf_karte_1_01_wandsbek_over = newImage("fileadmin/bgfg/minikarte/bfgf_karte_1_01-wandsbek_ov.gif");
		bfgf_karte_1_01_nord_over = newImage("fileadmin/bgfg/minikarte/bfgf_karte_1_01-nord_over.gif");
		bfgf_karte_1_01_altona_over = newImage("fileadmin/bgfg/minikarte/bfgf_karte_1_01-altona_over.gif");
		bfgf_karte_1_01_harburg_over = newImage("fileadmin/bgfg/minikarte/bfgf_karte_1_01-harburg_ove.gif");
		bfgf_karte_1_01_Mitte_over = newImage("fileadmin/bgfg/minikarte/bfgf_karte_1_01-Mitte_over.gif");
		bfgf_karte_1_01_eimsbuettel_over = newImage("fileadmin/bgfg/minikarte/bfgf_karte_1_01-eimsbuettel.gif");
		bfgf_karte_1_01_ausserhalb_over = newImage("fileadmin/bgfg/minikarte/bfgf_karte_1_01-ausserhalb_.gif");
		preloadFlag = true;
	}
}

function bgfgInitMap(center, zoom) {   // bind to "Show on map" links
    
    zoom = typeof(zoom) != 'undefined' ? zoom : 14;
    center = typeof(center) != 'undefined' ? center : "Weidestraße, Hamburg, Germany";    
    
    var geoCoordinates;
    $.ajax({ // get geoCoordinates
        url: '/bgfg.php',
        success: function(data) {           
           geoCoordinates = data;
        },
        data: {          
          currentAddress: center
        },
        dataType: 'json',
        async: false
    });
    
    dialogNode = $('<div id="bgfg-full-map-dialog" />');        
    dialogNode.dialog({ // map-containing overlay               
        width: 700,
        height: 500,
        modal: true                
    });
    
    dialogNode.gMap({  
        markers: geoCoordinates,
        zoom: zoom,           
        address: center + ', Hamburg, Germany' // address to center on, creates to marker. Google looks up lat&long for this
    });     
}

function bgfgInitEmbeddedMap() { 
    
    var bgfgCenter = "Weidestraße, Hamburg, Germany";    
    var mapNode = $('<div id="bgfg-full-map" />');
    var mapNodeEnlarge = $('<a id="bgfg-full-map-enlarge">Karte vergrößern</a>');
    
    $('div#col2_content')
        .append('<h2 id="bgfg-full-map-heading">BGFG-Wohnanlagen im Überblick</h2><p>Um mehr über eine Wohnanlage zu erfahren, klicken Sie auf der Karte direkt auf die Wohnanlage oder wählen Sie rechts einen Stadtteil.</p>')
        .append(mapNode);    
    
    var geoCoordinates;
    $.ajax({ // get geoCoordinates
        url : '/bgfg.php',
        success : function(data) {           
           geoCoordinates = data;
        },
        dataType : 'json',
        async: false
    });
    
    mapNode.gMap({
        markers: geoCoordinates,
        zoom: 11,               
        address: bgfgCenter // address to center on, creates to marker. Google looks up lat&long for this
    }); 
    mapNode.before(mapNodeEnlarge);
       
    mapNodeEnlarge.click(function(e){   
        e.preventDefault();
        bgfgInitMap(bgfgCenter, 11);
    });
}

// this function will open an issuu-document 
function issuuInit(documentId) {    
    var target = $('div#col2_content #issuu-container');    
    swfobject.embedSWF(
      "http://static.issuu.com/webembed/viewers/style1/v1/IssuuViewer.swf", 
      "issuu-object", 
      "462", 
      "297", 
      "9.0.0", 
      "expressInstall.swf",
      {
        mode: "embed",
        documentId: documentId,
        docName: "2008-12",
        er: 67
      },
      {
        allowfullscreen: "true",
        menu: "false",
        wmode: "transparent"
      }
    );
        
}

$(document).ready(function(){
	/*---------------------------------------------------------------------------------------------------- */
    /* SITECHIPS PRUEFUNGEN */
	var scCB = (location.href.match('/redaktion/') != null);
	var scMSIE = ($.browser.msie == true) ? true : false ;
	var scMOZILLA = ($.browser.mozilla == true) ? true : false ;
	var scOPERA = ($.browser.opera == true) ? true : false ;
	var scWEBKIT = ($.browser.webkit == true) ? true : false ;
	var scCHROME = (navigator.userAgent.toLowerCase().indexOf('chrome') > -1) ? true : false ;
	
	/*---------------------------------------------------------------------------------------------------- */
	/* HAUPTNAVIGATION HOVER BEHANDLUNG */
	$('#nav-main ul li').find('a:first').next().addClass('menu-hover');
	$('#nav-main ul li ul li ul').removeClass('menu-hover');
    $('.menu-hover').hover(
		function(){
	        $(this).prev().addClass('hover');
	    }, function(){
	        $(this).prev().removeClass('hover');
	    }
	);
	
	/*---------------------------------------------------------------------------------------------------- */
	/* IE6 li:hover fix */
	if(scMSIE && jQuery.browser.version == 6)
	{
		$('#nav-main li.first-layer').mouseenter(function(){
			$('#nav-main li ul').hide();
			$(this).find('ul').show();
		});
		
		$('#nav-main li.first-layer').mouseleave(function(){
			$('#nav-main li ul').hide();
		});
	};

	/*---------------------------------------------------------------------------------------------------- 
     * SCHRIFTGROESSE ANPASSEN UND FUER FOLGESEITEN MERKEN
     * benoetigt: http://plugins.jquery.com/project/cookie
     */
    var defaultSize = 100;
    var currentSize = 100;
    
	/*---------------------------------------------------------------------------------------------------- */
    /* setzen der Schriftgroesse auf #main */
    var setFontSize = function(size){
        if (size <= 150) {
            if (size >= 90) {
                currentSize = size;
                $.cookie('BGFG_FONTSIZE', size);
                $('#main').css('font-size', size + '%');
            };
		};
	};
    
	/*---------------------------------------------------------------------------------------------------- */
    /* ermitteln Schriftgroesse fuer Seiten refresh und setzen des Cookies */
    var getFontSize = function(){
        var cookieSize = $.cookie('BGFG_FONTSIZE');
        if (cookieSize == null) {
            $.cookie('BGFG_FONTSIZE', defaultSize);
            setFontSize(defaultSize);
        }
		else {
            setFontSize(cookieSize);
        };
    };
    getFontSize();
    
    /*---------------------------------------------------------------------------------------------------- */
    /* Schriftgroesse + */
    $('.font_big').click(function(e){
        e.preventDefault();
        var newSize = parseInt(currentSize) + 10;
        setFontSize(newSize);
    });
    
	/*---------------------------------------------------------------------------------------------------- */
    /* Schriftgroesse - */
    $('.font_small').click(function(e){
        e.preventDefault();
        var newSize = parseInt(currentSize) - 10;
        setFontSize(newSize);
    });
    
	/*---------------------------------------------------------------------------------------------------- */
    /* Schriftgroesse reset */
    $('.font_normal').click(function(e){
        e.preventDefault();
        currentSize = defaultSize;
        setFontSize(currentSize);
    });
    
    /* -- */
    /* Infoblock Accordion */
    $('div#green-infoblock').hover(
      function() {        
        $('p', $(this)).stop(true, true);
        $('p', $(this)).toggle('blind', 'slow');        
      }      
    );
    
    /* Google Maps Handling   */
    // BGFG-Adresse im Footer als Google Map Link aktivieren
    $('#bgfg-footer-address-link').click(function(e){     
      e.preventDefault();
       
      if ($(this).attr('title')) {
        currentAddress = $(this).attr('title');
      } else {
        currentAddress = $(this).text();
      }
      
      dialogNode = $('<div id="bgfgGoogleMapOpen" />');        
      dialogNode.dialog({ // map-containing overlay               
          width: 700,
          height: 500,
          modal: true                
      });
      
      dialogNode.gMap({  
          markers: new Array({
            address: currentAddress,
            popup: true,
            html: currentAddress,
            icon: {
              image: 'http://www.bgfg.de/fileadmin/templates/templavoila-bgfg/resources/images/kartenmarker.png',
              iconsize: [40,27],
              shadowsize: [0,0]
            }
          }),
          zoom: 14,           
          address: currentAddress + ', Germany' // address to center on, creates to marker. Google looks up lat&long for this
      });   
    });    
    
    /* -- Carousel & Gallery & Fullscreen -- */       
    $('div.csc-textpic-imagewrap').each(function(index) {  // this should be each gallery
        var container = $(this);        
        if (container.find('ul.bgfg-gallery').length > 0) { return true; }  // skip if already initialized        
        
        // remove headline "Bilder"
        if (container.parent().parent().children('div.csc-header').children('h1').length == 1) {
            //container.parent().parent().children('div.csc-header').remove();
        }
        
        if (container.hasClass('csc-textpic-single-image')) { // only colorbox for fullscreen if single pic
            container.find('a').onClick = undefined;        
            container.find('a').colorbox({
                'type' : 'ajax'
            });            
            return true; // skip gallery-init if single pic (stupid Typo)
        } 
        
        container.css({
            'width' : 'auto',
            'overflow' : 'hidden'
        });               
        container.wrapInner('<ul class="bgfg-gallery jcarousel-skin-bgfg" />'); 
        var newGallery = container.children('ul.bgfg-gallery');
        container.find('div.csc-textpic-imagerow').wrap('<li style="padding: 0 4px;" />');
        container.find('div.csc-textpic-imagerow').css({
            'float' : 'left',
            'clear' : 'none',
            'width' : 'auto'
        });
        
        // enable colorbox per pic, remove typo-crap first        
        container.find('div.csc-textpic-imagerow .csc-textpic-image a').removeAttr("onclick");
        
        // add rel-tags to make colorbox aware of depended pics         
        container.find('div.csc-textpic-imagerow .csc-textpic-image a').attr('rel', 'group' + index);            
        
        
        container.find("a[rel='group" + index + "']").colorbox({
          width: 800,
          height: 600,
          scrolling: false
        });
        
        // initiate gallery
        // /*
        newGallery
        .jcarousel({
            // Configuration goes here
            itemFallbackDimension: 180              
        })
        .css('left', '0px');
        // */
    });

    
    /* -- BGFG Wohnanlagen Handling -- */    
    $('li.bgfg-suburb-item > ul').hide(); // looks nicer if closed, but has to be open on load for non-JS users    
    
    // open/close/handle apartment list under each suburb    
    $('div.bgfg-suburb-name').click(function() { // this opens all apartments per suburb
        var suburb = $(this).parent();
        var open = suburb.hasClass('open');
        $('li.bgfg-suburb-item.open ul.bgfg-apartment-list').hide('blind', 'slow');
        $('li.bgfg-suburb-item.open').removeClass('open');
        if (!open) {                    
            suburb.addClass('open');
            $('ul.bgfg-apartment-list', suburb).show('blind', 'slow'); // switch visibilty of apartment-list 
        }
    });
    
    // open/close/handle details of each apartment
    $('div.bgfg-apartment-headline').click(function(e){
      e.preventDefault();      
      $('div.bgfg-apartment-details').hide();
      $(this).hide();
      details = $(this).next();           
      details.parent().css('height', details.height());      
      details.show('blind', 'slow');
	  details.addClass('print-me');
    });
    
    $('a.apartment-hide-details').click(function(e){
      details = $(this).parent().parent();      
      details.hide();
      details.prev().show();
      details.parent().css('height', 'auto');
    });
    
    if ($('li.bgfg-apartment-item.open').length) {
      var apartment = $('li.bgfg-apartment-item.open').first();    
      apartment.parent().parent().children('div.bgfg-suburb-name').trigger('click');
      apartment.children('div.bgfg-apartment-headline').trigger('click');      
    }    
    
    $('div.apartment-map-link').click(function(e){
      e.preventDefault();
      center = $('a', $(this)).first().attr('rel');     
      bgfgInitMap(center);
    });
    
    /* ImmoSolve iframes maximieren */
    $('a.bgfg-immosolve').click(function(e){
      e.preventDefault();      
      var link = $(this);
      if (link.hasClass('parking')) {        
        immosolve = $('<iframe width="1000" height="660" src="https://asp-02.immosolve.eu/immosolve_presentation/pub/modern/2784361/stellplaetze/immo.jsp" />');
      } else if (link.hasClass('business')) {
        immosolve = $('<iframe width="1000" height="660" src="https://asp-02.immosolve.eu/immosolve_presentation/pub/modern/2784361/gewerbemiete/immo.jsp" />');
      } else if (link.hasClass('application')) {
        immosolve = $('<iframe width="1000" height="660" src="https://asp-02.immosolve.eu/immosolve_presentation/pub/modern/2784361/mietwohnungen/immo.jsp?showAgent=true" />');
      } else {
        immosolve = $('<iframe width="1000" height="660" src="https://asp-02.immosolve.eu/immosolve_presentation/pub/modern/2784361/mietwohnungen/immo.jsp" />');
      }
      
      dialogNode = $('<div class="bgfgFullscreenDialog" />');          
      //console.log(item);
      //immosolve.attr('width', 990);
      immosolve.css('border', 'none');
      dialogNode.append(immosolve);      
      dialogNode.dialog({
        width: 1030,
        height: 710,
        modal: true       
      });      
    });
    
    
    // issuu stuff   
    if (document.URL.indexOf("mitgliederzeitung-bei-uns") != -1) { 
        $.ajax({
            url : 'issuu.php', // this a simple file that servers as a proxy. Some-origin-policy would prevent direct ajax-calls to issuu
            data : {
                username : 'bgfg', // what to look for. 
                //sortBy : 'epoch',
                pageSize : 50,
                responseParams : 'title,docname,documentId,tag'
            },            
            dataType: 'json',
            success : function(data) {   
                if (data.response.docs.length == 0) {
                    return true;
                }
                var counter = 0;  
                var volumes = data.response.docs;
                
                function compare(a,b) {    
                  if (a.title.substr((a.title.length-2), 2) > b.title.substr((b.title.length-2), 2))
                     return -1;
                  if (a.title.substr((a.title.length-2), 2) < b.title.substr((b.title.length-2), 2))
                    return 1;
                  if (a.title.substr((a.title.length-2), 2) == b.title.substr((b.title.length-2), 2)) {                   
                    if (a.title.slice(0, -2) > b.title.slice(0, -2))
                     return -1;
                    if (a.title.slice(0, -2) < b.title.slice(0, -2))
                    return 1;
                  }
                  return 0;
                } 
                
                volumes.sort(compare); 
                issuuInit(volumes[0].documentId);

                $.each(volumes, function(key, value){               
                    counter++;
                    var hidden = "";
                    if (counter > 3) { // all issues over 3 are just shown on click
                        hidden='style="display: none;"';
                    }                   
                    $('div#col3_content').append( // this is the right column of typo3. Stupid Typo
                    '<li ' + hidden + 'class="bgfg-issuu-archive-item">'
                        +'<div class="bgfg-issuu-archive-description">'
                            +'<span><b>'+ value.title +'</b></span>'
                            +'<br />'
                            +'<a target="_blank" href="/fileadmin/bgfg/bei_uns/' + value.docname.replace(/\./g, '_').toLowerCase() + '.pdf">&raquo; PDF-Download</a>'
                            +'<br />'
                            +'<a onclick="issuuInit(\'' + value.documentId + '\')">&raquo; Online-Ansicht</a>'
                        +'</div>'
                        +'<img src="http://image.issuu.com/' + value.documentId + '/jpg/page_1_thumb_small.jpg" />'                        
                    +'</li>'); 
                    if(counter == 3) {
                        $('div#col3_content').append('<a class="bgfg-issuu-archive-more">&raquo; Mehr</a>');
                        $('a.bgfg-issuu-archive-more').click(function() {
                            $('a.bgfg-issuu-archive-more ~ li').toggle('slow');
                            $(this).hide();
                        });                        
                    }                   
                });
                $('div#col3_content').wrapInner('<ul class="bgfg-issuu-archive" />');
            }
        });          
    }
    
    
    
    /* Generic GoogleMap stuff */
    $('a.bgfgGoogleMapTrigger, div.bgfgGoogleMap').click(function(e){     
      e.preventDefault();
       
      currentAddress = $('div.bgfgGoogleMap span').first().text();
      
      dialogNode = $('<div id="bgfgGoogleMapOpen" />');        
      dialogNode.dialog({ // map-containing overlay               
          width: 700,
          height: 500,
          modal: true                
      });
      
      dialogNode.gMap({  
          markers: new Array({
            address: currentAddress,
            popup: true,
            html: currentAddress
          }),
          zoom: 14,           
          address: currentAddress + ', Germany' // address to center on, creates to marker. Google looks up lat&long for this
      });   
    });
    
    /* Mitarbeiterliste */
    if($('ul.wtdirectory_list.wtdirectory_list_wrap').length > 0) {         
      var list = $('ul.wtdirectory_list.wtdirectory_list_wrap');
      $('div.wtdirectory_all_div', list).each(function(index, item){
          
        if($('dd.wtdirectory_all_email', item).length == 0) { return true; }
        
        var contactAddress = $('div.wtdirectory_all_powermail a', item).attr('href');    
        $('dd.wtdirectory_all_name', item)
          .wrapInner('<a href="' + contactAddress + '" />')
          .click(function(event){
            event.preventDefault();            
            $('<div><iframe src="' + contactAddress + '&type=102' + '" scrolling="no" frameborder="0" class="bgfg-iframe" /></div>').dialog({
                title: 'Nachricht an ' + $(this).text(),
                modal: true,
                width: 600,
                height: 500
            });
          });
      });
    }
    
    if (document.URL.indexOf('mitarbeiter/') != -1) {   
      var department = new Object();
      var departments = new Array();
      var area = new Array();
      var employees = new Array();      
      
      var employeeList = $('.wtdirectory_list.wtdirectory_list_wrap').parent();
      
      $('div.wtdirectory_all_div dl.wtdirectory_all_list', employeeList).each(function(index, item) {   
        contact = $(item);
        
        employeeName = $('dd.wtdirectory_all_lastname', item).first().text() + ', ' + $('dd.wtdirectory_all_firstname', item).first().text();
        employees.push(employeeName);
        contact.attr('employee-name',employeeName);
        
        var currentDepartment = $('dd.wtdirectory_all_company', item).first().text();         
        if (!department[currentDepartment]) { 
          department[currentDepartment] = new Array(); 
          departments.push(currentDepartment);
        }
        contact.attr('department', currentDepartment);
       
        if ($('dd.wtdirectory_all_addressgroup', item).first().text() == "Hausverwalter") {
          currentArea = $('dd.wtdirectory_all_region', item).first().text();
          if (department['Hausverwalter']) {
            if (currentArea && $.inArray(currentArea, department['Hausverwalter']) == -1) { 
              department['Hausverwalter'].push(currentArea);
              areas = currentArea.split(',');            
              area = area.concat(areas); 
            }
          }
          contact.attr('area',currentArea);
          return true;  
        }
                
        
        var position = $('dd.wtdirectory_all_title', item).first().text();                 
        if (department[currentDepartment]) {
          if (position && $.inArray(position, department[currentDepartment]) == -1) { department[currentDepartment].push(position); }        
        }
        contact.attr('position',position);
      });
      
      area = $.map(area, function(a){
        return $.trim(a);
      });
      area.sort(); 
      departments.sort();   
      $.each(department, function(index, value){
        value.sort();
      });
      
      var contactSelectContainer = $('<div id="contact-select-container"/>');
      
      var departmentSelect = $('<select id="department-select"><option value="all">Unternehmensbereich wählen</option></select>');   
      var functionSelect = $('<select id="function-select" />');
      var employeeSelect = $('<select id="employee-select"><option value="all">Mitarbeiter A-Z</option></select>');   
         
      $.each(departments, function(index, value) {        
        departmentSelect.append($('<option>' + value + '</option>'));
      });
      /* -- Abteilungsauswahl -- */
      departmentSelect.change(function(e){
        employeeSelect.val('all');
        var selectedDepartment = $(this).val();
        if (selectedDepartment=="all") {
          $('div.wtdirectory_all_div dl.wtdirectory_all_list', employeeList).show();                    
          functionSelect.children().remove();
          return true;
        }
       
        $('div.wtdirectory_all_div dl.wtdirectory_all_list', employeeList).hide();        
        $('div.wtdirectory_all_div dl.wtdirectory_all_list[department*="' + selectedDepartment + '"]', employeeList).show();
        if(selectedDepartment == "Hausverwalter") {         
          functionSelect.children().remove();
          functionSelect.append('<option value="all">Stadtteil</option>');
          $.each(area, function(index, value) {
            functionSelect.append('<option>' + value + '</option>');
          });
          functionSelect.hide().show();
          functionSelect.change(function(e){
            selectedArea = $(this).val();
            if (selectedArea=="all") {
              $('div.wtdirectory_all_div dl.wtdirectory_all_list[department*="Hausverwalter"]', employeeList).show();
              return true;
            }
            $('div.wtdirectory_all_div dl.wtdirectory_all_list', employeeList).hide();        
            $('div.wtdirectory_all_div dl.wtdirectory_all_list[area*="' + selectedArea + '"]', employeeList).show();
          });
        } else {          
          functionSelect.children().remove();          
          if (!department[selectedDepartment].length) { return true; }
          functionSelect.prepend(
            $('<option></option>').val('all').html('Funktion wählen')          
          );         
          $.each(department[selectedDepartment], function(index, value) {         
            functionSelect.append(
              $('<option></option>').val(value).html(value)
            );                     
          });          
          functionSelect.hide().show();
          functionSelect.change(function(e){
            selectedFunction = $(this).val();
            if (selectedFunction=="all") {
              $('div.wtdirectory_all_div dl.wtdirectory_all_list[department*="' + selectedDepartment + '"]', employeeList).show();
              return true;
            }
            $('div.wtdirectory_all_div dl.wtdirectory_all_list', employeeList).hide();        
            $('div.wtdirectory_all_div dl.wtdirectory_all_list[department*="' + selectedDepartment + '"][position*="' + selectedFunction + '"]', employeeList).show();
          });
        }
      });
        
      $.each(employees, function(index, value) {        
        employeeSelect.append($('<option>' + value + '</option>'));
      });
      employeeSelect.change(function(e){
        departmentSelect.val('all');
        functionSelect.children().remove();
        selectedEmployee = $(this).val();
        if (selectedEmployee=="all") {
          $('div.wtdirectory_all_div dl.wtdirectory_all_list', employeeList).show();
          return true;
        }
        $('div.wtdirectory_all_div dl.wtdirectory_all_list', employeeList).hide();        
        $('div.wtdirectory_all_div dl.wtdirectory_all_list[employee-name*="' + selectedEmployee + '"]', employeeList).show();
      });
      
      contactSelectContainer.append(departmentSelect);
      contactSelectContainer.append(functionSelect);
      contactSelectContainer.append(employeeSelect);
      
      employeeList.prepend(contactSelectContainer);
    }
    
    /* Akkordeon Layout */
    $('div.csc-default.layout-1').each(function(index, item){
      var parent = $(this);
      var header = $('div.csc-header', parent).first();
      header.nextAll().hide();
      akkordeonOpen = $('<a class="akkordeon open" href="#">Öffnen</a>');
      akkordeonClose = $('<a class="akkordeon close" href="#">Schließen</a>');
      akkordeonOpen.click(function(e){        
        e.preventDefault();
      });
      akkordeonClose.click(function(e){        
        e.preventDefault();
      });
      header.append(akkordeonOpen, akkordeonClose);
      header.click(function(e){
        header.toggleClass('open');
        header.nextAll().toggle('blind','slow');
      });
    });
    
    /* Google Map Layout */
    $('div.csc-default.layout-3').each(function(index, item){
      var address = $('.csc-textpic-text > p', item).first();
      address.hide();
      var currentAddress = $.trim(address.text());      
      var link = $('<a href="#">» Lageplan öffnen</a>');
      var map = $('.csc-textpic-single-image > img', item).first();
      map.css('cursor','pointer');
      map.click(function(e){
        link.trigger('click');
      });
      var dialogNode = $('<div id="bgfg-full-map-dialog" />');        
      $('.csc-textpic-text', item).append(link);
      link.click(function(e){
        e.preventDefault();
        dialogNode.dialog({ // map-containing overlay               
          width: 700,
          height: 500,
          modal: true                
        });
        
        dialogNode.gMap({  
          // /*
          markers: [{
            "address": currentAddress,
            "html": currentAddress,
            "icon": {
              "image": "http://www.bgfg.de/fileadmin/templates/templavoila-bgfg/resources/images/kartenmarker.png",
              "iconsize": [40,27],
              "shadowsize": [0,0]
            }
          }], // */
          zoom: 14,           
          address: currentAddress + ', Germany' // address to center on, creates to marker. Google looks up lat&long for this       
        });
      });
    });
    
    /* Webcam Ohlsdorf */
    webcam = $('a#bgfg-webcam-ohlsdorf');
    $('img', webcam).css('cursor', 'pointer');
    webcam.click(function(e){
      e.preventDefault();
      thumb = $(this).children('img').first();
      link = thumb.attr('src');      
      dialogNode = $('<div class="bgfgFullscreenDialog" />');  
      dialogNode.css('text-align', 'center');        
      image = $('<img title="Ohlsdorf" src="' + link + '" />')
      dialogNode.append(image);
      
      dialogNode.dialog({
        width: 700,
        height: 550,
        modal: true       
      }); 
    });
    
    /* calendar */
    if ($('div.tx-cal-controller div.calendar').length) {
      
      var context = $('div.tx-cal-controller div.calendar').first();            
      var monthNames = new Array("january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december");   
      var currentDate = new Date();
      var currentMonth = currentDate.getMonth(); // ATTENTION: 0 = january; 1 = february..
      var currentYear = currentDate.getFullYear();
      
      if ($('div.year_navigation .year_title', context).text() != currentYear) {
        currentMonth = 0;
      }
      $('div.year_navigation').hide();
      $('div.month', context).hide();
      $('div.month td.medtitle a', context).append(' ' + $('div.year_navigation .year_title', context).text());
      var previousYearLink = $('<a href="' + $('.year_navigation.prev_year a', context).attr('href') + '" class="calendar-navigation">&raquo; Letztes Jahr</a>');
      var nextYearLink = $('<a href="' + $('.year_navigation.next_year a', context).attr('href') + '" class="calendar-navigation">&raquo; Nächstes Jahr</a>');
      $('.year_content', context).prepend(previousYearLink);      
      previousYearLink.hide();
      $('.year_content', context).append(nextYearLink);      
      nextYearLink.hide();        
        
      var previousMonthLink = $('<a class="calendar-navigation">&raquo; Vorheriger Monat</a>');
      previousMonthLink.click(function(e){
        currentMonth--;
        e.preventDefault();                            
        $('div.month.'+monthNames[currentMonth+3], context).hide();
        $('div.month.'+monthNames[currentMonth], context).show();
        nextYearLink.hide();
        nextMonthLink.show();          
        if (currentMonth == 0) {
          previousMonthLink.hide();
          previousYearLink.show();
        }
      });      
      $('.year_content', context).prepend(previousMonthLink);
      
      var nextMonthLink = $('<a class="calendar-navigation">&raquo; Nächster Monat</a>');
      nextMonthLink.click(function(e){
        currentMonth++;          
        e.preventDefault();             
        $('div.month.'+monthNames[currentMonth-1], context).hide();
        $('div.month.'+monthNames[currentMonth+2], context).show();
        previousYearLink.hide();
        previousMonthLink.show();          
        if (currentMonth > 8) {
          nextMonthLink.hide();
          nextYearLink.show();
        }
      });      
      $('.year_content', context).append(nextMonthLink);
      
      $('div.month.'+monthNames[currentMonth], context).show();
      $('div.month.'+monthNames[currentMonth+1], context).show();      
      $('div.month.'+monthNames[currentMonth+2], context).show();
            
      if (currentMonth == 0) {
        previousMonthLink.hide();
        previousYearLink.show();
      } else if (currentMonth > 8) {
        nextMonthLink.hide();
        nextYearLink.show();
      }
    }
    
    /* --- open search results in overlay --- */
    if ($('div.tx-indexedsearch-searchbox').nextAll().length) {
      var searchbox = $('div.tx-indexedsearch-searchbox');
      var dialogNode = $('<div class="tx-indexedsearch"></div>');
      dialogNode.append(searchbox.nextAll());
      dialogNode.dialog({
        width: 1030,
        height: 710,
        modal: true  
      });
    }
    
    /* employee vcard */
    $('#col3_content a[href^="mailto"]').each(function(index, item){
      var email = $(item);      
      var vcard = $('<div class="vcard" />');      
      
      $.ajax({ // get geoCoordinates
        url: '/bgfg-vcard.php',
        success: function(data) {   
          if (!data) return false;
          
          employee = data; 
          if (employee.image) vcard.append('<div class="vcard-picture"><img src="/uploads/pics/' + employee.image + '"/></span>');      
          if (employee.name) vcard.append('<span class="vcard-name"><a href="mailto:' + employee.email + '">' + employee.name + '</a></span>');      
          //if (employee.name) vcard.append('<span class="vcard-addressgroup">' + employee.addressgroup + '</span>');                                     
          if (employee.name) vcard.append('<span class="vcard-company">' + employee.company + '</span>');               
          if (employee.name) vcard.append('<span class="vcard-title">' + employee.title + '</span>');  
          if (employee.name) vcard.append('<span class="vcard-phone">T: ' + employee.phone + '</span>');               
          if (employee.name) vcard.append('<span class="vcard-fax">F: ' + employee.fax + '</span>');               
        },
        data: {          
          'email': email.text()
        },
        dataType: 'json',
        async: false
      });
      
      if(!employee) return false;
      
      var vcardAncor = $("#col3_content strong:contains('" + employee.name + "')").first();
      
      if (vcardAncor.length) {    
        vcardAncor.addClass('vcard-ancor');    
        vcardAncor.prepend(vcard);
        vcard.css('top', (vcardAncor.position().top + 16) + 'px');
        vcardAncor.hover(
          function(){vcard.fadeIn(500);},
          function(){vcard.fadeOut(500);}
        );        
      } else {
        email.prepend(vcard);
        vcard.css('top', (email.position().top - 145) + 'px');
        email.hover(
          function(){
            vcard.stop(true, true);
            vcard.fadeIn(500);
          },
          function(){vcard.fadeOut(0);}
        );
      }
    });
      
    /* advisor vcard */
    $('.bgfg-apartment-details a[href^="mailto"]').each(function(index, item){
      var email = $(item);      
      var vcard = $('<div class="vcard advisor" />');            
      var vcardAncor = email.prevAll('strong');
      
      vcardAncor.addClass('vcard-ancor advisor');    
      vcardAncor.prepend(vcard);
      vcard.css('top', '16px');
      vcardAncor.hover(
        function(){          
          vcard.stop(true, true);
          if (!vcard.children().length) {
            $.ajax({ // get geoCoordinates
              url: '/bgfg-vcard.php',
              success: function(employee) {   
                if (!employee) return false;
                if (employee.image) vcard.append('<div class="vcard-picture"><img src="/uploads/pics/' + employee.image + '"/></span>');      
                if (employee.name) vcard.append('<span class="vcard-name"><a href="mailto:' + employee.email + '">' + employee.name + '</a></span>');      
                //if (employee.addressgroup) vcard.append('<span class="vcard-addressgroup">' + employee.addressgroup + '</span>');                                  
                if (employee.company) vcard.append('<span class="vcard-company">' + employee.company + '</span>');               
                if (employee.title) vcard.append('<span class="vcard-title">' + employee.title + '</span>');            
                if (employee.phone) vcard.append('<span class="vcard-phone">T: ' + employee.phone + '</span>');               
                if (employee.fax) vcard.append('<span class="vcard-fax">F: ' + employee.fax + '</span>');               
                vcard.fadeIn(500);
              },
              data: {          
                'email': email.text()
              },
              dataType: 'json',
              async: false
            });
          }
          else {          
            vcard.fadeIn(500);
          }
        },
        function(){vcard.fadeOut(0);}
      );        
    });
    
        // BGFG Hamburg Mini-Karte
    if($('map#bgfg_mini_map').length) {
      var bgfgMiniMap = $('map#bgfg_mini_map');
      preloadImages();
      if (document.URL.indexOf("unsere-wohnanlagen/altona") != -1) {
        changeImages('bfgf_karte_1_01', 'fileadmin/bgfg/minikarte/bfgf_karte_1_01-altona_over.gif');
        bgfgMiniMap.mouseleave(function(){
          changeImages('bfgf_karte_1_01', 'fileadmin/bgfg/minikarte/bfgf_karte_1_01-altona_over.gif');
        });
      } else if (document.URL.indexOf("unsere-wohnanlagen/bergedorf") != -1) {
        changeImages('bfgf_karte_1_01', 'fileadmin/bgfg/minikarte/bfgf_karte_1_01-bergedorf_o.gif');
        bgfgMiniMap.mouseleave(function(){
          changeImages('bfgf_karte_1_01', 'fileadmin/bgfg/minikarte/bfgf_karte_1_01-bergedorf_o.gif');
        });
      } else if (document.URL.indexOf("unsere-wohnanlagen/wandsbek") != -1) {
        changeImages('bfgf_karte_1_01', 'fileadmin/bgfg/minikarte/bfgf_karte_1_01-wandsbek_ov.gif');
        bgfgMiniMap.mouseleave(function(){
          changeImages('bfgf_karte_1_01', 'fileadmin/bgfg/minikarte/bfgf_karte_1_01-wandsbek_ov.gif');
        });
      } else if (document.URL.indexOf("unsere-wohnanlagen/nord") != -1) {
        changeImages('bfgf_karte_1_01', 'fileadmin/bgfg/minikarte/bfgf_karte_1_01-nord_over.gif');
        bgfgMiniMap.mouseleave(function(){
          changeImages('bfgf_karte_1_01', 'fileadmin/bgfg/minikarte/bfgf_karte_1_01-nord_over.gif');
        });
      } else if (document.URL.indexOf("unsere-wohnanlagen/harburg") != -1) {
        changeImages('bfgf_karte_1_01', 'fileadmin/bgfg/minikarte/bfgf_karte_1_01-harburg_ove.gif');
        bgfgMiniMap.mouseleave(function(){
          changeImages('bfgf_karte_1_01', 'fileadmin/bgfg/minikarte/bfgf_karte_1_01-harburg_ove.gif');
        });
      } else if (document.URL.indexOf("unsere-wohnanlagen/mitte") != -1) {
        changeImages('bfgf_karte_1_01', 'fileadmin/bgfg/minikarte/bfgf_karte_1_01-Mitte_over.gif');
        bgfgMiniMap.mouseleave(function(){
          changeImages('bfgf_karte_1_01', 'fileadmin/bgfg/minikarte/bfgf_karte_1_01-Mitte_over.gif');
        });
      } else if (document.URL.indexOf("unsere-wohnanlagen/eimsbuettel") != -1) {
        changeImages('bfgf_karte_1_01', 'fileadmin/bgfg/minikarte/bfgf_karte_1_01-eimsbuettel.gif');
        bgfgMiniMap.mouseleave(function(){
          changeImages('bfgf_karte_1_01', 'fileadmin/bgfg/minikarte/bfgf_karte_1_01-eimsbuettel.gif');
        });
      } else if (document.URL.indexOf("unsere-wohnanlagen/ausserhalb") != -1) {
        changeImages('bfgf_karte_1_01', 'fileadmin/bgfg/minikarte/bfgf_karte_1_01-ausserhalb_.gif');
        bgfgMiniMap.mouseleave(function(){
          changeImages('bfgf_karte_1_01', 'fileadmin/bgfg/minikarte/bfgf_karte_1_01-ausserhalb_.gif');
        });
      } else if (document.URL.indexOf("unsere-wohnanlagen") != -1) {
        bgfgInitEmbeddedMap();       
      }            
    } 
});


