var MooToolsIScroll = new Class({
    Implements: [Events, Class.Occlude],
    
    property: 'iscroll',
    
    initialize: function (element) {
        this.element = element;
        
        if (this.occlude()) {
            return this.occluded.occludedInitialize();
        }
        
        return this.occludedInitialize();
    },
    
    occludedInitialize: function () {
        var options;
        
        options = {};
        
	if (this.element.get('iscroll_options')) {
	    options = JSON.decode('{' + this.element.get('iscroll_options') + '}');
        }
	
	this.element.addEvent('sizeChanged', function () {
	    this.refresh();
	}.bind(this));
        
        options.onScrollEnd = function () {
            this.onScrollEnd();
        }.bind(this);
        
        //options.desktopCompatibility = true; // Should be commented-out unless testing
        
        this.desktopCompatibility = options.desktopCompatibility;
        
        if (this.iscroll) {
            this.iscroll.destroy();
        }
        
        this.iscroll = null;
        
        if ((options.desktopCompatibility && (Browser.chrome || Browser.safari)) || Browser.Platform.ios || Browser.Platform.android) {
            this.iscroll = new iScroll(this.element, options);
            
            this.refresh.delay(150, this);
        }
    },
    
    refresh: function () {
        if ((!this.desktopCompatibility || (!Browser.chrome && !Browser.safari)) && !Browser.Platform.ios && !Browser.Platform.android) {
            return; // Not needed if iScroll is not actually used
        }
        
        if (this.iscroll) {
            this.iscroll.refresh();
        }
        else {
            throw "iScroll instance has been destroyed, needs to be re-instantiated";
        }
    },
    
    scrollTo: function (x, y, runtime) {
        if ((!this.desktopCompatibility || (!Browser.chrome && !Browser.safari)) && !Browser.Platform.ios && !Browser.Platform.android) {
            return; // TODO? Add mootools-based scrolling to coordinates for unified interface?
        }
        
        if (this.iscroll) {
            this.iscroll.scrollTo(x, y, runtime);
        }
        else {
            throw "iScroll instance has been destroyed, needs to be re-instantiated";
        }
    },
    
    scrollToElement: function (element, runtime)  {
        if ((!this.desktopCompatibility || (!Browser.chrome && !Browser.safari)) && !Browser.Platform.ios && !Browser.Platform.android) {
            return; // TODO? Add mootools-based scrolling to coordinates for unified interface?
        }
        
        if (this.iscroll) {
            this.iscroll.scrollToElement(element, runtime);
        }
        else {
            throw "iScroll instance has been destroyed, needs to be re-instantiated";
        }
    },
    
    scrollToPage: function (pageX, pageY, runtime)  {
        if ((!this.desktopCompatibility || (!Browser.chrome && !Browser.safari)) && !Browser.Platform.ios && !Browser.Platform.android) {
            return; // TODO? Add mootools-based scrolling to coordinates for unified interface?
        }
        
        if (this.iscroll) {
            this.iscroll.scrollToPage(pageX, pageY, runtime);
        }
        else {
            throw "iScroll instance has been destroyed, needs to be re-instantiated";
        }
    },
    
    destroy: function ()  {
        if ((!this.desktopCompatibility || (!Browser.chrome && !Browser.safari)) && !Browser.Platform.ios && !Browser.Platform.android) {
            return; // Not needed if iScroll is not actually used
        }
        
        if (this.iscroll) {
            this.iscroll.destroy();
            this.iscroll = null;
        }
        else {
            throw "iScroll instance is already destroyed";
        }
    },
    
    onScrollEnd: function () {
        this.fireEvent('scrollend', this.element);
    }
});


function activateScrollers(element) {
    element.getElements('.iscroll_content').each(function (element) {
	new MooToolsIScroll(element);
    }.bind(this));
}

window.element_activators.push(activateScrollers);

window.addEvent('domready', function (event) {
    document.addEvent('touchmove', function (event) {
	event.stop();
    }.bind(this));
}.bind(this));

