var Custom = new Class();			
	
// Use this method for any duration to allow transitions to be turned off
Custom.showTransitions = true;
Custom.getDuration = function(duration) {
	return Custom.showTransitions && duration != null ? duration : 0;
};

Element.implement({
	getFullHeight: function() {
		return this.offsetHeight.toInt() +
			this.getStyle('marginTop').toInt() +
			this.getStyle('marginBottom').toInt();
	},
	getFullWidth: function() {
		return this.offsetWidth.toInt() +
			this.getStyle('marginLeft').toInt() +
			this.getStyle('marginRight').toInt();
	}
});

/* Gallery */
Custom.Gallery = new Class({
	Implements: Options,

	options: {
		fadeDuration:700,
		display:0,
		thumbFaded:0.5,
		thumbFadeDuration:200,
		addHolderLink:true,
		imageElements:'img',
		align:'left'
	},

	initialize: function(holder, thumbnails, thumbnailHolder, options) {
		this.setOptions(options);

		this.holder = $(holder);
		this.images = this.holder.getElements(this.options.imageElements);

		if(thumbnails)
			this.thumbnails = $(thumbnails).getElements(thumbnailHolder);

		this.currentIndex = -1;
		this.maxIndex = this.images.length - 1;

		if(this.maxIndex < 0)
			return;

		var align = (this.options.align == 'right') ? 'right' : 'left';

		this.images.each(function(item, index) {
			item = item.setStyles({
				position:'absolute',
				//left:'0px',
				//top:'0px',
				opacity:0
			});

			if(align != 'left')
			item.setStyle(align, '0px');
			item.set('tween', {'link':'cancel','duration': Custom.getDuration(this.options.fadeDuration)});

			if(item.get('tag') == 'a' || item.getParent().get('tag') == 'a')
				this.options.addHolderLink = false;
		}, this);

		// If images don't already have links on them, add next() link to holder
		if(this.options.addHolderLink) {
			this.holder.setStyles({
				cursor:'pointer'
			}).addEvent('click', this.next.bind(this));
		}

		if(this.thumbnails) {
			this.thumbnails.each(function(item, index) {
				item
				.setStyles({cursor:'pointer','opacity':this.options.thumbFaded})
				.set('tween', {'link':'cancel','duration': Custom.getDuration(this.options.thumbFadeDuration)})
				.addEvent('click', function() {
					this.display(index);
				}.bind(this))
				.addEvent('mouseover', function() {
					item.tween('opacity', 1);
				}.bind(this))
				.addEvent('mouseout', function() {
					if(index != this.currentIndex)
						item.tween('opacity', this.options.thumbFaded);
				}.bind(this));
			}, this);
		}

		// Make sure image is loaded before displaying
		var img = this.images[this.options.display];
		if(img.get('tag') != 'img')
			img = img.getElement('img');
		if(!img || img.complete) this.display(this.options.display, true);
		else img.onload = this.display.bind(this, [this.options.display, true]);
	},

	display: function(index, firstTime) {
		if(this.currentIndex == index)
			return;

		// Update holder height
		this.holder.setStyle('height', this.images[index].getFullHeight());

		if(this.currentIndex >= 0) {
			this.images[this.currentIndex].tween('opacity', 0);
			if(this.thumbnails)
				this.thumbnails[this.currentIndex].tween('opacity', this.options.thumbFaded);
		}

		if(firstTime)
			this.images[index].setStyle('opacity', 1);
		else
			this.images[index].tween('opacity', 1);
		if(this.thumbnails)
			this.thumbnails[index].set('opacity', 1);

		this.currentIndex = index;

		if(this.options.onDisplay)
			this.options.onDisplay.apply(this, [index, this.images[index]]);
	},

	prev: function() {
		var index = this.currentIndex - 1;
		this.display(index < 0 ? this.maxIndex : index);
	},

	next: function() {
		var index = this.currentIndex + 1;
		this.display(index > this.maxIndex ? 0 : index);
	},

	link: function() {
		var item = this.images[this.currentIndex];
		var parent = item.getParent();

		if(parent.get('tag') == 'a') {
			if(parent.hasEvent('click'))
				parent.fireEvent('click');
			else {
				var href = parent.href;
				if($type(href) == 'function')
					(href)();
				else if($type(href) == 'string' && href != '')
					document.location.href = href;
			}
		}
	},
	// Unload
	unload: function() {
		this.holder = null;
		this.images = null;
		this.thumbnails = null;
	}
});

Custom.Accordion = new Class({
	Implements: Options,
	
	options: {
		link:'cancel',
		opacity:true,
		display:-1,
		alwaysHide:true,
		duration:700,
		transition:'expo:out',
		openClass:'selected',
		frame:'.accordion%level%',
		togglers:'.acc-toggler%level%',
		contents:'.acc-content%level%'
	},

	/*
	 * togglers
	 * contents
	 * options:
	 *   [accordion options]
	 *   openClass
	 */
	initialize: function(options) {
		this.setOptions(options);

		// Fix for IE6
		//var heightValue = (window.ie6) ? '100%' : '';

		var _onComplete = options ? options.onComplete : null;
		var _onActive = options ? options.onActive : null;
		var _onBackground = options ? options.onBackground : null;
		
		this.options.onComplete = function(toggler, content) {
			//var element = $(this.elements[this.previous]);
			//if(element && element.offsetHeight > 0) element.setStyle('height', heightValue);
			
			if(_onComplete)
				_onComplete(toggler, content, this.options.level);
		};
		this.options.onActive = function(toggler, content) {
			if(this.options.openClass)
				toggler.addClass(this.options.openClass);
			if(_onActive)
				_onActive(toggler, content, this.options.level);
		};
		this.options.onBackground = function(toggler, content) {
			if(this.options.openClass)
				toggler.removeClass(this.options.openClass);
			if(_onBackground)
				_onBackground(toggler, content, this.options.level);
		};

		var frameCounter = 1;
		
		// Go through each level
		do {
			var frames = $$(this.options.frame.replace(/%level%/g, frameCounter));
			var moreFrames = (frames.length > 0);
			
			if(moreFrames) {
				// Create accordions for each frame
				frames.each(function(frame, index) {
					var togglers = frame.getElements(this.options.togglers.replace(/%level%/g, frameCounter));
					var contents = frame.getElements(this.options.contents.replace(/%level%/g, frameCounter));
				
					var valid = (togglers.length > 0 && togglers.length == contents.length);
					
					if(valid) {
						// Display contents if hidden
						contents.each(function(item) {
							if(item.getStyle('display') == 'none')
								item.setStyle('display', 'block');
						});
						
						// Check which item to display
						var i = 0;
						while(i < togglers.length && !togglers[i].hasClass(this.options.openClass))
							i++;							
						this.options.display = (i < togglers.length) ? i : -1;
		
						// Create Accordion
						this.options.level = frameCounter;
						var acc = new Fx.Accordion(togglers, contents, this.options);
						acc.options.wait = false;
						
						if(frame.id != '')
							window['accordion-' + frame.id] = acc;
					}
					
				}, this);
				
				frameCounter++;
			}
		} while(moreFrames);
	}
});



Custom.HierMenu = {
	init: function(id) {
		var menus = $$('#' + id + ' ul ul');
		
		menus.each(function(menu) {
			var items = menu.getElements('li');
			
			if(items.length > 1) {
				var split = Math.ceil(items.length / 2);
				
				// Create new list
				var ul = new Element('ul');
				
				ul.inject(menu.parentNode);
				
				for(var i = items.length - 1; i >= split; i--) {
					var item = items[i];
					
					item.inject(ul, 'top');
				}
			}
		});
	}
};

Custom.init = function() {
	window.addEvent('domready', function() {
		// Skip transitions on ipod 
		if(Browser.Platform.ipod)
			Custom.showTransitions = false;
		
		// Create any accordions on the page
		new Custom.Accordion({
			frame:'.acc-frame-%level%',
			togglers:'.acc-toggler-%level%',
			contents:'.acc-content-%level%',
			openClass:'selected'
		});
		
	
		
		Custom.HierMenu.init('main-menu');
	});
};
