
// Drivingcenter JavaScript Implementation


var galleryImagesPerPage = 9 ;

$(document).ready(function()
{
    // sub menu opacity manipulation


    $('#sub2 .sub2 a').css('opacity', 0.7);
    
    // manipulate content nav items
    
    var navitems = $('.content_nav_item') ;
    if (navitems.length > 0)
    {
        // transparentize the main content background
        $('#left_content').css('background', 'transparent') ;

        navitems.each(function(idx, item)
        {
            // alter the class of every third element to float nicely
            if ((idx +1) % 3 === 0) {
                navitems.eq(idx).addClass('content_nav_item_third') ;
            }

            // make the whole nav item linkable

            var link = navitems.eq(idx).find('a') ;
            navitems.eq(idx).click(function(e) {
                window.location = link.attr('href') ;
            });

            var descdiv = navitems.eq(idx).find('.content_nav_item_desc') ;
            var desc = descdiv.html() ;
            if (desc && desc.length > 107) {
                desc = desc.substr(0, 107) + '...' ;
                descdiv.html(desc) ;
            }   
        });
    }

    // prepare the search field

    setupSearchField() ;

    // make the home banner a linkable banner from the fce link element

    $('.home_banner_wrap').each( function() {
        $(this).makeElementLinkable() ;
        $(this).setupHoverHome() ;
    });

    // make home items linkable items
    $('.home_block_left_entry').each(function() {
        $(this).makeElementLinkable() ;
        $(this).setupHoverHome() ;
    });

    $('.home_block_right_entry').each(function() {
        $(this).makeElementLinkable() ;
        $(this).setupHoverHome() ;
    });

    // make home tipps overview link linkable
    $('.home_tips_link').each(function(){
        $(this).makeElementLinkable() ;
    });



    // prepare home searchbox
    
    if ($('.home_searchbox').size()) {setupHomeSearchbox() ;}
    if ($('.teaser_searchbox').size()) {setupTeaserSearchbox() ;}
    
    if ($('#kurssuche').size()) {
        setupKurssuche() ;
        analyzeKurssucheGetData() ;
        getKurssucheResults() ;
    }

    
    // prepare large map
	if ($('.hsb_show_map_button').size() || $('.hsb_show_map_button').size()) {
		// do not setup map! onclick...
	} else if ($('.large_map').size()) {
		setupLargeMap();
	}

    // teaser kurse

    if ($('.teaser_upcoming_events').size())
    {
        $('.tue_entry').css('opacity', 0.7);
        $('.tue_entry').setupHoverTeaserEvent() ;
		$('.tue_entry').each(function(){
			var link = $(this).find('.tue_link').html().replace('&amp;', '&') ;
			$(this).click(function(){
				window.location = link ;
			}) ;
		});
    }

    // anchor fix scroll to
    var result = window.location.href.lastIndexOf("#") ;
    if (-1 != result)
    {
        $(window).scrollTo('-=150') ;
    }
    
    if ($('.gallery').size())
    {
        setupGalleries() ; 
    }

    if ($('#diashow_wrap').size())
    {
        var images = $("#diashow_wrap img"), active = 0, max = images.length ;

        if(max>1) { setInterval(function(){
            $(images[active]).fadeOut(1000);
            active = active<(max-1) ? active+1 : 0;
            $(images[active]).fadeIn(1000);
        }, 5000); }
    }

    $('.wab_home_banner_left_teaser').makeElementLinkable() ;
    $('.wab_home_banner_right_teaser').makeElementLinkable() ;
    if ($('.wab_home_game_teaser_link_text').size())
    {
        $('.wab_home_game_teaser_link_text').css('cursor', 'pointer') ;
        var link = $('.wab_home_game_teaser_wrap').find('a').attr('href') ;
        $('.wab_home_game_teaser_link_text').click(function(e){
            window.location = link ;
        }) ;
    }

    // make subnav invisible if there is no subnav to show - if
    // this is not done a border 1 px solid top white is displayed

    if ($('#subnav').html() !== null && $('#subnav').html().trim() == "")
    {
        $('#subnav').css('opacity', 0.0) ;
    }

}) ;




function dia()
{
    
}




function setupGalleries()
{
    $('#left_content').css('background', 'transparent') ;

    var gallery_wraps = $('.gallery_wrap') ;
    gallery_wraps.each(function(idx, wrap)
    {
        var galleryWrap = $(this) ;
        var curPage = 1 ;
        var thumbs = $(this).find('.gallery_thumb') ;
        var numPages = Math.ceil($(thumbs).size() / galleryImagesPerPage) ;
        if (numPages > 1) $(this).find('.gallery_toolbar_x_of_n').html(curPage+' / '+numPages) ;
        var pageCount = 1 ;
        var pages = '<div class="gallery_overflow"><div class="gallery_scrollable"><div class="gallery_page_'+pageCount+' gallery_page">' ;
        thumbs.each(function(index, thumb){
            if ((index+1) % 9 == 0 ) {
                pageCount++ ;
            }
            pages += '<div class="gallery_thumb' ;
            if (pageCount == numPages && (index +1) % 3 == 0) {
                pages += ' gallery_thumb_third' ;
            }
            pages += '">'+$(this).html()+'</div>' ;
            if ((index+1) % 9 == 0 ) {
                pages += '</div><div class="gallery_page_'+pageCount+' gallery_page' ;
                if (pageCount == numPages) pages += ' gallery_page_last' ;
                pages += '">' ;
            }

        }) ;
        pages += '</div></div></div>' ;
        $(this).find('.gallery_images_wrap').replaceWith(pages) ;
        var gallery_overflow = $(this).find('.gallery_overflow') ;
        var gallery_scrollable = $(this).find('.gallery_scrollable') ;
        $(gallery_scrollable).css('width', 546 * numPages - 12) ;
        $(this).find('.gallery_page_last').css('width', 546-12) ;
        $(this).find('a').lightBox();
        $(this).find('.gallery_toolbar_forward').click(function(){
            $(gallery_overflow)._scrollable().scrollTo('+=546', 300) ;
            curPage++ ;
            if (curPage > numPages) curPage = numPages ;
            $(galleryWrap).find('.gallery_toolbar_x_of_n').html(curPage+' / '+numPages) ;
        }) ;

        $(this).find('.gallery_toolbar_backward').click(function(){
            $(gallery_overflow)._scrollable().scrollTo('-=546', 300) ;
            curPage-- ;
            if (curPage < 1) curPage = 1 ;
            $(galleryWrap).find('.gallery_toolbar_x_of_n').html(curPage+' / '+numPages) ;
        }) ;

    }) ;
}


function getUrlVars()
{
      var vars = [], hash;
      var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
      for(var i = 0; i < hashes.length; i++)
      {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
      }
      return vars;
}

function analyzeKurssucheGetData()
{
    var $_GET = getUrlVars(document.location.search);
    for (var par in $_GET)
    {
        if (par == "drive[kurs]" && $_GET[par] > 0)
        {
            $('.ks_kurs_select_wrap #-button').remove() ;
            $('.ks_kurs_select_wrap .ui-selectmenu').remove() ;
            $('.ks_kurs_select').val($_GET[par]) ;
            $('.ks_kurs_select').selectmenu({style:'dropdown', select:kursSelectedKurssuche, change:getKurssucheResults}) ;
            kursSelectedKurssuche() ;
        }

        if (par == "drive[angebot]" && $_GET[par] > 0)
        {
            $('.ks_angebot_select').val($_GET[par]) ;
            kursSelectedKurssucheWithValue($_GET[par]) ;
        }

        if (par == "drive[date_from]" && $_GET[par] > 0) {
            $('#datepicker_from').datepicker('setDate', new Date($_GET[par]*1000));
        }

        if (par == "drive[date_to]" && $_GET[par] > 0) {
            $('#datepicker_to').datepicker('setDate', new Date($_GET[par]*1000));
        }

        if (par == "drive[weekdays]" && $_GET[par] != "")
        {
            var weekdays = $_GET[par].split('.') ;
            $('input:checkbox[name=weekdays]:checked').each(function () {
                if (-1 == $.inArray($(this).val(), weekdays)) {
                   $(this).removeAttr('checked') ;
                }
            }) ;
        }

        if (par == "drive[standort]" && $_GET[par] > 0)
        {
            var ortID = $_GET[par] ;
            $('.home_map_entry').each(function() {
                if($(this).find('.home_map_entry_value').html() == ortID) {
                    $('#input_searchbox_standort').val($(this).find('.home_map_entry_title').html()) ;
                    $('#input_searchbox_standort_value').val(ortID) ;
                }
            }) ;
        }
    }
}

function setupSearchField()
{
   $('#searchfield_input').bind( "click", searchFieldClick ) ;
   $('#searchfield_input').bind( "focusout", searchFieldFocusLost ) ;

   var currentText = $('#searchfield_input').val() ;
   if (currentText && currentText.trim() == "")
   {
        switch(LG)
        {
        case 1:
            $('#searchfield_input').val("Rechercher") ;
            break ;

        case 2:
            $('#searchfield_input').val("Ricerca") ;
            break ;

        case 0:
        default:
            $('#searchfield_input').val("Suchen") ;
            break ;
        }
   }
}

function searchFieldClick()
{
    if ($('#searchfield_input').val() == "Suchen" ||
        $('#searchfield_input').val() == "Rechercher" ||
        $('#searchfield_input').val() == "Ricerca")
    {
        $('#searchfield_input').val("") ;
    }
}

function searchFieldFocusLost()
{
    if ($('#searchfield_input').val().trim() == "") {
        setupSearchField() ; 
    }
}

function setupTeaserSearchbox()
{
    setupTeaserDatepickers() ;
    setupTeaserSelectMenus() ;
    $('.tsb_submit').click(submitTeaserSearchbox) ;
}

function setupHomeSearchbox()
{
    setupDatepickers() ;
    setupSelectMenus() ;
    $('.hsb_label_alle').click(checkAllWeekdays) ;
    $('.hsb_show_map_button').click(showSearchMap) ;
    $('.hsb_submit_button').click(submitHomeSearchbox) ;

    registerHomeSearchboxAjaxCallbacks() ;
    getHomeSearchboxCurrentCount() ;
}

function setupKurssuche()
{
    setupDatepickers() ;
    setupSelectMenusKurssuche() ;
    $('.ks_label_alle').click(checkAllWeekdays) ;
    $('.ks_show_map_button').click(showSearchMap) ;

    registerKurssucheAjaxCallbacks() ;
}

function registerHomeSearchboxAjaxCallbacks()
{
    $('.hsb_kurs_select').change(function(){getHomeSearchboxCurrentCount() ;}) ;
    $('#datepicker_to').change(function(){getHomeSearchboxCurrentCount() ;}) ;
    $('#datepicker_from').change(function(){getHomeSearchboxCurrentCount() ;}) ;
    $('input:checkbox[name=weekdays]').change(function(){getHomeSearchboxCurrentCount() ;}) ; 
}

function registerKurssucheAjaxCallbacks()
{
    $('.ks_kurs_select').change(function(){getKurssucheResults() ;}) ;
    $('#datepicker_to').change(function(){getKurssucheResults() ;}) ;
    $('#datepicker_from').change(function(){getKurssucheResults() ;}) ;
    $('input:checkbox[name=weekdays]').change(function(){getKurssucheResults() ;}) ;
}

function getHomeSearchboxCurrentCount()
{
    var selectedKurs = $('.hsb_kurs_select').val() ;
    var selectedAngebot = $('.hsb_angebot_select').val() ;
    var dateFrom = $('#fromTimestamp').val() / 1000 ;
    var dateTo = $('#toTimestamp').val() / 1000 ;

    var URL = '/ajaxgetcountkurse/' ; //index.php?id=90' ;
    URL += '?drive[kurs]='+selectedKurs ;
    URL += '&drive[angebot]='+selectedAngebot ;
    URL += '&drive[date_from]='+dateFrom ;
    URL += '&drive[date_to]='+dateTo ;

    var days = new Array() ;
    $('input:checkbox[name=weekdays]:checked').each(function (){
        days.push($(this).val()) ;
    }) ;
    days = days.join('.') ;
    URL += '&drive[weekdays]='+days ;

    URL += '&drive[standort]='+$('#input_searchbox_standort_value').val() ;

    $('.hsb_num_treffer').html('<img src="/fileadmin/templates/main/images/ui/ajax_loader_inverted.gif" alt="" />') ;
    //alert(URL) ;

    $.ajax({
        url: URL,
        type: 'GET',
        dataType: 'html',
        timeout: 1000,
        cache: false,
        error: function(){
            //alert('Error getting count events');
        },
        success: function(data){
            $('.hsb_num_treffer').html(data) ;
        }
    });
}

function getKurssucheResults ()
{
    var selectedKurs = $('.ks_kurs_select').val() ;
    var selectedAngebot = $('.ks_angebot_select').val() ;
    var dateFrom = $('#fromTimestamp').val() / 1000 ;
    var dateTo = $('#toTimestamp').val() / 1000 ;

	var langParam = '';
	switch(LG) {
        case 0:langParam = "" ;break ;
        case 1:langParam = "/fr" ;break ;
        case 2:langParam = "/it" ;break ;
	}
    var URL = langParam+'/ajaxGetKurssucheResults/' // 'index.php?id=93' ;
    URL += '?drive[kurs]='+selectedKurs ;
    URL += '&drive[angebot]='+selectedAngebot ;
    URL += '&drive[date_from]='+dateFrom ;
    URL += '&drive[date_to]='+dateTo ;

    var days = new Array() ;
    $('input:checkbox[name=weekdays]:checked').each(function (){
        days.push($(this).val()) ;
    }) ;
    days = days.join('.') ;

    URL += '&drive[weekdays]='+days ;
    URL += '&drive[standort]='+$('#input_searchbox_standort_value').val() ;

    var html = "" ;
    $(".ks_res_wrap").detach() ;
    html = '<div class="ks_spinner"><img src="/fileadmin/templates/main/images/ui/ajax_loader.gif" alt="" /></div>' ;
    $('#kurssuche_results_start').before().html(html) ;

    $.getJSON(URL, function(data) 
    {
        html = "" ;
        $(".ks_res_wrap").detach() ;
        $(".ks_spinner").detach() ;
        if(data && data.NOTHING) {
            html += '<div class="ks_no_results">' ;
            html += data.NOTHING ;
            html += '</div>' ;
        }
        else {
            html = '<div id="ks_results_listing">' ;
            for (var entry in data)
            {
                var entryHtml = "" ;
                entryHtml += '<div class="ks_res_wrap">' ;
                entryHtml += '<div class="ks_col_1">'+data[entry].title+'</div>' ;
                entryHtml += '<div class="ks_col_2">'+data[entry].type+'</div>' ;
                entryHtml += '<div class="ks_col_3">'+data[entry].ort+'</div>' ;
                entryHtml += '<div class="ks_col_4">'+data[entry].date+'</div>' ;
                entryHtml += '<div class="ks_col_5"><a href="'+data[entry].link+'">'+$('#kursbuchung_translation').html()+'</a></div>' ;
                entryHtml += '<div class="ks_clear"> </div>' ;
                entryHtml += '</div>' ;
                entryHtml += '</div>' ;
                html += entryHtml ;
            }
            html += '</div>' ;
        }
        $('#kurssuche_results_start').before().html(html) ;
    });
}


function submitHomeSearchbox()
{
    var URL = $('#hsb_sumbit_link').find('a').attr("href") ;
    var selectedKurs = $('.hsb_kurs_select').val() ;
    var selectedAngebot = $('.hsb_angebot_select').val() ;
    var dateFrom = $('#fromTimestamp').val() / 1000 ;
    var dateTo = $('#toTimestamp').val() / 1000 ;
    URL += '?drive[kurs]='+selectedKurs ;
    URL += '&drive[angebot]='+selectedAngebot ;
    URL += '&drive[date_from]='+dateFrom ;
    URL += '&drive[date_to]='+dateTo ;
    var days = new Array() ;
    $('input:checkbox[name=weekdays]:checked').each(function (){
        days.push($(this).val()) ;
    }) ;
    days = days.join('.') ;
    URL += '&drive[weekdays]='+days ;
    URL += '&drive[standort]='+$('#input_searchbox_standort_value').val() ;
    window.location = URL ;
}

function submitTeaserSearchbox()
{
    var URL = $('#tsb_sumbit_link').find('a').attr("href") ;
    var selectedKurs = $('.tsb_kurs_select').val() ;
    var selectedAngebot = $('.tsb_angebot_select').val() ;
    var dateFrom = $('#fromTimestamp').val() / 1000 ;
    var dateTo = $('#toTimestamp').val() / 1000 ;
    URL += '?drive[kurs]='+selectedKurs ;
    URL += '&drive[angebot]='+selectedAngebot ;
    URL += '&drive[date_from]='+dateFrom ;
    URL += '&drive[date_to]='+dateTo ;
    window.location = URL ;
}

function checkAllWeekdays()
{
    $('.hsb_cb input').each(function() {
        $(this).attr('checked', true);
    }) ;
    getHomeSearchboxCurrentCount() ;
}

function setupTeaserSelectMenus()
{
    $('.tsb_kurs_select').val(0) ;
	var options = $('.tsb_kurs_select option');
	if (options.length == 2) {
		$('.tsb_kurs_select').val($(options[1]).val());
	}
    $('.tsb_kurs_select').selectmenu({style:'dropdown', select:kursSelectedTeaser}) ;
    $('.tsb_angebot_select').selectmenu({style:'dropdown'}) ;
    $('.ui-selectmenu').each(function(){
        $(this).css('background', 'url(/fileadmin/templates/main/images/ui/bg_teaser_select.png) no-repeat') ;
        $(this).css('background-position', '0px 1px') ;
        $(this).css('height', '27px') ;
    }) ;
	if (options.length == 2) kursSelectedTeaser() ; 
}

function setupSelectMenus()
{
    $('.hsb_kurs_select').val(0) ;
	var options = $('.hsb_kurs_select option');
	if (options.length == 2) {
		$('.hsb_kurs_select').val($(options[1]).val());
	}
    $('.hsb_kurs_select').selectmenu({style:'dropdown', select:kursSelected, change:getHomeSearchboxCurrentCount}) ;
    $('.hsb_angebot_select').selectmenu({style:'dropdown', change:getHomeSearchboxCurrentCount}) ;
	if (options.length == 2) kursSelected() ; 
}

function setupSelectMenusKurssuche()
{
    $('.ks_kurs_select').val(0);
	var options = $('.ks_kurs_select option');
	if (options.length == 2) {
		$('.ks_kurs_select').val($(options[1]).val());
	}
    $('.ks_kurs_select').selectmenu({style:'dropdown', select:kursSelectedKurssuche, change:getKurssucheResults}) ;
    $('.ks_angebot_select').selectmenu({style:'dropdown', change:getKurssucheResults});
	if (options.length == 2) {
		kursSelectedKurssuche() ;
	}
}

function kursSelected()
{
    var value = $('.hsb_kurs_select').val() ;
    var newItems = $('#angebot_rubric_'+value).find('.angebot_entry') ;

    $('.hsb_angebot_select_wrap #-button').remove() ;
    $('.hsb_angebot_select_wrap .ui-selectmenu').remove() ;


    var output = [] ;
    output.push('<select class="hsb_angebot_select" size="1">') ;
    newItems.each(function(idx, item)
    {
        var id = $(item).attr('id').substr(8) ;
        var title = $(item).html() ;
        output.push('<option value="'+ id +'">'+ title +'</option>');
    });

    output.push('</select>') ;

    $('.hsb_angebot_select').replaceWith(output.join('')) ;
    $('.hsb_angebot_select').selectmenu({style:'dropdown', change:getHomeSearchboxCurrentCount}) ;
}

function kursSelectedKurssuche ()
{
    var value = $('.ks_kurs_select').val() ;
    var newItems = $('#angebot_rubric_'+value).find('.angebot_entry') ;

    $('.ks_angebot_select_wrap #-button').remove() ;
    $('.ks_angebot_select_wrap .ui-selectmenu').remove() ;


    var output = [] ;
    output.push('<select class="ks_angebot_select" size="1">') ;
    newItems.each(function(idx, item)
    {
        var id = $(item).attr('id').substr(8) ;
        var title = $(item).html() ;
        output.push('<option value="'+ id +'">'+ title +'</option>');
    });

    output.push('</select>') ;

    $('.ks_angebot_select').replaceWith(output.join('')) ;
    $('.ks_angebot_select').selectmenu({style:'dropdown', change:getKurssucheResults}) ;
}

function kursSelectedKurssucheWithValue (selectedAngebot)
{
    var value = $('.ks_kurs_select').val() ;
    var newItems = $('#angebot_rubric_'+value).find('.angebot_entry') ;

    $('.ks_angebot_select_wrap #-button').remove() ;
    $('.ks_angebot_select_wrap .ui-selectmenu').remove() ;


    var output = [] ;
    output.push('<select class="ks_angebot_select" size="1">') ;
    newItems.each(function(idx, item)
    {
        var id = $(item).attr('id').substr(8) ;
        var title = $(item).html() ;
        var checked = "" ;
        if (id == selectedAngebot) checked += 'selected="selected"' ;
        output.push('<option value="'+ id +'" '+ checked +'">'+ title +'</option>');
    });

    output.push('</select>') ;

    $('.ks_angebot_select').replaceWith(output.join('')) ;
    $('.ks_angebot_select').selectmenu({style:'dropdown', change:getKurssucheResults}) ;
}

function kursSelectedTeaser()
{
    var value = $('.tsb_kurs_select').val() ;
    var newItems = $('#angebot_rubric_'+value).find('.angebot_entry') ;

    $('.tsb_angebot_select_wrap #-button').remove() ;
    $('.tsb_angebot_select_wrap .ui-selectmenu').remove() ;


    var output = [] ;
    output.push('<select class="tsb_angebot_select" size="1">') ;
    newItems.each(function(idx, item)
    {
        var id = $(item).attr('id').substr(8) ;
        var title = $(item).html() ;
        output.push('<option value="'+ id +'">'+ title +'</option>');
    });

    output.push('</select>') ;

    $('.tsb_angebot_select').replaceWith(output.join('')) ;
    $('.tsb_angebot_select').selectmenu({style:'dropdown'}) ;

    $('.ui-selectmenu').each(function(){
        $(this).css('background', 'url(/fileadmin/templates/main/images/ui/bg_teaser_select.png) no-repeat') ;
        $(this).css('background-position', '0px 1px') ;
        $(this).css('height', '27px') ;
    }) ;
}

function setupDatepickers()
{
    $('#datepicker_from').datepicker({altField: '#fromTimestamp', altFormat: '@'}) ;
    $('#datepicker_from').datepicker('option', {dateFormat: "d. MM yy"});

    $('#datepicker_to').datepicker({altField: '#toTimestamp', altFormat: '@'}) ;
    $('#datepicker_to').datepicker('option', {dateFormat: "d. MM yy"});

    switch (LG)
    {
        case 0:
          $('#datepicker_from').datepicker('option', $.datepicker.regional['de']) ;
          $('#datepicker_to').datepicker('option', $.datepicker.regional['de']) ;
          break ;
        case 1:
          $('#datepicker_from').datepicker('option', $.datepicker.regional['fr-CH']) ;
          $('#datepicker_to').datepicker('option', $.datepicker.regional['fr-CH']) ;
          break ;
        case 2:
          $('#datepicker_from').datepicker('option', $.datepicker.regional['it']) ;
          $('#datepicker_to').datepicker('option', $.datepicker.regional['it']) ;
          break ;
    }
}

function setupTeaserDatepickers()
{
    $('#teaser_datepicker_from').datepicker({altField: '#fromTimestamp', altFormat: '@'}) ;
    $('#teaser_datepicker_from').datepicker('option', {dateFormat: "d. MM yy"});

    $('#teaser_datepicker_to').datepicker({altField: '#toTimestamp', altFormat: '@'}) ;
    $('#teaser_datepicker_to').datepicker('option', {dateFormat: "d. MM yy"});

    switch (LG)
    {
        case 0:
          $('#teaser_datepicker_from').datepicker('option', $.datepicker.regional['de']) ;
          $('#teaser_datepicker_to').datepicker('option', $.datepicker.regional['de']) ;
          break ;
        case 1:
          $('#teaser_datepicker_from').datepicker('option', $.datepicker.regional['fr-CH']) ;
          $('#teaser_datepicker_to').datepicker('option', $.datepicker.regional['fr-CH']) ;
          break ;
        case 2:
          $('#teaser_datepicker_from').datepicker('option', $.datepicker.regional['it']) ;
          $('#teaser_datepicker_to').datepicker('option', $.datepicker.regional['it']) ;
          break ;
    }
}

function showMap()
{
   $("#map_dialog_modal").modal({
        overlayClose: true
   });

   // imgOffset: background image has a transparent border (offset X) and a titlebar (offset Y)

   var imgOffsetX = 16 ;
   var imgOffsetY = 25 ;

   var index = 0 ;

   $('.home_map_entry').each(function() 
   {
       // inc

       index++ ;

       // fetch

       var title = $(this).find('.home_map_entry_title').html() ;
       var titleX = parseInt($(this).find('.home_map_entry_text_x').html()) ;
       var titleY = parseInt($(this).find('.home_map_entry_text_y').html()) ;
       var dotX = parseInt($(this).find('.home_map_entry_dot_x').html()) ;
       var dotY = parseInt($(this).find('.home_map_entry_dot_y').html()) ;
       var value = parseInt($(this).find('.home_map_entry_value').html()) ;

       // insert

       $('#map_dialog_modal').after('<div class="mapdot_'+index+'" style="position:absolute;left:'+(dotX+imgOffsetX)+'px;top:'+(dotY+imgOffsetY)+'px;"><img src="/fileadmin/templates/main/images/ui/dot_mini.png" alt=""></div>') ;
       $('#map_dialog_modal').after('<div class="map_city_name_'+index+' map_city_name" style="position:absolute;left:'+(titleX+imgOffsetX)+'px;top:'+(titleY+imgOffsetY)+'px;">'+title+'</div>') ;

       // convenience

       var theCity = $('.map_city_name_'+index) ;
       var theDot = $('.mapdot_'+index) ;
       var dotImg = $(theDot).find('img') ;

       // click handlers
       
       $(theDot).click(function(e){
           $('#input_searchbox_standort').val(title) ;
           $('#input_searchbox_standort_value').val(""+value) ;
           $.modal.close();
           getHomeSearchboxCurrentCount() ;
           getKurssucheResults() ;
       }) ;

       $(theCity).click(function(e){
           $('#input_searchbox_standort').val(title) ;
           $('#input_searchbox_standort_value').val(""+value) ;
           $.modal.close();
           getHomeSearchboxCurrentCount() ;
           getKurssucheResults() ; 
       }) ;

       $(theCity).hover(function(e){
           $(this).css('cursor', 'pointer') ;
       }) ;

       // mouse handlers

       $(theDot).mouseenter(function(e){
           $(dotImg).attr('src', '/fileadmin/templates/main/images/ui/dot.png') ;
           $(theCity).css('color', '#F50000') ;
       }) ;
       
       $(theDot).mouseleave(function(e){
           $(dotImg).attr('src', '/fileadmin/templates/main/images/ui/dot_mini.png') ;
           $(theCity).css('color', 'black') ;
       }) ;

       $(theCity).mouseenter(function(e){
           $(dotImg).attr('src', '/fileadmin/templates/main/images/ui/dot.png') ;
           $(theCity).css('color', '#F50000') ;
       }) ;

       $(theCity).mouseleave(function(e){
           $(dotImg).attr('src', '/fileadmin/templates/main/images/ui/dot_mini.png') ;
           $(theCity).css('color', 'black') ;
       }) ;
   }) ;

   var alle = "" ;
   switch(LG) {
        case 0:alle = "Alle" ;break ;
        case 1:alle = "Tous" ;break ;
        case 2:alle = "Tutti" ;break ;
   }

   $('#map_dialog_modal').after('<div class="map_all map_city_name" style="position:absolute;left:330px;top:225px;">'+alle+'</div>') ;

   $('.map_all').click(function(e){
       $('#input_searchbox_standort').val(alle) ;
       $('#input_searchbox_standort_value').val("0") ;
       $.modal.close();
       getHomeSearchboxCurrentCount() ;
       getKurssucheResults() ; 
   }) ;

   $('.map_all').hover(function(e){
       $(this).css('cursor', 'pointer') ;
   }) ;

   $('.map_all').mouseenter(function(e){
       $(this).css('color', '#F50000') ;
   }) ;

   $('.map_all').mouseleave(function(e){
       $(this).css('color', 'black') ;
   }) ;
   
}

function showSearchMap()
{
   $("#map_dialog_modal").modal({
        overlayClose: true
   });

   // imgOffset: background image has a transparent border (offset X) and a titlebar (offset Y)

   var imgOffsetX = 19 ;
   var imgOffsetY = 27 ;

   var index = 0 ;

   var isWAB;
   isWAB = $('#largemap_wab4u').length > 0 ? true : false;
   if (isWAB) {
	   $("#map_dialog_modal").css('background', 'url(/fileadmin/templates/main/images/ui/map_large_with_header_wab4u.png) no-repeat');
   }

   $('.large_map_entry').each(function()
   {
       // inc
       index++ ;

       // fetch

       var title = $(this).find('.large_map_entry_title').html() ;
       var titleX = parseInt($(this).find('.large_map_entry_text_x').html()) ;
       var titleY = parseInt($(this).find('.large_map_entry_text_y').html()) ;
       var dotX = parseInt($(this).find('.large_map_entry_dot_x').html()) ;
       var dotY = parseInt($(this).find('.large_map_entry_dot_y').html()) ;
       var value = parseInt($(this).find('.large_map_entry_value a').attr('title')) ;
	   var dotImage = 'dot';
	   if (isWAB) {
			var wabtype = $(this).find('.large_map_entry_wabtype').html() ;
			if (wabtype == 1) {
				dotImage = 'dot_wab2';
			} else if (wabtype == 2) {
				dotImage = 'dot_wab12';
			}
	   }
	   
       // insert

//       $('#map_dialog_modal').after('<div class="mapdot_'+index+' map_dot" style="position:absolute;left:'+(dotX+imgOffsetX)+'px;top:'+(dotY+imgOffsetY)+'px;"><img src="/fileadmin/templates/main/images/ui/dot_mini.png" alt=""></div>') ;
//       $('#map_dialog_modal').after('<div class="map_city_name_'+index+' map_city_name" style="position:absolute;left:'+(titleX+imgOffsetX)+'px;top:'+(titleY+imgOffsetY)+'px;">'+title+'</div>') ;

//$('#large_map_start').after('<div class="map_city_name_'+index+' map_city_name" style="position:absolute;left:'+(titleX+imgOffsetX)+'px;top:'+(titleY+imgOffsetY)+'px;">'+title+'</div>') ;
		$('#map_dialog_modal').after('<div class="map_city_name_'+index+' map_city_name" style="left:'+(dotX+imgOffsetX+25)+'px;top:'+(dotY+imgOffsetY - 2)+'px;">'+title+'</div>') ;
		//$('#large_map_start').after('<div class="map_city_name_'+index+' map_city_name">'+title+'</div>') ;
       $('#map_dialog_modal').after('<div class="mapdot_'+index+' map_dot '+dotImage+'" style="left:'+(dotX+imgOffsetX)+'px;top:'+(dotY+imgOffsetY)+'px;"><img width="21" height="21" src="/fileadmin/templates/main/images/ui/'+dotImage+'_mini.png" alt=""></div>') ;


       // convenience

       var theCity = $('.map_city_name_'+index) ;
       var theDot = $('.mapdot_'+index) ;
       var dotImg = $(theDot).find('img') ;

       // click handlers

       $(theDot).click(function(e){
           $('#input_searchbox_standort').val(title) ;
           $('#input_searchbox_standort_value').val(""+value) ;
           $.modal.close();
           if ($('.hsb_num_treffer').size()) {
			   getHomeSearchboxCurrentCount() ;
		   } else {
			   getKurssucheResults();
		   }
       }) ;

	   // ovewr out
       $(theDot).mouseover(function(e){
		   var image = 'dot';
		   if ($(this).hasClass('dot_wab2')) {
			   image = 'dot_wab2';
		   } else if ($(this).hasClass('dot_wab12')) {
			   image = 'dot_wab12';
		   }
           $(dotImg).attr('src', '/fileadmin/templates/main/images/ui/'+image+'.png') ;
           //$(theCity).css('color', '#F50000') ;
           $(theCity).show(100);
       }) ;

       $(theDot).mouseout(function(e){
           var image = 'dot';
		   if ($(this).hasClass('dot_wab2')) {
			   image = 'dot_wab2';
		   } else if ($(this).hasClass('dot_wab12')) {
			   image = 'dot_wab12';
		   }
		   $(dotImg).attr('src', '/fileadmin/templates/main/images/ui/'+image+'_mini.png') ;
           //$(theCity).css('color', 'black') ;
		   $(theCity).hide(100);
       }) ;


   }) ;

   var alle = "" ;
   switch(LG) {
        case 0:alle = "Alle" ;break ;
        case 1:alle = "Tous" ;break ;
        case 2:alle = "Tutti" ;break ;
   }

   $('#map_dialog_modal').after('<div class="map_all">'+alle+'</div>') ;

   $('.map_all').click(function(e){
       $('#input_searchbox_standort').val(alle) ;
       $('#input_searchbox_standort_value').val("0") ;
       $.modal.close();
		if ($('.hsb_num_treffer').size()) {
		   getHomeSearchboxCurrentCount() ;
	   } else {
		   getKurssucheResults();
	   }
   }) ;

   $('.map_all').hover(function(e){
       $(this).css('cursor', 'pointer') ;
   }) ;

   $('.map_all').mouseenter(function(e){
       $(this).css('color', '#F50000') ;
   }) ;

   $('.map_all').mouseleave(function(e){
       $(this).css('color', 'black') ;
   }) ;
}




function setupLargeMap()
{
   var imgOffsetX = 0 ;
   var imgOffsetY = 0 ;

   var index = 0 ;

   var isWAB;
   isWAB = $('#largemap_wab4u').length > 0 ? true : false;
  
   $('.large_map_entry').each(function()
   {
       // inc

       index++ ;

       // fetch

       var title = $(this).find('.large_map_entry_title').html() ;
       var titleX = parseInt($(this).find('.large_map_entry_text_x').html()) ;
       var titleY = parseInt($(this).find('.large_map_entry_text_y').html()) ;
       var dotX = parseInt($(this).find('.large_map_entry_dot_x').html()) ;
       var dotY = parseInt($(this).find('.large_map_entry_dot_y').html()) ;
       var value = $(this).find('.large_map_entry_value a').attr('href') ;
	   var dotImage = 'dot';
	   if (isWAB) {
			var wabtype = $(this).find('.large_map_entry_wabtype').html() ;
			if (wabtype == 1) {
				dotImage = 'dot_wab2';
			} else if (wabtype == 2) {
				dotImage = 'dot_wab12';
			}
	   }

       // insert

		//$('#large_map_start').after('<div class="map_city_name_'+index+' map_city_name" style="position:absolute;left:'+(titleX+imgOffsetX)+'px;top:'+(titleY+imgOffsetY)+'px;">'+title+'</div>') ;
		$('#large_map_start').after('<div class="map_city_name_'+index+' map_city_name" style="left:'+(dotX+imgOffsetX+25)+'px;top:'+(dotY+imgOffsetY - 2)+'px;">'+title+'</div>') ;
		//$('#large_map_start').after('<div class="map_city_name_'+index+' map_city_name">'+title+'</div>') ;
       $('#large_map_start').after('<div class="mapdot_'+index+' map_dot '+dotImage+'" style="left:'+(dotX+imgOffsetX)+'px;top:'+(dotY+imgOffsetY)+'px;"><img width="21" height="21" src="/fileadmin/templates/main/images/ui/'+dotImage+'_mini.png" alt=""></div>') ;
       

       // convenience

       var theCity = $('.map_city_name_'+index) ;
       var theDot = $('.mapdot_'+index) ;
       var dotImg = $(theDot).find('img') ;

	   // click handlers

       $(theDot).click(function(e){
           window.location = value ;
           // firefox back button fix
           $(dotImg).attr('src', '/fileadmin/templates/main/images/ui/dot_mini.png') ;
           $(theCity).css('color', 'black') ;
           // end firefox back button fix
           
       }) ;

       $(theDot).mouseover(function(e){
		   var image = 'dot';
		   if ($(this).hasClass('dot_wab2')) {
			   image = 'dot_wab2';
		   } else if ($(this).hasClass('dot_wab12')) {
			   image = 'dot_wab12';
		   }
           $(dotImg).attr('src', '/fileadmin/templates/main/images/ui/'+image+'.png') ;
           //$(theCity).css('color', '#F50000') ;
           $(theCity).show(100);
       }) ;

       $(theDot).mouseout(function(e){
           var image = 'dot';
		   if ($(this).hasClass('dot_wab2')) {
			   image = 'dot_wab2';
		   } else if ($(this).hasClass('dot_wab12')) {
			   image = 'dot_wab12';
		   }
		   $(dotImg).attr('src', '/fileadmin/templates/main/images/ui/'+image+'_mini.png') ;
           //$(theCity).css('color', 'black') ;
		   $(theCity).hide(100);
       }) ;

   }) ;

}


// remove whitespace

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
};
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
};
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
};


// stuff used only on the home page

jQuery.fn.makeElementLinkable = function() {
    $(this).css('cursor', 'pointer') ;
    var link = $(this).find('a').attr('href') ;
    $(this).click(function(e){
        window.location = link ;
    }) ;
};

jQuery.fn.setupHoverHome = function() {
    $(this).hover(
        function () {
            $(this).find('.home_flesh img').attr('src', "/fileadmin/templates/main/images/ui/home_flesh_on.png") ;
            $(this).find('h1').css('color', '#F50000') ;
        },
        function () {
            $(this).find('.home_flesh img').attr('src', "/fileadmin/templates/main/images/ui/home_flesh.png") ;
            $(this).find('h1').css('color', '#878787') ;
        }
    );
};

jQuery.fn.setupHoverTeaserEvent = function() {
    $(this).hover(
        function () {
            $(this).css('opacity', '1.0') ;
        },
        function () {
            $(this).css('opacity', '0.7') ;
        }
    );
};




