/* 
 *   RichComments! v0.1
 *   The Wordpress Plugin that lets your blog subscribers attach photos and videos
 *   to their comments
 *   
 *   Copyright (C) 2007 Stefano Verna <stefano.verna@gmail.com>
 *   
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 2 of the License, or
 *   (at your option) any later version.
 *   
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *   
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>.
 */

$(
	'<link rel="stylesheet" href="'+RCS.path+'/js/jquery/ui/themes/flora/flora.all.css" type="text/css" />' +
	'<link rel="stylesheet" href="'+RCS.path+'/js/thickbox_reloaded/jquery.thickbox.css" type="text/css" />' +
	'<link rel="stylesheet" href="'+RCS.path+'/style.css" type="text/css" media="screen" />' +
	'<script type="text/javascript" src="'+RCS.path+'/js/jquery/jquery.dimensions.js"></script>' +
	'<script type="text/javascript" src="'+RCS.path+'/js/jquery/ui/ui.lite.js"></script>'
).appendTo("head");

var __RichCommentsData = {
	
	services: [
		{
			code: "flickr",
			title: RCS._e("Flickr photos"),
			description1: RCS._e("Please insert here the URL of the Flickr page that contains the photo."),
			description2: [RCS._e("Image Found!"), RCS._e("Is the previewed image the one you were looking for?")],
			dialogSize: {width: 460, height: 275},
			previewSize: {width: 240, height: 180},
			thumbSize: {width: 75, height: 75},
			stack: []
		},
		{
			code: "youtube",
			title: RCS._e("Youtube videos"),
			description1: RCS._e("Please insert here the URL of the YouTube page that contains the video."),
			description2: [RCS._e("Video Found!"), RCS._e("Is the previewed image a frame of the video you were looking for?")],
			dialogSize: {width: 400, height: 180},
			previewSize: {width: 130, height: 97},
			thumbSize: {width: 130, height: 97},
			thickboxSize: {width: 500, height: 430},
			stack: []
		}
	],
	
	setup: function() {
		
		if ($("form#commentform input#submit").length == 0) {
			return;
		}
		
		$("body").addClass("flora");
		
		// we have to insert stuff this way cause we don't know if current theme supports 'comment-form' action
		$('<div id="rc_request">'+
				RCS._e('Do you want to insert photos and videos to your comment?')+' '+
				'<strong><a href="#" id="rc_request_link">'+RCS._e("Click here.")+'</a></strong>'+
			'</div>'+
			'<div style="display:none;" >'+
				'<div id="rc_insert_request">'+
					'<div class="preview"></div>'+
					'<div class="description1">'+
						'<p></p><input type="text" value="" class="url" />'+
					'</div>'+
					'<div class="description2">'+
						'<span class="found"></span><br/>'+
						'<span class="title"></span><br/>'+
						'<p></p>'+
					'</div>'+
				'</div>'+
			'</div>'
		).insertBefore("#commentform #submit");
		$("#rc_request_link").click(function(e) { __RichCommentsData.openDialog(); return false; });
		
		for (var i=0; i<__RichCommentsData.services.length; i++) {
			var type = __RichCommentsData.services[i];
			$(
				'<div id="rc_'+type.code+'_list" class="rc_list">'+
					type.title+':'+
					'<div class="list"></div>'+
					'<br style="clear:both; display: block; height: 0px;" />'+
				'</div>'
			).insertBefore("#commentform #submit");
			$('#rc_'+type.code+'_list').hide();
			$('.thickbox_'+type.code).thickbox(type.thickboxSize);
		}
		
		$("#commentform").submit(function() {
			
			for (var i=0; i<__RichCommentsData.services.length; i++) {
				var type = __RichCommentsData.services[i];
				var count = 0;
				$('#rc_'+type.code+'_list .rc_thumb').each(function(i) {
					var id = this.id.replace("rc_", "");
					var page = jQuery.grep(type.stack, function(p, ii){
							return p.id == id;
					})[0].url;
					$("#commentform").append('<input type="hidden" name="'+type.code+'_'+i+'" value="'+page+'" />');
					count++;
				});
				$("#commentform").append('<input type="hidden" name="'+type.code+'_number" value="'+count+'" />');
			}
			
		});
	},
	
	openDialog: function() {
		
		var go = function(code) { return function() {$(this).dialogClose(); __RichCommentsData.openInsertDialog(code) }};
		
		var b = {};
		for (var i=0; i<__RichCommentsData.services.length; i++) {
			var type = __RichCommentsData.services[i];
			b[type.title] = go(type.code);
		}
		$("<p>"+RCS._e("What do you want to insert?")+"</p>").dialog({
			title: 'RichComments!',
			position: 'center',
			width: 300,
			height: 120,
			buttons: b
		});
	},
	openInsertDialog: function(code) {

		var service = jQuery.grep(__RichCommentsData.services, function(p, ii){
			return p.code == code;
		})[0];
		
		$("#rc_insert_request .description2").hide();
		$("#rc_insert_request .description2 .found").text(service.description2[0]);
		$("#rc_insert_request .description2 p").text(service.description2[1]);
		$("#rc_insert_request .description1").show();
		$("#rc_insert_request .description1 p").text(service.description1);
		$("#rc_insert_request .preview").empty().css({
			width: service.previewSize.width,
			height: service.previewSize.height
		});
		$("#rc_insert_request input").val('');
		$("#rc_insert_request .description2 .title").text('');
		$(".ui-dialog-buttonpane button").text("OK");
		__RichCommentsData.isPreviewing = false;
		
		var b = {};
		b[RCS._e("OK")] = function() {
			if (!__RichCommentsData.isPreviewing) {
				var url = $("#rc_insert_request input").val();
				
				$.getScript(
					RCS.path + "/processURL.php?url="+escape(url)+"&site="+service.code,
					function() {
						if (processURL.success) {
							__RichCommentsData.isPreviewing = true;
							$("<img src=\""+processURL.previewSrc+"\" />").appendTo("#rc_insert_request .preview");
							$("#rc_insert_request .description1").hide();
							$("#rc_insert_request .description2").show();
							$("#rc_insert_request .description2 .title").text(processURL.title);
							$(".ui-dialog-buttonpane button").text(RCS._e("Use it!")).blur();
						} else {
							alert(RCS._e("URL not valid."));
							$("#rc_insert_request input").val('').focus();
						}
					}
				);

			} else {

				var valid = true;
				for (var i=0; i<service.stack.length; i++) {
					if (processURL.id == service.stack[i].id) {
						valid = false;
						break;
					}
				}

				if (valid) {
					service.stack.push( { id: processURL.id, url: processURL.requestedURL } );

					if (service.stack.length == 1) {
						$("#rc_"+service.code+"_list").show();
					}
					var elementid = 'rc_'+processURL.id;
					$('<div class="rc_thumb" id="'+elementid+'"><a href="'+processURL.pageURL+'" target="_black"><img src="'+processURL.thumbSrc+'" /></a></div>').appendTo("#rc_"+service.code+"_list .list").hide().fadeIn();
					$("#rc_"+service.code+"_list .list").sortable({ items: "div" });
					$("#"+elementid).css({
						width: service.thumbSize.width,
						height: service.thumbSize.height
					});
				} else {
					alert(RCS._e("The resource has already been attached!"))
				}
				__RichCommentsData.isPreviewing = false;
				$(this).dialogClose();
			}
		};
		
		$("#rc_insert_request").dialog({
			title: service.title,
			position: 'center',
			width: service.dialogSize.width,
			height: service.dialogSize.height,
			buttons: b
		});
	}
};

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