$(document).ready(function() {
    // Main navigation
    $('#mainnavigation li').hover(
        function () {
            $(this).find('a').addClass('current');
            $(this).find('.popup').fadeIn('fast');
        },
        function () {
            $(this).find('a').removeClass('current');
            $(this).find('.popup').fadeOut('fast');
        }
    );


    // Tooltips
    $("img.tooltip.telephone, img.tooltip").each(function () {
        $(this).tooltip({ // Telephone numbers
            showURL: false,
            fade: 250
    	});
    });


    $('img.image-tooltip').tooltip({
    delay: 0,
    showURL: false,
    extraClass: "image",
    bodyHandler: function() {
        return $("<img/>").attr("src", $(this).attr('rel'));
    }
    });


    // Checkboxes
    $("input:checkbox").addClass('checkbox');


    // Tabs on homepage
    $('dl.tabs').each(function () {
        var tabContainers = $(this).nextAll('div.tab');
        var tabs = $(this);

        $('dd>a', tabs).click(function () {
            tabContainers.hide().filter(this.hash).fadeIn();

            $('dd>a', tabs).removeClass('selected');
            $(this).addClass('selected');

            return false;
        }).filter(':first').click();
    });


    // Carfinder asset, tab alignment...
    var width  = 0;
    var height = 0;

    $('#carfinder dd').not('.last').each(function () {
        width += $(this).outerWidth();
        height = Math.max($(this).outerHeight(), height);
    });

    $('#carfinder dd.last').width(950 - width - 2).height(height - 1);


    // Set height of two dives to equal height
    var boxes = [$('body.home div.vehicleteaserlisting'), $('body.home div.summarypage.small')];
    if (boxes[0].height() < boxes[1].height()) {
        boxes[0].height(boxes[1].height() - 3);
    }


    // ... and AJAX stuff ...
    $('#carfinder select').bind('change', function () {
        findCars(this, true);
    });


    // ... and sliders
    $('.ui-slider').slider({
        range: true,
        min: 0,
        max: 100000,
        slide: function(event, ui) {
            $(this).find('.min').text(CarFinder.Currency + ' ' + ui.values[0]);
            $(this).find('.max').text(CarFinder.Currency + ' ' + ui.values[1]);
        },
        change: function(event, ui) {
            $(this).find('input[name=preis_von]').val(ui.values[0]);
            $(this).find('input[name=preis_bis]').val(ui.values[1]);

            $(this).find('.min').text(CarFinder.Currency + ' ' + ui.values[0]);
            $(this).find('.max').text(CarFinder.Currency + ' ' + ui.values[1]);
        },
        stop: function(event, ui) {
            $(this).find('input[name=preis_von]').val(ui.values[0]);
            $(this).find('input[name=preis_bis]').val(ui.values[1]);

            findCars(this, true);
        }
    });

    // Update slider values
    $('.ui-slider').each(function () {
        var min = $(this).find('input[name=preis_von]').val();
        var max = $(this).find('input[name=preis_bis]').val();

        $(this).slider('values', 0, min);
        $(this).slider('values', 1, max);
    });

    $('#carfinder form').each(function () {
        findCars($(this).find('select').filter(':first'), false);
    });

    // jQuery Lightbox
   /*
 var lb_config = {
        txtImage : 'Bild',
        txtOf : 'von'
    };
*/

    $('#Lightbox_Gallery a.lightbox').lightBox(lb_config);
     
	
    $('#Vehicle_Zoom dl a').lightBox(lb_config);
    
    
   
    $('.fadein img:gt(0)').hide();
    setInterval(function(){
      $('.fadein :first-child').fadeOut()
         .next('img').fadeIn()
         .end().appendTo('.fadein');}, 
      5000);
	

	// Addresses on Homepage
	
	// show first
	$('.address-item:eq(0)').css({"display": "block", "opacity": 0}).animate( {opacity: 1}, 200);
	$('.address-nav a.show:eq(0)').addClass('active');
	
	$('.address-nav a.show').click(function() {
		var i = $('.address-nav a.show').index(this);
		$(this).addClass('active');
		$('.address-nav a.show:lt('+i+')').removeClass('active');
		$('.address-nav a.show:gt('+i+')').removeClass('active');
		$('.address-item:lt('+i+')').hide();
		$('.address-item:gt('+i+')').hide();
		$('.address-item:eq('+i+')').css({"display": "block", "opacity": 0}).animate( {opacity: 1}, 200);
	})
	
	
	
});

// show first image of Lightbox
function showLightbox() {
	$('#Lightbox_Gallery a.lightbox-start').trigger("click")
}
// Helper for ajax/find/cars
function findCars(obj, submit) {
    var form = $(obj).parent().parent();
    
    if (!submit || use_ajax) {
        var div  = form.find('div.resultbox')
        var p    = div.find('p span');
    
        div.addClass('loading');
    
        $.getJSON('carfinder.php', form.serialize(), function (data) {
            p.html(data.resultcount);
            div.removeClass('loading').hide().fadeIn('slow');
        });
    }
    
    if (!use_ajax && submit) {
        form.submit();
    }
}

