
//------------------------------------------------------------------------------

function maillistClass(){
	this.url = '/?module=maillist&media=ajax';

	this.subscribe = function(form){

		new Ajax.Request(this.url, {
			method: 'POST',
			parameters: {action: 'subscribe', name: $F(form.name), email: $F(form.email)},
			onCreate: function(){
				$('maillistMessDiv').update('');
				$('maillistButton').disable();
			},
			onSuccess: function(transport){
				var text = transport.responseText;
				if(text.length > 0 && text.isJSON()){
					alert(text.evalJSON());
				}
				else if(!transport.responseText.blank()){
					$('maillistMessDiv').update(transport.responseText);
				}
			}.bind(this),
			onComplete: function(){
				$('maillistButton').enable();
			},
			onException: function(a, b){
				alert('maillistClass error: #' + b.message);
			}
		});
	}
}

//------------------------------------------------------------------------------

var collageClass = {
	container: 'collage_canvas',
	b_left: 'collage_left',
	b_right: 'collage_right',
	iterator: 0,
	delay: 7,
	timer: null,
	busy: false,

	images: ['main_1.jpg', 'main_2.jpg', 'main_3.jpg'/*, 'main_4.jpg'*/],

	click: function(e){
		var offset = e.element().id == collageClass.b_left ? -1 : 1;
		collageClass.change(offset);
		if(collageClass.timer != null) clearInterval(collageClass.timer);
		collageClass.timer = setInterval(collageClass.change, collageClass.delay * 1000);
	},

	change: function(offset){
		if(collageClass.busy == true) return;
		collageClass.busy = true;

		var offset = offset || 1;
		collageClass.iterator += offset;
		if(collageClass.iterator < 0) collageClass.iterator = collageClass.images.length - 1;
		if(collageClass.iterator > collageClass.images.length - 1) collageClass.iterator = 0;

		new Effect.Opacity(collageClass.container, {
			from: 1.0,
			to: 0.0,
			duration: 0.5,
			afterFinish: function(){
				$(collageClass.container).setStyle({backgroundImage: 'url(/collages/' + collageClass.images[collageClass.iterator] + ')'});
				new Effect.Opacity(collageClass.container, {
					from: 0.0,
					to: 1.0,
					duration: 0.5,
					afterFinish: function(){
						collageClass.busy = false;
					}
				});
			}		
		});
	},

	init: function(){
		if($(collageClass.container)){
			$A(collageClass.images).each(function(i){
				new Element('img', {src: '/collages/' + i});
			});
			$(collageClass.container).setStyle({backgroundImage: 'url(/collages/' + collageClass.images[collageClass.iterator] + ')'});
			$(collageClass.b_left).observe('click', collageClass.click);
			$(collageClass.b_right).observe('click', collageClass.click);
			collageClass.timer = setInterval(collageClass.change, collageClass.delay * 1000);
		}
	}
};

//------------------------------------------------------------------------------

function scrollerClass(container, buttons, options){
	this.container	= $(container);
	this.btn_left	= $(buttons[0]);
	this.bnt_right	= $(buttons[1]);

	this.content = null;
	this.listener = null;
	this.timer = null;
	
	this.container_width = 0;
	this.content_width = 0;
	this.position = 0;

	this.options = Object.extend({
		offset: 5, delay: 100
	}, options);

	this.init = function(){
		this.content = this.container.down('div.scrollContent');
		this.listener = this.action.bindAsEventListener(this);

		$A([this.btn_left, this.bnt_right]).each(function(btn){
			Event.observe(btn, 'mouseover', this.listener);
			Event.observe(btn, 'mouseout', this.listener);
		}.bind(this));

		this.reset();
	};

	this.reset = function(){
		this.container_width = this.container.getDimensions().width;
		this.content_width = this.content.firstDescendant().getDimensions().width;
		this.position = 0;
		this.move();
	};

	this.action = function(e){
		var delta = e.element() === this.btn_left ? 1 : (e.element() === this.bnt_right ? -1 : 0);
		switch(e.type){
			case "mouseover":
			case "mousedown":
				if(this.container_width < this.content_width && delta != 0){
					this.scroll(delta);
					this.timer = setInterval(this.scroll.bind(this, Math.round(delta * this.options.offset)), this.options.delay);
				}
				break;
			case "mouseout":
			case "mouseup":
				clearInterval(this.timer)
				break;
		}
	};

	this.scroll = function(offset){
		if(offset < 0){ // right button (move left)
			this.position = Math.max(this.position + offset, this.container_width - this.content_width);
		}
		if(offset > 0){ // left button (move right)
			this.position = Math.min(this.position + offset, 0);
		}                                                                    

		this.move();
	};

	this.move = function(position){
		this.content.setStyle({marginLeft: this.position + 'px'});
	}

	Event.observe(window, 'load', this.init.bindAsEventListener(this));
	Event.observe(window, 'resize', this.reset.bindAsEventListener(this));
}

//------------------------------------------------------------------------------

Event.observe(document, 'dom:loaded', function(e){
	collageClass.init();

	$$('input[rel="clear"]').each(function(el, i){
		if(el.type.match(/text/i) && !$F(el).blank()){
			var defValue = el.value;
			el.observe('focus', function(e, defValue){
				if(this.value == defValue) this.value = "";
			}.bindAsEventListener(el, defValue));
			el.observe('blur', function(e, defValue){
				if(this.value == "") this.value = defValue;
			}.bindAsEventListener(el, defValue));
		}
	});

	$$('img[rel~="hover"]').each(function(el, i){
		if (el.src.blank() == false) {   
			var defsrc = el.src;   
			var hovsrc = el.src.substring(0, el.src.length - 4) + '_a' + el.src.substring(el.src.length - 4);
			el.observe('mouseover', function(e, src){
				this.src = src;
			}.bindAsEventListener(el, hovsrc));
			el.observe('mouseout', function(e, src){
				this.src = src;
			}.bindAsEventListener(el, defsrc));
			new Element('img', {src: hovsrc});
		}   
	});

	$$('img[rel~="hbg"]').each(function(el, i){
		var defbg = el.up('td').style.backgroundImage;
		var hovbg = 'url(/menu/bg_menu.gif)';
		el.observe('mouseover', function(e, bg){
			this.up('td').setStyle({backgroundImage: bg});
		}.bindAsEventListener(el, hovbg));
		el.observe('mouseout', function(e, bg){
			this.up('td').setStyle({backgroundImage: bg});
		}.bindAsEventListener(el, defbg));
	});
	
	$$('img[rel="fade"]').each(function(el, i){
		el.setOpacity(0.6);
		(function(object, start, finish, ptr){
			object.observe('mouseover', function(e){
				if(ptr !== null) ptr.cancel();
				ptr = new Effect.Appear(object, {duration: 0.3, from: start, to: finish, afterFinish: function(){
					ptr = null;
				}});
			});
			object.observe('mouseout', function(e){
				if(ptr !== null) ptr.cancel();
				ptr = new Effect.Fade(object, {duration: 0.2, from: finish, to: start, afterFinish: function(){
					ptr = null;
				}});
			});
		})(el, 0.6, 1.0, null);
	});	
});
