var myTips;
window.addEvent('domready',function(){
	function tooltip(msg){
		return '<table class="fc-tbx" border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="tl"></td><td class="t"></td><td class="tr"></td></tr><tr><td class="l"></td><td class="c"><div class="err"><p>'+msg+'</p></div></td><td class="r"></td></tr><tr><td class="bl"></td><td class="b"></td><td class="br"></td></tr></tbody></table>'; //<a class="close"></a>
	}

	$$('.vote1,.vote2,.vote3, .social a, .fia').each(function(el){ el.store('tip:title', tooltip(el.get('title'))); })
	
	myTips = new Tips('.vote1,.vote2,.vote3, .social a', { 'offset': { 'x': -38, 'y': -68 } });

	new Tips('.fia', { 'offset': { 'x': -38, 'y': -108 } });

    Sexy = new SexyAlertBox({
        onCloseComplete:function(){
            $$('.fc-tbx').destroy()
        },
		onShowComplete:function(){
			if(onShowComplete)	{eval('onShowComplete()') }
		}
    });
    if($('message_error') && $('message_error').get('html')!=''){
        Sexy.alert($('message_error').get('html'));
    }
    $$('.votes').each(function(el){
        el.addEvent('click',function(e){
            new Event(e).stop();
            new Request({
                method: 'get',
                url: path+'index.php?article='+this.getParent('*[ref_article]').get('ref_article')+'&votes='+this.get('votes'),
                onSuccess:function(txt){
					if(txt.substr(0,1)!='!'){ this.getElement('span').set('text',this.getElement('span').get('text').toInt()+1); }else { txt = txt.substr(1);  }
					
                    Sexy.alert(txt);
					
                }.bind(this)
            }).send();
        })
    });
    $$('.votesComment').each(function(el){
        el.addEvent('click',function(e){
            new Event(e).stop();
            new Request({
                method: 'get',
                url: path+'index.php?commentaire='+this.getParent('*[ref_commentaire]').get('ref_commentaire')+'&votesComment='+this.get('votes'),
                onSuccess:function(txt){
 					if(txt.substr(0,1)!='!'){ this.getElement('span').set('text',this.getElement('span').get('text').toInt()+1); }else { txt = txt.substr(1);  }
                   Sexy.alert(txt);
                }.bind(this)
            }).send();
        })
    });
    $$('.aValider').each(function(el){
        new FormCheck(el);
    });

    $$('.favoris').addEvent('click',function(e){
        new Event(e).stop();
		var el=this;
        new Request({
            method: 'get',
            url: path+'index.php?id_article='+this.getParent('*[ref_article]').get('ref_article')+'&togglefavoris',
            onSuccess:function(txt){
            	if(txt!='-1'){
					el.set('in_favoris',el.get('in_favoris')==1?0:1);
					testFavoris(el);
				}else{
					Sexy.alert('Vous devez avoir un compte et &ecirc;tre identifier pour g&eacute;rer des favoris');
				}

            }
        }).send();
        
        this.getParent('*[ref_article]').highlight('#00CC00');
        testFavoris(this);
    });
    $$('.favoris').each(function(el){
        testFavoris(el);
    });
    function testFavoris(el){
        if(el.get('in_favoris')==1){
			var element = new Element('img', { src:'../files/images/favoris-not.jpg', title:'Retirer aux favoris', styles:{ 'cursor':'pointer' } });
        }else{
 			var element = new Element('img', { src:'../files/images/favoris.png', title:'Ajouter aux favoris', styles:{ 'cursor':'pointer' } });
        }
		myTips.attach(element);
		element.replaces(el.getFirst());
    }


    $$('.counter').each(function(el){
        el.getProperty("class").split(' ').each(function(classX) {
            if(classX.match(/^validate(\[.+\])$/)) {
                var reg = new RegExp(/length(\[.+\])$/);
                matches = classX.match(reg);
                if(matches[1]){
                    el.nbLength=matches[1].split(',')[0].substr(1);
                    el.counter=new Element('div',{
                        'html':el.nbLength+' caract&egrave;res minimum'
                    }).inject(el,'after')
                    el.addEvent('keyup',function(){


                        remaining_chars=this.nbLength-this.get('value').trim().length;
                        this.counter.set('html',remaining_chars+' caract&egrave;res minimum');
                        if(remaining_chars==0){
                            this.highlight('#00CC00');
                        }
			
                        if(remaining_chars<1){
							this.counter.set('html','<span style="color:#00CC00">Vous pouvez poster!</span>');	
                            //this.counter.setStyle('display', 'none');
                        }else{
                            this.counter.setStyle('display', 'block');
                        }
                    }.bind(el))
                }
            }
        });
    
    })

});
