﻿


$(window).bind('load', function() {

	 var i = 1;
	 var projectImgs = $('div:visible .preload').length;
	 var int = setInterval(function() {
		if(i >= projectImgs) clearInterval(int);
		$('div:visible .preload:hidden:first').fadeIn(200);
		i++;
	 }, 200);

});

$(window).load(function() {    
        var theWindow        = $(window),
            $bgImage              = $("#bgimage"),
            $bgVideo              = $("#bgvideo");
      
        theWindow.resize(function() {
                $(this).resizeBg($bgImage);
					 $(this).resizeBg($bgVideo);					 
        }).trigger("resize");

});

$(document).ready(function(){

	$('div:visible .preload').hide();	
	/* SECTION MENU NAV */
	
	 $("#gallery-nav .gallery").jGalleryLite({
			speed:300,
			btnNext: "#gallery-nav .next",
			btnPrev: "#gallery-nav .prev",
			btnHover:true,
			auto:4000,
			startHide:true,
			gallery:	"#gallery-container",
			gallerySource: ".gallerySource",
			gallerySourceAllBtn: ".diaporama",
			galleryBtnClose: "#gallery-container .close",			
			galleryBtnNext:'#gallery-container .galleryNext',
			galleryBtnPrev:'#gallery-container .galleryPrev',
			galleryNavHide:3000,
			easing:'swing', /* swing is possible do a small stop on each */
			container:"#gallery-nav",
			imagesPath:'../design/theatreducentaure/images/'
	});

	
	$("#nav-carousel .carousel").jGalleryLite({
		speed:300,
		btnNext: "#nav-carousel .next",
		btnPrev: "#nav-carousel .prev",
		btnHover:true,
		easing:'swing', /* swing is possible do a small stop on each */
		container:"#nav-carousel"
	});
		$("#menu-carousel .menu").jGalleryLite({
		speed:300,
		btnNext: "#menu-carousel .next",
		btnPrev: "#menu-carousel .prev",
		btnHover:true,
		easing:'swing', /* swing is possible do a small stop on each */
		container:"#menu-carousel"
	});
	var speed=500;
	var timer=false;
	$("#nav-container").bindDelayed('mouseenter', function(event){
		$("#nav-carousel-container-mask").css('z-index',1000);
		$("#nav-menu-container-mask").css('z-index',1000);
		
		$("#nav-menu-container").stop().animate({ marginTop:"0px"},speed);
		$("#nav-carousel-container").stop().animate({ marginTop:"0px"},speed);
		$("#page-content-mask").stop().fadeTo(200,0.9);
		},300,'mouseleave');

	$("#nav-container").bindDelayed('mouseleave', function(event){
					$("#nav-menu-container").stop().animate({ marginTop:"180px"},speed ,'linear',function(){		
						$("#nav-menu-container-mask").css('z-index',1);
						});
						
					$("#nav-carousel-container-mask:not('.on') #nav-carousel-container").stop().animate({ marginTop:"-163px"},speed ,'linear',function(){
						$("#nav-carousel-container-mask").css('z-index',1);
						});
							
					$("#page-content-mask").stop().fadeOut(200);
				}
			,600,'mouseenter');


	/* SECTION : ROLLOVER */
	
	var rollOn=1,rollOff=0.7,rollSpeed=80;
			
	
	$('.rollover')
		.mouseenter(function(){
				$(this).stop().fadeTo(rollSpeed,rollOn);
			})
		.mouseleave(function(){
				$(this).stop().fadeTo(rollSpeed,rollOff);
			})
		.click( function(event){
				// Brower doesn't follow the link
				/*
				event.preventDefault();
				if ( $(this).has('a').length && !$(this).hasClass('popup') )
				{
					document.location.href = $(this).find('a').attr('href');
				}
				else if ( $(this).is('a') && !$(this).hasClass('popup') )
				{
					document.location.href = $(this).attr('href');
				}
				*/
			})
		.each(function(){
			$(this).fadeTo(rollSpeed,rollOff);
			});
			
	/* SECTION : TOGGLE */
	
	var toggleSpeed=200;
	$('.toggle-container').hide();
	$('.toggle-click').click(function() {
		$(this).parent().children().not($(this)).find(".toggle-container").slideUp(toggleSpeed);
		$(this).parent().children().not($(this)).find(".toggle-icone").removeClass("on");
			
		$(this).children('.toggle-container').slideToggle(toggleSpeed)
		$(this).children('.toggle-icone').toggleClass('on');
	
	
	});
	
});

(function($) {   

$.fn.initHome = function() {
	 		$('#page-content').css('background-color','transparent');
			
			hideHomeMenu();
			
			$bg = $('#bgvideo');
			if($bg.attr('src')=='')
				$bg=$('#bgimage');
			
			var timer = null;
         timer = setTimeout(function() {fadeInHomeMenu();}, 3500);
        
			$bg.click(function(){
						if(timer != null){
							clearTimeout(timer);
						}
						fadeInHomeMenu();
						
					});
				
}

$.fn.resizeBg = function($bg) {
	 var theWindow        = $(window);
	  /* not resize  if video bg and videoapi (flash embeded)*/
	  if($bg.children('#bgvideo_api').length==0)
	  {
		  if (( (theWindow.width() / theWindow.height()) < ($bg.width() / $bg.height()) )) {
			  $bg
					.removeClass()
					.addClass('bgheight');
			} else {
			  $bg
					.removeClass()
					.addClass('bgwidth');
			}
		}
		else
		{
			  $bg.addClass('bgheight');
			  $bg.addClass('bgwidth');
			  $bg.css('width','100%');
			  $bg.css('height','100%');
		}
		
		
		
}
                                       // Compliant with jquery.noConflict()
$.fn.setBg = function(bgSrc,bgType) {
	$('body').addClass('preloadingBig');
	if(bgType=='header')
	{
		$('#bgvideo')
			.attr('src','')
			.html('')
			.hide();
		
		var $bgImage=$('#bgimage');
		$bgImage
			.attr('src',bgSrc)
			.hide();
		
		if ( $.browser.msie ) {
			$bgImage.resizeBg($bgImage);
			$bgImage.fadeTo(500,1);	
			
		}
		else{
			$('#bgimage').load(function(){
				$('body').removeClass('preloadingBig');
				$(this).resizeBg($(this));
				$(this).fadeTo(500,1);				
			});			
		}

			

	}
	else if(bgType=='video')
	{
		var deviceAgent = navigator.userAgent.toLowerCase();
		var iosDevice = deviceAgent.match(/(iphone|ipod|ipad)/);
		// disable background video if ipad iphone ipod
		if (iosDevice) {
		 	$(this).setBg('design/theatreducentaure/images/'+ bgSrc + '.jpg','header');
		}
		else
		{
			$('#bgimage')
				.attr('src','')
				.hide();
			
			var htmlvideosource = '<source src="design/theatreducentaure/images/'+ bgSrc + '.mp4"></source>'
				+'<source src="design/theatreducentaure/images/'+ bgSrc + '.ogv"></source>';
			
			$('#bgvideo')
				.hide()
				.addClass('bgWidth')
				.html(htmlvideosource)
				.bind('ended',function(){
					this.play();				
				})
				.attr('poster','design/theatreducentaure/images/'+ bgSrc + '.jpg')
				.bind('canplay',function(){
					$(this)
						.fadeIn(700)
						.resizeBg($('#bgvideo'));
					});					
		}
		
	}
}

function hideHomeMenu()
{
	$('#nav-carousel-container-mask').hide();
	$('#nav-bar').hide(0);
	$('#page-content-container').hide();
	$('#nav-menu-container-mask').hide();
	$('#nav-rollover-mask').hide();
}

function fadeInHomeMenu()
{
	$('#nav-bar,#page-content-container').fadeIn(600);
	$('#nav-carousel-container-mask').fadeIn(600,function(){$(window).trigger('resize');});
	
	$('#nav-rollover-mask').show();
	$('#nav-menu-container-mask').show();
	
}


})(jQuery);


