﻿var currentItem, items;
items = new Array();

$(document).ready(function()
{
  $.ajax({
    type: "GET",
    url: "xml/superbox.xml",
    dataType: "xml",
    success: parseXml
  });
});

function showItem(i)
{
    $('#content').empty().append($('#templates>#title').clone());
    $('#title').html(items[i].title);
    if(items[i].description.length)
    {
      $('#content').append($('#templates>#description').clone());
      $('#description').html(items[i].description);    
    }
    
    if(items[i].link.length)
    {
      $('#content').append($('#templates>#link').clone());
      $('#link').html(items[i].link);    
    }
    setButtonState();
}

function parseXml(xml)
{
    $('#prev').html($(xml).find("prev_button_label").text()).click(function(){
        if(!$(this).hasClass('disabled')) showItem(++currentItem);
    });
    
    $('#next').html($(xml).find("next_button_label").text()).click(function(){
        if(!$(this).hasClass('disabled')) showItem(--currentItem);
    });
    
    $(xml).find("summary").each(function()
    {
        items.push( {
            title: $(this).find("title").text(),
            description: $(this).find("description").text(),
            link: $(this).find("link").text()
      });
    });
    currentItem = 0;
    showItem(currentItem);
    setButtonState();
}

function setButtonState()
{
    if(currentItem == items.length-1 || items.length == 1)
    {
        $('#prev').addClass('disabled');
    }
    else
    {
        $('#prev').removeClass('disabled');
    }
    
    if(currentItem == 0 || items.length == 1)
    {
        $('#next').addClass('disabled');
    }
    else
    {
        $('#next').removeClass('disabled');
    }        
}
