(function(jQuery){

    jQuery.fn.jAutomobile = function(settings) {

        if(window.jAutomobile === true){
            return;
        }
        else{


            window.jAutomobile = true;
            var config = {
                'car' : 'images/car.jpg',
                'easeIn' : 'linear',
                'easeOut' : 'linear',
                'msgIn' : '',
                'msgOut' : ''
            };

            if (settings) jQuery.extend(config, settings);

            return this.each(function() {

                var timer = false;
                
                window.setInterval(function(){
                    timer = new Date();
                },1000);

                var firstMove = true;
                var oboverflw = false;
                var interval = false;
                var resizetimer = false;
                var dh = jQuery(document).height();

                var car = jQuery('<div>');
                var ctl = jQuery('<a>');
                ctl.attr({
                    href : 'javascript:;'
                }).addClass('jAutomobile-ctl').css({
                    position : 'fixed',
                    bottom : 10,
                    right : 10
                }).html('<img src="immagini/chiudi.png" alt"Chiudi" title="Chiudi">').click(function(){
                    ctl.remove();
                    stopAnimation();
                    jQuery('body').css({
                        overflowX : oboverflw
                    });
                    car.remove();
                });


                if(config.msgIn != '' && config.msgOut != ''){
                    car.append('<div class="jAutomobile-msg">'+config.msgIn+'</div>');
                }

                car
                //.addClass('jAutomobile-car')
                .append('<a id="anmz" style="display: block; height: 218px; width: 578px;" href="http://www.incap.it/" target="_blank"><img src="'+config.car+'" alt="Incap" /></a>')
                .append('<img id="anmz" src="'+config.car+'" alt="car" />')
                .css({
                    position : 'absolute',
                    visibility : 'hidden',
                    zIndex : 10000
                });

                

                

                oboverflw = jQuery('body').css('overflow-x');
                jQuery('body').append(car).append(ctl).css({
                    overflowX : 'hidden'
                });

                jQuery('.jAutomobile-msg',car).css({
                    position : 'absolute',
                    display: 'none'
                });

                jQuery(window).bind(
                    'resize scroll',
                    function(){

                        if(resizetimer){
                            window.clearTimeout(resizetimer);
                        }
                        
                        resizetimer = window.setTimeout(
                            function(){
                                if((jQuery(window).height() + jQuery(window).scrollTop()) <dh){
                                    car.css({
                                        //top : (jQuery(window).height() - car.children('img').height() + jQuery(window).scrollTop())
																				top : (jQuery(window).height() - car.children('#anmz').height() + jQuery(window).scrollTop())
                                    });
																}
                            },100
                        );
                        
                    }
                );

                var init = function(){
										//alert(car.children('#anmz').height());
                    car.css({
                        //top : (jQuery(window).height() - car.children('img').height() + jQuery(window).scrollTop()),
												top : (jQuery(window).height() - car.children('#anmz').height() + jQuery(window).scrollTop()),
                        //right : -1 * car.children('img').width()
												right : -1 * car.children('#anmz').width()
                    });

                    carAnimation();

                }

                var carAnimation = function(){

                    jQuery('.jAutomobile-msg',car).hide().text(config.msgIn);

                    car
                    .css({
                        visibility : 'visible'
                    }).animate(
                        {
                            //right : ((jQuery(window).width() - car.children('img').width())/2)
														right : ((jQuery(window).width() - car.children('#anmz').width())/2) 
                        },
                        1000,
                        config.easeIn,
                        function(){
                            jQuery('.jAutomobile-msg',car).fadeIn(200);
                        }
                    );

                    window.setTimeout(
                        function(){
                            jQuery('.jAutomobile-msg',car).fadeOut(200,function(){
                                jQuery('.jAutomobile-msg',car).text(config.msgOut).fadeIn(200)
                            });
                            car
                            .animate(
                                {
                                    //right : (jQuery(window).width() + car.children('img').width())
																		right : (jQuery(window).width() + car.children('#anmz').width())
                                },
                                1000,
                                //config.easeOut,
                                function(){
                                    if(firstMove){
                                        firstMove = false;
                                        loop();
                                    }
                                }
                            );
                        },6000
                    );

                }

                var stopAnimation = function(){
                    if(interval!==false){
                        window.clearInterval(interval);
                    }
                }

                var loop = function(){
                    interval = window.setInterval(function(){
                        init();
                    },10000);
                }

                //car.children('img').load(function(){
								car.children('#anmz').load(function(){
                    init();
                });

            });

        }

    };

})(jQuery);

