/************************************************************************************************************
DHTML Blocked
Copyright (C) August 2010  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, 2010
Owner of DHTMLgoodies.com

************************************************************************************************************/

if(!window.DG) {
	window.DG = {};
};


DG.ScrollingContent = new Class( {
	Extends: Events,
	config : {
		pause : 1000,
		scrollPixels : 4,
		scrollTimeout : 20,
		autoScroll : true

	},
	internal : {
		isPaused : false,
		pixelsToScroll : 0,
		pixelsScrolled : 0,
	},
	html : {
		el : null,
		scrollingContainer : null,
		currentActiveNewsStory : null
	},

	initialize : function(config) {
		this.html.el = config.el;
		if(config.pause) {
			this.config.pause = config.pause * 1000;
		}
		if(config.scrollPixels) {
			this.config.scrollPixels = config.scrollPixels;
		}
		if(config.scrollTimeout) {
			this.config.scrollTimeout = config.scrollTimeout;
		}
		if(config.autoScroll != null) {
			this.config.autoScroll = config.autoScroll;
		}
		this._setupDom();
		this.nextStory.delay(this.config.pause, this);

	},
	_setupDom : function() {

		var el = new Element('div');
		this.html.scrollingContainer = el.id = this._getUniqueId();
		el.setStyles({
			position: 'absolute',
			width : '100%',
			top : '0px',
			left : '0px'
		});
		$(this.html.el).adopt(el);

		var size = this._getSizeOfContainer();
		$(this.html.el).setStyles({
			width : size.x,
			height : size.y,
			position: 'relative',
			overflow:'hidden'
		})


		var news = $(this.html.el).getElements('.dg-scrolling-content-story');
		var countNews = news.length;
		for(var i=0;i<countNews;i++) {
			news[i].addEvent('mouseover', this._pause.bind(this));
			news[i].addEvent('mouseout', this._resume.bind(this));
			news[i].addEvent('click', this._newsNavigate.bind(this));
			news[i].id = this._getUniqueId();
			if(news[i].getProperty('href')){
				news[i].setStyle('cursor','pointer');
			}
			el.adopt(news[i]);
		}
	},
	_getSizeOfContainer : function() {
		var parentEl = $(this.html.el).getParent();
		var size = parentEl.getSize();
		var width = parentEl.getStyle('width');
		if (width) {
			size.x = width.replace('px','')/1;
		}
		else {
			var paddingLeft = parentEl.getStyle('padding-left').replace('px', '') / 1;
			var paddingRight = parentEl.getStyle('padding-right').replace('px', '') / 1;
			size.x -= (paddingLeft + paddingRight);
			var borderLeft = parentEl.getStyle('border-left-width').replace('px', '') / 1;
			var borderRight = parentEl.getStyle('border-right-width').replace('px', '') / 1;
			size.x -= (borderLeft + borderRight);
		}

		var height = parentEl.getStyle('height');
		if (height) {
			size.y = height.replace('px','')/1;
		}
		return size;
	},
	nextStory : function() {
		this._clearActiveNewsStory();
		var nextNewsStory = this._getNextNewsStory();
		this.internal.pixelsToScroll = this._getNextNewsStory().offsetTop;
		this.internal.pixelsScrolled = 0;
		this._scroll();


	},
	_scroll : function() {
		if (this.internal.pixelsScrolled < this.internal.pixelsToScroll) {
			if (!this.internal.isPaused) {
				this.internal.pixelsScrolled += this.config.scrollPixels;
				if (this.internal.pixelsScrolled > this.internal.pixelsToScroll) {
					this.internal.pixelsScrolled = this.internal.pixelsToScroll;
				}
				$(this.html.scrollingContainer).setStyle('top', this.internal.pixelsScrolled * -1);
			}
			this._scroll.delay(this.config.scrollTimeout, this);
		}
		else {
			this._getReadyForNextStory();
		}
	},

	_getReadyForNextStory : function() {
		var el = $(this.html.scrollingContainer);
		el.setStyle('top','0px');
		el.adopt(el.getElements('.dg-scrolling-content-story')[0]);

		if (this.config.autoScroll) {
			this.nextStory.delay(this.config.pause, this);
		}
	},
	_getNextNewsStory : function() {
		return $(this.html.scrollingContainer).getElements('.dg-scrolling-content-story')[1];
	},
	_getUniqueId : function() {
		return 'dg-scrolling-content-container-' + Math.round(Math.random()*100000);
	},
	_pause : function(e) {
		this.internal.isPaused = true;
		var target = this._getTarget(e);
		if(this.html.currentActiveNewsStory != target.id) {
			this._clearActiveNewsStory();
			this.html.currentActiveNewsStory = target.id;
			target.addClass('dg-scrolling-content-story-mouseover');
		}
	},
	_resume : function(e) {
		this.internal.isPaused = false;
		var target = this._getTarget(e);
		if(this._isChildOfNewsStory(e)){
			this._clearActiveNewsStory();
		}

	},
	_isChildOfNewsStory : function(e) {
		var el = $(e.target).getParent('.dg-scrolling-content-story');
		if(el){
			return true;
		}else{
			return false;
		}
	},
	_clearActiveNewsStory : function() {
		if (this.html.currentActiveNewsStory) {
			$(this.html.currentActiveNewsStory).removeClass('dg-scrolling-content-story-mouseover');
			this.html.currentActiveNewsStory = null;
		}
	},
	_getTarget : function(e) {
		var target = $(e.target);
		var clsNewsStory = 'dg-scrolling-content-story';
		if(!target.hasClass(clsNewsStory)){
			target = target.getParent('.' + clsNewsStory);
		}

		return target;
	},

	_newsNavigate : function(e) {
		var storyEl = this._getTarget(e);
		var href = storyEl.getProperty('href');
		if(href){
			location.href = href;
		}

	}



});
