var clipId = -1;

// generic configuration switcher
function setConfig(config) { 
	$("#demo")[0].setConfig(getConfig(config));		
}

// called when page loads
$(function() { 
	
 	// setup colorpicker
	$("#colorPicker div.picker").hover(function() { 
		$(this).css({backgroundPosition: '0 -18px'});		
	}, function() {
		$(this).css({backgroundPosition: '0 0'});	
		
	}).click(function()  {
		var color = $(this).attr("color");
		setConfig({controlBarBackgroundColor: '0x' + color, autoPlay:false});
	});
	
	$("#player").click(function() {
		flashembed(this, {
				src:'video/FlowPlayerDark.swf',
				width:'380', 
				height:'250',
				bgcolor: '#000',
				id: 'demo'
			},
			getConfig
		);				
		$(this).unbind("click");
	});

	
	// quick demo links
	$("#thumbs img").each(function(index) {
		var el = $(this);
		
		el.fadeTo(1, 0.4).filter(".selected").fadeTo(1, 1.0);
		
		el.hover(
			function() {
				if (!$(this).is(".selected")) $(this).fadeTo(10, 0.8);
			},
			function() {
				if (!$(this).is(".selected")) $(this).fadeTo(500, 0.4);
			} 
		);
		
		el.click(function() {
			clipId = index;
			$("#thumbs img").not(el).removeClass("selected").fadeTo(500, 0.4);
			$(this).addClass("selected").fadeTo(1, 1.0);
			$("#colorPicker").css("visibility", index == 0 ? "visible" : "hidden");
			
			
			// perform initial loading of not done yet using flashembed.js loadEvent
			if ($("embed, object").length == 0) $("#player").click();
			else setConfig();  
		});    
	});


}); 

// flowplayer dynamic configuration (beware: this function is far from nice piece of code)	
function getConfig(conf) {
	
	var video = 'http://akvideos.metacafe.com/ItemFiles/%5BFrom%20www.metacafe.com%5D%20862422.5100050.11.flv';
	if (conf == null) conf = {};
	
	if (clipId != -1) {
		$.extend(conf, {
			autoPlay: false,
			playList: [ 
				{ url: 'img/demo/' + (clipId + 1) + 'm.jpg', overlayId: clipId == 1 ? '' : 'play' }, 
				{ url: video,  duration: 80}  
			]				
		});
	}
	
	if (clipId == 0 && conf.controlBarBackgroundColor == null) {
		$.extend(conf, {controlBarBackgroundColor: '0x0e3d08'});	
	}
	
	if (clipId == 1) {
		$.extend(conf, {
			showStopButton: false, 
			showScrubber: false, 
			showVolumeSlider: false,
			showMuteVolumeButton: false, 
			showFullScreenButton: false, 
			showMenu: false, 
			controlsOverVideo: 'locked',
			controlBarBackgroundColor: -1,
			controlBarGloss: 'none'
		});
	}
	
	if (clipId == 2) {
		$.extend(conf, {
			controlsOverVideo: 'ease', 
			showMenu:false, 
			showVolumeSlider:false
		});
	}  
		
	var defaults = {
		autoPlay: true, 
		autoBuffering: false, 
		autoRewind:true,
		loop:false,
		playList: [
			{ url: 'img/demo/splash.jpg', overlayId: 'play' },
			{ url: video,  duration: 80}  
		],	
		initialScale: 'scale', 
		useNativeFullScreen: true, 
		emailPostUrl: false,
		controlBarBackgroundColor: '0x222222',
		useHwScaling:true
	};
	
	$.extend(defaults, conf);
	var returned = $("#demo").length == 0 ? {config: defaults} : defaults;
	return returned;
	
}