﻿jQuery.fn.imageRotator = function(obj) {

    // Properties
    var Parent = this;
    this.Obj = obj;

    // Methods    
    this.Url = function() {
        return Parent.Obj.url ? Parent.Obj.url : '';
    }

    this.Interval = function() {
        return Parent.Obj.interval ? Parent.Obj.interval : 1000;
    }

    this.Load = function(cnt) {

        $.ajax({
            url: Parent.Url(),
            contentType: 'text/xml',
            cache:false,
            success: function(xmlDoc) {

                cnt.empty();

                $('Image', xmlDoc).each(function() {

                    var divEle = $('<div></div>');
                    cnt.append(divEle);

                    if (obj.direction == 'horizontal') {
                        divEle.css('width', obj.imageSize);
                        divEle.css('float', 'left');
                        divEle.css('margin', '10px');
                    }

                    var imageUrl = $('ImageUrl', $(this));
                    var navUrl = $('NavigateUrl', $(this));

                    var hrefEle = $('<a href="' + navUrl.text() + '"></a>');
                    divEle.append(hrefEle);

                    hrefEle.append('<img src="' + imageUrl.text() + '" />');

                });

            } // end success

        }); // end ajax  
    }

    return this.each(function() {

        var cnt = $(this);
        Parent.Load(cnt)

        setInterval(function() { Parent.Load(cnt); }, Parent.Interval());

    });

}         // end function  