(function () {
    jQuery.fn.infiniteCarousel = function () {
        function repeat(str, n) {
            return new Array( n + 1 ).join(str);
        }
        
        return this.each(function () {
            // magic!
            var wrapper = jQuery('> div', this).css('overflow', 'hidden'),
                slider = wrapper.find('> ul').width(9999),
                items = slider.find('> li'),
                single = items.filter(':first')
                
                singleWidth = single.outerWidth(),
                visible = Math.ceil(wrapper.innerWidth() / singleWidth),
                currentPage = 1,
                pages = Math.ceil(items.length / visible);
                
            /* TASKS */
            
            // 1. pad the pages with empty element if required
            if (items.length % visible != 0) {
                // pad
                slider.append(repeat('<li class="empty" />', visible - (items.length % visible)));
                items = slider.find('> li');
            }
            
            // 2. create the carousel padding on left and right (cloned)
            items.filter(':first').before(items.slice(-visible).clone().addClass('cloned'));
            items.filter(':last').after(items.slice(0, visible).clone().addClass('cloned'));
            items = slider.find('> li');
            
            // 3. reset scroll
            wrapper.scrollLeft(singleWidth * visible);
            
            // 4. paging function
            function gotoPage(page) {
                var dir = page < currentPage ? -1 : 1,
                    n = Math.abs(currentPage - page),
                    left = singleWidth * dir * visible * n;
                
                wrapper.filter(':not(:animated)').animate({
                    scrollLeft : '+=' + left
                }, 500, function () {
                    // if page == last page - then reset position
                    if (page > pages) {
                        wrapper.scrollLeft(singleWidth * visible);
                        page = 1;
                    } else if (page == 0) {
                        page = pages;
                        wrapper.scrollLeft(singleWidth * visible * pages);
                    }
                    
                    currentPage = page;
                });
            }
            
            // 5. insert the back and forward link
            wrapper.after('<a href="#" class="arrow back">&lt;</a><a href="#" class="arrow forward">&gt;</a>');
            
            // 6. bind the back and forward links
            jQuery('a.back', this).click(function () {
                gotoPage(currentPage - 1);
                return false;
            });
            
            jQuery('a.forward', this).click(function () {
                gotoPage(currentPage + 1);
                return false;
            });
            
            jQuery(this).bind('goto', function (event, page) {
                gotoPage(page);
            });
            
            // THIS IS NEW CODE FOR THE AUTOMATIC INFINITE CAROUSEL
            jQuery(this).bind('next', function () {
                gotoPage(currentPage + 1);
            });
        });
    };
})(jQuery);

function clearAllOthersOffers( offerId )
{
	for( var i=1; i<6; i++ )
	{
		if( i != offerId )
		{
			jQuery( '.offer'+i+'b' ).css( 'display', 'none' );
			jQuery( '.offer'+i ).css( 'display', 'block' );
		}
	}
}

function showFirstOffer( offerId, element )
{
	clearAllOthersOffers( offerId );

	if( jQuery(element).children( '.offer'+offerId ).css( 'display' ) == 'none' )
	{
		jQuery(element).children( '.offer'+offerId+'b' ).css( 'display', 'none' );
		jQuery(element).children( '.offer'+offerId ).css( 'display', 'block' );
	}
}

function showSecondOffer( offerId, element )
{
	clearAllOthersOffers( offerId );

	if( jQuery(element).children( '.offer'+offerId ).css( 'display' ) != 'none' )
	{
		jQuery(element).children( '.offer'+offerId ).css( 'display', 'none' );
		jQuery(element).children( '.offer'+offerId+'b' ).css( 'display', 'block' );
	}
}

function updateApteoCamSimulation()
{
	var nbApteoCam		= jQuery('#nbApteoCam').val();
	var optionConfort	= jQuery('#optionConfort').attr('checked');
	var optionISS		= jQuery('#optionISS').attr('checked');

	var monthlyFee		= 0;

	if( nbApteoCam > 0 )
	{
		monthlyFee	+= 19.9;
	}
	if( nbApteoCam > 1 )
	{
		monthlyFee	+= 12.9;
	}
	if( nbApteoCam > 2 )
	{
		monthlyFee	+= 9.9;
	}
	if( nbApteoCam > 3 )
	{
		monthlyFee	+= (nbApteoCam-3)*5.9;
	}

	if( optionConfort )
	{
		monthlyFee	+= 4.9;
	}

	monthlyFee	= Math.round( monthlyFee * 100 ) / 100;

	jQuery('#monthlyFee').html( monthlyFee );
}

function updateApteoCamProSimulation()
{
	var nbApteoCam		= jQuery('#nbApteoCam').val();
	var optionConfort	= jQuery('#optionConfort').attr('checked');
	var optionISS		= jQuery('#optionISS').attr('checked');

	var monthlyFee		= 0;

	if( nbApteoCam > 0 )
	{
		monthlyFee	+= 16.64;
	}
	if( nbApteoCam > 1 )
	{
		monthlyFee	+= 10.79;
	}
	if( nbApteoCam > 2 )
	{
		monthlyFee	+= 8.28;
	}
	if( nbApteoCam > 3 )
	{
		monthlyFee	+= (nbApteoCam-3)*4.94;
	}

	if( optionConfort )
	{
		monthlyFee	+= 4.1;
	}

	monthlyFee	= Math.round( monthlyFee * 100 ) / 100;

	jQuery('#monthlyFee').html( monthlyFee );
}


function updateApteoMonSimulation()
{
	var passerelle	= jQuery('#passerelle').val();
	var tpe		= jQuery('#tpe').val();
	var tpe2	= jQuery('#tpe2').val();

	var monthlyFee	= 0;

	if( passerelle != '' )
	{
		passerelle	= passerelle.split( '_' );
		monthlyFee	+= parseInt( passerelle[1] )/100;
	}

	if( tpe != '' )
	{
		tpe		= tpe.split( '_' );
		monthlyFee	+= parseInt( tpe[1] )/100;
	}

	if( tpe2 != '' )
	{
		tpe2		= tpe2.split( '_' );
		monthlyFee	+= parseInt( tpe2[1] )/100;
	}

	monthlyFee	= Math.round( monthlyFee * 100 ) / 100;

	jQuery('#monthlyFee').html( monthlyFee );
}


$(document).ready(function()
{
	jQuery.noConflict();

	// Arrondis
	if( jQuery(".rounded").length != 0 )
	{
		jQuery(".rounded").corner();
	}
	if( jQuery(".rounded2").length != 0 )
	{
		jQuery(".rounded2").corner();
	}

	// Carousel
	if( jQuery(".infiniteCarousel").length != 0 )
	{
		var autoscrolling = true;

		jQuery('.infiniteCarousel').infiniteCarousel().mouseover(function () {
			autoscrolling = false;
		}).mouseout(function () {
			autoscrolling = true;
		});

		setInterval(function () {
			if (autoscrolling) {
				jQuery('.infiniteCarousel').trigger('next');
			}
		}, 6000);
	}

	// Offres
	if( jQuery('.offer1').length != 0 )
	{
		jQuery('.offer1').mouseover(function(){
			showSecondOffer( 1, jQuery(this).parent() );
		});
		jQuery('.offer1b').mouseout(function(){
			showFirstOffer( 1, jQuery(this).parent() );
		});

		jQuery('.offer2').mouseover(function(){
			showSecondOffer( 2, jQuery(this).parent() );
		});
		jQuery('.offer2b').mouseout(function(){
			showFirstOffer( 2, jQuery(this).parent() );
		});

		jQuery('.offer3').mouseover(function(){
			showSecondOffer( 3, jQuery(this).parent() );
		});
		jQuery('.offer3b').mouseout(function(){
			showFirstOffer( 3, jQuery(this).parent() );
		});

		jQuery('.offer4').mouseover(function(){
			showSecondOffer( 4, jQuery(this).parent() );
		});
		jQuery('.offer4b').mouseout(function(){
			showFirstOffer( 4, jQuery(this).parent() );
		});

		jQuery('.offer5').mouseover(function(){
			showSecondOffer( 5, jQuery(this).parent() );
		});
		jQuery('.offer5b').mouseout(function(){
			showFirstOffer( 5, jQuery(this).parent() );
		});
	}

	// Simulateur Apteo Video Sécurité
	if( jQuery('#ApteoCamSimulator').length != 0 )
	{
		jQuery('#nbApteoCam').bind('blur', function(e) {
			updateApteoCamSimulation();
		});
		jQuery('#optionConfort').bind('click', function(e) {
			updateApteoCamSimulation();
		});
	}
	if( jQuery('#ApteoCamProSimulator').length != 0 )
	{
		jQuery('#nbApteoCam').bind('blur', function(e) {
			updateApteoCamProSimulation();
		});
		jQuery('#optionConfort').bind('click', function(e) {
			updateApteoCamProSimulation();
		});
	}

	// Simulation Apteo Monétique
	if( jQuery('#ApteoMonSimulator').length != 0 )
	{
		jQuery('#passerelle').bind('change', function(e) {
			updateApteoMonSimulation();
		});
		jQuery('#tpe').bind('change', function(e) {
			updateApteoMonSimulation();
		});
		jQuery('#tpe2').bind('change', function(e) {
			updateApteoMonSimulation();
		});
	}
});

