/**
 * Integration of the jwplayer video and the carousel. 
 * @author		Nako Siskov
 * @copyright	http://www.netcetera.mk
 */

var VIDEOINTEGRATION = {};

(function (jQuery) {
	VIDEOINTEGRATION = {
			/* Creates player and mounts it on the movieId component.
			 * Uses the options to setup the video. After its creation
			 * auto-plays the video content. 
			*/
			setupPlayer: function(movieId, options) {
				jwplayer(movieId).setup({
					flashplayer: "/swf/player.swf",
					file: options.file,
					image: options.image,
					controlbar: options.controlbar,
					autostart: options.autostart,
					stretching: "exactfit",
					height: options.height,
					width: options.width,
					wmode: "opaque",
					events: {
						//the event is fired multiple times in a second
					    onTime: function(evt) {
							if (options.loop != 'none') {
								if(evt.position >= evt.duration - 0.5) {
									this.seek(0);
								}
							}
						}
					}
				});
			},
			
			handleNavigation: function(previous, next) {
				//stop the previous movie
				var movieObjectPrev = jQuery(previous).find("object");
				var isFlashAnimationPrev = movieObjectPrev.parent(".pfister-flash-container").length;
				var isFlashVideoPrev = movieObjectPrev.parent().children(".pfister-movie-setup").length;
				//if there is existing video object then stop it
				if (movieObjectPrev.length > 0) {
					if (isFlashVideoPrev > 0) {
						var movieId = movieObjectPrev.attr("id");
						jwplayer(movieId).stop();
					} else if (isFlashAnimationPrev > 0) {
						movieObjectPrev.parent(".pfister-flash-container").flash(function(){this.StopPlay();})
					}
				}

				//create new video object if is not created already
				if (!VIDEOINTEGRATION.createVideoComponent(next)) {
					//if there is existing video object then play it
					var movieObjectNext = jQuery(next).find("object");
					if (movieObjectNext.length > 0) {
						var movieId = movieObjectNext.attr("id");
						jwplayer(movieId).play();
					}
				}
			},
			handleBeforeMove: function(item) {
				var movieObject = jQuery(item).find("object");
				if (movieObject.length > 0) {
					var movieId = movieObject.attr("id");
					var parent = movieObject.parent();
					jwplayer(movieId).remove();
					movieObject.remove();
					jQuery(parent).prepend('<div id="'+movieId+'" class="pfister-movie-container"/>');
				}
			},

			/** Create video component if there is video container and video setup.
			*/
			createVideoComponent: function(item) {
				var movieContainer = jQuery(item).find(".pfister-movie-container");
				var movieSetup = jQuery(item).find(".pfister-movie-setup");
				if (movieContainer.length > 0
						&& movieSetup.length > 0) {
					var containerId = movieContainer.attr("id");
					var optionsLine = movieSetup.attr("rel");
					
					var options = VIDEOINTEGRATION.parseOptions(optionsLine);
					VIDEOINTEGRATION.setupPlayer(containerId, options);
					return true;
				} 
				if (jQuery(item).find(".pfister-flash-container").length > 0) {
					return VIDEOINTEGRATION.flashAnimations.load(item);
				}
				return false;
			},
			parseOptions: function(query) {
				var params = {};
				var pairs = query.split(/[;]/);
				for (var i = 0; i < pairs.length; i++ ) {
					var keyVal = pairs[i].split('=');
					//skip invalid properties
					if (!keyVal || keyVal.length != 2 ) {
						continue;
					}
					var key = unescape(keyVal[0]);
					var val = unescape(keyVal[1]);
					params[key] = val;
				}
				return params;
			},
			
			loadMovies: function() {
				var movieContainers = jQuery('body').find(".pfister-movie-container").filter(
						function() { 
							return jQuery(this).parents(".fullsize-carousel").size() == 0 
						});
				jQuery.each(movieContainers, function() {
					VIDEOINTEGRATION.createVideoComponent(jQuery(this).parent());
				});
			},

			flashAnimations :  {
					setupFlash: function(item, params) {
						jQuery(item).flash({
							swf: params.file,
							width: params.width,
							height: params.height});
					},
					//loads all flash content excluding the carousel
					loadAll: function() {
						var flashContainers = jQuery('body').find(".pfister-flash-container").filter(
								function() { 
									return jQuery(this).parents(".fullsize-carousel").size() == 0 
								});
						jQuery.each(flashContainers, function() {
							var flashSetup = jQuery(this).parent().find(".pfister-flash-setup");
							params = VIDEOINTEGRATION.parseOptions(flashSetup.attr("rel"));
							VIDEOINTEGRATION.flashAnimations.setupFlash(this, params);
						});
					},
					//this is called to load flash content in the carousel
					load: function(item) {
						if (item == null) {
							return false;
						}
						var flashContainer = jQuery(item).find(".pfister-flash-container");
						var flashSetup = jQuery(item).find(".pfister-flash-setup");
						if (flashContainer.length > 0 && flashSetup.length > 0) {
							params = VIDEOINTEGRATION.parseOptions(flashSetup.attr("rel"));
							VIDEOINTEGRATION.flashAnimations.setupFlash(flashContainer, params);
							return true;
						}
						return false;
					}
			}
	};
})(jQuery);
