/* Note to site visitors viewing this file: this is not the typical means of setting up the code. 
See the download file for demonstration of implementation. */

dw_Util.writeStyleRule('div.demo_notes { display:none; }' );
dw_Util.preloadImg('/images/common/loading.gif');

dw_Util.inArray = function(val, ar) {
    for (var i=0; ar[i]; i++) {
        if ( ar[i] == val ) {
            return true;
        }
    }
    return false;
}

// object detection from http://benalman.com/projects/jquery-hashchange-plugin/
dw_Util.supportsHashchange = function() {
    return 'onhashchange' in window && ( !document.documentMode || document.documentMode > 7 );
}

dw_RotatorDemos = {
    current: null, // current demo 
    request: null, timer: null,
    
    setupLinks: function (id) {
        if ( dw_Util.supportsHashchange() ) { return; } // use onhashchange instead of function call onlick
        var links = document.getElementById(id).getElementsByTagName('a');
        for (var i=0; links[i]; i++) {
            if (links[i].className.indexOf('rotator_') !== -1) {
                links[i].onclick = dw_RotatorDemos.getData;
            }
        }
    },
    
    doOnHashChange: function () {
        var _this = dw_RotatorDemos;
        var demo, hash = location.hash.length > 1? location.hash.slice(1): '';
        if ( hash && dw_Util.inArray(hash, DEMOS_AR) ) {
            demo = hash;
        } else if ( _this.current && !location.search ) {
            demo = _this.current;
        } else {
            demo = STARTING_DEMO;
        }
        _this.getData(demo);
    },
    
    getData: function (demo) {
        var _this = dw_RotatorDemos;
        if ( typeof demo != 'string' && this.className ) { // link clicked
            demo = this.className.slice(8); // 'rotator_' length
        }
        // encodeURIComponent
        var url = 'demos/data.php?demo=' + demo + '&i=' + new Date().getTime();
        if ( _this.request ) {
            _this.request.abort();
        }
        if (_this.timer ) {
            clearTimeout(_this.timer); _this.timer = 0;
        }
        var req = _this.request = dw_XHR.makeRequest(url, _this.callback );
        var el = document.getElementById('rotate_' + demo);
        if (req) {
            dw_Rotator.stop(); dw_Rotator.col = [];
            _this.toggleNotes(demo);
            el.innerHTML = '<img src="/images/common/loading.gif" width="99" height="16" alt="" />';
        } else {
            el.innerHTML = 'Unable to retrieve demo.';
        }
        return false; // may be link onclick
    },
    
    toggleNotes: function (id) {
        if (this.current) {
            document.getElementById(this.current).style.display = 'none';
        }
        document.getElementById(id).style.display = 'block';
        this.current = id;
    },
    
    handleData: function () {
        var _this = dw_RotatorDemos;
        var req = _this.request;
        var str = req.responseText;
        var data = str.split('||');
        var el = document.getElementById('rotate_' + _this.current);
        var imgStr = data[0];
        var r = eval( '(' + data[1] + ')' );
        var r2 = eval( '(' + data[2] + ')' );
        if ( imgStr == '[RANDOM]' ) {
            imgStr = dw_getRandomImage(r, true);
        }
        //imgStr += '<p class="rotator_controls"><a class="stop" href="#">Stop</a> | <a class="start" href="#">Restart Rotation</a> </p>';
        el.innerHTML = imgStr;
        dw_Rotator.setup(r, r2);
        //_this.timer = setTimeout( function(){ dw_Rotator.setup(r, r2) }, 100);
        //dw_Rotator.addControls();
    },
    
    handleFailure: function() {
        var _this = dw_RotatorDemos;
        if (_this.current) {
            var el = document.getElementById('rotate_' + _this.current);
            el.innerHTML = 'Unable to retrieve demo.';
        }
    }
}

dw_RotatorDemos.callback = {
    success: dw_RotatorDemos.handleData,
    failure: dw_RotatorDemos.handleFailure
}
