var Tour = {
  XML:null,
  thumbnails:null,
  thumbIndex: 0,
  thumbInterval:null
};

$j(document).ready(function () {
  
  $j.get('free_samples.xml',parseXML);
  
  $j('#PictureGallery li a').live('click', function(event){
    event.preventDefault();
    window.open(this.href, 'FreeSample', 'status=0, toolbar=0, location=0, menubar=0, resizable=1, scrollbars=1');
  });
  
  setupPictureTab(0);
  setupPictureTab(1);
  
})



function setupPictureTab(index){
  $j('#PictureTabs a').eq(index).click(function(event){
    event.preventDefault();
    $j('#PictureTabs a').removeClass('ActiveTab');
    $j(this).addClass('ActiveTab');
    loadPictures(index + 1);
  })
}



function loadPictures(Page){
  
  $j('#PictureGallery li').remove();
  
  if(Page == 1)
  {
    $j(Tour.XML).find('Picture').each(function(index, Element){
      if(index <= 19)
        buildPicGallery(index, Element)
    });  
  }
  else
  {
    $j(Tour.XML).find('Picture').each(function(index, Element){
    if(index > 19)
      buildPicGallery(index, Element)
    });
  }
}



function buildPicGallery(index, Element){
  $j('<li>').append(
      $j('<a>',{href: '/tour/free_samples/samplepictures/' + $j(Element).attr('File')})
        .css('background-image', 'url(/tour/free_samples/samplepictures/thumbs/' + $j(Element).attr('File') + ')')
    )  
  .appendTo($j('#PictureGallery'));
}



function parseXML(Data)
{
  Tour.XML = Data;
  $j(Tour.XML).find('Sample').each(function(){
    var Link;
    
    $j('<li>')
      .append(Link = $j('<a>', {href: '#'})
        .text($j(this).attr('Title')))
      .appendTo($j('#SampleTabs'));
      
    Link.click(tabClick);
      
  });
  
   $j('#SampleTabs a:first').trigger('click');
   
   loadPictures(1);
  
}




function tabClick(event)
{
  event.preventDefault();
  clearInterval(Tour.thumbInterval);
  Tour.thumbIndex = 0;
  $j('#SampleTabs').find('a').removeClass('ActiveTab');
  $j(event.currentTarget).addClass('ActiveTab');
  loadVideo(event.currentTarget)
}





function loadVideo(clickedElement)
{
  var xmlSampleElement = $j(Tour.XML).find('Sample[Title="' + $j(clickedElement).text() + '"]')
  var format = (xmlSampleElement.attr('Format'));
  Tour.thumbnails = xmlSampleElement.find('Thumbnail');
  var files = (xmlSampleElement.find('File'));
  
  $j('.DLDiv').remove();
  
  var i = 1;
  files.each(function(){
    var DIV = $j('<div>',{id: 'DLDiv' + i++, 'class': 'DLDiv'})
      .append($j('<p>',{'class': 'SizeAndFormat'}).text($j(this).attr('Size') + ': ' + $j(this).attr('Format')))
      .append($j('<a>', {'class': 'VideoLink', href: 'http://download.inthecrack.com/tour/free_samples/samplevids/' + $j(this).attr('Location')}).text('Download'))
      .append($j('<p>', {'class': 'Seconds'}).text($j(this).attr('Seconds') + ' seconds'))
      .append($j('<p>', {'class': 'Megabytes'}).text($j(this).attr('Megabytes') + ' MB'))
      .appendTo($j('#SampleVideos'));
      
      if($j(this).attr('Size') == 'Large')
      {
        DIV.find('a').hover(
          function(event){
            $j('#Warning').css('top', $j(event.currentTarget).offsetParent().position().top + 80 + 'px').css('left', '50px').show();
          },
          function(event){
            $j('#Warning').hide();
          }
        );
      }
      
  });
  
  $j('#Previews img').remove();
  
  Tour.thumbnails.each(function(index, Element){
    $j('<img>',{id: 'Thumbnail_' + index, src: '/assets/images/tour/samples/' + $j(Element).attr('File')}).appendTo($j('#Previews'));
  });
  
 
  $j('#Previews').attr('class', format);

  if(Tour.thumbnails.length > 1)
    Tour.thumbInterval = window.setInterval(nextThumb, 3000);  
}



function nextThumb()
{
    if(Tour.thumbIndex == Tour.thumbnails.length - 1){
      $j('#Previews img#Thumbnail_' + Tour.thumbIndex).fadeOut();
      Tour.thumbIndex = 0;
      $j('#Previews img#Thumbnail_' + Tour.thumbIndex++).fadeIn(); 
    }else{
      $j('#Previews img#Thumbnail_' + Tour.thumbIndex).fadeOut();
      $j('#Previews img#Thumbnail_' + ++Tour.thumbIndex).fadeIn();      
    }

}


