/************************************************************************************************************
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

************************************************************************************************************/
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,
			parentdiv: 'newsRotator',
			newsstory: 'newsRotatorStory',
      newstooltip: 'newsRotatorTooltip',
      newsheading: 'newsRotatorHeading',
      newsingress: 'newsRotatorIngress',
      newsnavigator: 'newsRotatorNavigator',
      newslinks : 'newsRotatorNavigatorLinks',
      newslinksactive: 'newsRotatorNavigatorActiveLink',
      shownavigation: true
		},
		inSlideProperties: {
			currentIndex: 0,
			coordinates: []

		}     
	},       
	slideOptions: ['fromLeft', 'fromTop', 'fromRight', 'fromBottom', 'fromTopLeft', 'fromTopRight', 'fromBottomLeft', 'fromBottomRight'],

	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(this.config.userProperties.parentdiv);
		this._parseNews();
		this._createNavigatorPane();
		this._createHintObject();
	},
	_createHintObject: function(){
		var el = this.config.html.hint = new Element('div');
		el.addClass(this.config.userProperties.newstooltip);
		el.setStyles({
			position: 'absolute',
			visibility: 'hidden',
			'z-index': 100000
		});
		this.config.html.main.adopt(el);
	},
	_parseNews: function(){
		this.config.html.stories = this.config.html.main.getElements('.' + this.config.userProperties.newsstory);

		var numberOfStories = this._getNumberOfStories();
		for (var i = 0; i < numberOfStories; i++) {
		  if (i > 0) {
        this.config.html.stories[i].fade(0);
      }
			this.config.html.stories[i].setStyles({
				position: 'absolute',
				'z-index': this.config.html.stories.length - i,
				left: 0,
				top: 0
			});
		}
	},

	_getNumberOfStories: function(){
		return this.config.html.stories.length;
	},

	_getToolTipText: function(storyObject){
		var txt = '';

		var heading = storyObject.getElements('.' + this.config.userProperties.newsheading)[0];
		
		var ingress = storyObject.getElements('.' + this.config.userProperties.newsingress)[0];
		if (heading) {
			txt = txt + '<b>' + heading.get('text') + '</b><br>';
		}
		if (ingress) {
			txt = txt + ingress.get('text');
		}
		return txt;
	},
	_displayTooltip: function(e){
		var storyObj = this.config.html.stories[e.target.getProperty('storyIndex')];

		var txt = this._getToolTipText(storyObj);

		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(){
		var el = this.config.html.navigator = new Element('div');
		el.addClass(this.config.userProperties.newsnavigator);
		this.config.html.main.adopt(el);
		el.setStyle('z-index', 100000);


			var linkDiv = new Element('div');
			linkDiv.addClass(this.config.userProperties.newslinks);
			el.adopt(linkDiv);

	


		var numberOfStories = this._getNumberOfStories();
		for (var i = numberOfStories - 1; i >= 0; i--) {
			var linkDiv = new Element('div');
			
			if (this.config.userProperties.shownavigation) {
			 linkDiv.addClass(this.config.userProperties.newslinks);
			}
			el.adopt(linkDiv);

			var link = new Element('a');
			link.set('html', '');
			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);
			link.addClass('rotatorButton');
			linkDiv.setProperty('storyIndex', i);
			if (i == 0) {
				this._setActiveStory(i);
			}
		}
			var linkDiv = new Element('div');
			linkDiv.addClass(this.config.userProperties.newslinks);
			el.adopt(linkDiv);

		
	},
	_getSlideMethod: function(){
		if (this.config.userProperties.slide != 'random')
			return this.config.userProperties.slide.getRandom();

		return this.slideOptions[Math.floor(Math.random() * 8)];
	},
	_slideInStory: function(){
		var slideMethod = this._getSlideMethod();
		var storyObj = this.config.html.stories[this.config.currentStoryIndex];
    storyObj.hide();
		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 'fromtop':
				position.left = 0;
				position.top = 0 - this.config.html.main.offsetHeight;
				break;
			case 'fromright':
				position.left = this.config.html.main.offsetWidth;
				position.top = 0;
				break;
			case 'frombottom':
				position.top = this.config.html.main.offsetHeight;
				position.left = 0;
				break;
			case 'frombottomleft':
				position.top = this.config.html.main.offsetHeight;
				position.left = 0 - this.config.html.main.offsetWidth;
				break;
			case 'frombottomright':
				position.top = this.config.html.main.offsetHeight;
				position.left = this.config.html.main.offsetWidth;
				break;
			case 'fromtopleft':
				position.top = 0 - this.config.html.main.offsetHeight;
				position.left = 0 - this.config.html.main.offsetWidth;
				break;
			case 'fromtopright':
				position.top = 0 - this.config.html.main.offsetHeight;
				position.left = this.config.html.main.offsetWidth;
				break;
			case 'fade':
			  position.top = 0;
				position.left = 0;
				
			break;
		}
		storyObj.setStyles(position);
    storyObj.show();
    storyObj.fade(1);
    
    		
		
	},
	


	_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){
	
		this._hideTooltip();
		var previousIndex = parseInt(this.config.currentStoryIndex);
		var storyIndex = $(e.target).getProperty('storyIndex');

		if (storyIndex == this.config.currentStoryIndex)
			return;
    var previousStoryObj = this.config.html.stories[previousIndex];
    previousStoryObj.fade(0);			
		this._setActiveStory(storyIndex);
		this._slideInStory();
		return false;
	},
	_clickOnNextLink: function(e){
    var currentStoryIndex = parseInt(this.config.currentStoryIndex);
    this.config.html.stories = this.config.html.main.getElements('.' + this.config.userProperties.newsstory);
		var numberOfStories = parseInt(this._getNumberOfStories());
	
    var storyIndex = parseInt(currentStoryIndex);
    var previousIndex = storyIndex;
    
    if (numberOfStories <= (storyIndex + 1)) {
      storyIndex = 0;
    } else {
      storyIndex = (storyIndex + 1);    
    }
    
    
    if (storyIndex == this.config.currentStoryIndex)
			return;
    var previousStoryObj = this.config.html.stories[previousIndex];
    previousStoryObj.fade(0);
		this._setActiveStory(storyIndex);
		this._slideInStory();
		return false;   
	  /*
		this._hideTooltip();
		var storyIndex = $(e.target).getProperty('storyIndex');
		*/
	},
	_clickOnPreviousLink: function(e){
    var currentStoryIndex = parseInt(this.config.currentStoryIndex);
    this.config.html.stories = this.config.html.main.getElements('.' + this.config.userProperties.newsstory);
		var numberOfStories = parseInt(this._getNumberOfStories());	
    var storyIndex = parseInt(currentStoryIndex);
    var previousIndex = storyIndex;
        
    if (currentStoryIndex == 0) {
      storyIndex = (numberOfStories - 1)
    } else {
      storyIndex = (currentStoryIndex - 1)
    }
    
    if (storyIndex == this.config.currentStoryIndex)
			return;
    var previousStoryObj = this.config.html.stories[previousIndex];
    previousStoryObj.fade(0);			
		this._setActiveStory(storyIndex);
		this._slideInStory();
		return false;   
	  /*
		this._hideTooltip();
		var storyIndex = $(e.target).getProperty('storyIndex');
		*/
	},  	
	
	_setActiveStory: function(indexOfStory){
		if (this.config.currentStoryIndex !== false) {
			$(this.config.html.main.getProperty('id') + '_link' + this.config.currentStoryIndex).removeClass(this.config.userProperties.newslinksactive);
		}		
		this.config.currentStoryIndex = indexOfStory;
		$(this.config.html.main.getProperty('id') + '_link' + this.config.currentStoryIndex).addClass(this.config.userProperties.newslinksactive);
	}
	
});
