/**
 * SPETACLES
 * 
 * 	$.la.spectacles.update_spectacle_and_refresh_tags(representation)
 * 
 **** REFRESH DES TAGS
 * 	$.la.spectacles.refresh_tags()
 * 
 * *** MISE A JOUR DES DONNEES DU SPECTACLES
 * 
 * 	$.la.spectacles.update_spectacle_details(representation)
 * 	$.la.spectacles.update_alldates_list(data, node_url, node_id)
 *  $.la.spectacles.init() 
 *  
 * 
 */

(function($){
    $.la = $.la||{};
    $.extend(true,$.la,
    {
       	/* THEATRE */
    	spectacles:{
    	
            ////////
            //
            // Affichage des informations sur le spectacle en cours
            //
            ////////
    		update_spectacle_and_refresh_tags: function(representation, data, node_url, node_id){
    				this.update_spectacle_details(representation, data, node_url, node_id);
    				this.refresh_tags();    				
    		
    				if((typeof data == "object") && (data['representations'].length > 1  ))
    				{
    					this.update_alldates_list(data, node_url, node_id, representation['evenement_simple_id']);
    				}
    		},
    		
    		refresh_tags: function(){
  				refreshPub();
				refreshWRP(); 
    		},
    		
 	        update_spectacle_details: function(representation, data, node_url, node_id){    

					/** MISE A JOUR DU TITRE DE LA CATEGORIE DU THEATRE **/
	                if(representation['categorie_spectacle'] != '')
		               {	
                		$('.descriptif_spectacle h4 span').html(representation['categorie_spectacle']);
	               		}
    			
	                /** MISE A JOUR DES DONNEES SUR L'ADDRESS DU THEATRE **/
	                var town_info = '<strong><a href="http://spectacles.premiere.fr/pariscope/' + representation['url_alias'] + '" title="' + representation['nom'] + '">' + representation['nom'] + ' ' + representation['ville'] + '</a></strong><br /><br />';
	                
	                if(representation['numeroAdresse'] != ''){
		                town_info += '<div class="rSnippet" itemprop="address" itemscope itemtype="http://schema.org/PostalAddress"><div class="rSnippet" itemprop="streetAddress">'+representation['numeroAdresse'] + ', ';
		            }
	                else{
	                	town_info += '<div class="rSnippet" itemprop="address" itemscope itemtype="http://schema.org/PostalAddress"><div class="rSnippet" itemprop="streetAddress">';
	                }
	                
		            town_info += representation['adresse']+'</div>';
		            town_info += '<br /><div class="rSnippet" itemprop="postalCode">' + representation['codePostal'] + '</div> <div class="rSnippet" itemprop="addressLocality">' + representation['ville']+'</div></div>';
		            if(representation['acces'] != ''){
		                town_info += '<br />Metro: ' + representation['acces'] ;
		            }
		            if(representation['telephone'] != ''){
		            	var tel = representation['telephone'];
		            	town_info += '<br />Tel : <div class="rSnippet" itemprop="telephone">' + tel.substr(0,2) + " " + tel.substr(2,2)+ " " + tel.substr(4,2)+ " " + tel.substr(6,2)+ " " + tel.substr(8,2)+'</div>' ;
    				}
		            
		            if(representation['url'] != '')
		            {
		                var url_spectacles = representation['url'].split('http://');
		                town_info += '<br /><strong>Site :</strong> <a href="http://' + url_spectacles[0] + '" target="_blank" itemprop="url" title="site officiel : ' + representation['nom'] + '"><div class="rSnippet" itemprop="name">'+ url_spectacles[0] + '</div></a>';
		            }
		            
		            $('#le_lieu').html(town_info);
		            $('#lieu_dates').show();
	           
		            /** MISE A JOUR DES DATES DU SPECTACLES **/
		            var date_info = '';
		            
		            var aDateDebut = representation['datedebut'].split('-');
		            var datedebut = aDateDebut[2] + "/" + aDateDebut[1] + "/" + aDateDebut[0];
		            
		            if(representation['provisoire'] == '1')
		            {
		                date_info = '&agrave; partir du ' + datedebut;
	                }
	                else
	                {
	                    if(representation['datedebut'] == representation['datefin'])
	                    {
	                        
	                        date_info = 'le ' + datedebut;
	                    }
	                    else
	                    {
	                        var aDateFin = representation['datefin'].split('-');
	                        var datefin = aDateFin[2] + "/" + aDateFin[1] + "/" + aDateFin[0];
	                        date_info = 'du ' + datedebut + ' au ' + datefin;
	                    }
	                }
		   
		            date_info += '<br /><br />';       
	                         
	                date_info += representation['horaires'];
		            
	                /*
	                if(representation['tarif_unique'].length > 0)
	                {
	                    date_info += '<strong>Tarif :</strong>' + representation['tarif_unique']; 
	                }
	                else
	                {
	                    if(representation['tarif_plein'].length > 0)
	                    {
	                        date_info += '<strong>Tarif plein :</strong> ' +  representation['tarif_plein']; 
	                    }
	                    
	                    if(representation['tarif_reduit'].length > 0)
	                    {
	                        date_info += '<strong>Tarif réduit :</strong> ' +  representation['tarif_reduit'];
	                    }
	                }
		            */
	                
		            $('#les_dates').html(date_info);
		            $('#les_dates').show();

	    		    ////////
	                //
	                // Ajout des boutons reserver et localiser sur le plan
	                //
	                //////// 
		            
		           var bloc_tools = '';
		           
	               if(representation['lieu_id'] != '')
	               {
	                    bloc_tools += '<li class="item0"><a href="http://spectacles.premiere.fr/pariscope/horaire/theatremap?idtheatre=LI' + representation['lieu_id'] + '" title="Localiser le th&eacute;atre sur un plan" alt="Localiser le th&eacute;atre sur un plan"></a></li>';
	               }
	               if(representation['fnacId'] != '')
	               {
	                    bloc_tools += '<li class="item1"><a target="_blank" href="http://pariscope.fnacspectacles.com/ficheManifestation.do?codman=' + representation['fnacId'] + '" title="R�server"></a></li>';
	                }
	 
	                $('#bloc_tools').html(bloc_tools);
	                $('#bloc_tools').show();
	                
	                $('.bloc_tools_ttesvillesdates .liste').slideUp('normal');

    		},
    		
	    	/*
	    	 * Update list dates and towns for a show
	    	 */
    		update_alldates_list: function(data, node_url, node_id, exclude_location){

    		    ////////
                //
                // Ajout du bloc toutes les villes, toutes les dates
                //
                //////// 
    			
	            var html_ttesvilles = '';
	            var show = '';
	            var show_url = '';
	            var delimiter = '';
	            var counter_spectacles = 0;
	            
	            for(ville in data['representations'])
	            {
	               show = data['representations'][ville];
                   show_url = node_url + '#'+ show['lieu_id'] ;
                   
                   if(show['evenement_simple_id'] == exclude_location )  	continue;
                   
	               html_ttesvilles += delimiter + '<li>';
                   /*
	                   html_ttesvilles += '<span class="ville"><a href="' + show_url + '" style="text-decoration:none;" title="' + show['ville'] + '">' + show['ville'] + '</a></span>';
	                   html_ttesvilles += '<span class="lieu"><a href="' + show_url + '" style="text-decoration:none;" title="' + show['nom'] + '">' + show['nom'] + '</a></span>';
                   */
	               
	               html_ttesvilles += '<span class="ville"><a href="' + show_url + '" onclick="$.la.spectacles.update_spectacle_and_refresh_tags(spectacle_' + node_id+ '[\'representations\'][' + ville + '], spectacle_' + node_id + ', \'' + node_url + '\', \'' + node_id + '\'); return false;" style="text-decoration:none;" title="' + show['ville'] + '">' + show['ville'] + '</a></span>';
                   html_ttesvilles += '<span class="lieu"><a href="' + show_url + '" onclick="$.la.spectacles.update_spectacle_and_refresh_tags(spectacle_' + node_id+ '[\'representations\'][' + ville + '], spectacle_' + node_id + ', \'' + node_url + '\', \'' + node_id + '\'); return false;" style="text-decoration:none;" title="' + show['nom'] + '">' + show['nom'] + ' ' + show['ville'] + '</a></span>';
	               
                   html_ttesvilles += '<span class="date">';
                   html_ttesvilles += '<a href="' + show_url + '" onclick="$.la.spectacles.update_spectacle_and_refresh_tags(spectacle_' + node_id+ '[\'representations\'][' + ville + '], spectacle_' + node_id + ', \'' + node_url + '\', \'' + node_id + '\'); return false;" style="text-decoration:none;" title="En savoir plus">';
                   
                    aDateDebut = show['datedebut'].split('-');
                    datedebut = aDateDebut[2] + "/" + aDateDebut[1] + "/" + aDateDebut[0];
                   
                    if(show['provisoire'] == '1')
	                {
	                    html_ttesvilles += '&agrave; partir du ' + datedebut;
	                }
	                else
	                {
	                    if(show['datedebut'] == show['datefin'])
	                    {
	                        html_ttesvilles +=  'le ' + datedebut;
	                    }
	                    else
	                    {
	                        aDateFin = show['datefin'].split('-');
	                        datefin = aDateFin[2] + "/" + aDateFin[1] + "/" + aDateFin[0];
	                        html_ttesvilles +=  'du ' + datedebut + ' au ' + datefin;
	                    }
	                }
                   
                   html_ttesvilles += '</a>';
                   html_ttesvilles += '</span>';
                   html_ttesvilles += '<span class="link">&gt; <a href="' + show_url + '" onclick="$.la.spectacles.update_spectacle_and_refresh_tags(spectacle_' + node_id+ '[\'representations\'][' + ville + ']); return false;" title="En savoir plus">En savoir plus</a></span>';
	               html_ttesvilles += '</li>';
	               delimiter = '<li class="sepa"></li>';
	               counter_spectacles++;
	            }

                var height_roller = counter_spectacles * 72;
                if(height_roller > 213) height_roller = 213;
                $('.bloc_tools_ttesvillesdates .liste').height( height_roller );
                
	            $('#ttesvilles').html(html_ttesvilles);

	            $('#bloc_ttesvilles').show();
    		},
    		init: function(node_id, lieu_id, data, url_alias){
    			      if(typeof data['representations'][0] != "undefined"){
    		                ////////
    		                // VERIFICATION DE LA VILLE
    		                ////////
    			                if(lieu_id != "false")
    			                {
    			                    for(rep in data['representations'])
    			                    {
    			                        if(data['representations'][rep]['lieu_id'] == lieu_id)
    			                        {
    			                        	var representations = data['representations'][rep];
    			                        	break;
    			                        }	
    			                    }
    			                }
    			                else
    			                {
    			                    var representations = data['representations'][0];
    			                }
    		           
    		                this.update_spectacle_details(representations, data, url_alias, node_id);
    		                if(data['representations'].length > 1)
    		                	{
    		                		this.update_alldates_list(data, url_alias, node_id, representations['evenement_simple_id']);
    		                	}
    			        }

    			
    		}
    			
    	}               
    });
})(jQuery);
