Site = {
	init: function() {
		Site.setupInterface();
		Site.setupBanners();
		Site.setupFormControls();
		Site.setupTopForty();
		Site.setupYoutube();
	},
	
	setupInterface: function() {		
		$$('.sideBox').each(function(el) {
			if (el.getElement('.isOpen')) {
				el.store('isOpen', true);
			} else {
				el.setStyle('height', el.getElement('h2').getFullHeight());
			}
			
			el.set('tween', {
				duration: 500,
				onComplete: function() {
					if (el.retrieve('isOpen')) el.setStyle('height', 'auto');
				} 
			});

			new Element('a', {
				'class': el.getElement('.isOpen') ? 'closeLink' : 'openLink',
				'href': '#',
				'text': 'View',
				'title': 'View Contents'
			}).addEvent('click', function(e) {
				e.stop();

				if (el.retrieve('isOpen')) {
					this.set({ 'class': 'openLink', 'text': 'View', 'title': 'View Contents' });
					el.store('isOpen', false).tween('height', el.getFullHeight(), el.getElement('h2').getFullHeight());

				} else {
					this.set({ 'class': 'closeLink', 'text': 'Hide', 'title': 'Hide Contents' });
					el.store('isOpen', true).tween('height', el.getFullHeight());
				}
			}).inject(el, 'top'); 
		});
	},

	setupBanners: function() {
		var bannerBlock = $('bannerItems');
		
		if (!bannerBlock) return;
		
		if (bannerBlock.getChildren().length > 1) {
			new ContentScroller({
				slides: bannerBlock.getChildren()
			}).start();
		}
	},
	
	setupFormControls: function() {
		var deliveryDetailsBlock = $('deliveryDetailsBlock');
		 
		if (deliveryDetailsBlock) {
			var checkBox = deliveryDetailsBlock.getElement('input[type=checkbox]');
		
			checkBox.addEvent('click', function(e, keepExisting) {
				$('deliveryDetailsBlock').getElements('input[type=text]').each(function(el) {
					el.set({ 'readonly': this.checked, 'disabled': this.checked });
					
					if (this.checked) {
						el.value = $(el.get('id').replace(/txtShip/i, 'txtBill')).value;
					} else if (!$chk(keepExisting) || !keepExisting) {
						el.value = '';
					}

					var validator = el.retrieve('validator');
					if (validator) validator.setCheckClass('statusUnmarked'); 
				}.bind(this));
			}).fireEvent('click', [null, true]);

			$('inputBox').getElements('input[type=text][id*=txtBill]').addEvent('blur', function(e) {
				checkBox.fireEvent('click', [null, true]);
			});
		}
	},
	
	setupTopForty: function () {
		var topFortyTab = $('topFortyLink');
		var topFortyList = $('topFortyList');
		
		if (!topFortyTab || !topFortyList) return;

		topFortyList.setStyle('height', 0).set('tween', {
			duration: 500,
			onComplete: function() {
				if (topFortyList.retrieve('isOpen')) {
					$('accountLink').addClass('disabled');
					topFortyList.setStyle('height', 'auto');
				}
			} 
		});

		topFortyTab.addEvent('click', function(e) {
			e.stop();

			if (topFortyList.retrieve('isOpen')) {
				$('accountLink').removeClass('disabled');
				topFortyList.store('isOpen', false).tween('height', topFortyList.getFullHeight(), 0);
			} else {		
				topFortyList.store('isOpen', true).tween('height', topFortyList.getFullHeight());
			}
		});
	},

	setupYoutube: function () {
		var youtubeContainer = $('youtubeVideos');

		if (youtubeContainer) {
			var youtubeVideos = youtubeContainer.getChildren();
			
			youtubeVideos.each(function(el) {
				new Swiff('http://www.youtube.com/v/' + el.get('class').replace(/^youtube\-/i, '') + '?version=3', {
					'container': el,
					'width': 455,
					'height': 371
				});
			});

			if (youtubeVideos.length > 1) {
				var controls = new Element('div', { 'class' : 'pagingLinks' }).inject(youtubeContainer, 'after');
				
			        new ContentPager(youtubeContainer, {
			                pageLinksContainer: controls,
			                includeBackNextLinks: true
				});
			}
		}
	}
}

window.addEvent('domready', Site.init);

Element.implement({
	applyRollOver: function() {
		if (this.src.test(/\.(gif|jpg|jpeg|png)$/i)) {
			// preload image
			var preload = new Image();
			preload.src = this.src.replace(/\.(gif|jpg|jpeg|png)/, '-over.$1');

			this.addEvents({
				mouseover: function() {
					this.src = this.src.replace(/\.(gif|jpg|jpeg|png)/, '-over.$1');
				},

				mouseout: function() {
					this.src = this.src.replace(/-over\.(gif|jpg|jpeg|png)/, '.$1');
				}
			});
		}

		return this;
	},
	
	getFullHeight: function() {
		var origHeight = this.getStyle('height').toInt();
		var fullHeight = this.setStyle('height', '').getStyle('height').toInt();

		this.setStyle('height', origHeight);
		return fullHeight;
	}
});
