var isBlog; 
var isHome;
var creationId = null;
var webroot = webroot == null ? './' : webroot;
var flashIsReady = false;
var intervalId = 0;
function setFlashReady(){
	flashIsReady = true;
}
function pauseBackground(){
	if(flashIsReady==true){
		clearInterval (intervalId);
		getFlashMovieObject("video-bg-flash").swfPauseVideo();
	}
}
function unpauseBackground(){
	if(flashIsReady==true){
		clearInterval (intervalId);
		getFlashMovieObject("video-bg-flash").swfStartVideo();
	}
}
function getFlashMovieObject(movieName){
	  if (window.document[movieName]) 
	  {
		  return window.document[movieName];
	  }
	  if (navigator.appName.indexOf("Microsoft Internet")==-1)
	  {
		if (document.embeds && document.embeds[movieName])
		  return document.embeds[movieName]; 
	  }
	  else // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
	  {
		return document.getElementById(movieName);
	  } 	
	}
function addVimeoEvent(){
		/*var playerIframe = document.getElementById("vimeo_player");
		playerIframe.addEvent("onLoad", function() {
			
			playerIframe.addEvent( "onPlay", function(){
				var flashObj = getFlashMovieObject("video-bg-flash");
				flashObj.swfPauseVideo();
			});
			playerIframe.addEvent( "onPause", function(){
				var flashObj = getFlashMovieObject("video-bg-flash");
				flashObj.swfStartVideo();
			});
			playerIframe.addEvent( "onFinish", function(){
				var flashObj = getFlashMovieObject("video-bg-flash");
				flashObj.swfStartVideo();
			});
		 });*/
		//playerIframe.api("api_play");
	}
	
$(document).ready(function()
{
	
	isBlog = window.location.href.indexOf('/blog/') != -1;
	isHome = !isBlog && $('#wrap').attr("class") == "";
	var creation = $('#creation').length > 0 ? $('#creation') : $('#edit_creation');
	creationId = creation.length > 0 ? creation.attr("class").split('_')[1] : null;

	writeVideoBackground();
	
	$(window).resize(function() {
		/*if(!isHome) {*/
	  		$('#video-bg-flash').height(isHome ? $(window).height():$(document).height());
	  		$('#video-bg-flash').width(isHome ? $(window).width() : $(document).width() - $('#wrap').outerWidth());
		/*} else {
			$('#video-bg-flash').css('width','100%');
	  		$('#video-bg-flash').css('height','100%');
		}*/
	});
	
	$(window).load(function() { $(window).trigger('resize'); });
	
	try {
		$(".scrollable").scrollable();
	}
	catch(e){}
	
	if($('.vimeoVideo').length) {
		$('.vimeoVideo').each(function() {
				$(this).html('<div id="vimeoMainPlayer"></div>')
				writeVimeoplayer($(this).attr('id'));					   
		});
	}
	
	$(".items img").click(function() {

		// see if same thumb is being clicked
		if ($(this).hasClass("active")) { return; }
		var html = '';
		if($(this).attr('alt') != '') //Vimeo
		{
		$("#image_wrap .image").html('<div id="vimeoMainPlayer"></div>');	
			writeVimeoplayer($(this).attr('alt'));
			if(flashIsReady==true){
				unpauseBackground();
			}
			//html = '<iframe id="vimeo_player" name="vimeo_player" src="http://player.vimeo.com/video/' + $(this).attr('alt') + '?js_api=1&js_swf_id=vimeo_player" frameborder="0"></iframe>';
			
			/*clearInterval (intervalId);
			if(flashIsReady==true){
				pauseBackground();
			} else {
				intervalId = setInterval ( "pauseBackground()", 1000 );	
			}*/
		}
		else
		{
			html = '<img src="' + $(this).attr('src').replace('&size=small', '&size=medium') + '" alt="" />';
			if(flashIsReady==true){
				unpauseBackground();
			}
			/*clearInterval (intervalId);
			if(flashIsReady==true){
				unpauseBackground();
			} else {
				intervalId = setInterval ( "unpauseBackground()", 1000 );	
			}*/
		}
		// get handle to element that wraps the image and make it semi-transparent
		var wrap = $("#image_wrap .image").fadeTo("medium", 0.5, function() {
			if(html.length){
				wrap.html(html);
			}
			wrap.fadeTo("medium", 1);
			}
		);
			
		// activate item
		$(".items img").removeClass("active");
		$(this).addClass("active");
	
	// when page loads simulate a "click" on the first image
	});
	
	if($(".items div").length <= 2 && $(".items div").eq(1).html()== '' )
		$('a.right').addClass("disabled");
	$(".items img").eq(0).click();
	
	if(!isBlog){
		setupTMCE();
	}
	
});

function writeVideoBackground() {
	var type = isHome ? 'accueil' : (creationId != null ? creationId : 'creation');
	var path = webroot + (isBlog ? '../' : '') + 'swf/get.php?type=' + type + (creationId == null ? '&' + Math.random() : '');
	
	//var so = new SWFObject(path, "video-bg-flash", isHome ? $(document).width() : $(document).width() - $('#wrap').outerWidth(), $(document).height(), "9", "ffffff");

	var so = new SWFObject(path, "video-bg-flash", isHome ? '100%' : $(document).width() - $('#wrap').outerWidth(), isHome ? '100%' : $(document).height(), "9", "ffffff");
	so.addParam("scale", "noscale");
	so.addParam("align", "tl");
	so.addParam("menu", "false");
	if(isHome)
		so.addParam("wmode", "transparent");
	so.addParam("allowfullscreen", "true");
	so.write("video-bg");
	
	/*if($("#vimeo_player").length==0){
		intervalId = setInterval ( "unpauseBackground()", 1000 );	
	}*/
}

function writeVimeoplayer(_videoID) {
	var path = webroot + 'swf/videoplayer.swf';
	var so = new SWFObject(path, "vimeoplayer_flash", '640', '360', "10", "ffffff");
	so.addParam("scale", "noscale");
	so.addParam("align", "tl");
	so.addParam("menu", "false");
	so.addParam("allowfullscreen", "true");
	so.addParam("allowscriptaccess", "always");
	so.addVariable("videoID", _videoID);
	so.write("vimeoMainPlayer");
}

function popup(url, name, popw, poph, parm) {
  var w = screen.width;
  var h = screen.height;
		
  if (popw == undefined) {
		popw=725;
	}
		
  if (poph == undefined) {
	poph=625;
  }
		
  var l = (w-popw)/2, t = (h-poph)/2;
		
  if (name == undefined) {
	name="";
  }
		
  if (parm == undefined) {
	var w = window.open(url, name, "left="+l+",top="+t+",width="+popw+"+,height="+poph+",fullscreen=no,scrollbars=yes,resizable=yes");
	} else {
		var w = window.open(url, name, "left="+l+",top="+t+",width="+popw+"+,height="+poph+","+parm);
	 }
		
}
function open_window(url) {
	mywin = window.open(url,"win",'toolbar=0,location=0,directories=0,status=0, scrollbars=0, menubar=0,resizable=0,marginheight=0, marginwidth=0,leftmargin=0,topmargin=0,width=720,height=600');
	mywin.focus(); 
}


