var list_img = 0;
var current_img = 1;
var _left = 0;
var time_interval = 5000;
var _interval = null;
function init(){
  $.ajax({ url: "config/slide.xml", 
          dataType: "xml", 
          success: function(xml){
            slide_list = '<ul class="slide_wrap">'
           $(xml).find('slide').each(function(){
            if ($(this).attr('video_id')!=undefined){
              video_id = $(this).attr('video_id');
              slide_list += '<li><object width="780" height="500"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=' + video_id + '&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1" /><embed src="http://vimeo.com/moogaloop.swf?clip_id=' + video_id + '&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="780" height="500" wmode="opaque" ></embed></object></li>';
            }else{
              if ($(this).attr('href')!=undefined){
                slide_list += '<li><a href="' + $(this).attr('href') + '" onclick="window.open(this.href);return false;"><img src=" ' + $(this).attr('src') + '" alt="'+ $(this).attr('alt') +'" class="img_slide"/></a></li>';
              }else{
                slide_list += '<li><img src=" ' + $(this).attr('src') + '" alt="'+ $(this).attr('alt') +'" class="img_slide"/></li>';
              }
            }
           });
           slide_list += '<ul>'
           $('#img_home').after('<div class="slide_container">' + slide_list + '</div>');
           $('#img_home').remove();
           list_img = $('ul.slide_wrap li').size();
          }
          
    });
  
   
   
   $('#freccia-dx').click(function(){
    StopSlide();
    if (current_img<list_img){
      current_img++;
      _left = _left - 779;
      $('.slide_wrap').animate({'left': _left + 'px'}, 1000);
    }else{
      current_img=1;
      _left = 0;
      $('.slide_wrap').animate({'left': _left + 'px'}, 1000);
    }
   });
    $('#freccia-sx').click(function(){
     StopSlide();  
    if (current_img>1){
      current_img--;
      _left = _left + 779;
      $('.slide_wrap').animate({'left': _left + 'px'});
    }
   });
  if ($('body').attr('id')=='home'){
     _interval = setInterval( "slideSwitch()", time_interval );
  }
}

function StopSlide(){
  window.clearInterval(_interval)
  _interval = window.setTimeout('_SetInteval()', 8000)
}

function _SetInteval(){
  _interval = setInterval( "slideSwitch()", 5000);
}

function slideSwitch() {
    if (current_img<list_img){
      current_img++;
      _left = _left - 779;
      $('.slide_wrap').animate({'left': _left + 'px'}, 1000);
    }else{
      current_img=1;
      _left = 0;
      $('.slide_wrap').animate({'left': _left + 'px'}, 1000);
    }
}
