$(function() {			
	// Function to set the contents of the modal popup (Fancy)box
	function setModalPlayer(url,container){
		pause();		
		$("div#" + container).html("<div id=\"" + container + "Player\"></div>");
		var attributes = {
			data:url, 
			width:"640", 
			height:"385"};
		var flashvars = {};
		var params = {
			menu: "false",
			allowFullScreen: "true",
			allowScriptAccess: "always",
			scale: "noscale"
		};
		swfobject.createSWF(attributes, params, container + "Player");
	}
	
	// Function to remove the contents of the modal popup (Fancy)box
	function removeModalPlayer(container){
		swfobject.removeSWF(container + "Player");
		$("div#fancy_div *").remove();
		play();
	}
	
	// Video carousel events
	$("div#videos ul li img").click(function(){
		$(this).parent().next().click();
	});
	
	$("div#videos ul li a").each(function(i) {
		var youtubeId = $(this).attr("id");
		var url = "http://www.youtube.com/v/" +youtubeId+ "&hd=1&fs=1&autoplay=1";		
		$(this).attr("href", "#modalVideo");
		$(this).fancybox({
			'frameWidth': 640,
			'frameHeight': 385,
			'overlayOpacity': 0.7,		
			'callbackOnStart': function(){
				$("object#flashcontent").css("visibility", "hidden");
				setModalPlayer(url,"modalVideo");
			},
			'callbackOnClose': function(){
				removeModalPlayer('modalVideo');
				$("object#flashcontent").css("visibility", "visible");
			}
		});
	});	

	// Screenshots carousel events
	$("div#screenshots ul li a").fancybox({
		'imageScale': false,
		'overlayOpacity': 0.7,
		'callbackOnStart': function(){
			$("object#flashcontent").css("visibility", "hidden");
		},
		'callbackOnClose': function(){
			$("object#flashcontent").css("visibility", "visible");
		}
	});
	
	// Wallpapers carousel events
	$("div#wallpapers ul li a").each(function(){
		var url = $(this).attr("href");
		$(this).attr("href", "#wallpaperContainer");
		$(this).fancybox({
			'frameWidth': 800,
			'frameHeight': 638,	
			'overlayOpacity': 0.7,
			'hideOnContentClick' : false,
			'callbackOnStart': function(){
				$("object#flashcontent").css("visibility", "hidden");
				var link = $("div#wallpaperContainer a");
				var image = $("div#wallpaperContainer img");
				$("div#wallpaperContainer a").attr("href", url);
				$("div#wallpaperContainer img").attr("src", url.replace(".zip", ".jpg"));
			},
			'callbackOnClose': function(){
				$("object#flashcontent").css("visibility", "visible");
			}
		});				
	});			
});	