﻿/* Javascript by Daniel Cohen Gindi (c) danielgindi@gmail.com 054-5655765 */
/* Version: 2009-10-21 */

function fadingNews() { };

fadingNews.prototype = {

    create: function() {
        var _thisObj = this;
        this._thisObj = this;
        this.$IEFilter = navigator.appVersion.indexOf('MSIE');
        this._FNextNews = function() { return _thisObj.nextNews(); };
        this._FAnimationNext = function() { return _thisObj.animationNext(); };
        this._FCreate = function() { return _thisObj._create(); };
        this._create();
    },

    element: null,
    width: null,
    height: null,
    newsDelay: 4000,
    beginDelay: 0,
    news: Array(),
    _oldDiv: null,
    _newDiv: null,
    preload: true,
    _curIdx: -1,
    animationInterval: 10,
    animationDuration: 1000, /*ms*/
    vAlign: 'middle', // top|middle|bottom
    hAlign: 'center', // left|center|right
    animationType: 0,
    ANIMATION_TYPE: {
    	FADE_TOGETHER: 0,
    	FADE_APART: 1
    },

    _create: function() {
        if (typeof (this.element) == typeof ('')) {
            var el = this.getElement(this.element);
            if (el) this.element = el;
            else {
                if (document.readyState == 'complete') return false;
                setTimeout(this._FCreate, 200);
                return false;
            }
        }
        
        this.width = this.getWidth(this.element);
        this.height = this.getHeight(this.element);
        
        var newEl = document.createElement('div');
        newEl.style.position='relative';
        newEl.style.width = this.width + 'px';
        newEl.style.height = this.height + 'px';
        this.element.appendChild(newEl);
        this._element = this.element;
        this.element = newEl;

        if (this.news.length > 0) this.nextNews();
        return true;
    },
    preloadNext: function() {
    	if (this.preload) {
	    	if (!this.preloader) {
	    		this.preloader = document.createElement('div');
	        this.preloader.style.position = 'absolute';
	        this.preloader.style.top = '-1000px';
	        this.preloader.style.width = '0px';
	        this.preloader.style.height = '0px';
	        this.preloader.style.overflow = 'hidden';
	        document.body.appendChild(this.preloader);
	    	}
	      var idx = this._curIdx+1;
	      if (idx >= this.news.length) idx = 0;
	    	this.preloader.innerHTML = this.news[this._curIdx];
	    }
	    else
    	{
    		if (this.preloader) {
    			document.body.removeChild(this.preloader);
    			delete this.preloader;
    			this.preloader = null;
    		}
    	}
    },
    nextNews: function() {
        var needsAbort = false;
        if (this._newDiv) {
            this.animationFinish();
            needsAbort = true;
        }
        
        this.vAlign = this.vAlign.toLowerCase();
        this.hAlign = this.hAlign.toLowerCase();
        if (this.vAlign != 'top' && this.vAlign != 'middle' && this.vAlign != 'bottom')
        	this.vAlign == 'middle';
        if (this.hAlign != 'left' && this.hAlign != 'center' && this.hAlign != 'right')
        	this.hAlign == 'center';

        this._curIdx++;
        if (this._curIdx >= this.news.length) this._curIdx = 0;
        if (this.news.length == 0) return false;
        this._newDiv = document.createElement('div');
        this._newDiv.style.position = 'absolute';
        this._newDiv.style.left = '0px';
        this._newDiv.style.top = '0px';
        this._newDiv.style.width = this.element.style.width;
        this._newDiv.style.height = this.element.style.height;
        var table = document.createElement('table');
        table.style.width = this.element.style.width;
        table.style.height = this.element.style.height;
        table.cellSpacing = '0';
        table.cellPadding = '0';
        table.borderStyle = 'none';
        table.borderCollapse = 'collapse';
        var tbody = document.createElement('tbody');
        var tr = document.createElement('tr');
        var td = document.createElement('td');
        td.style.width = '100%';
        td.style.height = '100%';
        td.style.verticalAlign = this.vAlign;
        td.style.textAlign = this.hAlign;
        td.style.padding = '0';
        td.style.margin = '0';
        td.style.borderStyle = 'none';
        tr.appendChild(td);
        tbody.appendChild(tr);
        table.appendChild(tbody);
        td.innerHTML = this.news[this._curIdx];
        this._newDiv.appendChild(table);
        this.animationStart(needsAbort);
    },
    animationStart: function(needsAbort) {
        try {
		        if (this._newDiv) {
		            if (this.$IEFilter) this._newDiv.style.filter = 'alpha(opacity=0)';
		            this._newDiv.style.opacity = '0';
		            this._newDiv.style.MozOpacity = '0';
		            this._newDiv.style.KhtmlOpacity = '0';
		        }
		        
            this.element.appendChild(this._newDiv);
        
            if (this._oldDiv) this._animationStart = (new Date()).getTime();
            else this._animationStart = (new Date()).getTime() - this.animationDuration / 2;
            if (!needsAbort) setTimeout(this._FAnimationNext, this.animationInterval);
        }
        catch (E) {
        }
    },
    animationFinish: function() {
        if (this._newDiv) {
		        if (this._oldDiv) {
		            this.element.removeChild(this._oldDiv);
		            try { delete this._oldDiv; } catch (E) { };
		        }
            this._oldDiv = this._newDiv;
            this._newDiv = null;
        }
        if (this.$IEFilter) this._oldDiv.style.filter = (this._oldDiv.style.filter_clear = '', this._oldDiv.style.filter_clear);
        this._oldDiv.style.opacity = '';
        this._oldDiv.style.MozOpacity = '';
        this._oldDiv.style.KhtmlOpacity = '';
        if (this.news.length > 1) {
            var d = this.newsDelay - this.animationInterval;
            if (this.beginDelay > 0) {
                d += this.beginDelay;
                this.beginDelay = 0;
            }
            setTimeout(this._FNextNews, d);
        }
        this.preloadNext();
    },
    animationNext: function() {
        var fadePos = (new Date()).getTime() - this._animationStart;
        fadePos /= this.animationDuration;
        if (fadePos > 1) fadePos = 1;
        var fadeArgOld, fadeArgNew;
        
        if (this.animationType != this.ANIMATION_TYPE.FADE_APART) {
        	fadeArgOld = 1 - fadePos;
        	fadeArgNew = fadePos;
        } else {
        	fadeArgOld = 1 - ((fadePos < 0.5) ? fadePos : 0.5) * 2;
        	fadeArgNew = ((fadePos < 0.5) ? 0 : (fadePos - 0.5)) * 2;
        }
        
        if (this._oldDiv) {
            if (this.$IEFilter) this._oldDiv.style.filter = 'alpha(opacity=' + (fadeArgOld * 100) + ')';
            this._oldDiv.style.opacity = fadeArgOld;
            this._oldDiv.style.MozOpacity = this._oldDiv.style.opacity;
            this._oldDiv.style.KhtmlOpacity = this._oldDiv.style.opacity;
        }
        if (this._newDiv) {
            if (this.$IEFilter) this._newDiv.style.filter = 'alpha(opacity=' + (fadeArgNew * 100) + ')';
            this._newDiv.style.opacity = fadeArgNew;
            this._newDiv.style.MozOpacity = this._newDiv.style.opacity;
            this._newDiv.style.KhtmlOpacity = this._newDiv.style.opacity;
        }

        if (fadePos >= 1) {
            this.animationFinish();
            return;
        }

        setTimeout(this._FAnimationNext, this.animationInterval);
    },
		getElement: function(n) { if (n && n.nodeType && n.nodeType == 1) return n; return document.getElementById ? document.getElementById(n) : (document.layers ? document.layers[n] : document.all[n]); },
		getHeight: function(e) {
		    if (e) {
		        if (document.layers) ret = e.clip.height;
		        else {
		            var ret =
			        ((e.clip && e.clip.bottom) ? (e.clip.bottom - e.clip.top) : ((e.document && e.document.height) ? e.document.height : null)) ||
			        e.style.height || e.clientHeight || e.height || e.offsetHeight || e.style.pixelHeight ||
			        e.parentNode.style.height || e.parentNode.clientHeight || e.parentNode.height;
		        }
		    }
		    if (ret) return parseInt(ret); else return null;
		},
		getWidth: function(e) {
		    if (e) {
		        if (document.layers) ret = e.clip.width;
		        else {
		            var ret =
			        ((e.clip && e.clip.right) ? (e.clip.right - e.clip.left) : ((e.document && e.document.width) ? e.document.width : null)) ||
			        e.style.width || e.clientWidth || e.width || e.offsetWidth ||
			        e.parentNode.style.width || e.parentNode.clientWidth || e.parentNode.width;
		        }
		    }
		    if (ret) return parseInt(ret); else return null;
		}
};

