var CR_PATH = "js/cover_rotator/";
var cr_total = CR_INFO.length;
var cr_current = 1;
var cr_started = true;
var CR_TIMEOUT = 6000;

$(document).ready(function() {
    $("#rotator_placeholder").append(
        '<div id="cover_rotator_top"></div>' +
        '<div id="cover_rotator">' +
        '<div id="cover_rotator_right"><div id="cover_rotator_title">' + CR_INFO[0].title + '</div>' +
        '<div id="cover_rotator_author">' + CR_INFO[0].author +
        '</div>' +
        '<div id="cover_rotator_content">' + CR_INFO[0].content +
        '</div></div>' +
        '<div id="cover_rotator_left">' +
        '<div id="cover_rotator_month">' + CR_MONTH +
        '</div>' +
        '<div id="cover_rotator_image_placeholder">' +
        '<img src="' + CR_INFO[0].image + '" width="199" height="132" alt="" style="cursor:pointer;" onclick="location.href=\'article.php?sid=' + CR_INFO[0].id + '\'" />' +
        '</div></div>' +
        '<div id="cover_rotator_inner">' +
        '<div id="cover_rotator_inner_right"><div id="cover_rotator_toolbar">' +
        '<div id="cover_rotator_tablo">' +
        '<span id="cover_rotator_current">' + cr_current + '</span>' +
        '/' + cr_total +
        '</div>' +
        '<img id="cover_rotator_prev" src="'  + CR_PATH + 'images/prev.jpg" alt="" />' +
        '<img id="cover_rotator_pause" src="' + CR_PATH + 'images/pause.jpg" alt="" />' +
        '<img id="cover_rotator_play" src="'  + CR_PATH + 'images/play.jpg" alt="" style="display: none" />' +
        '<img id="cover_rotator_next" src="'  + CR_PATH + 'images/next.jpg" alt="" />' +
        '</div></div>' +
        '<div id="cover_rotator_inner_left"><div id="cover_rotator_linkbox"><a id="cover_rotator_link" href="article.php?sid=' + CR_INFO[0].id +
        '">Click Here for the full article</a></div></div>' +
        '</div></div>' +
        '<div id="cover_rotator_bottom"></div>'
    );

    $("#cover_rotator_prev").click(function() {
        cr_current--;
        cr_update();
    });

    $("#cover_rotator_next").click(function() {
        cr_play();
    });

    $("#cover_rotator_pause").click(function() {
        cr_started = false;
        $("#cover_rotator_play").show();
        $("#cover_rotator_pause").hide();
    });

    $("#cover_rotator_play").click(function() {
        cr_started = true;
        $("#cover_rotator_play").hide();
        $("#cover_rotator_pause").show();
    });

    $.timer(CR_TIMEOUT, function(timer) {
         if (cr_started) {
             cr_play();
         }
     });
});

function cr_play() {
    cr_current++;
    cr_update();
}

function cr_update(i) {
    if (cr_current > cr_total) {
        cr_current = 1;
    } else if (cr_current < 1) {
        cr_current = cr_total;
    }

    $("#cover_rotator_content").html(CR_INFO[cr_current - 1].content);
    $("#cover_rotator_author").html(CR_INFO[cr_current - 1].author);
    $("#cover_rotator_title").html(CR_INFO[cr_current - 1].title);
    $("#cover_rotator_current").html(cr_current);
    $("#cover_rotator_link").attr("href", "article.php?sid=" + CR_INFO[cr_current - 1].id);
    $("#cover_rotator_image_placeholder").html('<img src="' + CR_INFO[cr_current - 1].image + 
    		'" width="' + CR_INFO[cr_current - 1].width + 
    		'" height="' + CR_INFO[cr_current - 1].height + '" alt="" style="cursor:pointer;"' +
    		'onclick="location.href=\'article.php?sid=' + CR_INFO[cr_current - 1].id + '\'" />');
}

//timer support
jQuery.timer = function (interval, callback) {
    var interval = interval || 100;
    if (!callback) return false;

    _timer = function (interval, callback) {
        this.stop = function () {
            clearInterval(self.id);
        };

        this.internalCallback = function () {
            callback(self);
        };

        this.reset = function (val) {
            if (self.id) clearInterval(self.id);
            var val = val || 100;
            this.id = setInterval(this.internalCallback, val);
        };

        this.interval = interval;
        this.id = setInterval(this.internalCallback, this.interval);

        var self = this;
    };

    return new _timer(interval, callback);
};
