/*
SYNTAXE :

new Moofle( options );

options :
	[ images : ( defaut [ ] ) image ou liste d'images à arrondir ]
	[ rayon : ( defaut 20)  rayon par défaut à appliquer ]
	[ classe : ( defaut '' )  les image contenant cette classe seront arrondie, le parametre 'image' ne sera interprété
	
		html :
			<img src="mon_image.png" alt="mon image" class="arrondir" />
		javascript :
			new Moofle({'classe':'arrondir'});
	
		la classe peut contenir des parametres optionnel :
		classe( [rayon] [,no] [,ne] [,se] [,so] )
		rayon : rayon à appliquer
		hg ou tl : arrondir le coin haut/gauche
		hd ou tr : arrondir le coin haut/droite
		bd ou br : arrondir le coin bas/droite
		bg ou bl : arrondir le coin bas/gauche
		
		Si les paramètres no, ne, se, so ne sont pas renseigné, tout les coins seront arrondi.
		
		html : pour arrondir les coins haut/droite et bas/gauche uniquement et avec un rayon de 15px
			<img src="mon_image.png" alt="mon image" class="arrondir(15,hg,bd)" />
		javascript :
			new Moofle({'classe':'arrondir'});
	]
	
*/


var Moofle = new Class({
	Implements: [Options],
	
	options: {
		'images':[],
		'rayon':20,
		'borduretaille':0,
		'bordurecouleur':'#000000',
		'classe':''
    },
	
    initialize: function( options ){
		this.setOptions(options);
		if( this.options.classe != '' ){
			this.images = $$('img[class*='+this.options.classe+']');
			this.options.enclasse = true;
		} else{
			this.images = [this.options.images].flatten();
			this.options.enclasse = false;
		}
		
		this.images.each( function( image ){
			new Asset.image( image.store( 'moofle' , this.options ).get('src') ,{onload:this.arrondir.bind(image)});
		}.bind(this));
		
	},
	
	arrondir:function(){
		
		moofle = this.retrieve( 'moofle' );
		
		this.taille = this.getSize();
		
		this.conteneur = new Element('div').setStyles({
			'width':this.taille.x,
			'height':this.taille.y,
			'margin':0,
			'padding':0,
			'display':'inline-block',
			'text-align':'left',
			'position':'relative'
		}).inject( this.setStyle('display','none') , 'after' );
		
		// Récupération du lien parent
		this.parent = this.getParent('a');
		var lightbox = false;
		var rel = this.parent.get('rel');
		if( rel ){
			lightbox = !this.parent.get('rel').indexOf('lightbox')==-1
		}
		if( this.parent && this.parent.get('href') != '' && this.parent.get('href')!='#' && lightbox){
			this.conteneur.store('lien',this.parent.get('href')).addEvent('click',function(){
				document.location.href=this.retrieve('lien');
			});
		}

		//
		
		this.marge = 0;
		this.largeur = 0;
		
		//  OPTIONS
		this.rayon = moofle.rayon;
		
		if( this.taille.x / 2 < this.rayon ){ this.rayon = this.taille.x; }
		if( this.taille.y / 2 < this.rayon ){ this.rayon = this.taille.y; }
		
		this.no = false;
		this.ne = false;
		this.se = false;
		this.so = false;
		
		this.borduretaille = moofle.borduretaille;
		this.bordurecouleur = moofle.bordurecouleur;
		
		// FIN OPTIONS
		
		if( moofle.enclasse ){
			this.classe = this.get('class');
			this.debut = this.classe.indexOf( moofle.classe+'(' ) + moofle.classe.length + 1;
			this.fin = this.classe.indexOf( ')' , this.debut );
			this.parametres = this.classe.substring( this.debut , this.fin ).split(',');
			for( parametre = 0 ; parametre < this.parametres.length ; parametre ++ ){
				this.parametres[parametre] = this.parametres[parametre].trim();
				if( this.parametres[parametre].toInt() > 0 ){
					this.rayon = this.parametres[parametre].toInt();
				} else if( this.parametres[parametre] == 'hg' || this.parametres[parametre] == 'tl' ){
					this.no = true;
				} else if( this.parametres[parametre] == 'hd' || this.parametres[parametre] == 'tr' ){
					this.ne = true;
				} else if( this.parametres[parametre] == 'bd' || this.parametres[parametre] == 'br' ){
					this.se = true;
				} else if( this.parametres[parametre] == 'bg' || this.parametres[parametre] == 'bl' ){
					this.so = true;
				} else if( this.parametres[parametre].indexOf( 'bt:' ) > -1 ){
					this.borduretaille = this.parametres[parametre].substr(3).toInt();
				} else if( this.parametres[parametre].indexOf( 'bc:' ) > -1 ){
					this.bordurecouleur = this.parametres[parametre].substr(3);
				}
			}
		}
		
		if( !this.no && !this.ne && !this.se && !this.so ){
			this.no = true;
			this.ne = true;
			this.se = true;
			this.so = true;
		}
		
		
		for( i = 1 ; i <= this.taille.y ; i++ ){

			this.hauteur = 1;
			this.largeur = this.taille.x - 2*this.borduretaille;
			this.haut = i-1;
			this.marge_haut = -i+1;
			
		
			if( i <= this.rayon ){
				this.marge = Math.round( this.rayon - ( Math.sqrt( Math.pow( this.rayon , 2 ) - Math.pow( this.rayon - i , 2 ) ) ) );
				if( this.ne ){ this.largeur -= this.marge; }
				if( this.no ){ this.largeur -= this.marge;	}
				else{ this.marge = 0; }
			} else if( i >= this.taille.y - this.rayon ){
				this.marge = Math.round( this.rayon - ( Math.sqrt( Math.pow( this.rayon , 2 ) - Math.pow( this.rayon - ( this.taille.y - i + 1 ) , 2 ) ) ) );
				if( this.se ){ this.largeur -= this.marge; }
				if( this.so ){ this.largeur -= this.marge;	}
				else{ this.marge = 0; }
			} else{
				i = this.taille.y-this.rayon;
				this.hauteur = this.taille.y-2*this.rayon;
				this.marge = 0;
			}
		
			if( i <= this.borduretaille ){
				this.background = this.bordurecouleur;
			} else if( i > this.taille.y - this.borduretaille ){
				this.background = this.bordurecouleur;
			} else{
				this.background = 'transparent';
			}
		
			this.styleligne = {
				'width':this.largeur,
				'height':this.hauteur,
				'overflow':'hidden',
				'margin':0,
				'padding':0,
				'position':'absolute',
				'left':this.marge,
				'top':this.haut,
				'border-width':0,
				'border-left-width':this.borduretaille,
				'border-right-width':this.borduretaille,
				'border-style':'solid',
				'border-color':this.bordurecouleur,
				'background-color':this.background
			};
			
			
			this.styleintraligne = {
				'width':this.taille.x,
				'height':this.taille.y,
				'margin':0,
				'padding':0,
				'margin-left':-this.marge-this.borduretaille,
				'margin-top':this.marge_haut
			};
			
			this.ligne = new Element( 'div' ).setStyles(this.styleligne);
			
			if( i > this.borduretaille && i <= this.taille.y - this.borduretaille ){
				this.ligne.adopt(
					new Element( 'div').setStyles(this.styleintraligne).adopt(
						this.clone().setStyle('display','')
					)
				)

			}
			
			this.ligne.inject( this.conteneur );
			
		}
	
	}
	
});
  
 
