﻿/*---------------------------------------------------------------------*\
    Scroller v1.0
    Copyright (c) 2008 by DWS01
\*---------------------------------------------------------------------*/
// Create Object Class
function Scroller(){
    this.initialize.apply(this, arguments);
}

// Array keep Objects
Scroller.ArrayObj = new Array();

// Create Prototype for Object Class
Scroller.prototype = {
    name: 'Scroller',
    version: 20080916,
    initialize: function(){
        // Config for many Scroller Object
        this.index = Scroller.ArrayObj.length;
        Scroller.ArrayObj[this.index] = this;
        
        this.isPause = false;
        this.isStop = false;
        this.scrollTo = 0;
        
        // Options
        this.options = jQuery.extend({
            //id: 'Scroller_' + new Date().getTime(),
            id: 'Scroller_' + this.index, 
            parent: document.body,
            prefixCssClass: 'Scroller',
            width: '100%',
            height: '100px',
            seek: 1,
            timeSeek: 35,
            deplay: 2000,
            direction: 'up', // 'up' | 'down'
            zzz:''}, arguments[0]);
        this.options.direction = this.options.direction.toLowerCase();
                        
        // Array keep Item
        this.Items = new Array();
        this.ItemFocus = null;
                                
        // Check Parent
        if (this.options.parent != document.body) 
        {
            this.options.parent = $('#'+this.options.parent)[0];
            if(!this.options.parent)
            {
                alert('Parent not found in DOM.');
                return false;
            }
        }
                
        // Check Exist Object
        if($('#'+this.options.id)[0])
        {
            alert('Object ID is ' + this.options.id + ' already exist in DOM.');
            return false;
        }
        
        // Create New PBL
        this.element = this.create(this.options.id);
        this.elementToolTip = this.createToolTip();
        this.element.scroller = this;
                        
        // Add Event
        $(this.element).mouseover(this.evtMouseOver);
        $(this.element).mouseout(this.evtMouseOut);
    },
    /*---------------------------------------------------------------------*\
        Object Event 
    \*---------------------------------------------------------------------*/
    evtMouseOver: function(event){
        //alert(this.parent
        if(!this.scroller.isPause) this.scroller.isPause = true; //alert(this.scroller.options.id);
        //window.document.title = 'Over ' + this.isPause + ' Obj ID ' + this.options.id;
    },
    evtMouseOut: function(event){
        this.scroller.isPause = false;
    },
    /*---------------------------------------------------------------------*\
        Create Object
    \*---------------------------------------------------------------------*/
    create: function(id)
    {
        var obj = document.createElement("div");
        this.options.parent.appendChild(obj);
        obj.id = id;        
        obj.className = this.options.prefixCssClass + "_Container";
        obj.style.width = this.options.width;
        obj.style.height = this.options.height;
        return obj;
    },
    /*---------------------------------------------------------------------*\
        Create Object ToolTip
    \*---------------------------------------------------------------------*/
    createToolTip: function()
    {
        var obj = document.createElement("div");
        if(document.body) document.body.appendChild(obj);
        else this.options.parent.appendChild(obj);
        obj.className = this.options.prefixCssClass + "_ItemToolTip";
        
        obj.elementContent = document.createElement("div");
        obj.appendChild(obj.elementContent);
        
        var dvArrow = document.createElement("div");
        obj.appendChild(dvArrow);
        dvArrow.style.position = 'absolute';
        dvArrow.style.background = 'url(Resources/Images/arrowToolTip.gif)';
        dvArrow.style.top = '0px';
        dvArrow.style.right = '-21px';
        dvArrow.style.width = dvArrow.style.height = '21px';
        //dvArrow.className = this.options.prefixCssClass + "_ItemToolTipArrow";        
        //dvArrow.innerHTML = 'asdfasd';
        //dvArrow.style.position = 'absolute';//dvArrow.style.right = '-21px';//alert(dvArrow.className);
                
        $(obj).hide();
        return obj; //obj.innerHTML = '<div style="position:absolute;top:0px;right:-21px;width:21px; height:21px;background:url(JS/arrowToolTip.gif">asdf</div>';        
    },
    /*---------------------------------------------------------------------*\
        Add Item
    \*---------------------------------------------------------------------*/
    addItem: function()
    {
        // Initialize
        var Item = jQuery.extend(
            {
                id: this.options.id + '_Item_' + this.Items.length,
                title: 'Title...',
                width: '100%',
                height: null,
                bgcolor1: '#e2eaed',
                bgcolor2: '#fff',
                contentLenDisplay: null,
                url: null,
                urlTarget: '_blank',
                toolTip: this.Items.length + ' default tooltip',
                toolTipWidth: '700px',
                toolTipHeight: null,
                toolTipPosition: 'left', // 'left' | 'right'
                content: this.Items.length + ' default content'
            }, arguments[0]);
        
        // Check Exist Object
        if($('#'+Item.id)[0])
        {
            alert('Item ID is ' + Item.id + ' already exist in DOM.');
            return false;
        }
        
        // Set Parent for this Item
        Item.parent = this;
                
        // Create Object Item
        Item.obj = document.createElement("div");
        Item.obj.parentItem = Item;
        Item.obj.id = Item.id;
        Item.obj.className = this.options.prefixCssClass + "_Item";
        if(Item.width) Item.obj.style.width = Item.width;
        if(Item.height) Item.obj.style.height = Item.height;        
        Item.obj.style.backgroundColor = (this.Items.length%2 == 0) ? Item.bgcolor1 : Item.bgcolor2;
        Item.obj.innerHTML = (Item.contentLenDisplay && Item.contentLenDisplay > 0)?this.subContent(Item.content,Item.contentLenDisplay):Item.content;
        this.element.appendChild(Item.obj);
        
        // Control ToolTip
        /*
        $(Item.obj).mouseover(function(event){ //alert($(this.parentItem.parent.element).offset().left);
            var eleTip = Item.parent.elementToolTip; 
            if(Item.toolTipWidth) eleTip.style.width = Item.toolTipWidth;
            eleTip.elementContent.innerHTML = this.parentItem.toolTip;
            
            if(Item.toolTipPosition=='left'){
                eleTip.style.left = ($(Item.parent.element).offset().left - $(eleTip).width() - 21) + 'px';
                eleTip.style.top = event.pageY -25+ 'px';//($(Item.parent.element).offset().top - 15) + 'px';
            }
            $(eleTip).show();
            //alert($(document).width());
        });
        $(Item.obj).mouseout(function(event){
            var eleTip = Item.parent.elementToolTip;
            eleTip.elementContent.innerHTML = '';
            $(eleTip).hide();
        });
        */
//        $(Item.obj).mousemove(function(event){
//            var eleTip = this.parentItem.parent.elementToolTip;            
//            eleTip.style.left = event.pageX + 10 + 'px';//eleTip.style.left = event.pageX - ($(eleTip).width()/2) + 'px';
//            eleTip.style.top = event.pageY + 20 + 'px';
//            //document.title = event.pageX + ' x ' + event.pageY;//document.title = this.parentItem.id + ' positon: '+ event.x + ' x '+ event.y; //alert(this.parentItem.parent.elementToolTip.innerHTML);
//        });

        if(Item.url){
            $(Item.obj).click(function(event){
                window.open(Item.url,Item.urlTarget);
            });
        }
        
        // Validate this.element
        if(this.options.direction == 'down') this.element.scrollTop = $(this.element.firstChild).height();
        //window.status = this.element.scrollHeight;
        
        // Add to array Items
        this.Items[this.Items.length] = Item;
        return Item;
    },
    subContent: function(content,len){
        if(content.length <= len) return content;
        var result = content.substring(0,len);
        while((result.length>0) && (result.substring(result.length-1)!=' ')){
            result = result.substring(0,result.length-1);
        }
        return result + '...';
    },    
    /*---------------------------------------------------------------------*\
        up Scroll
    \*---------------------------------------------------------------------*/
    upScroll: function(){
        if(this.isStop) return false;
        this.scrollTo = $(this.element.firstChild).height();
        this.upSeekItem();
    },    
    upSeekItem: function(){//alert(this.isPause); window.document.title = this.isPause;
        if(this.isPause==false)
            this.element.scrollTop+=this.options.seek;
            
        if(this.element.scrollTop < this.scrollTo){
            window.setTimeout('Scroller.ArrayObj['+this.index+'].'+this.options.direction+'SeekItem()', this.options.timeSeek);
        }
        else{
            this.element.appendChild(this.element.firstChild);
            this.element.scrollTop = 0;
            window.setTimeout('Scroller.ArrayObj['+this.index+'].'+this.options.direction+'Scroll()', this.options.deplay);
        }
    },
    /*---------------------------------------------------------------------*\
        down Scroll
    \*---------------------------------------------------------------------*/
    downScroll: function(){
        if(this.isStop) return false;
        this.scrollTo = 0;//this.element.scrollTop - $(this.element.firstChild).height();
        this.downSeekItem();
    },    
    downSeekItem: function(){//alert(this.isPause); window.document.title = this.isPause;
        if(this.isPause==false)
            this.element.scrollTop -= this.options.seek; //window.document.title = 'this.element.scrollTop='+this.element.scrollTop + ' this.scrollTo=' + this.scrollTo;
                    
        if(this.element.scrollTop > this.scrollTo){
            window.setTimeout('Scroller.ArrayObj['+this.index+'].'+this.options.direction+'SeekItem()', this.options.timeSeek);
        }
        else{
            this.element.insertBefore(this.element.lastChild,this.element.firstChild);
            this.element.scrollTop = $(this.element.firstChild).height();
            window.setTimeout('Scroller.ArrayObj['+this.index+'].'+this.options.direction+'Scroll()', this.options.deplay);
        }
    },
    /*---------------------------------------------------------------------*\
        Controls
    \*---------------------------------------------------------------------*/
    start: function(){
        window.setTimeout('Scroller.ArrayObj['+this.index+'].'+this.options.direction+'Scroll()', this.options.deplay);
    },
    stop: function(){
        this.isStop = true;
    }    
}