var shootingManager = {
	cPhoto:null,
	effect:null,
	entity:null,
	setting:null,
	sites:null,
	isRunning:false,
	maxPhoto: null,
	takePictureTimeout:null,
	showPictureTimeout:null,
	pictureLoadTryouts: null,

	init: function(entity, sites, maxPhoto) {
		this.entity = entity;
		this.isRunning = true;

		this.cPhoto = 0;

		this.pictureLoadTryouts = 0;

		BonusManager.initBonus();

		this.sites = $H(sites);

		var firstSite = this.sites.keys().first();//id du premier
		this.selectSite(firstSite);
		var self = this;

		this.maxPhoto = maxPhoto;

		Module.hideSubTable($('garment-title'), 0);

		$('cupboard').select('tr.title').each(function(tr, i) {
			tr.addClassName('photo-disabled');
		});
		$('photoValidation').addClassName('photo-disabled');

		$('photoResult').down('ul').childElements().each(function(li, i) {
			li.observe('mouseover', function(event) {
				self.selectPhotoPresentation(li);
			});
		});

		$('photoChangeButton').observe('click', function(event) {
			event.stop();
			self.showCupboard();
		});

	},

	initSites: function(sites) {
		this.sites = $H(sites);

		if($('preview-'+this.setting) != undefined) {
			$('preview-'+this.setting).addClassName('selected');
		}
	},

	selectPhotoPresentation: function(li) {
		li.addClassName('selected');
		id = li.id.substr(('photo-').length);
		$('photoPresentation-'+id).show();
		li.siblings().each(function(sibling, j) {
			sibling.removeClassName('selected');
			siblingId = sibling.id.substr(('photo-').length);
			$('photoPresentation-'+siblingId).hide();
		});
	},

	showCupboard: function() {
		new Effect.Parallel( [
				new Effect.BlindUp($('photoStep'), { sync : true}),
				new Effect.BlindDown($('scrollCupboard0').up('div.background-wrapper').up('div.wrapper'), {sync: true}),
				new Effect.BlindUp($('photoResult'), { sync : true})
			],
			{duration : 1.5}
		);
	},


	selectSite : function(id, showMask) {
		this.setting = id;
		var qname = this.sites.get(id).qName;
		$$('.location-preview').each(function(preview) {
			preview.down('span').removeClassName('selected');
		});
		$('preview-'+id).down('span').addClassName('selected');
		if (showMask == true) {
			$('dressing-mask').className = "active";
		} else {
			$('dressing-mask').className = "";
		}
		$('dressing-background').style.backgroundImage = "url('"+projectUrl+"/media/fashiown/image/content/illustration/product/site/photo/923x633/"+qname+".jpg')";

		return false;
	},

	chooseSite : function() {

		$('photoLocation').hide();
		$('cupboard').show();
		$('photoValidation').show();

		var self = this;
		new Effect.Parallel( [
				new Effect.BlindUp($('photoLocation'), { sync : true}),
				new Effect.BlindDown($('scrollCupboard0').up('div.background-wrapper').up('div.wrapper'), {sync: true})
			],
			{duration : 1.5}
		);

		$("photoValidation").observe('click', function(event) {

			if(lock('photoValidation')) {

				var index = Module.getSelectedTitle('cupboard');
				var effects = new Array(
					new Effect.BlindDown($('photoStep'), { sync : true}),
					new Effect.BlindUp($('scrollCupboard'+index).up('div.background-wrapper').up('div.wrapper'), {sync: true})
				);

				if(self.cPhoto > 0) {
					effects[2] = new Effect.BlindDown('photoResult', { sync : true});
				}
				new Effect.Parallel( effects, {duration : 1.5});
				Module.unSelect('cupboard', index);
				$('modelViewZoomContainer').setStyle({visibility : 'hidden'});

				$('photoChangeButton').hide();
				$('dressingValidationButton').show();
			}
		});

		Module.init('cupboard');

		$('cupboard').select('tr.title').each(function(tr, i) {
			tr.observe('click', function(event) {

				if(lock('cupboard')) {
					new Effect.Parallel( [
							new Effect.BlindUp($('photoStep'), {sync: true}),
							new Effect.BlindUp('photoResult', {sync: true})
						],
						 {duration : 0.5}
					);
					$('modelViewZoomContainer').setStyle({visibility : 'visible'});

				}

			});

			tr.removeClassName('photo-disabled');
		});

		$('photoValidation').removeClassName('photo-disabled');
	},

	takeScreenshot: function(photo) {
		try {
		FlashManager.changeModelPart('', "setting", this.sites.get(this.setting).qName, null, 'site');
		} catch (err) {}
		var flash = getFlash('modelView');

		flash.screenshotAS3(photo, this.entity);
	},

	useEffect : function() {
		this.effect = $F("effects");
		if(this.effect != "") {
			// update flash with effect
		} else {
			// clean flash
			this.effect = null;
		}
	},

	takePhoto : function() {
		if(this.cPhoto < this.maxPhoto) {

			this.cPhoto ++;
			var bonusParams = BonusManager.getBonusParams();

			Ajax.JSON(new DoTakePhotoAjax({params: 'entity='+this.entity+'&site='+this.setting+'&effect='+this.effect+bonusParams}));

			this.disablePhoto();
			$('photoLeft').innerHTML = this.maxPhoto - this.cPhoto + ' / '+ this.maxPhoto;

		}
	},

	disablePhoto: function() {
		$('dressingValidationButton').disabled=true;

		this.takePictureTimeout = setTimeout("shootingManager.enablePhoto()", 3000);
	},

	enablePhoto: function() {
		$('dressingValidationButton').disabled=false;
		$("management").setStyle({'cursor': 'default'});
		clearTimeout(this.takePictureTimeout);
	},

	initNewPhoto : function(previousResult) {

		if($('effects') != undefined) {
			$("effects").value = "";
			$("effects").selected = "";

			var self = this;
			$("effects").observe('change', function(event) {
				self.useEffect();
			});
		}


		$('photoPresentation-'+this.cPhoto).down('div.resultPresentation').innerHTML = previousResult;
		$('photo-'+this.cPhoto).show();
		this.selectPhotoPresentation($('photo-'+this.cPhoto));

		if(this.cPhoto < this.maxPhoto) {
			if(this.cPhoto == 1) {
				$('photoValidation').down('i').show();
			}
			new Effect.BlindDown($('photoResult'));

			$('photoChangeButton').show();
			$('dressingValidationButton').hide();
		} else {
			this.isRunning = false;
			$('endSession').show();
			$('photoValidation').hide();
			$('cupboard').select('tr.title').each(function(tr, i) {
				tr.addClassName('photo-disabled');
			});
		}
	},
	displayPicture: function(photo) {
		$('photoPresentation-'+this.cPhoto).down('div.resultPresentation').down('div.photoMediumSize').down('div.block').down('div').innerHTML = '<img src="http://static.fashiown.com.s3.amazonaws.com/'+projectMode+'/photo/medium/'+this.entity+'-'+photo+'.png"/>';

	}
}
Object.extend(shootingManager, BonusManager);

var GetSitesAjax = Class.create();
GetSitesAjax.prototype = {

	page: projectUrl + '/member/shooting/getSites',
	execute: function(object) {

		if(object.sitesContent != undefined) {
			$('siteThumbnails').innerHTML = object.sitesContent;
			shootingManager.initSites(object.sites);
		}
	}
}
Object.extend(GetSitesAjax.prototype, FashiownAjax);


function refreshPicture(photo, model) {
	hideLoading();
	shootingManager.displayPicture(photo);

	var contentTutorial = this.contentTutorial || window.contentTutorial;
	contentTutorial && contentTutorial.process('tutorial-modelconsole-006');
}

var GetSpecialEffectsAjax = Class.create();
GetSpecialEffectsAjax.prototype = {

	page: projectUrl + '/member/shooting/getSpecialEffects',
	customExecute: function(object) {
		if(object.shooting != undefined) {
			$('shooting').innerHTML = object.shooting;
			shootingManager.initNewPhoto();

		}
	}
}
Object.extend(GetSpecialEffectsAjax.prototype, FashiownAjax);

var DoTakePhotoAjax = Class.create();
DoTakePhotoAjax.prototype = {

	page: projectUrl + '/member/shooting/doTakePhoto',
	customExecute: function(object) {
		if(object.errors.size() == 0) {
			if($('bonus-button')) { $('bonus-button').style.display = 'none'; }

			showLoading($('photoLoading').innerHTML);
			shootingManager.takeScreenshot(object.photoId);

			shootingManager.initNewPhoto(object.result);

			$('modelEnergy').innerHTML = object.modelEnergy;
		}
	}
}
Object.extend(DoTakePhotoAjax.prototype, FashiownAjax);