
$().ready(function(){
	
	$('#scrollbar1').tinyscrollbar();
	
	var cp;
	if($('#colorpicker').length > 0){
		cp = $.farbtastic('#colorpicker');
		cp.linkTo($('#color'));
	}
	
	$("a[rel='galerie']").colorbox({transition:"fade",current : "{current} sur {total}"});
	
	/* Renomer dans admin */
	var nbrAlbum = $('.nomal').length;
	for(i = 1; i <= nbrAlbum; i++){
		$(".rename-"+i).colorbox({width:"30%", height: "36%", inline:true, href:"#renameBox-"+i});		
	}
	
	$('#rename').live('submit', function(){
		
		var idAl = $('#idal', this).val();
		var newName = $('#name', this).val();
		newName = $.trim(newName);
		if(newName != ''){
			$.ajax({
				type: "POST",
				url: "index.php?page=admin&pageAdmin=photo&actionA=renommer",
				data: {album: idAl, nom: newName},
				success: function(msg){
					$('.id-'+idAl+' span').text(newName);
					$('.errorN').show().addClass('green').text('Titre modifié avec succès');
				}
			});
		}
		else{
			$('.errorN').show().addClass('red').text('Le nom ne doit pas être vide !');
		}
		
		return false;
	});
	
	
	$('.click').click(function(){
		
		var type = $(this).attr('class').split(' ')[2];
		
		if(type == 'lien'){
			insertTag('', '', 'textareaBBCODE', 'lien');
		}
		else if (type == 'floatr') {
			insertTag('[flottant="right"]', '[/flottant]', 'textareaBBCODE');
			$('.dflottant').hide();
		}
		else if (type == 'floatl') {
			insertTag('[flottant="left"]', '[/flottant]', 'textareaBBCODE');
			$('.dflottant').hide();
		}
		else{
			insertTag('['+type+']', '[/'+type+']', 'textareaBBCODE');
		}
	});
	
	$('.send').click(function(){
		if(cp.color != null){
			insertTag('[color="'+cp.color+'"]', '[/color]', 'textareaBBCODE');
			$(this).parent().parent().hide();
		}
	});
	
	$('.expand').click(function(){
		
		var type = $(this).attr('class').split(' ')[2];

		$('.d'+type).toggle();
	});
	var ij = 'music-nu-jazz-project';
	/* Cas ou on doit delete quelque chose, on demande bien avant ! */
	$('.delete').click(function(event){
		
		if (!confirm("Etes vous sur ??? ")){
			event.preventDefault();
		}
	});
	
	$('.first').click(function(e){
		
		$("#floatinfBar #link").slideToggle();
		e.preventDefault();
	});
	/*
	var don;
	$.ajax({
		async : false,
		   url: "js/test.js",
		   dataType: "script",
		   success: function(e){
	   		don = e;
	  }
	});
	eval(don);
	alert(h);
	

	*/
	
	var Playlist = function(instance, playlist, options) {
		var self = this;

		this.instance = instance; // String: To associate specific HTML with this playlist
		this.playlist = playlist; // Array of Objects: The playlist
		this.options = options; // Object: The jPlayer constructor options for this playlist

		this.current = 0;

		this.cssId = {
			jPlayer: "jquery_jplayer_",
			interface: "jp_interface_",
			playlist: "jp_playlist_"
		};
		this.cssSelector = {};

		$.each(this.cssId, function(entity, id) {
			self.cssSelector[entity] = "#" + id + self.instance;
		});

		if(!this.options.cssSelectorAncestor) {
			this.options.cssSelectorAncestor = this.cssSelector.interface;
		}

		$(this.cssSelector.jPlayer).jPlayer(this.options);

		$(this.cssSelector.interface + " .jp-previous").click(function() {
			self.playlistPrev();
			$(this).blur();
			return false;
		});

		$(this.cssSelector.interface + " .jp-next").click(function() {
			self.playlistNext();
			$(this).blur();
			return false;
		});
	};

	Playlist.prototype = {
		displayPlaylist: function() {
			var self = this;
			$(this.cssSelector.playlist + " ul").empty();
			
			for (i=0; i < this.playlist.length; i++) {
				
				var listItem = (i === this.playlist.length-1) ? "<li class='jp-playlist-last'>" : "<li>";
				listItem += (i+1) + ".<a href='#' id='" + this.cssId.playlist + this.instance + "_item_" + i +"' tabindex='1'>"+ this.playlist[i].name +"</a>";
				listItem += "</li>";

				// Associate playlist items with their media
				$(this.cssSelector.playlist + " ul").append(listItem);
				$(this.cssSelector.playlist + "_item_" + i).data("index", i).click(function() {
					var index = $(this).data("index");
					if(self.current !== index) {
						self.playlistChange(index);
					} else {
						$(self.cssSelector.jPlayer).jPlayer("play");
					}
					$(this).blur();
					return false;
				});

			
			}
		},
		playlistInit: function(autoplay) {
			if(autoplay) {
				this.playlistChange(this.current);
			} else {
				this.playlistConfig(this.current);
			}
		},
		playlistConfig: function(index) {
			$(this.cssSelector.playlist + "_item_" + this.current).removeClass("jp-playlist-current").parent().removeClass("jp-playlist-current");
			$(this.cssSelector.playlist + "_item_" + index).addClass("jp-playlist-current").parent().addClass("jp-playlist-current");
			this.current = index;
			$(this.cssSelector.jPlayer).jPlayer("setMedia", this.playlist[this.current]);
		},
		playlistChange: function(index) {
			this.playlistConfig(index);
			$(this.cssSelector.jPlayer).jPlayer("play");
		},
		playlistNext: function() {
			var index = (this.current + 1 < this.playlist.length) ? this.current + 1 : 0;
			this.playlistChange(index);
		},
		playlistPrev: function() {
			var index = (this.current - 1 >= 0) ? this.current - 1 : this.playlist.length - 1;
			this.playlistChange(index);
		}
	};


	var audioPlaylist = new Playlist("2", getPL(), {
		ready: function() {
			audioPlaylist.displayPlaylist();
			audioPlaylist.playlistInit(false); // Parameter is a boolean for autoplay.
		},
		ended: function() {
			audioPlaylist.playlistNext();
		},
		play: function() {
			$(this).jPlayer("pauseOthers");
		},
		swfPath: "swf",
		solution: "html, flash",
		supplied: "mp3"
	});
	
	function getPL(){
		
		if($('.jp-type-playlist').height() != null){
			var returnVar;
			$.ajax({
				url: "ajax/?page=music",
				type: "POST",
				data: ({val : ij}),
				async:false,
				success: function(rep){
					returnVar = rep;
				}
			});
		}
		return returnVar;
	}
	
	

	
	
});

function insertTag(startTag, endTag, textareaId, tagType) {
	var field = document.getElementById(textareaId);
	var scroll = field.scrollTop;
	field.focus();
	
	
	if (window.ActiveXObject) {
		var textRange = document.selection.createRange();            
		var currentSelection = textRange.text;
	} else {
		var startSelection   = field.value.substring(0, field.selectionStart);
		var currentSelection = field.value.substring(field.selectionStart, field.selectionEnd);
		var endSelection     = field.value.substring(field.selectionEnd);
	}
	
	if (tagType) {
		switch (tagType) {
			case "lien":
					endTag = "[/lien]";
					if (currentSelection) {
							if (currentSelection.indexOf("http://") == 0 || currentSelection.indexOf("https://") == 0 || currentSelection.indexOf("ftp://") == 0 || currentSelection.indexOf("www.") == 0) {
									var label = prompt("Quel est le libellé du lien ?") || "";
									startTag = "[lien=\"" + currentSelection + "\"]";
									currentSelection = label;
							} else {
									var URL = prompt("Quelle est l'url ?");
									startTag = "[lien=\"" + URL + "\"]";
							}
					} else {
							var URL = prompt("Quelle est l'url ?") || "";
							var label = prompt("Quel est le libellé du lien ?") || "";
							startTag = "[lien=\"" + URL + "\"]";
							currentSelection = label;                     
					}
			break;
		}
	}
	
	if (window.ActiveXObject) {
		textRange.text = startTag + currentSelection + endTag;
		textRange.moveStart('character', -endTag.length-currentSelection.length);
		textRange.moveEnd('character', -endTag.length);
		textRange.select();  
	} else { // Ce n'est pas IE
		field.value = startSelection + startTag + currentSelection + endTag + endSelection;
		field.focus();
		field.setSelectionRange(startSelection.length + startTag.length, startSelection.length + startTag.length + currentSelection.length);
	}  
	
	field.scrollTop = scroll;   
}
