if(typeof benchmark == "undefined") var benchmark = new Object();
if(typeof carte == "undefined") benchmark.carte = new Object();


if((!benchmark.framework ||!benchmark.framework.ihm || !benchmark.framework.ihm.ObjPopup) && window.console)console.log("Objet non trouvée : benchmark.framework.ihm.ObjPopup");
if((!benchmark.framework.net || !benchmark.framework.net.ObjControleurAjax) && window.console)console.log("Objet non trouvée : benchmark.framework.net.ObjControleurAjax");
if((!window.deconcept || !deconcept.SWFObject) && window.console)console.log("Objet non trouvée : deconcept.SWFObject");


/**
 * Le constructeur de la classe.
 */
benchmark.carte.ObjCartePersonnaliseeVisionneuse = function() {
	this._ecranCapture = $('#objcartepersonnaliseevisionneuse_ecran_capture');
	this._ecranAnimation = $('#objcartepersonnaliseevisionneuse_ecran_animation');
	this._ecranImage = $('#objcartepersonnaliseevisionneuse_ecran_image');
	this._ecranImageControleur = $('#objcartepersonnaliseevisionneuse_ecran_image_controleur');
	this._ecran = $('#objcartepersonnaliseevisionneuse_ecran');
	this._chargement = $('#objcartepersonnaliseevisionneuse_chargement');
	this._btSuivant = $('#objcartepersonnaliseevisionneuse_bt_suivant');
	this._btPrecedent = $('#objcartepersonnaliseevisionneuse_bt_precedent');
	this._btTailleReelle = $('#objcartepersonnaliseevisionneuse_bt_taille_reelle');
	this._main = $('#objcartepersonnaliseevisionneuse');
	this._titre = $('#objcartepersonnaliseevisionneuse_titre');
	
	this._popup = new benchmark.framework.ihm.ObjPopup(); 
	this._listeUrlAjax = [];
	
	this._eventDispatcher = new benchmark.framework.util.EventDispatcher();
	$('#objcartepersonnaliseevisionneuse_conteneur').css('display','block');
};

benchmark.carte.ObjCartePersonnaliseeVisionneuse.CHANGE = "CHANGE";

benchmark.carte.ObjCartePersonnaliseeVisionneuse.prototype = {
	/** methodes public de l'objet **/
	afficher : function(pUrl){
		var nbUrlAjax = this._listeUrlAjax.length;
		for(var i=0;i<nbUrlAjax;i++){
			benchmark.framework.net.ObjControleurAjax.supprime(this._listeUrlAjax[i]);
		}
		this._listeUrlAjax = new Array();
		this._afficherCapture();
		benchmark.framework.net.ObjControleurAjax.appelScriptAction(
			this,
			pUrl,
			{
				SUCCES: this._ecouteurSuccesAffiche,
				AVANT_ENVOI: this._ecouteurAvantEnvoi,
				ERREUR: this._ecouteurErreur
			}
		);
		this._listeUrlAjax.push(pUrl);
	},
	
	/** methodes privee de l'objet **/
	_afficherTailleReelle: function(pUrl){
		this._afficherCapture();
		benchmark.framework.net.ObjControleurAjax.appelScriptAction(
			this,
			pUrl,
			{
				SUCCES: this._ecouteurSuccesDemoDetaille,
				AVANT_ENVOI: this._ecouteurAvantEnvoi,
				ERREUR: this._ecouteurErreur
			}
		);
		this._listeUrlAjax.push(pUrl);
	},
	_chargeLecteurAnimation : function(pUrl){
		pUrl += '&largeur='+this._ecranImage.width()+'&hauteur='+this._ecran.height();
		benchmark.framework.net.ObjControleurAjax.appelScriptAction(
			this,
			pUrl,
			{
				SUCCES: this._ecouteurSuccesLecteur,
				AVANT_ENVOI: this._ecouteurAvantEnvoiLecteur,
				ERREUR: this._ecouteurErreur
			}
		);
		this._listeUrlAjax.push(pUrl);
	},
	_afficherCapture: function(){
		this._ecranCapture.show();
		// hack IE permettant de bien supprimer une animation flash dans la page
		$('object',this._ecranAnimation).remove();
		this._ecranAnimation.empty();
		this._ecran.css({height:'auto'});
	},
	_genereBoutonSuivPrec : function(pBt,pUrl){
		if(pUrl){
			pBt.show();
			pBt.unbind("click");
			pBt.bind("click",benchmark.framework.util.delegate(this,function(){
				this.afficher(pUrl);
				return false;
			}));
			this._listeUrlAjax.push(pUrl);
		}else{
			pBt.unbind("click");
			pBt.hide();
		}
		
	},
	/** methodes privees (ecouteur d'evenement )de l'objet **/
	_ecouteurSuccesAffiche : function(pEvent){
		var self = pEvent.currentTarget;
		if(pEvent.json.code_retour){
			
			var ressource = pEvent.json.message_retour.courant;
			
			var evt = new benchmark.framework.util.Event(benchmark.carte.ObjCartePersonnaliseeVisionneuse.CHANGE);
			evt.info = ressource;
			evt.currentTarget = this;
			self.dispatchEvent(evt);
			
			self._titre.html(ressource.libelle);
			self._btTailleReelle.unbind("click");
			self._btTailleReelle.bind("click",function(){
				self._afficherTailleReelle(ressource.url_demo_detaillee);
				return false;
			});
			if(ressource.image){
				var image = new Image();
				image.onload = function(){
					self._ecranImage.attr('src',this.src);
					self._ecranImage.attr('alt','Carte personnalisée '+ressource.libelle);
					self._ecran.unbind("click");
					if(ressource.flag_type_ressource == 0){
						self._ecran.css({cursor: 'pointer'});
						self._ecranImageControleur.show();
						self._ecran.bind("click",function(){
							self._chargeLecteurAnimation(ressource.url_lecteur);
							return false;
						});
					}else{
						self._ecranImageControleur.hide();
						self._ecran.css({cursor: ''});
					}
					self._ecran.css({width: this.width});
					self._ecranImage.css({visibility: 'visible'});
					self._chargement.fadeOut(200);
				}
				image.src = ressource.image;				
			}else{
				self._chargement.fadeOut(200);
			}
			self._genereBoutonSuivPrec(self._btSuivant,pEvent.json.message_retour.suivant);
			self._genereBoutonSuivPrec(self._btPrecedent,pEvent.json.message_retour.precedent);
		}else{
			self._chargement.fadeOut(200);
			self._main.html("Impossible de lire la carte personnalisée");
		}
	},
	_ecouteurSuccesDemoDetaille : function(pEvent){
		var self = pEvent.currentTarget;
		self._chargement.fadeOut(200);
		if(pEvent.json.code_retour){
			self._popup.ouvreHtml(
				pEvent.json.message_retour,
				{fermetureSurClic: false}
			);
		}
	},
	_ecouteurSuccesLecteur : function(pEvent){
		var self = pEvent.currentTarget;
		self._chargement.fadeOut(200);
		if(pEvent.json.code_retour){
			var hauteur = self._ecran.height();
			var largeur = self._ecran.width();
			self._ecran.css({height:hauteur,width:largeur});
			self._ecranCapture.hide();
			self._ecranAnimation.html(pEvent.json.message_retour);
		}
	},
	_ecouteurAvantEnvoiLecteur : function(pEvent){
		var self = pEvent.currentTarget;
		self._ecran.css({cursor: ''});
		self._chargement.css('height',self._main.height()+'px');
		self._chargement.fadeIn(200);
	},
	_ecouteurErreur : function(pEvent){
		var self = pEvent.currentTarget;
		self._chargement.fadeOut(200);
		self._main.html("Impossible de lire la carte personnalisée");
	},
	_ecouteurAvantEnvoi : function(pEvent){
		var self = pEvent.currentTarget;
		self._chargement.css('height',self._main.height()+'px');
		self._chargement.fadeIn(200);
	},
	
	//************** Methode de gestion d'evenement (voir benchmark.framework.util.EventDispatcher) ***********************//
	addEventListener: function(pEventType,pFunction){
		return this._eventDispatcher.addEventListener(pEventType,pFunction);
	},
	removeEventListener: function(pEventType,pFunction){
		return this._eventDispatcher.removeEventListener(pEventType,pFunction);
	},
	dispatchEvent: function(pEvent){
		this._eventDispatcher.dispatchEvent(pEvent);
	},
	hasEventListener: function(pEventType,pFunction){
		return this._eventDispatcher.hasEventListener(pEventType,pFunction);
	}
	
	
}
