Slideshow = new Class({

	initialize: function(container, props) {

		this.props = props;
		this.container = $(container);
		this.cntsize = this.container.getSize();
		this.duration = this.props.duration ? this.props.duration : 4000;
		this.default_effect = this.props.default_effect ? this.props.default_effect : 'fade';
		this.image_effect = this.props.image_effect && this.props.image_effect != '' ? this.props.image_effect : this.default_effect;
		this.effect_duration = this.props.effect_duration ? this.props.effect_duration : 5000;
		this.fade_duration = this.props.fade_duration ? this.props.fade_duration : 1500;

		this.current = 0;
		if(this.props.images)this.load();
	},


	load: function(loc) {

		var url = false;

		if(loc && typeOf(loc) != "string") {
			if($(loc).get('linkto'))url = $(loc).get('linkto');
			loc = $(loc).get('rel');
		}

		this.loader = new Image();

		this.loader.src = loc ? BASE_URL + loc.trim() : BASE_URL +  this.props.images[this.current].trim();

		if(url) {
			$(this.loader).addEvent('click', function(){window.open(url);});
			$(this.loader).setStyle('cursor', 'pointer');
		}

		var images = [this.loader.src];

		var loader = new Asset.images(images, {

			onComplete: function() {

				if(this.container.getElement('img')) {

					var img = this.container.getElement('img');

					var iFx = new Fx.Morph(img, {duration: this.effect_duration, onComplete: function() {
						
						img.dispose();
						
					}}).start({opacity: [1,0]});

				}

				this.show(loc);

			}.bind(this)
		});

	},



	show: function(loc) {

		if(this.loader.complete) {

			$(this.loader).inject($(this.container), 'inside');

			var size = this.loader.getSize();

			var effect = this.image_effect && this.image_effect != '' ? this.image_effect : this.default_effect;

			new Fx.Morph(this.loader, {duration: this.fade_duration}).start({opacity: [0,1]});

			if(effect == 'elastic') {

				var myFx = this.current % 2 ? new Fx.Morph(this.loader, {duration: this.effect_duration*2, transition: Fx.Transitions.Elastic.easeOut}).start({'left': [0-size.x, 0], 'top': [0, 0]}) :
				new Fx.Morph(this.loader, {duration: this.effect_duration*2, transition: Fx.Transitions.Elastic.easeOut}).start({'left': [size.x, 0], 'top': [0, 0]});

			}

			else if(effect == 'linear') {

				var myFx = this.current % 2 ? new Fx.Morph(this.loader, {duration: this.effect_duration, transition: Fx.Transitions.linear.easeIn}).start({'left': [0-size.x, 0], 'top': [0, 0]}) :
				new Fx.Morph(this.loader, {duration: this.effect_duration, transition: Fx.Transitions.linear.easeIn}).start({'left': [size.x, 0], 'top': [0, 0]});

			}

			else if(effect == 'bounce') {

				var myFx = this.current % 2 ? new Fx.Morph(this.loader, {duration: this.effect_duration*2, transition: Fx.Transitions.Bounce.easeOut}).start({'left': [0-size.x, 0], 'top': [0, 0]}) :
				new Fx.Morph(this.loader, {duration: this.effect_duration*2, transition: Fx.Transitions.Bounce.easeOut}).start({'left': [size.x, 0], 'top': [0, 0]});
			}

			else if(effect == 'back') {

				var myFx = this.current % 2 ? new Fx.Morph(this.loader, {duration: this.effect_duration, transition: Fx.Transitions.Back.easeOut}).start({'left': [0-size.x, 0], 'top': [0, 0]}) :
				new Fx.Morph(this.loader, {duration: this.effect_duration, transition: Fx.Transitions.Back.easeOut}).start({'left': [size.x, 0], 'top': [0, 0]});

			}

			else {

				var myFx = this.current % 2 ? new Fx.Morph(this.loader, {duration: this.effect_duration}).start({'left': [this.cntsize.x-size.x, 0], 'top': [this.cntsize.y-size.y, 0]}) : 		
				new Fx.Morph(this.loader, {duration: this.effect_duration}).start({'left': [0,this.cntsize.x-size.x], 'top': [0,this.cntsize.y-size.y]});

			}

			this.current++;

			if(this.props.images) {

				if(this.current >= this.props.images.length)this.current = 0;

				var images = [(loc ? BASE_URL + loc.trim() : BASE_URL +  this.props.images[this.current].trim())];
				var loader = new Asset.images(images);

				this.timeout = this.load.delay(this.duration, this);

			}

		}

		else { this.timeout = this.show.delay(100, this); }

	}


});

var dly;

function closeMenu() {

	var scroller = new Fx.Scroll($('gallery-menu-cnt'), {duration: 500, wait: false, onComplete: function(){
	
		$('mspan').setStyle('visibility', 'visible');
	
	}});
	scroller.toRight();

}

function openMenu() {

	clearTimeout(dly);

	$('mspan').setStyle('visibility', 'hidden');

	var scroller = new Fx.Scroll($('gallery-menu-cnt'), {duration: 500, wait: false});
	scroller.toLeft();

}


window.addEvent('domready', function() {

	if($("thumbs-cnt")) {

		var scrollSize = $("thumbs-cnt").getScrollSize();
		var thmSize = $("thumbs-cnt").getSize();
		var scrollPos = 0;
		var scrollSpeed = 0;

		var myShow = new Slideshow('slideshow',{'effect_duration': 1000, 'image_effect': $("thumbs-cnt").getElement('.thumb').get('effect'), 'default_effect': ($('slideshow').get('rel') ? $('slideshow').get('rel') : 'fade')}); 

		myShow.load($("thumbs-cnt").getElement('.thumb'));

		var scroller = new Fx.Scroll($('thumbs-cnt'), {duration: $("thumbs-cnt").getElements('.thumb').length*330, wait: false});
		
		$("thumbs-cnt").addEvent('mousewheel', function(event) {

			event = new Event(event);
			scrollPos = $("thumbs-cnt").getScroll();

			if (event.wheel > 0) {

				scroller.set(0,scrollPos.y-84);
			}

			else {

				scroller.set(0,scrollPos.y+84);

			}
		});
	
		
		$('thumbs-up').addEvents({'mouseenter': function(){
			
			scrollPos = $("thumbs-cnt").getScroll();
			scrollSpeed = scrollPos.y*3;
			scroller = new Fx.Scroll($('thumbs-cnt'), {duration: scrollSpeed*2, wait: false, 'transition': Fx.Transitions.linear});

			scroller.toTop();

			
		},'mouseout': function() {scroller.stop();}});
		
		
		
		$('thumbs-down').addEvents({'mouseenter': function(){
			
			scrollPos = $("thumbs-cnt").getScroll();
			scrollSpeed = (scrollSize.y-scrollPos.y-thmSize.y)*3;
			scroller = new Fx.Scroll($('thumbs-cnt'), {duration: scrollSpeed*2, wait: false, 'transition': Fx.Transitions.linear});

			scroller.toBottom();
			
		},'mouseout': function() {scroller.stop();}});

		$$('.thumb').addEvents({'mouseover': function() {this.fade(0.5);}, 'mouseout': function() {this.fade(1);}, 'click': function() {
		
			myShow.image_effect = this.get('effect');
			myShow.load(this);
		
		}});
	}

	if($('gallery-menu-cnt')) {

		dly = closeMenu.delay(1500);

		$('gallery-menu-cnt').addEvents({'mouseover': function(){openMenu();}, 'mouseout': function(){dly = closeMenu.delay(1500);}});

	}

});
