/**
* filename :
* author   : asrada2001@hotmail.com
* date     : 2007-01-12
* desc     : ÃÖ±Ù °Ô½Ã¹° ³×ºñ°ÔÀÌ¼Ç

* »ç¿ë¹ý

½ºÅ©¸³Æ® ·Îµù
<script type="text/javascript" src="/js/latest.js"></script>

°´Ã¼ »ý¼º
var latestObj = new LatestNews();

·Ñ¸µ ¼³Á¤
latestObj.setRolling(bool);

* 2007-08-03 asrada2001@hotmail.com °´Ã¼È­
**/

function LatestNews(){
    var latest_layer = document.getElementById('latest_new_layer');
 
    var sub_layers = new Array();
    var cnt = 0;
	
    for(var i=0,max=latest_layer.childNodes.length; i<max; i++){
        if(latest_layer.childNodes[i].nodeType == 1){
            var temp = latest_layer.childNodes[i];
            temp.model = this;

            sub_layers.push(temp);
            cnt++;
        }
    }
	
    this.layers = sub_layers;
    this.total = cnt;
    this.current = 0;
    this.timer = null;
    this.interval = 3000;
    this.rolling = false;

    window.latest = this;

    this.buttonInit();
}

LatestNews.prototype.next = function(){
    this.current = (this.isLast())?0:++this.current;
    this.go();
}

LatestNews.prototype.prev = function(){
    this.current = (this.isFirst())?this.total - 1:--this.current;
    this.go();
}

LatestNews.prototype.go = function(){
    for(var i=0; i<this.total; i++){
        var display = (i == this.current)?'block':'none';
        this.layers[i].style.display = display;
    }

    this.btnSwap();
}

LatestNews.prototype.btnSwap = function(){
    var btn_prev = document.getElementById('btn_latest_prev_layer');
    var btn_next = document.getElementById('btn_latest_next_layer');

    if(this.current == 0){
        btn_prev.style.visibility = 'hidden';
        btn_next.style.visibility = 'visible';
    }else if(this.current == this.total - 1){
        btn_next.style.visibility = 'hidden';
        btn_prev.style.visibility = 'visible';
    }else{
        btn_prev.style.visibility = 'visible';
        btn_next.style.visibility = 'visible';
    }
}

LatestNews.prototype.isLast = function(){
    if(this.current == (this.total - 1)){
        return true;
    }else{
        return false;
    }
}

LatestNews.prototype.isFirst = function(){
    if(this.current == 0){
        return true;
    }else{
        return false;
    }
}

LatestNews.prototype.setRolling = function(bool){
    this.rolling = bool;

    if(bool){
        this.triggerEvent();
        this.start();
    }else{
        this.stop();
        this.removeEvent();
    }
}

LatestNews.prototype.isRolling = function(){
    return this.rolling;
}

LatestNews.prototype.setInterval = function(interval){
    this.interval = interval;
}

LatestNews.prototype.start = function(){
    if(this.timer == null){
        this.timer = window.setInterval('window.latest.next()',this.interval);
    }
}

LatestNews.prototype.stop = function(){
    if(this.timer != null){
        window.clearInterval(this.timer);
    }

    this.timer = null;
}

LatestNews.prototype.buttonInit = function(){
    var btn_prev = document.getElementById('btn_latest_prev_layer');
    var btn_next = document.getElementById('btn_latest_next_layer');

    btn_prev.model = this;
    btn_next.model = this;

    btn_prev.onclick = function(e){
        this.model.prev();
    }

    btn_next.onclick = function(e){
        this.model.next();
    }
}

LatestNews.prototype.triggerEvent = function(){
    for(var i=0,max=this.layers.length; i<max; i++){
        var temp = this.layers[i];

        temp.onmouseover = function(e){
            this.model.stop();
        }

        temp.onmouseout = function(e){
            this.model.start();
        }
    }
}

LatestNews.prototype.removeEvent = function(){
    for(var i=0,max=this.layer.length; i<max; i++){
        var temp = this.layer[i];

        temp.onmouseover = null;
        temp.onmouseout  = null;
    }
}
