// JavaScript Document
addLoadEvent(initTopNews);

var interval;

function initTopNews() {
    if(document.getElementById('TopNews')==null){return}
	else{
	obj = new TopNews();
    obj.abc();
	}
}

/* class TopNews */
function TopNews() 
{    
	
	var div = document.getElementById('TopNews');
    this.numberOfItems = null;
    this.currentItem = 0;
    var that = this; 
    
    if (div) initialize();
      
    function initialize() {
    	var previewBox = document.getElementById('fastpreviewBox');
    	previewBox.parentNode.setAttribute('id', 'masterPreviewBox');		
		previewBox.parentNode.removeChild(previewBox);
		var masterPreviewBox = document.getElementById('masterPreviewBox');
    	
        var photoBox = document.createElement('li');
		photoBox.setAttribute('id', 'photoBox');
        
        var photoAnchor = document.createElement('a');
        photoAnchor.setAttribute('class', 'photoAnchor');
        photoAnchor.setAttribute('id', 'TopNewsPhotoAnchor');
        
        var photoImg = document.createElement('img');
        photoImg.setAttribute('id', 'TopNewsImg');
                
        photoAnchor.appendChild(photoImg);
        photoBox.appendChild(photoAnchor);
        masterPreviewBox.appendChild(photoBox);
        
        var lis = div.getElementsByTagName('li');
        that.numberOfItems = lis.length;
        for (var i = 0; i < lis.length; i++) {
            Show(lis[i],i);
        }
    }
      
    function clearAllListItems() {
        var lis = div.getElementsByTagName('li');
        for (var i = 0; i < lis.length; i++) {
            lis[i].className='';
        }
    }
    
    function setCurrent(li) {
        li.className = 'choose';
        var img = li.getElementsByTagName('img')[0];
        if (img) {
            var src = img.getAttribute('src');
            var TopNewsImg = document.getElementById('TopNewsImg')
            TopNewsImg.setAttribute('src',src);
	
        }
        var a = document.getElementById('TopNewsPhotoAnchor');
        var h3 = li.getElementsByTagName('h3')[0];
        //var h3a = h3.getElementsByTagName('a')[0];
        var newHref = a.getAttribute('href');
        a.setAttribute('href', newHref);
    }
    
    function Show(li,i) {
        if (i==0) setCurrent(li);
        li.onmouseover = function() {
            clearAllListItems();
            setCurrent(this);
            //attachDecoration(this, 'underline');
            clearInterval(interval);
        }
        li.onmouseout = function() {
            //attachDecoration(this, 'none');
            that.abc();
        }
    }
    
    this.bar = function()
    {		
		var lis = div.getElementsByTagName('li');        
		for (var i = 0; i < lis.length; i++) {
            if (i == this.currentItem) {
                lis[i].className = 'choose';
                var img = lis[i].getElementsByTagName('img')[0];
                if (img) {
                    var src = img.getAttribute('src');
                    var TopNewsImg = document.getElementById('TopNewsImg')
                    TopNewsImg.setAttribute('src',src);
                }
            } else {
                lis[i].className='';
            }
        }
        this.currentItem++;
        if (this.currentItem == this.numberOfItems) this.currentItem = 0;
    }

    this.abc = function()
    {
        var self = that;
        interval = setInterval(function() { self.bar(); }, 9000);
    }
    
}


