/* VerbContent javascript library
 * Used to pull dynamic content out of a verb editor xml file
 */
//Constants
//You shouldn't need to change these!
var xmlContent = 'content/content.xml';
var imgDir = 'images/dynamic/';
var currentPage = '';
function verbContentInit() {
    //Get the current page
    currentPage = window.location.pathname;
    currentPage = currentPage.substring(currentPage.lastIndexOf("/") + 1);
    currentPage = currentPage.split(".");
    currentPage = currentPage[0];
    
    if(currentPage == '') {
        currentPage = "index";
    }
    
    $.ajax({
        type: "GET",
        url: xmlContent,
        dataType: "xml",
        success: function(xml) {
            var page = $(xml).find("page[id='" + currentPage + "']");
            setupImages(page);
            setupText(page);
            setupSlideShow(page);
        }
    });
}

//Apply the src to each image
function setupImages(xml) {
    $(document).find('.verbEditImage').each(function() {
        //Find the element
        var id = $(this).attr('id');
        var element = $(xml).find('element[id="' + id + '"]');
        var url = $(element).find('content').text();
        
        //If the element exists, then change its url
        if(url != "") {
            $(this).attr('src', imgDir + url);
        }
        
    });
}

function setupText(xml) {
    $(document).find('.verbEditText').each(function() {
        //Find the element
        var id = $(this).attr('id');
        var element = $(xml).find('element[id="' + id + '"]');
        var text = $(element).find('content').text();
        
        //If the element exists, then change its text
        if(text != "") {
            $(this).html(text);
        }
    });
}

var imgs = new Array();
var imgCaption = new Array();
var time_out;

function setupSlideShow(xml) {
    $(document).find('.verbEditSlide').each(function() {
        //Find the element 
        var id = $(this).attr('id');
        
        var element = $(xml).find('element[id="' + id + '"]');
        
        $(element).find('content').each(function() {
           imgs.push($(this).text());
        });
        
        slideShow( id );
    });
}

function slideShow(id) {
    //Setup counter
    //This works like a static variable
    if(typeof slideShow.counter == 'undefined') {
        slideShow.counter = 0;
    }
    
    slideShow.counter++;
    var strId = id;
    var slideMod = slideShow.counter % imgs.length;
    $('#' + id).fadeOut('fast', function() {
        $('#' + id).attr('src', 'images/dynamic/' + imgs[slideMod] );
        $('#' + id).fadeIn();
    });
    
    time_out = setTimeout('slideShow("' + strId + '")', '15000');
    
}
