/*
Script: Fx.Scroll.js
	Effect to smoothly scroll any element, including the window.

	License:
		MIT-style license.

	Authors:
		Valerio Proietti
*/

Fx.Scroll = new Class({

	Extends: Fx,

	options: {
		offset: {x: 0, y: 0},
		wheelStops: true
	},

	initialize: function(element, options){
		this.element = this.subject = document.id(element);
		this.parent(options);
		var cancel = this.cancel.bind(this, false);

		if ($type(this.element) != 'element') this.element = document.id(this.element.getDocument().body);

		var stopper = this.element;

		if (this.options.wheelStops){
			this.addEvent('start', function(){
				stopper.addEvent('mousewheel', cancel);
			}, true);
			this.addEvent('complete', function(){
				stopper.removeEvent('mousewheel', cancel);
			}, true);
		}
	},

	set: function(){
		var now = Array.flatten(arguments);
		this.element.scrollTo(now[0], now[1]);
	},

	compute: function(from, to, delta){
		return [0, 1].map(function(i){
			return Fx.compute(from[i], to[i], delta);
		});
	},

	start: function(x, y){
		if (!this.check(x, y)) return this;
		var offsetSize = this.element.getSize(), scrollSize = this.element.getScrollSize();
		var scroll = this.element.getScroll(), values = {x: x, y: y};
		for (var z in values){
			var max = scrollSize[z] - offsetSize[z];
			if ($chk(values[z])) values[z] = ($type(values[z]) == 'number') ? values[z].limit(0, max) : max;
			else values[z] = scroll[z];
			values[z] += this.options.offset[z];
		}
		return this.parent([scroll.x, scroll.y], [values.x, values.y]);
	},

	toTop: function(){
		return this.start(false, 0);
	},

	toLeft: function(){
		return this.start(0, false);
	},

	toRight: function(){
		return this.start('right', false);
	},

	toBottom: function(){
		return this.start(false, 'bottom');
	},

	toElement: function(el){
		var position = document.id(el).getPosition(this.element);
		return this.start(position.x, position.y);
	},

	scrollIntoView: function(el, axes, offset){
		axes = axes ? $splat(axes) : ['x','y'];
		var to = {};
		el = document.id(el);
		var pos = el.getPosition(this.element);
		var size = el.getSize();
		var scroll = this.element.getScroll();
		var containerSize = this.element.getSize();
		var edge = {
			x: pos.x + size.x,
			y: pos.y + size.y
		};
		['x','y'].each(function(axis) {
			if (axes.contains(axis)) {
				if (edge[axis] > scroll[axis] + containerSize[axis]) to[axis] = edge[axis] - containerSize[axis];
				if (pos[axis] < scroll[axis]) to[axis] = pos[axis];
			}
			if (to[axis] == null) to[axis] = scroll[axis];
			if (offset && offset[axis]) to[axis] = to[axis] + offset[axis];
		}, this);
		if (to.x != scroll.x || to.y != scroll.y) this.start(to.x, to.y);
		return this;
	}

});


/*
---

script: Fx.Scroll.Carousel.js

description: Extends Fx.Scroll to work like a carousel

license: MIT-style license.

authors: Ryan Florence

docs: http://moodocs.net/rpflo/mootools-rpflo/Fx.Scroll.Carousel

requires:
- more/1.2.4.2: [Fx.Scroll]

provides: [Fx.Scroll.Carousel]

...
*/


Fx.Scroll.Carousel = new Class({

	Extends: Fx.Scroll,

		options: {
			mode: 'horizontal',
			childSelector: false,
			loopOnScrollEnd: true
		},

	initialize: function(element, options){
		this.parent(element, options);
		this.cacheElements();
		this.currentIndex = 0;
	},

	cacheElements: function(){
		var cs = this.options.childSelector;
		if(cs){
			els = this.element.getElements(cs);
		} else if (this.options.mode == 'horizontal'){
			els = this.element.getElements(':first-child > *');
		} else {
			els = this.element.getChildren();
		}
		this.elements = els;
		return this;
	},

	toNext: function(){
		if(this.checkLink()) return this;
		this.currentIndex = this.getNextIndex();
		this.toElement(this.elements[this.currentIndex]);
		this.fireEvent('next');
		return this;
	},

	toPrevious: function(){
		if(this.checkLink()) return this;
		this.currentIndex = this.getPreviousIndex();
		this.toElement(this.elements[this.currentIndex]);
		this.fireEvent('previous');
		return this;
	},

	getNextIndex: function(){
		this.currentIndex++;
		if(this.currentIndex == this.elements.length || this.checkScroll()){
			this.fireEvent('loop');
			this.fireEvent('nextLoop');
			return 0;
		} else {
			return this.currentIndex;
		};
	},

	getPreviousIndex: function(){
		this.currentIndex--;
		var check = this.checkScroll();
		if(this.currentIndex < 0 || check) {
			this.fireEvent('loop');
			this.fireEvent('previousLoop');
			return (check) ? this.getOffsetIndex() : this.elements.length - 1;
		} else {
			return this.currentIndex;
		}
	},

	getOffsetIndex: function(){
		var visible = (this.options.mode == 'horizontal') ?
			this.element.getStyle('width').toInt() / this.elements[0].getStyle('width').toInt() :
			this.element.getStyle('height').toInt() / this.elements[0].getStyle('height').toInt();
		return this.currentIndex + 1 - visible;
	},

	checkLink: function(){
		return (this.timer && this.options.link == 'ignore');
	},

	checkScroll: function(){
		if(!this.options.loopOnScrollEnd) return false;
		if(this.options.mode == 'horizontal'){
			var scroll = this.element.getScroll().x;
			var total = this.element.getScrollSize().x - this.element.getSize().x;
		} else {
			var scroll = this.element.getScroll().y;
			var total = this.element.getScrollSize().y - this.element.getSize().y;
		}
		return (scroll == total);
	},

	getCurrent: function(){
		return this.elements[this.currentIndex];
	}

});


var doc_loaded = false;
var lay_text = false;
var main_flash_text = null;
var flash_DOM = null;
var picturepage = false;

function init_picture_detail_text(){
	picturepage = ($('image_scroller') != null) ? true : false;
	main_flash_text = $('image-text');
	flash_DOM = (picturepage) ? $(flash_id) : null;

	if(lay_text == true) lay_down_text(flash_DOM.get_tag_value(current_pic, 'img_text'), flash_DOM.get_tag_value(current_pic, 'img_link'));
	doc_loaded = true;
}

function switch_pic(ind)
{
	current_pic = ind;
	lay_down_text(flash_DOM.get_tag_value(ind, 'img_text'), flash_DOM.get_tag_value(ind, 'img_link'));
}

function flash_xml_parsed_handler(info_json){
	init_picture_detail_text();
	if (doc_loaded == false) lay_text = true;
	else lay_down_text(flash_DOM.get_tag_value(current_pic, 'img_text'), flash_DOM.get_tag_value(current_pic, 'img_link'));
}

function lay_down_text(text, link){
	var clean_text = text.replace('<p>', '').replace('</p>', '');
	var lines = clean_text.split('<br />');
	if (lines.length == 1) lines = clean_text.split('<br/>');
	if (lines.length == 1) lines = clean_text.split('<br>');
	if (lines.length == 1) lines = clean_text.split('\n');

	main_flash_text.set('html', text);
}

