window.addEvent('domready',function() {
	if ($('footer')) footerglow();
	if ($('gripper')) textscroll();
	if ($$('.interview')) tooltips();
	if ($('videos')) videoovers();
	if ($('collapse')) collapseList();
	if ($('questionsubmit')) submitquestion();
	if ($('paintingslist')) swapcopy();
	if ($('ligallerylighting')) lihits();
	if ($('image_rotate_1')) setupimagerotation();
	externalLinks();
});


function subsectiondivs(subsection) {
	$$('.linkcopy').each(function(el) {
		var mydivs = el.getChildren('div');
		mydivs.each(function(el) {
			if (el.hasClass(subsection)) {
				if (el.hasClass('hidden')) {
					el.removeClass('hidden');
					el.addClass('visible');
				}
			} else {
				if (el.hasClass('visible')) {
					el.removeClass('visible');
					el.addClass('hidden');
				}
			}
		});
	});
}

function enlargeimage() {
	var active = '';
	var gallery = $('rightcopy').get('class');
	var list = $('paintingslist').getChildren('li');
	list.each(function(el) {
		var link = el.getFirst('a');
		if (link.hasClass('hit')) {
			active = link.get('id');
		}
	});
	var imagepath = '/library/images/image-enlarged-'+gallery+'-'+active+'.jpg';
	$('enlargeimage').set('href',imagepath);
}


function swapcopy() {
	enlargeimage();
	// subsection
	var subsection = null;
	var sublinks = $('subsection').getChildren('li').getFirst('a');
	sublinks.each(function(el,index) {
		if (index < 2) {
			// find the current subsection (one with the hit class)
			if (el.hasClass('hit')) {
				subsection = el.get('id');
				subsectiondivs(subsection);
			}
			
			el.onclick = function() {
				subsection = this.get('id');
				subsectiondivs(subsection);
				sublinks.each(function(el) {
					if (el.hasClass('hit')) el.removeClass('hit');
				});
				el.addClass('hit');
				textscroll();
				return false;
			}
		}
	});
	
	var lis = $('paintingslist').getChildren('li');
	lis.each(function(el,index) {
		var link = el.getFirst('a');
		var linkid = link.get('id');
		
		link.onclick = function() {
			var div = $('copy').getChildren('.linkcopy');
			var targetid = 'copy'+this.get('id');
			lis.getChildren('a').each(function(el,index) { 
				if (el.hasClass('hit')) el.removeClass('hit');
			});
			if (!this.hasClass('hit')) {
				this.addClass('hit');
			}
			
			div.each(function(el,index) {
				if (el.get('id') == targetid) {
					if (el.hasClass('hidden')) {
						el.removeClass('hidden');
						el.addClass('visible');						
					}
					
					// find div with current subsection and turn on... turn others off
					var copydivs = el.getChildren('div');
					copydivs.each(function(el,index) {
						if (el.hasClass(subsection)) {
							if (el.hasClass('hidden')) {
								el.removeClass('hidden');
								el.addClass('visible');
							}
						} else {
							if (el.hasClass('visible')) {
								el.removeClass('visible');
								el.addClass('hidden');
							}
						}
					});
					
				} else {
					if (el.hasClass('visible')) {
						el.removeClass('visible');
						el.addClass('hidden');
					}
				}
			});
			enlargeimage();
			textscroll();
			return false;
		}
	});
}

function submitquestion() {
	
	var section = $('videosection').value;
	$('question').onclick = function() { this.set('html',''); }
	$('questionsubmit').onclick = function() {
		var question = $('question').get('value');
		if (question == '' || question == ' ' || question == 'Have a question? Submit it here and look for your answer to appear below.') {
			$('question').set('html','Invalid Question');
		} else {
			var req = new Request({
				method: 'post',
				url: '/library/ajax/send-email.php',
				data: {
					'question' : question,
					'section': section
				},
				onRequest: function() {
					$('question').set('html','Processing your request...');
				},
				onComplete: function(response) {
					$('question').set('value',response);
				}
			}).send();
		}
		
		return false;
	}
}

function collapseList() {
	var list = $('collapse');
	var li = list.getChildren('li');
	li.each(function(el,index) {
		var link = el.getFirst('a');
		link.onclick = function() {
			var div = this.getNext('div');
			div.toggleClass('hidden');
			return false;
		}
	});	
}

function footerglow() {
	var footer = $('footer');
	footer.set('opacity',.5);
	footer.onmouseover = function() { this.fade(1); }
	footer.onmouseout = function() { this.fade(.5); }
}

function textscroll() {
	var el = $('gripper'), copy = $('copy');
	var scrollheight = $('scrollcopy').getSize().y;
	var height = copy.getSize().y;
	var ch = el.getSize().y+2;
	//alert(ch);
	var max = -(height-ch);
	copy.setStyle('margin-top',0);
	
	if (height > scrollheight) {
		$('gripper').set('opacity',1);
		$('scrollcopy').setStyle('width','454px');
		// Create the new slider instance
		var slider = new Slider(el, el.getElement('.knob'), {
			steps: 35,	// There are 35 steps
			range: [0,max],	// Minimum value is 8
			mode: 'vertical',
			onChange: function(value){
				// Everytime the value changes, we change the font of an element
				copy.setStyle('margin-top', value);
			}
		}).set(0);
		
		// Scroll the content element when the mousewheel is used within the 
		// content or the scrollbar element.
		$$(copy, el).addEvent('mousewheel', function(e){	
			e = new Event(e).stop();
			var step = slider.step + e.wheel * 30;	
			slider.set(step);				
		});

		// Stops the handle dragging process when the mouse leaves the document body.
		$(document.body).addEvent('mouseleave',function(){slider.drag.stop()});
		
	} else {
		// hide gripper
		$('scrollcopy').setStyle('width','448px');
		$('gripper').set('opacity',0);
	}
	
	//alert("scroll height: "+scrollheight+"\n"+"copy height: "+height);
}

function videoovers(){
	var list = $('videos').getChildren('li');
	list.each(function(el, index) {
		if (el.hasClass('hit')) {
			el.setStyle('opacity', 1);
		} else {
			el.setStyle('opacity', .6);
			el.onmouseover = function() { 
				this.set('opacity', 1);
				this.setStyle('cursor','pointer');
			}
			el.onmouseout = function() {
				this.set('opacity', .6);
				this.setStyle('cursor','default');
			}
		}
	});
}

function lihits() {
	$('ligallerylighting').onclick = function() { window.location = '/videos/gallery-lighting/'; }
	$('lientrancegallery').onclick = function() { window.location = '/videos/entrance-gallery/'; }
	$('limaingallery').onclick = function() { window.location = '/videos/main-gallery/'; }
	$('licubegallery').onclick = function() { window.location = '/videos/cube-gallery/'; }
	$('lilowergallery').onclick = function() { window.location = '/videos/lower-gallery/'; }
}

function tooltips() {
	var tooltip = new Tips('.tooltip',{
		className: 'tooltips',
		fixed: false,
		offsets: {'x':-200, 'y':-20}
	});
	
	tooltip.addEvent('show', function(tip) {
		tip.fade('in');
	});
	tooltip.addEvent('hide', function(tip) {
		tip.fade('out');
	});
}


function externalLinks() {
	var anchors = $$('a');
	anchors.each(function(el,index) {
		if (el.get('href') && el.get('rel') == 'external') el.set('target','_blank');
	});
}

var current_image = 1;
function setupimagerotation(){
	$('image_rotate_2').fade('hide');
	imagerotation.periodical(5000);
}

function imagerotation(){
	$('image_rotate_'+current_image).fade('out');
		
	current_image++;
	
	if(!$('image_rotate_'+current_image)){
		current_image = 1;
	}
	
	$('image_rotate_'+current_image).fade('in');
}





