/**
 * TvAnnimation Class
 * 
 * @author   Volodymyr Iatsyshyn
 * @date     2009-07-01
 */

dojo.require('dojo.fx.easing');

if (!window['Clarovision'])
	window.Clarovision = {};

(function () {
	dojo.provide('Clarovision.TvAnnimation');
	var self = dojo.declare('Clarovision.TvAnnimation', null, {
		container: null,
		menu: null,
		features: null,
		tv_container: null,
		duration: 750,
		is_showed: false,
		
		constructor: function (aContainer, aMenu, aFeatures, aTvContainer) {
			this.container = dojo.byId(aContainer);
			this.menu = dojo.byId(aMenu);
			this.features = dojo.byId(aFeatures);
			this.tv_container = dojo.byId(aTvContainer);
			
			var cb = dojo.contentBox(dojo.body());
			var left = cb.w * .52;
			
			dojo.style(this.container, {
				width: '',
				left: Math.max(630, left) + 'px' 
			});
			
			dojo.subscribe('show-tv', this, 'onShowTv_');
			dojo.subscribe('show-tv-content', this, 'onShowTvContent_');
			dojo.subscribe('hide-tv', this, 'onHideTv_');
			dojo.connect(window, 'onresize', this, 'onWindowResized_');
		},
		
		getT: function (p) { return this.duration * p; },
		
		onHideTv_: function () {
			
			this.is_showed = false;
			
			dojo.empty('tv-screen');
			
			if (self.getWindowHeight() < self.MIN_SCREEN_HEIGHT) {
				var a = new dojo._Animation({duration:this.getT(.5), rate: 5,curve:[0, 30]});
				dojo.connect(a, "onAnimate", dojo.hitch(this.container, function(e) {
					return dojo.style(this, 'top', e + 'px');
				}));
				a.play();
			}		
			
			dojo.anim(this.menu, { opacity: 1 }, this.getT(.75), null, null, this.getT(.25));
			dojo.anim(this.features, { opacity: 1 }, this.getT(.75), null, null, this.getT(.25));
			dojo.anim(dojo.byId('backToFeaturesHolder'), { opacity: 0 }, this.getT(.5));
			
			var coords = dojo.coords(this.tv_container);
			var animation = new dojo._Animation({duration:this.getT(1), rate: 25,curve:[coords.l, 0], easing:dojo.fx.easing.expoInOut});
			dojo.connect(animation, "onAnimate", dojo.hitch(this.tv_container, function(e) {
				return dojo.style(this, 'left', e + 'px');
			}));
			animation.play();		
			
			return this;
		},
		
		onShowTv_: function (movie) {
			dojo.anim(this.menu, { opacity: 0 }, this.getT(.5));
			dojo.anim(this.features, { opacity: 0 }, this.getT(.5));
			dojo.anim(dojo.byId('backToFeaturesHolder'), { opacity: 1 }, this.getT(.25), null, null, this.getT(.25));
			
			var c = dojo.coords(this.container);
			var cb = dojo.contentBox(dojo.body());
			var end = -c.l + ((cb.w - 1042) >> 1);
			
			var animation = new dojo._Animation({duration:this.getT(1), rate: 25,curve:[0, end], easing:dojo.fx.easing.expoOut});
			dojo.connect(animation, "onAnimate", dojo.hitch(this.tv_container, function(e) {
				return dojo.style(this, 'left', e + 'px');
			}));
			dojo.connect(animation, "onEnd", function() {
				dojo.publish('show-tv-content', [movie]);
			});
			animation.play();
			
			if (self.getWindowHeight() < self.MIN_SCREEN_HEIGHT) {
				var a = new dojo._Animation({duration:this.getT(.5), rate: 5,curve:[30, 0],delay:this.getT(.95)});
				dojo.connect(a, "onAnimate", dojo.hitch(this.container, function(e) {
					return dojo.style(this, 'top', e + 'px');
				}));
				a.play();
			}
			
			return this;
		},
		
		onShowTvContent_: function(movie) {
			this.is_showed = true;
			
			dojo.place('<object width="933" height="525"><param name="allowfullscreen" '
		        + 'value="true" /><param name="allowscriptaccess" value="always" /><param '
		        + 'name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=' + movie + '&amp;' 
		        + 'server=vimeo.com&amp;show_title=0&amp;show_byline=0&amp;show_portrait=0&'
		        + 'amp;color=585589&amp;fullscreen=1&autoplay=1" /><param name="bgcolor" value="#000000" '
		        + '/><embed src="http://vimeo.com/moogaloop.swf?clip_id=' + movie + '&amp;server='
		        + 'vimeo.com&amp;show_title=0&amp;show_byline=0&amp;show_portrait=0&amp;color'
		        + '=585589&amp;fullscreen=1&autoplay=1" width="933" height="525" type="application/x-shock'
		        + 'wave-flash" allowfullscreen="true" allowscriptaccess="always" movie="http://'
		        + 'vimeo.com/moogaloop.swf?clip_id=' + movie + '&amp;server=vimeo.com&amp;show_title='
		        + '0&amp;show_byline=0&amp;show_portrait=0&amp;color=585589&amp;fullscreen=1" '
		        + 'bgcolor="#000000"></embed></object>', 'tv-screen', 'only');
			
			return this;
		},
		
		onWindowResized_: function () {
			var co = dojo.coords(this.container);
			var cb = dojo.contentBox(dojo.body());
			var c = dojo.coords(this.tv_container);
			var begin = c.l;
			var end = -co.l + ((cb.w - 1042) >> 1);
			var target = this.tv_container;

			if (!this.is_showed) {
				begin = co.l;
				end = Math.max(630, cb.w * .52);
				target = this.container;
			}
			
			var animation = new dojo._Animation({duration:this.getT(1), rate: 25,curve:[begin, end], easing:dojo.fx.easing.expoOut});
			dojo.connect(animation, "onAnimate", dojo.hitch(target, function(e) {
				return dojo.style(this, 'left', e + 'px');
			}));
			animation.play();

			return this;
		}
	});
	
	// static
	dojo.mixin(self, {
		MIN_SCREEN_HEIGHT: 645,
		
		getWindowHeight: function () {
			if (typeof window.innerWidth != 'undefined')
			      return window.innerHeight;
			 
			return document.documentElement.clientHeight;
		},
		
		getWindowWidth: function () {
			if (typeof window.innerWidth != 'undefined')
			      return window.innerWidth;
			 
			return document.documentElement.clientWidth;
		},
		
		initialize: function () {
			return new self('anotherElement', 'helloText', 'featureMenu', 'tv-container');
		}
	});
	dojo.addOnLoad(self, 'initialize');
})();
