var __GALERIE_PHOTO_INTERVAL = 5000;
var __SWF_PLAYER = 'fileadmin/templates/html/swf/media_player.swf';

/**
 * @author gterrain
 */
function PatrimoineGalerie() {
	
	this.init = function() {
		this.currentPhoto = 0;
		this.currentPage = 1;
		this.photos = new Array();
		this.videos = new Array();
		this.plans = new Array();
		this.initPhoto();
	}
	
	this.initPhoto = function(){
		
		$('#thumbs').css('display', 'block');
		$('#galerie_photo').css('display', 'block');
		
		$('#thumbs_videos').css('display', 'none');
		$('#video_player').css('display', 'none');
		
		$('#thumbs_plans').css('display', 'none');
		$('#galerie_plan').css('display', 'none');
		
		if (this.interval) {
			clearInterval(this.interval);
		}
		this.setIntervalPhoto();
	}
	
	this.initVideo = function() {

		$('#thumbs').css('display', 'none');
		$('#galerie_photo').css('display', 'none');
		
		$('#thumbs_videos').css('display', 'block');
		$('#video_player').css('display', 'block');
		
		$('#thumbs_plans').css('display', 'none');
		$('#galerie_plan').css('display', 'none');
		
		if (this.interval) {
			clearInterval(this.interval);
		}
		
		this.setCurrentVideo(0);
	}
	
	this.initPlan = function() {

		$('#thumbs').css('display', 'none');
		$('#galerie_photo').css('display', 'none');
		
		$('#thumbs_videos').css('display', 'none');
		$('#video_player').css('display', 'none');
		
		$('#thumbs_plans').css('display', 'block');
		$('#galerie_plan').css('display', 'block');
		
		if (this.interval) {
			clearInterval(this.interval);
		}
		
		this.setCurrentPlan(0);
		
	}
	
	this.setIntervalPhoto = function() {
		var _this = this;
		this.interval = setInterval(function() { _this.showNextPhoto(); }, __GALERIE_PHOTO_INTERVAL);
	}
	
	this.addPhoto = function(index, photo) {
		this.photos[index] = photo;
		var imgObject = new Image();
		imgObject.src = photo.src_grand;
		this.photos[index].imgObject = imgObject;
	}
	
	this.addVideo = function(index, video) {
		this.videos[index] = video;
	}
	
	this.addPlan = function(index, plan) {
		this.plans[index] = plan;
		var imgObject = new Image();
		imgObject.src = plan.src_grand;
		this.plans[index].imgObject = imgObject;
	}
	
	this.setCurrentPhoto = function(index) {
		clearInterval(this.interval);
		this.currentPhoto = index;
		this.setIntervalPhoto();
		this.showPhoto();
	}
	
	this.setCurrentVideo = function(index) {
		this.currentVideo = index;
		this.showVideo();
	}
	
	this.setCurrentPlan = function(index) {
		this.currentPlan = index;
		this.showPlan();
	}
	
	this.showPhoto = function() {
		var parent = $('#galerie_photo').parent();
		$('#galerie_photo').remove();
		var photo = $(this.photos[this.currentPhoto].imgObject).clone(false).attr('id', 'galerie_photo');
		
		parent.prepend(photo);
		$('#galerie_plein_ecran').attr('href', this.photos[this.currentPhoto].src_plein_ecran);
	}
	
	this.showVideo = function() {
		var so = new SWFObject(__SWF_PLAYER, 'flashplayer', '461', '301', '8', '#ffffff', 'high');
		so.addVariable("media", this.videos[this.currentVideo].src_video);
		so.write('video_player');
	}
	
	this.showPlan = function() {
		$('#galerie_plan').remove();
		var photo = $(this.plans[this.currentPlan].imgObject).clone(false).attr('id', 'galerie_plan');
		
		$('#video_player').after(photo);
		$('#galerie_plein_ecran').href = this.photos[this.currentPlan].src_plein_ecran;
	}
	
	this.showNextPhoto = function() {
		if ((this.currentPhoto + 1) == this.photos.length) {
			this.currentPhoto = 0;
		} else {
			this.currentPhoto++;
		}
		this.showPhoto();
	}
	
	this.nextPage = function() {
		this.nbPages = Math.ceil(this.photos.length/6);
		
		this.currentPage = this.currentPage + 1;
		
		this.showPage();
		
	}
	this.previousPage = function() {
		this.nbPages = Math.ceil(this.photos.length/6);
		
		this.currentPage = this.currentPage - 1;
		this.showPage();
	}
	this.showPage = function() {
		var min = (this.currentPage - 1) * 6;
		var max = min + 5;
		
		var i = 0;
		$('ul#thumbs li').each(function(){
			if (i >= min && i <= max) {
				$(this).show();
			} else {
				$(this).hide();
			}
			i++;
		});
		
		var pagin = '';
		if (this.currentPage > 1) {
			pagin += '<a href="javascript:void(0);" onclick="Galerie.previousPage();"><img src="fileadmin/templates/html/images/fleche_gauche.gif" /></a>';
		}
		if (this.currentPage < this.nbPages) {
			pagin += '<a href="javascript:void(0);" onclick="Galerie.nextPage();"><img src="fileadmin/templates/html/images/fleche_droite.gif" /></a>';
		}
		$('#galerie_pagin').html(pagin);
	}
}


