

$(document).ready(function () {
	
	// detect if user has Skype installed and add proper class to body element
	
	function updateInstalledClass() {
		$('body').addClass(SkypeDetection.installed ? 'skypeInstalled' : 'skypeNotInstalled');
	}

	if (SkypeDetection) {
		if (SkypeDetection.ready) {
			updateInstalledClass();
		} else {
			SkypeDetection.detect(updateInstalledClass);
		}
	}

	// detect if operating system add proper class to body element
	
	if (SKYPE.util.Browser) {
		if (SKYPE.util.Browser.isWindows) {
			$('body').addClass("windowsDetected");
		} else if (SKYPE.util.Browser.isMac) {
			$('body').addClass("macosDetected");
		}
	}
});

// postpone loading of hidden images
SKYPE.postLoadImage = function (element, src, alt) {
	$(document).ready(function(){
		$(element).after(["<img src=\"", src, "\" alt=\"", alt, "\"/>"].join(''));
	})
}

SKYPE.namespace("components.hero");

SKYPE.components.hero = {
	
	autoChangeDelay: 20, // auto slide change delay (in seconds)
	
	init: function (){
		$(document).ready(function(){
			$('div.heroWrapper').each(function(){
				var $hero = $(this);
				var $slides = $hero.find("div.heroSlide");
				var $controls = $hero.find("div.heroNavigation");
				var $slideLinks = $controls.find("a.heroSlideLink");
	
				var index = $slides.filter(".current").index();
				showSlide(index);
	
				$controls.delegate("a", "click", function(e){
					var $this = $(this);
					var newIndex = $this.hasClass("prev") ? index - 1 :
								   $this.hasClass("next") ? index + 1 :
								   $this.hasClass("heroSlideLink") ? $slideLinks.index($this) : -1;
	
					showSlide( newIndex );
					e.preventDefault();
				});
	
				/* shows slide with given index */
				var timeout;
				function showSlide(newIndex){
					newIndex = (newIndex < 0) ? $slides.length - 1 : (newIndex >= $slides.length) ? 0 : newIndex;
	
					if (newIndex != index) {
						index = newIndex;

						$slides.stop(true, true); // stop currently running animations

						$slides.filter(".current").fadeOut(function(){
							$(this).removeClass("current").hide();
						});
						$slides.eq(index).fadeIn(function(){
							$(this).addClass("current");
						});
	
						$slideLinks.removeClass("current").eq(index).addClass("current");
					}

					// auto change slides
					clearTimeout(timeout);
					if (SKYPE.components.hero.autoChangeDelay > 0) {
						timeout = setTimeout(function(){
							showSlide(index + 1);
						}, SKYPE.components.hero.autoChangeDelay * 1000)
					}
				}
				
			});
		});
	}
};

SKYPE.components.hero.init();


SKYPE.namespace("components.video");

SKYPE.components.video = {
	configuration: {},

	/* @param id - id of video container element
	 * @param componentConfig - component configuration:
	 * 		width, height: size of video
	 * 		bgColor:       background color of player (default white)
	 * 		player:        player skin to use ('VideoPlayer' or 'BusinessVideoPlayer')
	 */
	init: function (id, componentConfig)	{
		componentConfig = $.extend({
			width: 700,
			height: 300,
			bgColor: '#FFFFFF',
			player: 'VideoPlayer',
			videos: {}
		}, componentConfig);

		this.configuration[id] = componentConfig;
		
		var self = this;
		
		$(document).ready(function(){
			var $video = $('#' + id).closest(".video");
			var $moreVideos = $video.find("div.moreVideos");
			
			$video.find("a.toggleMoreVideos")
				.addClass("showMore").show()
				.click(function(e){
					$(this).toggleClass("showMore showLess");
					$moreVideos.slideToggle();
					e.preventDefault();
				})

			$moreVideos.delegate("a", "click", function(e) {
				var i = $(this).closest("li").index();
				writePlayer(i, $(this).find("span.title").html());
				e.preventDefault();
			});

			
			// make more videos 'visible' just to check which video to show first
			$moreVideos.css({ display: "block", position: "absolute", visibility:"hidden" });
			
			var firstAvailableVideo = $moreVideos.find("li:visible").eq(0).index();
			
			// and hide them back again
			$moreVideos.css({ display: "", position: "", visibility:"" });
			
			if (firstAvailableVideo != -1) {
				writePlayer(firstAvailableVideo, $moreVideos.find("a span.title").eq(firstAvailableVideo).html() );
			}

			function writePlayer(video, title) {
				var videoConfig = componentConfig.videos[video];
				if (title) {
					$video.find("h2.title").html( title );
				}
				SKYPE.flash[componentConfig.player].write(id, componentConfig.width, componentConfig.height, videoConfig, componentConfig.bgColor);
			}
		});
	}


};

