/*
*jQuery-husen ver1.0
*
*指定した要素の子要素を全て移動可能にします。
*ajaxモードをOnにすると移動終了時にhusenID,xy座標を指定したアドレスへPOSTします。
*複数設定できますが、[form+ajax]を利用するのは１つまでです。区別できません。
*ajaxでデータを送信する処理は_husensubmitで規定しています。
*追加でなんらかのデータを追加したい場合はそこに追記をしてください。
*
*This plugin is released under the MIT license.
* Depends:
*	ui.core.js        http://jquery.com/
*	ui.draggable.js   http://docs.jquery.com/UI/Draggables
*	jquery.hotkeys.js http://code.google.com/p/js-hotkeys/
*
*All you have to do is call the function like this: 
*$(document).ready(function(){$('#example').husen();});
*
*If you use ajax mode 
*$(document).ready(function(){$('#example').husen({ajax: 1});});
*
* Author URL: http://blog.gifumaster.com/
*/

;(function() {
	
	$(document).bind('keydown', 'Ctrl+z',function (evt){
		if( $(".ui-draggable").hasClass("ui-draggable-disabled") ){
			$(".ui-draggable-child").draggable('enable');
		}else{
			$(".ui-draggable-child").draggable('disable');
		}
		return false;
	});

	//フォーム表示
	$(document).bind('dblclick',function (evt){
		//値を設定
		mouseX = evt.pageX;
		mouseY = evt.pageY;
		$("#husen_x").val(mouseX);
		$("#husen_y").val(mouseY);
		//移動
		$("#ui-draggable-form").show();
		$("#ui-draggable-form").animate({
			top :mouseY +"px",
			left:mouseX +"px",
			height: "100%"
			},500);
		//$("#ui-draggable-form").children("#husen_note").focus();
	});

	jQuery.fn.extend({
		husen : function (config) {
			config = jQuery.extend({
				url: "http://blog.gifumaster.com/husen/husen_submit.php",
				ajax: 0,
				destory: 1,
				zIndex: 10
			},config);
			
			//CSS 位置(0,0) margin,paddingを0にしないとフォーム,座標がずれるときがある(主にIE)
			this.css({top:0,left: 0,margin: 0,padding: 0,position: "absolute"});

			//husen属性にformが指定されてない要素は全て付箋として扱う
			this.children().filter("[id!=husenform]")._husenadd(config);
			
			//formが指定されている場合
			var formTarget = this.children().filter("[id=husenform]");
			formTarget.draggable({
				stop: function(event, ui){
					$(formTarget).children("#husen_x").val(ui.offset.left);
					$(formTarget).children("#husen_y").val(ui.offset.top);
				}
			}).attr("id","ui-draggable-form");
			formTarget.children("textarea").after('<span class="destory">×</span>');
			formTarget.children(".destory").bind("click",function(){
				$(this).parent().slideUp();
			}).addClass("ui-draggable-destory");

			formTarget.children(":submit").bind("click",function(){
				formTarget._husensubmit(config);
			});
			formTarget.children("#husen_note").bind('keydown', 'Ctrl+return',function (evt){
				formTarget._husensubmit(config);
			});
		},
		
		_husensubmit : function(config){
			
			var target = this;
			
			var targetX;
			var targetY;

			targetX    = target.children("#husen_x").val();
			targetY    = target.children("#husen_y").val();
			targetNote = target.children("#husen_note").val();
			

			if(targetNote != ""){
				config.zIndex += 1;
				if(config.ajax == 1){
					//AJAX送信してID等入手し、挿入する必要あり。
					param = {"note": targetNote,"offsetX": targetX,"offsetY": targetY,"mode": "create"};
					$.post(config.url,param,function(text){
						$(target).after("<li husenid="+ text +" style=\"z-index:"+ config.zIndex +";position:absolute;top:"+ targetY +"px;left:"+ targetX +"px;\"><pre class=\"text\"></pre></li>").next()._husenadd(config);
						if(!$.support.htmlSerialize){
							targetNote = document.createTextNode(targetNote.replace(/\r?\n/g, "\r"));
						}
						$(target).next().children(".text").html(targetNote);
						$(target).children("#husen_note").val("");
						$(target).slideUp();
					});
				}else{
					//ただ追加するだけ。
					$(target).after("<li style=\"z-index:" + config.zIndex + ";position:absolute;top:"+ targetY +"px;left:"+ targetX +"px;\"><pre class=\"text\"></pre></li>").next()._husenadd(config);
					$(target).next().children(".text").html(targetNote);
					$(target).children("#husen_note").val("");
					$(target).slideUp();
				}
			}
			return false;
		},
		
		_husenadd : function (config) {
			
			var target = this;
			
			//Click
			target.bind("click",function(){
				$(this).css("zIndex",config.zIndex += 1);
			});
			
			//ドラッグ
			target.draggable({
				snap: true,
				snapTolerance: 7,
				cursor: 'pointer',
				zIndex: config.zIndex,
				opacity:0.5,
				stop: function(event, ui) {
					if(config.ajax == 1){
						param = {"husenid": $(this).attr("husenid"),"offsetY": ui.offset.top,"offsetX": ui.offset.left,"mode": "update"};
						$.post(config.url,param,function(text){});
					}else{
					}
				}
			}).addClass("ui-draggable-child");

			//破棄 ajaxモードでない場合、非表示にするだけ。
			if(config.destory == 1){
				target.children(".text").after('<span class="destory">×</span>');
				target.children(".destory").bind("click",function(){
					target = $(this).parent();
					param = {"husenid": target.attr("husenid"),"mode": "delete"};
					if(config.ajax == 1){
						$.post(config.url,param,function(text){});
						$(this).parent().slideUp();
					}else{
						$(this).parent().slideUp();
					}
				}).addClass("ui-draggable-destory");
			}
		}
	});

})(jQuery);
