/* 
 * British Council - Venice Biennale
 *
 * @package		BCVB
 * @author		Oskar Krawczyk (o.krawczyk@keepthinking.it)
 * @version		1.2
 * @dependecies	MooTools 1.2+
 * @copyright	Copyright (c) 2009-2010, Oskar Krawczyk (Keepthinking Ltd.)		
 * @link		http://keepthinking.it
 * 
 ======================================================================= */

(function(){
	try {
		document.createEvent("TouchEvent");
	} catch(e) {
		return;
	}

	['touchstart', 'touchmove', 'touchend'].each(function(type){
		Element.NativeEvents[type] = 2;
	});

	var mapping = {
		'mousedown': 'touchstart',
		'mousemove': 'touchmove',
		'mouseup': 'touchend'
	};

	var condition = function(event){
		var touch = event.event.changedTouches[0];
		event.page = {
			x: touch.pageX,
			y: touch.pageY
		};
		return true;
	};

	for (var e in mapping){
		Element.Events[e] = {
			base: mapping[e],
			condition: condition
		};
	}
})();

var Engine = new Class({
	Implements: [Events, Options],
	options: {
		// playerUrl: $empty,
		// videoModalBox: $empty
	},
	
	initialize: function(options) {
		this.setOptions(options);
		
		this.wrapper 		= document.id('wrapper');
		this.showBg 		= document.id('show-background');
		this.globalSearchEl = document.id('quick-search');
		this.popups 		= $$('.externalLink, .window, .icBody p a');
		this.homeBottomTabs = $$('.hctCont');
		this.videos 		= $$('a[rel=boxedVideo]');
		this.featureNavEls 	= $$('#feature-nav li');
		this.changeTextEl 	= $$('#change-size-action a');
		this.exploreTimeline = $$('.exploreTimeline');
		this.exploreTimeline.fade(0.8);
		
		this.resetLast 	= $$('.icArtists ul.icList li:last-child, #artist-listing .alcsYears li:last-child, .profileInfo dd a:last-child');
		this.resetLast.addClass('removeBorder');
		
		$$('li.srapAction span').setStyle('opacity', 0.6);
		$$('.selfSubmit').addEvents({
			change: function(){
				this.getParent('form').submit();
			}
		});
		
		this.preloadBackground();
		this.showBackground();
		this.toggleGlobalSearch();
		this.externalLink();
		this.homeMovies();
		this.featureNav();
		this.changeTextSize();
		
		if(this.homeBottomTabs) this.homeBottomTabs.equalize('height', 'max', 220);		
	},

	/* 
	 ======================================================================= */
	featureNav: function() {
		if(!$chk(this.featureNavEls)) return false;
		
		this.featureNavEls.setStyle('opacity', 0.6);
		this.featureNavEls.addEvents({
			mouseover: function() {
				this.fade(1);
			},
			mouseleave: function() {
				this.fade(0.6);
			}
		});
	},
	
	/* 
	 ======================================================================= */
	preloadBackground: function() {		
		new Asset.image(this.options.bodyBackground, {
			onload: function() {
				if(this.get('src') != 'undefined'){
					$(document.body).setStyle('background-image', 'url('+this.get('src')+')');
				}
			}
		});
	},

	/* 
	 ======================================================================= */
	showBackground: function() {
		if(!$chk(this.showBg)) return false;
		
		var body = document.id(document.body);
		
		// toggle the content
		this.showBg.addEvent('click', function(e) {
			e.stop();
			var el = new Element(e.target);
						
			// hide the wrapper
			this.wrapper.fade('out');
			
			// create the 'show content' button and its events
			var showContent = new Element('span', {
				'class': 'showContent',
				'html': 'Click anywhere to show content',
				'styles': {
					'position': 'absolute',
					'top': el.getPosition().y,
					'left': el.getPosition().x
				}
			}).inject(document.body, 'bottom');
			
			document.id(document.body).addEvent('click', function() {
				// show content wrapper
				this.wrapper.fade('in');
				
				// hide the 'show content' button
				showContent.fade('out');
				
				// destroy the element when it's hidden
				(function() {
					showContent.destroy();
				}).delay(1000);

				// hide the scrollbars
				body.setStyle('overflow', 'auto');
				
				// remove attached click event
				this.removeEvent('click');
			}.bind(this));
			
			// set animation
			showContent.set('morph', {transition: 'sine:in:out'});
			
			// animate the button to bottom right corner of the browser
			(function() {
				showContent.morph({
					'top': (document.id(document.body).getScrollSize().y - showContent.getSize().y) - 10,
					'left': (document.id(document.body).getScrollSize().x - showContent.getSize().x) - 10
				});
			}).delay(800);
			
			// hide scrollbars
			body.setStyle('overflow', 'hidden');
		}.bind(this));
	},
	
	/* toggle focus/blur value of the global search input
	 ======================================================================= */
	toggleGlobalSearch: function() {
		altText = this.globalSearchEl.get('title');
		
		this.globalSearchEl.set('value', altText);
		
		this.globalSearchEl.addEvents({
			focus: function() {
				if(this.globalSearchEl.get('value') == altText) {
					this.globalSearchEl.set('value', '');
				}
			}.bind(this),
			blur: function() {
				if(this.globalSearchEl.get('value') == '') {
					this.globalSearchEl.set('value', altText);
				}
			}.bind(this)
		});
	},

	/* opens external links in a new window
	 ======================================================================= */
	externalLink: function(){
		this.popups.each(function(popup, index) {
			popup.addEvent('click', function(e) {
				e.stop();

				var newWindow = window.open(this.get('href'), '_blank');
				newWindow.focus();					
			});
		});
	},

	/* create modal window with a movie inside
	 ======================================================================= */
	homeMovies: function() {
		// url of the player
		var playerUrl 	= this.options.playerUrl;
		var videoSize 	= this.options.videoModalBox;
		
		// loop through the videos
		this.videos.each(function(video) {

			// set neccessery strings if config is available
			var addConfig = video.get('rev') ? ',"plugins":{'+video.get('rev')+'}' : '';
			
			// get title parts
			var movieDetails = video.get('title').split('::');
			
			// set the title property of the link
			video.set('title', movieDetails[0]);
			
			// attach squeezebox
			SqueezeBox.assign(video, {
				onOpen: function() {
					
					// create new flash object
					new Swiff(playerUrl, {
						id: 'sbox-swf',
						
						// add clip url and config
						params: {
							config: '{"clip":"'+video.get('href')+'"'+addConfig+'}'
						},
						vars: {
							config: '{"clip":"'+video.get('href')+'"'+addConfig+'}'
						},
						width: videoSize.width,
						height: videoSize.height,
						container: this.content
					});
					
					// title
					new Element('strong', {
						'class': 'movieTitle',
						'html': $pick(movieDetails[0], '')
					}).inject(this.content, 'bottom');
					
					// caption
					new Element('p', {
						'class': 'movieCaption',
						'html': $pick(movieDetails[1], '')
					}).inject(this.content, 'bottom');
					
					// credits
					new Element('p', {
						'class': 'movieCredits',
						'html': $pick(movieDetails[2], '')
					}).inject(this.content, 'bottom');
					
					// copyright
					new Element('p', {
						'class': 'movieCopyright',
						'html': $pick(movieDetails[3], '')
					}).inject(this.content, 'bottom');
					
				},
				handler: 'clone', 
				size: {x: videoSize.width, y: videoSize.height + 150}
			});
		});
	},

	/* change the size of the text
	 ======================================================================= */
	changeTextSize: function() {
		this.changeTextEl.each(function(link, index, all) {
			link.addEvent('click', function(e) {
				e.stop();
				
				// toggle highlight
				all.getParent().removeClass('active');
				this.getParent().addClass('active');
				
				// get the text size from REL property and apply it to the body
				$$('body').setStyle('font-size', this.get('rel'));
			});
		});
	}
});