/************************************************************************************************************
Ajax dynamic list
Copyright (C) August 2008  DTHMLGoodies.com, Alf Magne Kalleland

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

Dhtmlgoodies.com., hereby disclaims all copyright interest in this script
written by Alf Magne Kalleland.

Alf Magne Kalleland, 2008
Owner of DHTMLgoodies.com
	
************************************************************************************************************/	
var me;
var slideMethod = 'fromRight';
var moveBack = false;
var storyIndex = 0; 
var maxStoryIndex = 3;
var moveFlag = true; 
if(!window.DHTMLGoodies)window.DHTMLGoodies = {};
DHTMLGoodies.NewsFlasher = new Class( {
	config : {
		html :  {
			main : null,
			stories : [],
			hint : null,
			navigator : null
		},
		currentStoryIndex : false,
		currentZIndex : 100,
		userProperties : {
			slide : 'random',
			duration: 200
		},
		inSlideProperties : {
			currentIndex:0,
			coordinates : []
			
		}	
	},		
	initialize : function(props) {
		this._setProperties(props);
		this._parseHtml();			
		
	},
	_setProperties : function(properties) {			
		this.config.html.main = $(properties.html);
		for(var prop in properties) {
			this.config.userProperties[prop] = properties[prop];
		}
	}
	,
	_parseHtml : function() {			
		this.config.html.main.addClass('.newsRotator');
		this._parseNews();
		this._createNavigatorPane();
		this._createHintObject();
	},
	_createHintObject : function() {
		var el = this.config.html.hint = new Element('div');
		el.addClass('newsRotatorTooltip');
		el.setStyles( {
			position:'absolute',
			visibility : 'hidden',
			'z-index':100000			
		});
		this.config.html.main.adopt(el);
	},
	_parseNews : function() {
//	this.config.html.stories =  getElementByClassName1("newsRotatorStory",this, this.config.html.main.getElementsByTagName('div'));
		this.config.html.stories = this.config.html.main.getElements('.newsRotatorStory');
		for(var i=0;i<this.config.html.stories.length;i++) {
			this.config.html.stories[i].setStyles( {
				position:'absolute',
				'z-index' : this.config.html.stories.length-i,
				left:0,
				top:0
			}			
			);
		}			
	},
	_displayTooltip : function(e) {
		var storyObj = this.config.html.stories[e.target.getProperty('storyIndex')];
		var txt = '';
		
		var heading = storyObj.getElements('.newsRotatorHeading')[0];
		var ingress = storyObj.getElements('.newsRotatorIngress')[0];
		if(heading) {
			txt = txt + '<b>' + heading.get('text') + '</b><br>';
		}
		if(ingress) {
			txt = txt + ingress.get('text');
		}
		if(!txt)return;
		
		this.config.html.hint.set('html',txt);
		this.config.html.hint.setStyle('visibility','visible');
		
	},
	_hideTooltip : function() {
		
		this.config.html.hint.setStyle('visibility','hidden');
	},
	_createNavigatorPane : function() {
	         me=this;
	         temp();
//	    window.onload = this._clickOnNavigationLink.bind(this);
//		var el = this.config.html.navigator = new Element('div');
//		el.addClass('newsRotatorNavigator');	
//		this.config.html.main.adopt(el);
//		el.setStyle('z-index',100000);
//		
//		for(var i=this.config.html.stories.length-1;i>=0;i--) {
//			var linkDiv = new Element('div');
//			linkDiv.addClass('newsRotatorNavigatorLinks');
//			el.adopt(linkDiv);
//			
//			var link = new Element('a');
//			link.set('html',(i+1));
//			link.setProperty('href','#');
//			linkDiv.adopt(link);
//			link.setProperty('id',this.config.html.main.getProperty('id') + '_link' + i);
//			link.addEvent('click',this._clickOnNavigationLink.bind(this));
//			linkDiv.addEvent('mouseover',this._displayTooltip.bind(this));
//			linkDiv.addEvent('mouseout',this._hideTooltip.bind(this));
//			link.setProperty('storyIndex',i);
//			linkDiv.setProperty('storyIndex',i);
//			if(i==0) {
//				this._setActiveStory(i);	
//			}
//		}
		
	},
	_getSlideMethod : function() {
		if(this.config.userProperties.slide!='random')return this.config.userProperties.slide.getRandom();
		var num = Math.floor(Math.random()*8);
		switch(num) {
			case 0:
				return 'fromLeft';
			case 1:
				return 'fromTop';
			case 2:
				return 'fromRight';
			case 3:
				return 'fromBottom';
			case 4:
				return 'fromTopLeft';
			case 5:
				return 'fromTopRight'
			case 6:
				return 'fromBottomLeft'
			case 7:
				return 'fromBottomRight'
		}				
		
	}
	,
	_slideInStory : function() {	
		
		var storyObj = this.config.html.stories[this.config.currentStoryIndex];
		var position =  { 
			'z-index' : this.config.currentZIndex++ 
		};
		slideMethod = slideMethod.toLowerCase();
		switch(slideMethod) 
		{
			case 'fromleft':
				position.left = 0-this.config.html.main.offsetWidth;
				position.top = '0';
				break;
			case 'fromright':
				position.left = this.config.html.main.offsetWidth;
				position.top = 0;
				break;
	    }
		storyObj.setStyles(position);
		this._slide();		
	},
	_slide : function() {
		this._setSlideCoordinates();
		this._performSlide();		
	},
	_performSlide : function() {
		if(this.config.inSlideProperties.currentIndex<this.config.inSlideProperties.numberOfCoordinates) {

			var storyObj = this.config.html.stories[this.config.currentStoryIndex];
			storyObj.setStyles( {
				left : this.config.inSlideProperties.coordinates[this.config.inSlideProperties.currentIndex].left,
				top : this.config.inSlideProperties.coordinates[this.config.inSlideProperties.currentIndex].top					
			});	
			window.refToNewsFlasher = this;
			setTimeout('window.refToNewsFlasher._performSlide()',30);	
		}
		this.config.inSlideProperties.currentIndex++;
	},
	_setSlideCoordinates : function() {
		var numberOfCoordinates = Math.ceil(this.config.userProperties.duration / 20);
		var storyObj = this.config.html.stories[this.config.currentStoryIndex];
		var changes = {
			left : (storyObj.getStyle('left').toInt() * -1) / numberOfCoordinates,
			top : (storyObj.getStyle('top').toInt() * -1) / numberOfCoordinates
		}
		
		
		var initCoordinates = {
			left: storyObj.getStyle('left').toInt(),
			top : storyObj.getStyle('top').toInt()
		} 
		this.config.inSlideProperties.currentIndex = 0;
		this.config.inSlideProperties.coordinates = [];
		this.config.inSlideProperties.numberOfCoordinates = numberOfCoordinates;
		var coord = this.config.inSlideProperties.coordinates;
		for(var i=0;i<numberOfCoordinates;i++) {
			initCoordinates.left += changes.left;
			initCoordinates.top += changes.top;
			if(i==numberOfCoordinates.length-1) {
				initCoordinates.left = initCoordinates.top = 0;
			}
			coord[coord.length] =  {
				left : initCoordinates.left,
				top : initCoordinates.top	
			}			
		}		
	}
	,
	
	_clickOnNavigationLink : function(e) {
//		var storyIndex = $(e.target).getProperty('storyIndex');
//		 var storyIndex = 1;
//		for(var i=0; i<=3; i++){
		if(moveFlag)   
		{
		    if(!moveBack)
		    {
		        if(storyIndex == maxStoryIndex)
                    storyIndex = 0;                
                else
                    storyIndex++;
		    }
		    else
		    {
		        if(storyIndex == 0)
                    storyIndex = maxStoryIndex;
                else
                    storyIndex--;
		    }
            me = this;
            this.config.currentStoryIndex = storyIndex;
		    this._slideInStory();
           
		}
		return false;	
	} 
	,
	_setActiveStory : function(indexOfStory) {
		if(this.config.currentStoryIndex!==false) {
			$(this.config.html.main.getProperty('id') + '_link' + this.config.currentStoryIndex).removeClass('newsRotatorNavigatorActiveLink');	
		}			
		this.config.currentStoryIndex = indexOfStory;
		$(this.config.html.main.getProperty('id') + '_link' + this.config.currentStoryIndex).addClass('newsRotatorNavigatorActiveLink');	
	}
});
function temp(flag)
{
    me._clickOnNavigationLink();        
    if(!flag)
        setTimeout("temp()", 4000);
}

function getElementByClassName1(className, me, elem) {
var retnode = [];
var myclass = new RegExp('\\b'+className+'\\b');
//var elem = me.config.html.main.getElementsByTagName('div')
for (var i = 0; i < elem.length; i++) {
var classes = elem[i].className;
if (myclass.test(classes)) retnode.push(elem[i]);
}
return retnode;
};

function moveForward()
{    
    slideMethod = "fromRight";
    moveBack = false;
    temp(1);
//    WaitSometime();
}

function moveBack1()
{
    slideMethod = "fromLeft";
    moveBack = true;
    temp(1);
//    WaitSometime();
}
function stopSliding(flag)
{
    moveFlag = !flag;   
}
function WaitSometime()
{
    moveFlag = false;
    pause(3000);
    moveFlag = true;
}
function WaitFunct()
{
   
}
 function pause(numberMillis)
 {
    var now = new Date();
    var exitTime = now.getTime() + numberMillis;
    while (true)
    {
    now = new Date();
    if (now.getTime() > exitTime)
    return;
    }
} 