var CURIMAGE = 0;

var iAantal; 
var iCurCar = 0; 


$(document).ready(
	function()
	{
		if ($("body").attr("id") == 'home')
		{
			bindTeaser();
			setTimeout("bindHome()", 5000);
		}

		iAantal = $("#teaser-titles li").not("#up, #down").length;
		showCar();
		$("#teaser-titles li#up").show();
		$("#teaser-titles li#down").show();
		$("#teaser-content li:first").show();

		$("#footer-wrap ul li:last").addClass('last');

        
        //Pirobox
        $().piroBox({
			my_speed: 400, //animation speed
			bg_alpha: 0.7, //background opacity
			slideShow : false, // true == slideshow on, false == slideshow off
			slideSpeed : 4, //slideshow duration in seconds(3 to 6 Recommended)
            close_all : '.piro_close,.piro_overlay'// add class .piro_overlay(with comma)if you want overlay click close piroBox
        });
        
        
        $(function() {
            setInterval( "leftSlideSwitch()", 5000 );
        });
        
        doHomeTeaser();
        
	}
);


function leftSlideSwitch() {
    var $active = $('.img-left-images.active');

    if ( $active.length == 0 ) $active = $('.img-left-images:last');

    var $next =  $active.next().length ? $active.next()
        : $('.img-left-images:first');

    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
}

/** HEADER ** HEADER ** HEADER ** HEADER ** HEADER ** HEADER ** HEADER **/

doHomeTeaser = function(){
    
    rotateNext(true);
    
    $("#header-thumbs li a").click(function(e)
    {
        e.preventDefault();
        
        clearTimeout(play);
            
        $('#header-thumbs li').removeClass('active');
        $(this).parent().addClass("active");
        headerRotate(true);
    });
}

function headerRotate(timerStart)
{
    $("#header-image li").stop();
    lastActive = $("#header-image li.active");
    curActive = $("#header-image li").eq($('#header-thumbs li.active').index());
    
    lastActive.removeClass("active");
    lastActive.addClass("last-active");
    curActive.addClass("active");
    
    curActive.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            lastActive.removeClass('active last-active');
            
            if(timerStart == true)
            {
                play = setTimeout(rotateNext, 4000);
            }
        });
};


rotateNext = function(){
    var $active = $('#header-thumbs li.active').next();
    if ( $active.length === 0) { 
        var $active = $('#header-thumbs li:first'); 
    }
    $('#header-thumbs li').removeClass('active');
    $active.addClass('active');
    
    headerRotate(true);
}




/** HEADER ** HEADER ** HEADER ** HEADER ** HEADER ** HEADER ** HEADER **/








function bindHome()
{
	$("#content-beam .home"+CURIMAGE).css('z-index', 10).fadeOut(2000);
	CURIMAGE++;
	if (CURIMAGE > 2)
	{
		CURIMAGE = 0;
	}
	$("#content-beam .home"+CURIMAGE).css('z-index', 20).fadeIn(3000);

	setTimeout("bindHome()", 6000);
}

function bindTeaser()
{

	$("#teaser-content li:not(:first)").hide();	

	$("#teaser-titles li:not(#up, #down) a").click(
		function()
		{
			var link = $(this).attr("href");

			$("#teaser-content li:visible").fadeOut(250,
				function()
				{
					$("#teaser-content "+link).fadeIn(200);	
				}
			);
		}
	);

	$("#teaser-titles li").not("#up, #down").each(
		function(i, val)
		{
			if (i > 2)
			{
				$(this).hide();
			}
		}
	);

	$("#teaser-titles li#up a").click(
		function()
		{
			if (iCurCar > 0)
			{
				iCurCar--;
				showCar();
			}

			return false;
		}
	);
	

	$("#teaser-titles li#down a").click(
		function()
		{
			if (iCurCar < (iAantal-3))
			{
				iCurCar++;
				showCar();
			}

			return false;
		}
	);
}

function showCar()
{
	var iEind = iCurCar + 2;

	$("#teaser-titles li.last").removeClass('last');

	$("#teaser-titles li").not("#up, #down").each(
		function (i, val)
		{
			if (i < iCurCar || i > iEind)
			{
				$(this).hide();
			}
			else
			{
				$(this).show();
			}

			if (i == iEind)
			{
				$(this).addClass('last');
			}
		}
	);
}

