var __RichCommentsData = {
	setup: function() {
		$("body").addClass("flora");
		$("rc_blackhole").appendTo("body");
		$("#rc_request_link").click(function(e) { e.stopPropagation(); __RichCommentsData.openDialog(); return false; });
		
		$(
			'<div id="rc_photo_list">'+
				'Immagini allegate al commento:'+
				'<div class="list"></div>'+
				'<br style="clear:both; display: block; height: 0px;" />'+
			'</div>'+
			'<div id="rc_video_list">'+
				'Video allegati al commento:'+
				'<div class="list"></div>'+
				'<br style="clear:both; display: block; height: 0px;" />'+
			'</div>'
		)
		.insertBefore("#commentform #submit");
		
		$("#rc_photo_list, #rc_video_list").hide();
		
		$("#commentform").submit(function() {
			var p=0, v=0;
			$("#rc_photo_list .rc_photo").each(function (i) {
				var id = this.id.replace("rc_", "");
				var page = jQuery.grep(__RichCommentsData.photos, function(p, ii){
						return p.id == id;
				})[0].url;
				$("#commentform").append('<input type="hidden" name="photo_'+i+'" value="'+page+'" />');
				p++;
			});
			$("#rc_video_list .rc_video").each(function (i) {
				var id = this.id.replace("rc_", "");
				var page = jQuery.grep(__RichCommentsData.videos, function(p, ii){
						return p.id == id;
				})[0].url;
				$("#commentform").append('<input type="hidden" name="video_'+i+'" value="'+page+'" />');
				v++;
			});
			$("#commentform").append('<input type="hidden" name="video_number" value="'+v+'" />');
			$("#commentform").append('<input type="hidden" name="photo_number" value="'+p+'" />');
		});
	},
	openDialog: function() {
		$("#rc_dialog_request").dialog({
			title: 'RichComments!',
			position: 'center',
			width: 300,
			height: 120,
			buttons: {
				"Foto Flickr": function() { $(this).dialogClose(); __RichCommentsData.openFlickrDialog(); },
				"Video YouTube": function() { $(this).dialogClose(); __RichCommentsData.openYouTubeDialog(); }
			}
		});
	},
	openFlickrDialog: function() {
		$("#rc_flickr_request .description2").hide();
		$("#rc_flickr_request .description1").show();
		$("#rc_flickr_request .preview").empty();
		$("#rc_flickr_request input").val('');
		$("#rc_flickr_request .description2 .title").text('');
		$(".ui-dialog-buttonpane button").text("OK");
		__RichCommentsData.isPreviewing = false;
		
		$("#rc_flickr_request").dialog({
			title: 'Inserimento foto Flickr',
			position: 'center',
			width: 460,
			height: 275,
			buttons: {
				'OK': function() {
					if (!__RichCommentsData.isPreviewing) {
						var photourl = $("#rc_flickr_request input").val();
						
						$.getScript(
							__RichCommentsData.pluginPath + "/processURL.php?url="+escape(photourl)+"&site=flickr",
							function() {
								if (processURL.success) {
									__RichCommentsData.isPreviewing = true;
									$("<img src=\""+processURL.smallSrc+"\" />").appendTo("#rc_flickr_request .preview");
									$("#rc_flickr_request .description1").hide();
									$("#rc_flickr_request .description2").show();
									$("#rc_flickr_request .description2 .title").text(processURL.title);
									$(".ui-dialog-buttonpane button").text("Inserisci!").blur();
								} else {
									alert("L'URL inserito non è valido. Riprova!");
									$("#rc_flickr_request input").val('').focus();
								}
							}
						);
						
					} else {
						
						var valid = true;
						for (var i=0; i<__RichCommentsData.photos.length; i++) {
							if (processURL.id == __RichCommentsData.photos[i].id) {
								valid = false;
								break;
							}
						}
						
						if (valid) {
							__RichCommentsData.photos.push( { id: processURL.id, url: processURL.requestedURL } );
						
							if (__RichCommentsData.photos.length == 1) {
								$("#rc_photo_list").show();
							}
							var elementid = 'rc_'+processURL.id;
							$('<div class="rc_photo" id="'+elementid+'"><a href="'+processURL.pageURL+'" target="_black"><img src="'+processURL.squareSrc+'" /></a></div>').appendTo("#rc_photo_list .list").hide().fadeIn();
						
							$("#rc_photo_list .list").sortable({ items: "div" });
						
						} else {
							alert("L'immagine è già stata inserita in galleria!")
						}
						
						__RichCommentsData.isPreviewing = false;
						$(this).dialogClose();
					}
				}
			}
		});
	},
	openYouTubeDialog: function() {
		$("#rc_youtube_request .description2").hide();
		$("#rc_youtube_request .description1").show();
		$("#rc_youtube_request .preview").empty();
		$("#rc_youtube_request .description2 .title").text('');
		$("#rc_youtube_request input").val('');
		$(".ui-dialog-buttonpane button").text("OK");
		__RichCommentsData.isPreviewing = false;
		
		$("#rc_youtube_request").dialog({
			title: 'Inserimento video YouTube',
			position: 'center',
			width: 400,
			height: 180,
			buttons: {
				'OK': function() {
					if (!__RichCommentsData.isPreviewing) {
						var photourl = $("#rc_youtube_request input").val();
						
						$.getScript(
							__RichCommentsData.pluginPath + "/processURL.php?url="+escape(photourl)+"&site=youtube",
							function() {
								if (processURL.success) {
									__RichCommentsData.isPreviewing = true;
									//$(__RichCommentsData.youtube_player.replace(/videoID/g, processURL.id)).appendTo("#rc_youtube_request ");
									$(processURL.thumbSrc).appendTo("#rc_youtube_request .preview");
									$("#rc_youtube_request .description1").hide();
									$("#rc_youtube_request .description2").show();
									$("#rc_youtube_request .description2 .title").text(processURL.title);
									$(".ui-dialog-buttonpane button").text("Inserisci!").blur();
								} else {
									alert("L'URL inserito non è valido. Riprova!");
									$("#rc_youtube_request input").val('').focus();
								}
							}
						);
						
					} else {
						
						var valid = true;
						for (var i=0; i<__RichCommentsData.videos.length; i++) {
							if (processURL.id == __RichCommentsData.videos[i].id) {
								valid = false;
								break;
							}
						}
						
						if (valid) {
							__RichCommentsData.videos.push( { id: processURL.id, url: processURL.requestedURL } );
						
							if (__RichCommentsData.videos.length == 1) {
								$("#rc_video_list").show();
							}
							var elementid = 'rc_'+processURL.id;
							$('<div class="rc_video" id="'+elementid+'"><a href="'+processURL.pageURL+'" target="_black">'+processURL.thumbSrc+'</a></div>').appendTo("#rc_video_list .list").hide().fadeIn();
						
							$("#rc_video_list .list").sortable({ items: "div" });
						
						} else {
							alert("Il video già stato inserito in galleria!")
						}
						
						__RichCommentsData.isPreviewing = false;
						$(this).dialogClose();
					}
				}
			}
		});
	},
	photos: [],
	videos: [],
	youtube_player: '<object width="221" height="180"><param name="movie" value="http://www.youtube.com/v/videoID&rel=0&color1=0x006699&color2=0x54abd6&border=0"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/videoID&rel=0&color1=0x006699&color2=0x54abd6&border=0" type="application/x-shockwave-flash" wmode="transparent" width="221" height="180"></embed></object>'
};

$(document).ready(function() {
	__RichCommentsData.setup();
});
