
window.addEvent( 'domready', function() {
	var addThisLink = $('addThis');
	if (addThisLink) {
		new Asset.javascript('http://s7.addthis.com/js/250/addthis_widget.js');
		addThisLink.addEvents({
		  'mouseover': function() {
		  	  return addthis_open(this, '', '[URL]', '[TITLE]');
		  },
		  'mouseout': function() {
			  addthis_close();
		  },
		  'click': function () {
			  return addthis_sendto();
		  }
		});
	}
} );

/**
 * Lien rel=external s'ouvrent dans une nouvelle fenêtre
 */
window.addEvent('domready', function() {
		// permet de deplier / replier une description  
		if ($('description_less')) {
	
			$$('.edito .lien_vue a').addEvent('click', function() {
				var a = $(this);

				var action = a.hasClass('less')? 'less' : 'more' ;
				var otherAction = ( action == 'more' )? 'less' : 'more';
				
				$('description_'+ action).set({ styles: { display: 'inline' }});
				$('description_'+ otherAction).set({ styles: { display: 'none' }});
	
			//	a.set('text', otherAction );
				a.removeClass( action );
				a.addClass( otherAction );
		
				return false;
			});
		
		}
		
		
    var links = $$('a[rel=external]');
    links.each(function(link) {
    	var title = "";
    	if (link.get('title')) {
    		title = link.get('title') + " "; 
    	}
    	else {
    		for(var i = 0; i <link.childNodes.length; i++) {
                if (link.childNodes[i].nodeName == 'IMG' ) {
                    title += link.childNodes[i].attributes['alt'].nodeValue.trim() + " ";
                }
                else {
                    try{
                        title += link.childNodes[i].textContent.trim() + " ";
                    }
                    catch(err){
                        
                    }
                }
            }
    	}
    	link.set('title', title + '(nouvelle fenêtre)');
        link.addEvent('click', function() {
            window.open(this.href);
            return false;
        });
    });
    
    var dds = $$('dd.dd_select');
    dds.each(function(dd) {
             var select =  new Element('select', 
                                        {'events': {
                                            'change': function(){
                                                redirect(this);
                                            }
                                        }
                             });
             var opt1 = new Element( 'option', 
                                    {'value': '#',
                                    'html': '---'
                                    } );
             opt1.inject( select );
             var listli = dd.getElements('li');
             listli.each(function(li) {
                         var a = li.getElement( 'a' );
                         var href = a.href;
                         var label = a.innerHTML;
                         var spans = li.getElements( 'span' );
                         spans.each(function(span){
                                 label += ' '+span.innerHTML;
                                 });
                         var opt = new Element( 'option',                         
                                               {'value': href,
                                               'html': label
                                               });
                         opt.inject( select );
                         });
             dd.innerHTML = '';
             select.inject( dd );
    });
});

function redirect( sel ) 
{
	var opt = sel.options[sel.selectedIndex];
	document.location.href = opt.value;
}

function hsHtmlExpand( elt, type ) 
{
    //return 
    hs.htmlExpand(document.getElementById(elt), { objectType: type, slideshowGroup: 'gallery' } );
}

//@@@@ slideshow d'images dans les pages offres
/*
 * Replace : expression reguliere permettant de transformer l'url des images en l'url des vignettes
 * width et height : taille des images du diapo
 */
function slide_show(data, delay, replace, width, height){
	var widthDefault = 340;
	var heightDefault = 200;
	var replaceDefault = [/(we_view)/ , 'vdiapowe'];
	
	if ( replace != null )
    {
		replaceDefault = replace;
    }
	if( width != null )
    {
		widthDefault = width;
    }
	if( height != null )
    {
		heightDefault = height;
    }
    
	var myShow = new Slideshow('show', data, {controller: false, captions: true, height: heightDefault, hu: '', thumbnails: true, width: widthDefault, delay: delay, replace: replaceDefault});
    if ($$(".slideshow-thumbnails")){
        var precedent = new Element('p', {'class': 'prev' , text:'<'});
        var suivant = new Element('p', {'class': 'next' , text:'>'});
        $$(".slideshow-thumbnails").grab(precedent, 'top');   
        $$(".slideshow-thumbnails").grab(suivant, 'bottom');
    }
}

function slide_show_offre(data, delay){
    var myShow = new Slideshow('show', data, {controller: false, captions: true, height: 200, hu: '', thumbnails: true, width: 340, delay: delay, replace: [/(offre_view)/ , 'vdiapooffre'] });
    if ($$(".slideshow-thumbnails")){
        var precedent = new Element('p', {'class': 'prev' , text:'<'});
        var suivant = new Element('p', {'class': 'next' , text:'>'});
        $$(".slideshow-thumbnails").grab(precedent, 'top');   
        $$(".slideshow-thumbnails").grab(suivant, 'bottom');
    }
}

/* Mise à jour notation */
function updateCommentRating( value, nbStars )
{
	var input = $('comment_ratings_hidden_input');
    if ( input )
    {
        input.setAttribute( 'value', value );
    }
    var starLI = $('rating_percent_comment');
    if ( starLI )
    {
        var width = value / nbStars;
        width = width * 100;
        starLI.setStyle('width', width+'%');
    }
}



