var kShare = new Class({
	
	Implements: [Options],
	
	options: {
		nom_div: null,
		titre_page: null,
		lien_page: null,
		largeur_icone: null,
		hauteur_icone: null,
		icones_principales: null,
		nb_icones_par_ligne: 5,
		place_icones_principales: null,
		bit_ly: false
	},

	initialize: function(options) { 
		this.setOptions(options);
		this.getVariables();
		
		this.getTraduction();
		this.getLink();
		
		if(this.options.bit_ly == true) {
			this.getShareJSON.delay(1000, this);
		} else {
			this.getShareJSON();
		}

		this.addEvenements();
	},
	
	getVariables: function(){
		this.nom_div = this.options.nom_div;
		this.titre_page = this.options.titre_page;
		this.lien_page = this.options.lien_page;
		this.largeur_icone = this.options.largeur_icone;
		this.hauteur_icone = this.options.hauteur_icone;
		this.icones_principales = this.options.icones_principales;
		this.nb_icones_par_ligne = this.options.nb_icones_par_ligne;
		this.places_icones_principales = this.options.place_icones_principales;
		this.bit_ly = this.options.bit_ly;
		
		this.label = $$('div.'+this.nom_div)[0].innerHTML;
	},
	
	getTraduction: function(){
		if(this.options.langue == "en") {
			this.partagez_sur = "Share on";
			this.partagez_cette_page_sur = "Share this page on"
			this.partagez_par_mail = "Share by mail";
			this.ajoutez_cette_page_favoris = "Add this page on your bookmarks";
			
		} else if(this.options.langue == "nl") {
			this.partagez_sur = "Aandeel op";
			this.partagez_cette_page_sur = "Deel deze pagina";
			this.partagez_par_mail = "Aandeel per e-mail";
			this.ajoutez_cette_page_favoris = "Voeg deze pagina toe aan uw favorieten";
			
		} else {
			this.partagez_sur = "Partagez sur";
			this.partagez_cette_page_sur = "Partagez cette page sur";
			this.partagez_par_mail = "Partagez par mail";
			this.ajoutez_cette_page_favoris = "Ajoutez cette page dans vos favoris";
		}
	},
	
	getIconesPrincipales: function(response, lien_page){
		var lien_social;
		
		// On ajoute les icônes principales
		this.icones_principales.each(function(nom_social, index_social){
			response.share.each(function(item, index){
				var lien_social = item.link.substitute({title: this.titre_page, link: lien_page});
				
				if(nom_social == item.nom_minus)
				{
					$$('div.'+this.nom_div).each(function(j_item, j_index){
						$$('div.'+this.nom_div)[j_index].innerHTML += '<a href="'+lien_social+'" title="'+this.partagez_sur+' '+item.nom+'" target="_blank"><img src="'+item.icone+'" alt="'+item.nom+'" width="27" height="27" class="share_img_'+item.nom_minus+'" /></a>';
					}.bind(this));
				}
			}.bind(this))
		}.bind(this))
	},
	
	getIconesBox: function(response, lien_page){
		
		var i = 1;
		var lien_social;
		
		// On ajoute les icônes dans la box
		response.share.each(function(item, index){
			lien_social = item.link.substitute({title: this.titre_page, link: lien_page});
			
			if(i-1 == this.nb_icones_par_ligne) {
				$$('div.'+this.nom_div).each(function(item, index){
					$$('span.kShare_box')[index].innerHTML += '<br />';
				}.bind(this));
			}
			
			$$('div.'+this.nom_div).each(function(j_item, j_index) {
				$$('span.kShare_box')[j_index].innerHTML += '<a href="'+lien_social+'" title="'+this.partagez_sur+' '+item.nom+'" target="_blank"><img src="'+item.icone+'" alt="'+item.nom+'" width="'+this.width+'" height="'+this.height+'" class="share_img_'+item.nom_minus+'"/></a>';
			}.bind(this));
			
			i++;
		}.bind(this));
		
		$$('div.'+this.nom_div).each(function(j_item, j_index){
			
			$$('span.kShare_box')[j_index].innerHTML += '<br /><a href="mailto:?subject='+this.titre_page+'&body='+lien_page+'" title="'+this.partagez_par_mail+'"><img src="/kSharePlugin/images/mail.png" alt="Mail" width="'+this.width+'" height="'+this.height+'" class="share_img_mail"/></a>';
			$$('span.kShare_box')[j_index].innerHTML += '<a href="'+lien_page+'" title="'+this.ajoutez_cette_page_favoris+'"><img src="/kSharePlugin/images/bookmark.png" alt="Bookmark" width="'+this.width+'" height="'+this.height+'" class="share_img_bookmark" /></a>';

		}.bind(this));
	},
	
	addEvenements: function(){
		
		$$('img.share_img_bookmark').addEvent('click', function() {
			if (navigator.appName != "Microsoft Internet Explorer")	{
				window.sidebar.addPanel(this.titre_page,this.lien_page,"");
			} else {
				window.external.AddFavorite(this.lien_page,this.titre_page);
			}
			return false;
		}.bind(this));
		
		$$('div.'+this.nom_div).each(function(j_item, j_index){
			//alert($$('span.kShare_box')[j_index]);
			if ($$('span.kShare_box')[j_index] != null)
				$$('span.kShare_box')[j_index].set('opacity', 0);
		});
		
		$$('div.kShare_partager').addEvent('mouseenter', function(){
			this.getChildren('span.kShare_box').setStyle('display', 'inline').fade('in');
		});
		
		$$('div.'+this.nom_div).addEvent('mouseleave', function(){
			$$('div.'+this.nom_div).each(function(item, index){
				$$('span.kShare_box')[index].fade('out');
			}.bind(this));
		}.bind(this));	
	},
	
	getBitLyLink: function(lien_page){
		this.lien_page = lien_page;
	},
	
	getLink: function(lien_page){
		var login = 'romains';
		var apiKey = 'R_c7d37ba96d54ebf12f37b4f879a485c2';

		if(lien_page) {
			this.lien_page = lien_page;
		} else {
			this.lien_page = this.lien_page.substring(this.lien_page.lastIndexOf("/")+1).split('.')[0]; // nom de la page
		}

		if(document.location.toString().test('#')) {
			var site = document.location.toString().substring('0', document.location.toString().lastIndexOf("/")+1).split('#')[0];
		} else {
			var site = document.location.toString().substring('0', document.location.toString().lastIndexOf("/")+1);
		}
		
		if(this.lien_page) {
			this.lien_page = site+this.lien_page+'.html';
		} else {
			this.lien_page = site;
		}

		if(this.bit_ly == true) {
			var bitly = new Request.JSONP({
				url: 'http://api.bit.ly/v3/shorten?login='+login+'&apiKey='+apiKey+'&longUrl='+this.lien_page+'&format=json',
				onSuccess: function(response){
					this.getBitLyLink(response.data.url)
				}.bind(this)
			}).send()
		}
	},
	
	getShareJSON: function(){
		var req = new Request.JSON({
			url: 'share.html',
			method: 'post',
			onSuccess: function(response){
				
				// On vide complètement la div
				$$('div.'+this.nom_div).each(function(item, index){
					$$('div.'+this.nom_div)[index].innerHTML = '';
				}.bind(this));
				
				// Si on choisit de placer les icônes principales à gauche
				if(this.places_icones_principales == "left") {
					this.getIconesPrincipales(response, this.lien_page);
				}
				
				// On remet le label
				$$('div.'+this.nom_div).each(function(item, index){
					$$('div.'+this.nom_div)[index].innerHTML += "<div class=\"kShare_partager\">"+this.label+"<span class=\"kShare_box\"></span></div>";
				}.bind(this));
				
				// Si on choisit de placer les icônes principales à droite
				if(this.places_icones_principales == "right") {
					this.getIconesPrincipales(response, this.lien_page);
				}
				
				// On complète la box
				$$('div.'+this.nom_div).each(function(item, index){
					$$('span.kShare_box')[index].innerHTML += this.partagez_cette_page_sur+' :<br />';
				}.bind(this));
				this.getIconesBox(response, this.lien_page);
				
				// On ajoute les évènements
				this.addEvenements();
				
				// On ajoute l'évenement sur les liens du menu, pour la mise à jour des liens lors d'un changement de slide
				this.boundChangeLink = this.changeLink.bind(this);
				$$('ul#content_top li a').addEvent('click', this.boundChangeLink);
				
			}.bind(this)
		}).send();
	},

	changeLink: function(event){
		this.getLink(event.target.getParent().get('class'));
		
		// On retire l'évenement car il va être redéfini dans getShareJSON()
		$$('ul#content_top li a').removeEvent('click', this.boundChangeLink);
		
		if(this.bit_ly == true) {
			this.getShareJSON.delay(1000, this);
		} else {
			this.getShareJSON();
		}
	}
});