var VideoCafe = {
	_currentCategory: null, // Stores the ID of the current category
	
	init: function(video) {
		// Setup category links
		$('#cafe_nav a').each(function(i, a) {
			var category = a.parentNode.id.substr(4);
			a.onclick = function() {
				VideoCafe.showCategory(category);
				return false;
			}
		});
		
		// Setup video thumbnail links
		$('.gallery a, #carousel .frame a').each(function(i, a) {
			for (var type in VideoCafe.videoTypes) {
				var result = VideoCafe.videoTypes[type].test(a);
				if (result) {
					(function(type, result){
					
					a.onclick = function() {
						document.location.href = '#' + this.id.substr(6);
						VideoCafe.showVideo.call(VideoCafe, type, result);
						return false;
					}
					
					})(type, result);
					break;
				}
			}
		});
		
		if (video) {
			var a = $('#video-' + video);
			
			a.get(0).onclick();
			
			var category = a.parents('.gallery');
				
			this.showCategory(category.attr('id'));
		}
		else {
			this.showCategory($('.gallery:first').get(0).id);
		}
	},
	
	
	
	showCategory: function(name) {
		if (this._currentCategory != name) {
			if (this._currentCategory) {
				VideoCafe.Category.hide(this._currentCategory);
			}

			this._currentCategory = name;
			VideoCafe.Category.show(name);
		}
	},
	
	showVideo: function(type, params) {
		$('#carousel').fadeOut();
		var width = this.videoTypes[type].show.apply(this, params);
		if (width) {
			$('#videoPlayer').css({
				width: width,
				left: '50%',
				marginLeft:-width/2
			});
		}
		else {
			$('#videoPlayer').css({
				width: '100%',
				left: 0,
				marginLeft: 0
			});
		}
	},
	
	videoTypes: {
		youtube: {
			test: function(a) {
				if (a.href.substr(0, 31) == 'http://www.youtube.com/watch?v=') {
					return [a.href.substr(31)];
				}
			},
			show: function(video) {
				var width = 560;
				var height = 340;
			
				var html = '<object width="' + width + '" height="' + height + '"><param name="movie" value="http://www.youtube.com/v/' + video + '&hl=en_US&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/' + video + '&hl=en_US&fs=1&autoplay=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="' + width + '" height="' + height + '"></embed></object>';
				
				$('#videoPlayer').html(html);
				
				return width;
			}
		},
		streaming: {
			test: function(a) {
				if (a.href.substr(0, 4) == 'rtmp') {
					var queryStringStart = a.href.indexOf('?');
					return [a.href.substr(0, queryStringStart), a.href.substr(queryStringStart + 1)];
				}
			},
			show: function(video, param) {
				var player = 'flash/videocafe_' + param;
				
				AC_FL_RunContent(
					'el', $('#videoPlayer').get(0),
					'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0',
					'width', '950',
					'height', '340',
					'src', player,
					'quality', 'high',
					'pluginspage', 'http://www.macromedia.com/go/getflashplayer',
					'align', 'middle',
					'play', 'true',
					'loop', 'true',
					'scale', 'showall',
					'wmode', 'window',
					'devicefont', 'false',
					'id', player,
					'name', player,
					'menu', 'true',
					'allowFullScreen', 'true',
					'allowScriptAccess','sameDomain',
					'movie', player,
					'salign', '',
					'flashVars', 'video_path=' + video
				); //end AC code
			}
		}
	}
}


VideoCafe.Category = {
	show: function(id) {
		var el = $('#' + id);
		el.css('position', 'static');
		$('#lnk-' + id + ' a').addClass('active');
		el.fadeIn();
	},
	hide: function(id) {
		var el = $('#' + id);
		el.css({
			'position': 'absolute',
			'left': el.position().left,
			'top': el.position().top
		})
		$('#lnk-' + id + ' a').removeClass('active');
		el.fadeOut();
	}
}

$(document).ready(function() {
	var hashIndex = document.location.href.indexOf('#');
	if (hashIndex != -1) {
		var video = document.location.href.substr(hashIndex + 1);
		VideoCafe.init(video);
	}
	else {
		VideoCafe.init();
	}
	new Carousel('#carousel', 10000);
});
