﻿/// <reference path="../json2.js" />
/// <reference path="../enviro.js" />
/// <reference path="../jquery/jquery-1.3.2.js" />
/// <reference path="../jquery/jquery.pan.js" />
/// <reference path="../swfobject-redux.js" />
/// <reference path="../template.js" />

window.$video = function() {
	// Private Variables
	var _loaded = false;

	// Builders
	var _createVideo = function() {
		return $("<div id=\"video\"><div id=\"video_back\"><div class=\"panel\"><div id=\"stream\"></div>"
			+ "</div></div></div>").insertBefore("#nav");
	};

	// Callbacks
	var _updateSection = function(obj) {
		if (!obj || !obj.d) {
			return;
		}

		changeTitle(obj.d.MetaTitle);
	};

	// Ajax Calls
	var _getSection = function() {
		getSection("video", _updateSection);
	};

	// Private Methods
	var _init = function() {
		if (!entry.video) {
			_createVideo();
		}

		if (flashIsGood()) {
			$so.createSWF({
				data: "/flash/promo.swf",
				width: 600,
				height: 375
			}, {
				quality: "high"
			}, "stream");
		}
		else {
			$("#stream").append("<p>The latest version of Adobe Flash is required to view this movie.</p>");
		}
	};

	var _show = function() {
		var video = $("#video");
		video.css({
			"display": "block"
		});

		_getSection();

		$("#video_back").css({ backgroundPosition: "-950px 0" })
			.animate({ backgroundPosition: "(0px 0px)" }, { duration: 1000 });

		var stream = $("#stream")[0];
		if (stream.playPromo) {
			stream.playPromo();
		}
	};

	var _hide = function() {
		var stream = $("#stream")[0];
		if (stream.stopPromo) {
			stream.stopPromo();
		}

		var video = $("#video");
		video.css({
			"display": "none"
		});
	};

	return {
		// Public Properties
		isLoaded: function() {
			return _loaded;
		},
		type: "video",
		// Public Methods
		load: function() {
			if (!_loaded) {
				_init();
				_loaded = true;
			}
			_show();
		},
		show: _show,
		hide: _hide
	};
} ();