// Slideshow-Bilder
// Bilder der Slideshow
var images = new Array( "bilder/slideshow/01.jpg",
						"bilder/slideshow/02.jpg",
						"bilder/slideshow/03.jpg",
						"bilder/slideshow/04.jpg",
						"bilder/slideshow/05.jpg",
						"bilder/slideshow/06.jpg",
						"bilder/slideshow/07.jpg",
						"bilder/slideshow/08.jpg"
			 );

// Beschreibungen der Bilder der Slideshow
 var description = new Array( 	"Autohaus Sperber",
                                "Golf VI GTI / 2009",
                                "Audi RS4 / 2008",
                                "Golf VI GTI / 2009",
                                "Audi RS4 / 2008",
								"Audi Q7 / 2010",
								"Golf VI GTI / 2009",
								"Audi R8 / 2008"
      				);

// Allgemeines
$(document).ready(function() {
	// Navigation
	$('#naviul > li').bind('mouseover', navi_open)
	$('#naviul > li').bind('mouseout',  navi_timer)

	// Slideshow
	slideshow_images = '';
	slideshow_navi = ''
	image_count = images.length;

	for(i=0; i < image_count; i++) {
		slideshow_images = slideshow_images + '<img src="' + images[i] + '" alt="' + description[i] + '" ' + (i == 0 ? ' class="active"':'') + ' id="ssimg-' + i + '" />';
		slideshow_navi = slideshow_navi + '<a href="#" onclick="ss_jump_to(' + i + ')"><img src="bilder/slideshow-' + (i != 0 ? 'i':'') + 'a.png" alt="" id="ssbutton-' + i + '" /></a>';
	}

	document.getElementById('slideshow-container').innerHTML = slideshow_images;
	document.getElementById('ssnavi').innerHTML = slideshow_navi;
	document.getElementById("sstitle").innerHTML = description[0];

	showtime = setInterval("slideshow()", 4000);

	// Angebote
	$('.angebot img').css({opacity: 0.7});

	$('.angebot img').mouseover(function() {
		$(this).css({opacity: 1});
	}).mouseout(function() {
		$(this).css({opacity: 0.7});
	});

    $('.angebot a').imgPreview({
        imgCSS: {width: 260},
        srcAttr: 'rel'
    });

    // Footer
    $('#footer-autohaus-sperber').mouseover(function () {
    	$(this).attr('src', 'bilder/footer-autohaus-sperber-a.jpg');
    }).mouseout(function () {
    	$(this).attr('src', 'bilder/footer-autohaus-sperber-ia.jpg');
    });

    $('#footer-vw').mouseover(function () {
    	$(this).attr('src', 'bilder/footer-vw-a.jpg');
    }).mouseout(function () {
    	$(this).attr('src', 'bilder/footer-vw-ia.jpg');
    });

    $('#footer-audi').mouseover(function () {
    	$(this).attr('src', 'bilder/footer-audi-a.jpg');
    }).mouseout(function () {
    	$(this).attr('src', 'bilder/footer-audi-ia.jpg');
    });
    
    // Mitarbeiter
    $.fn.fancyzoom.defaultsOptions.imgDir='js/jquery/external/fancyzoom/';
    $('#body a').fancyzoom({}); 
});

// Navigation
var timeout    = 500;
var closetimer = 0;
var item = 0;

function navi_open() {
	navi_canceltimer();
	navi_close();
	item = $(this).find('ul').css('visibility', 'visible');}

function navi_close() {
	if(item) {
		item.css('visibility', 'hidden');
	}
}

function navi_timer() {
	closetimer = window.setTimeout(navi_close, timeout);
}

function navi_canceltimer() {
	if(closetimer) {
		window.clearTimeout(closetimer);
		closetimer = null;
	}
}

document.onclick = navi_close;


// Slideshow
function slideshow() {
	var $alt = $('#slideshow-container IMG.active');

    if($alt.next().length) {
    	var $neu = $alt.next();
    } else {
    	var $neu = $('#slideshow-container IMG:first');
    }
    
    document.getElementById("sstitle").innerHTML = $neu.attr("alt");

    var image_alt = $alt.attr("id").substring(6, $alt.attr("id").length);
    document.getElementById("ssbutton-" + image_alt).src = "bilder/slideshow-ia.png";

    var image_neu = $neu.attr("id").substring(6, $neu.attr("id").length);
    document.getElementById("ssbutton-" + image_neu).src = "bilder/slideshow-a.png";

    $alt.addClass('last-active');
    $alt.removeClass('active');
    $neu.css({opacity: 0.0});
    $neu.addClass('active');

    $neu.animate({opacity: 1.0}, 1000, function() {
    	$alt.removeClass('last-active');
    });
}

function ss_jump_to(image) {
	var $alt = $('#slideshow-container IMG.active');

	var $images = $('#slideshow-container IMG');

	for(i = 0; i < $images.length; i++) {
		$images.removeClass('active');
		$images.removeClass('last-active');
	}

    var $neu = $('#ssimg-' + image);

    document.getElementById("sstitle").innerHTML = $neu.attr("alt");

    var image_alt = $alt.attr("id").substring(6, $alt.attr("id").length);
    document.getElementById("ssbutton-" + image_alt).src = "bilder/slideshow-ia.png";

    var image_neu = $neu.attr("id").substring(6, $neu.attr("id").length);
    document.getElementById("ssbutton-" + image_neu).src = "bilder/slideshow-a.png";

    $neu.addClass('active');

    clearInterval(showtime);

    showtime = setInterval("slideshow()", 5000);
}