var arrImage = [];
var arrFamily = [];
var arrModel = [];
var slideWidth = 0;
var slide = 0;
var im = 0;
var last = 0;
var totImg = 0;
var prev = 0;
var gallery = '';
xmlRotator = function (myXml) {
    $.ajax({
        url: myXml,
        type: 'GET',
        dataType: 'xml',
        success: function (xml) { /*creo l'array*/

            $('image', xml).each(function () {
                gallery = $(this).parent().attr('name');
                var myUrl = $(this).find('url').text()
                var myModel = $(this).find('model').text()
                var myFamily = $(this).find('family').text()

                arrImage.push(myUrl);
                arrFamily.push(myFamily);
                arrModel.push(myModel);

            })
            totImg = arrImage.length - 1;
            //alert(totImg);
            im = Math.ceil((totImg-1) * Math.random());
            //alert(im);
            if (im > 0) {
                prev = im - 1;
            } else {
                prev = totImg;
            }
            last = im + 1;

            $('#slidesContainer').append('<div id="img' + prev + '" class="slide" style="background:url(' + arrImage[prev] + ') no-repeat 0px 0px;"><div class="desc"><h3>' + gallery + '</h3> <h2><span class="orange">' + arrFamily[prev] + '</span> ' + arrModel[prev] + '</h2></div></div>');

            $('#slidesContainer').append('<div id="img' + im + '" class="slide" style="background:url(' + arrImage[im] + ') no-repeat 0px 0px;"><div class="desc"><h3>' + gallery + '</h3> <h2><span class="orange">' + arrFamily[im] + '</span> ' + arrModel[im] + '</h2></div></div>');

            $('#slidesContainer').append('<div id="img' + last + '" class="slide" style="background:url(' + arrImage[last] + ') no-repeat 0px 0px;"><div class="desc"><h3>' + gallery + '</h3> <h2><span class="orange">' + arrFamily[last] + '</span> ' + arrModel[last] + '</h2></div></div>');

            $('.slide').corner();
            slide = parseInt($('.slide').css('width'));
            slideWidth = slide * 4;
            $('#slidesContainer').css({
                'width': slideWidth
            });
            //alert(slideWidth);

        } // End Success
    }); // End AJAX


    rotate = function (imRem) {
        //Slider Animation
        $('#slidesContainer').animate({
            'left': '-=' + slide
        }, 'slow', function () {
            // Animation complete.
            $('#img' + (imRem)).remove();
            $('#slidesContainer').css({
                'left': -slide
            });
            // alert(imRem);
        });
    };


    //Rotation  and Timing Event
    rotateSwitch = function () {
        if (im == totImg) {
            im = 0;
        } else {
            im++;
        }
        last = im + 1 > totImg ? 0 : im + 1;
        $('#slidesContainer').append('<div id="img' + last + '" class="slide" style="background:url(' + arrImage[last] + ') no-repeat 0px 0px;"><div class="desc"><h3>' + gallery + '</h3> <h2><span class="orange">' + arrFamily[last] + '</span> ' + arrModel[last] + '</h2></div></div>');
        $('.slide').corner();

        rotate((im == 0 ? totImg - 1 : (im - 2 < 0 ? totImg : im - 2)));

    };

    setInterval('rotateSwitch()', 5000);

};
