// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

var MenImpulse = Class.create({
		opened_teaser: 0, 
		teaser_to_open: 1,
		count_teasers: 0, //count of teaser items
		slideshow_seconds: 5000, //seconds a teaser item is shown in ms
		show_is_running: false,
		transition_time: 0.6, //time for the transition between two teaser items in seconds

		//shows teaser with given id
		openTeaser: function(id) {
			var instance = this;
			
			instance.teaser_to_open = id;
			if(instance.opened_teaser != instance.teaser_to_open) {
				if($('teaser_item_' + instance.opened_teaser)) {
					$('teaser_item_' + instance.opened_teaser).appear({ duration: instance.transition_time, from: 1, to: 0 });
					instance.deactivateNumber(instance.opened_teaser);
				}

				$('teaser_item_'+ instance.teaser_to_open).appear({ duration: instance.transition_time, from: 0, to: 1 });
				instance.activateNumber(instance.teaser_to_open);
				instance.opened_teaser = instance.teaser_to_open;
			}
			return false;
		},
		
		//stops slideshow before opening teaser with given id
		directOpenTeaser: function(id) {
			var instance = this;
			instance.stopSlideshow();
			instance.openTeaser(id);
			return false;
		},
		
		
		//hover and activ status for number controls
		activateNumber: function(id) {
			$('control_item_' + id).down('img').src = '/images/slideshow/number_' + id + '_active.png';
			
			return false;
		},
		
		//default status for number controls
		deactivateNumber: function(id) {
			var instance = this;
			if(id != instance.opened_teaser || id != instance.teaser_to_open) {
				$('control_item_' + id).down('img').src = '/images/slideshow/number_' + id + '.png';
			}
			
			return false;
		},
		
		//hover and activ status for slideshow controls
		activateControl: function(control) {
			$('control_' + control).down('img').src = '/images/slideshow/control_' + control + '_active.png';
			return false;
		},
		
		//default status for slideshow controls
		deactivateControl: function(control) {
			var instance = this;
			if (control == 'pause' && instance.show_is_running == false) {
			} else {
				$('control_' + control).down('img').src = '/images/slideshow/control_' + control + '.png';
			}
			return false;
		},
		
		//starts the slideshow interval and instantly shows the next teaser item
		startSlideshow: function(id) {
			var instance = this;
			instance.show_is_running = true;
			if(id) {
				instance.openTeaser(id);
			} else {
				instance.nextTeaser();
			}
			instance.slideInterval = setInterval(function() {
				instance.nextTeaser();
			}, instance.slideshow_seconds);
			return false;
		},
		
		//stops the slideshow and clears the interval
		stopSlideshow: function() {
			var instance = this;
			instance.show_is_running = false;
			instance.activateControl('pause');
			clearInterval(instance.slideInterval);
		},
		
		//starts and stops the slideshow.. called by pause button
		toggleSlideshow: function() {
			var instance = this;
			if(instance.show_is_running) {
				instance.activateControl('pause');
				instance.stopSlideshow();
			} else {
				$('control_pause').down('img').src='/images/slideshow/control_pause.png';
				instance.startSlideshow();	
			}
			
			return false;
		},
		
		//shows next teaser item
		nextTeaser: function() {
			var instance = this;
			instance.teaser_to_open += 1;
			if(instance.teaser_to_open > instance.count_teasers) {
				instance.openTeaser(1);
			} else {
				instance.openTeaser(instance.teaser_to_open);
			}
			return false;
		},
		
		//stops slideshow before showing the next teaser item
		directNextTeaser: function() {
			var instance = this;
			instance.stopSlideshow();
			instance.nextTeaser();
			return false;
		},
		
		//shows previous teaser item
		prevTeaser: function() {
			var instance = this;
			instance.teaser_to_open -= 1;
			if(instance.teaser_to_open < 1) {
				instance.openTeaser(instance.count_teasers);
			} else {
				instance.openTeaser(instance.teaser_to_open);
			}
			return false;
		},
		
		//stops slideshow before showing the previous teaser item
		directPrevTeaser: function(id) {
			var instance = this;
			instance.stopSlideshow();
			instance.prevTeaser();
			return false;
		}
		
		
		
	
});



