// Place application-specific JavaScript in here

function loadTour(link){
	Element.update('tourarea',makeEmbedTag(link));
	return false;
}

function openTourPopup(link){
	window.open(link,'tourpopup','width=520,height=520,scrollbars=no,toolbars=no');
	return false;
}

function openKBuilderPopup(link) {
	window.open(link,'tourpopup','width=785,height=459,scrollbars=no,toolbars=no');
	return false;
}

function makeEmbedTag(url,size){
	if(size == 'large') {
		width = '500';
		height = '500';
	}
	else {
		width = '352';
		height = '344';
	}
	
	embed  = '<embed class="qtembed" src="';
	embed += url;
	embed += '" width="'+width+'" height="'+height+'" autoplay="true">';
	return embed;
}

Carousel = Class.create();

Carousel.prototype = {
	initialize: function(id) {
		
		this.options = Object.extend({
			slide_width: 100,
			direction: 'horizontal'
		}, arguments[1] || {});
		
		this.element_id = id;
		this.element = $(id);
		this.previous_link = $(id + '_prev');
		this.next_link = $(id + '_next');
		this.slider = $(id + '_slider');
		
		this.element.makePositioned();
		this.slider.makePositioned();
		
		this.getSlides().each(function(elem){
			if(!elem.hasClassName('noLb')){
				elem.onclick = Lightbox.open;
			}
		})
		
		this.previous_link.carousel = this;
		this.next_link.carousel = this;
		
		if(this.options.direction == "horizontal"){
			num_children = this.getSlides().length
			this.slider.style.width = (num_children * this.options.slide_width) + 'px';
			
			this.previous_link.onclick = this.rewindHorizontal;
			this.next_link.onclick = this.advanceHorizontal;
		}
		else if(this.options.direction == "vertical"){
			this.previous_link.onclick = this.rewindVertical;
			this.next_link.onclick = this.advanceVertical;
		}
	},
	getSlides: function(){
		return $$('#' + this.element_id + ' a');
	},
	advanceHorizontal: function(){
		this.blur();
		increment = -this.carousel.options.slide_width;
		max_left  = -(this.carousel.slider.getWidth() - this.carousel.element.getWidth());
		
		this.carousel.slider.style.position = 'absolute';
		
		if(this.carousel.slider.offsetLeft > (max_left + 10)){
			new Effect.MoveBy(this.carousel.slider, 0, increment);
		}
		
		return false;
	},
	rewindHorizontal: function(){
		this.blur();
		increment = this.carousel.options.slide_width;
		
		if(this.carousel.slider.offsetLeft < 0){
			new Effect.MoveBy(this.carousel.slider, 0, increment);
		}
		return false;
	},
	advanceVertical: function(){
		this.blur();
		increment = -this.carousel.options.slide_width;
		max_top  = -(this.carousel.slider.getHeight() - this.carousel.element.getHeight());
		
		
		if(this.carousel.slider.offsetTop > (max_top + 10)){
			new Effect.MoveBy(this.carousel.slider, increment, 0);
		}
		return false;
	},
	rewindVertical: function(){
		this.blur();
		increment = this.carousel.options.slide_width;

		if(this.carousel.slider.offsetTop < 0){
			new Effect.MoveBy(this.carousel.slider, increment, 0);
		}
		return false;
	}
}

function showBox(elem) {
  var e = document.getElementsByClassName("active")[0];  // Should only be 1 at any given time
  var old_id = e.id.split('_')[1];     // Get the ID of our old item
  var new_id = elem.id.split('_')[1];  // Link is representative of our new item
  
  // Add/Remove class
  Element.removeClassName(e, 'active'); // Ditch the active class off of our current
  Element.addClassName('tab_' + new_id, 'active'); // Add it to our new one
  
  // Show/Hide
  Element.hide('box_' + old_id);
  Element.show('box_' + new_id);
  
  return false;
}

function navTo(controller, box) {
  // Get their selection
  if(!box || box.selectedIndex < 0) return;
  
  var option = box.options[box.selectedIndex].value;
  if(!option) return;
  
  // Fire a redirect
  window.location = controller + "/" + option;
}
