
/*
	オリジナル
*/

(function($)
{  
	$.fn.myajax = function(config)
	{
		config = $.extend(
		{
			p:'article_edit',
			a:'',
			in_title:'',
			in_contents:'',
			in_id:'0'
		}
		,config);
		
		form_data	= { 'ajax':'on','p':config.p,'a':config.a,'in_title':config.in_title,'in_contents':config.in_contents,'in_id':config.in_id };
		
		res = $.ajax({ type:'POST',url:'/',dataType:'text',data:form_data,async:false });
			
		return res.responseText;
	};
	
})(jQuery);

(function($)
{  
	$.fn.newajax = function(config)
	{
		config = $.extend(
		{
			param:"{ 'ajax':'on' }"
		}
		,config);
		
		form_data	= eval("( "+config.param+" )");
		
		res = $.ajax({ type:'POST',url:'/',dataType:'text',data:form_data,async:false });
			
		return res.responseText;
	};
	
})(jQuery);

(function($)
{  
	$.fn.editatitle = function(config)
	{
		config = $.extend(
		{
			fadespeed	: 150,
			insert		: false
		}
		,config);
		
		return this.each(function(idx)
		{
			var id	= $(this).attr('alt');
			
			if(!id.match(/^[1-9][0-9]*$/))
				return;
			
			makeTitleDirectEditor($(this),id,config);
			
		});
	};
	
	function makeTitleDirectEditor(target,id,cf)
	{
		identify_name	= target.attr('id');
		
		if(identify_name.match(/title$/))
		{
			target.after('<div id="' + identify_name + '_form" style="display:none;"><input id="' + identify_name + '_input" type="text">' + 
					  		'<img id="modify" src="/img/btn_modify.gif" width="63" height="24" align="absmiddle" style="margin:0 0 0 10px;"></div>');
			
			div_height 	= target.height();
			div_width 	= target.width() - 100;
			formTag 	= target.next();
			formTag.find('input').css({ 'width':div_width,'height':div_height,'border-color':'#cccccc','border-width':'1px','border-style':'solid','background':'#F7F7F7' });
			formTag.find('img').hover(function(){ $(this).css('cursor','pointer'); },function(){ $(this).css('cursor','default'); });
		}
		else if(identify_name.match(/contents$/))
		{
			insert_button_tag = '';
			
			if(cf.insert)
				insert_button_tag = '<img class="text_paste_button" src="/img/ia_continue.gif" width="22" height="22" style="margin:10px 0 0 10px;"><br />';
			
			target.after('<div id="' + identify_name + '_form" style="display:none; margin:20px 0 0 0;">' + 
						  	'<img id="modify" src="/img/btn_modify.gif" width="63" height="24"><img id="back" src="/img/btn_back.gif" width="63" height="24"><br />' +
							insert_button_tag +
								'<textarea id="' + identify_name + '_textarea" style="margin:0 0 0 10px;"></textarea></div>');
			
			if(target.height() < 80)
				div_height 	= 80;
			else
				div_height 	= target.height();
				
			div_width 	= target.width() - 40;
			formTag 	= target.next();
			textarea	= formTag.find('textarea');
			
			textarea.css({ 'width':div_width,'height':div_height,'border-color':'#cccccc','border-width':'1px','border-style':'solid','background':'#F7F7F7' });
			formTag.find("img").css({ 'margin':'5px 0 5px 10px','left':'-50px' }).hover(function(){ $(this).css('cursor','pointer'); },function(){ $(this).css('cursor','default'); });
			
			if(cf.insert)
			{
				formTag.find('img[class="text_paste_button"]').click(function(e)
				{
					target_area = $(this).parent().find('textarea');
					
					range = target_area.getSelection();
					target_area.replaceSelection('{continue}',true);
					e.preventDefault();
				});
			}
		}
		
		textTag = target;
		
		//	ダブルクリック時の挙動
		textTag.dblclick(function(event)
		{
			id_str = $(this).attr('id');
			
			if(id_str.match(/article/))
			{
				if(id_str.match(/title/))
					res_text = target.myajax({ 'a':'gettitle','in_id':id });
				else if(id_str.match(/contents/))
					res_text = target.myajax({ 'a':'getcontents','in_id':id });
			}
			else if(id_str.match(/comment/))
			{
				if(id_str.match(/title/))
					res_text = target.myajax({ 'p':'comment_edit','a':'gettitle','in_id':id });
				else if(id_str.match(/contents/))
					res_text = target.myajax({ 'p':'comment_edit','a':'getcontents','in_id':id });
			}
			
			if(res_text == '')
				return false;
			
			$(this).fadeOut(cf.fadespeed,function()
			{
				$(this).next().find('input[id$=_input],textarea[id$=_textarea]').val(res_text);
				$(this).next().fadeIn(cf.fadespeed);
			});
		});
		
		//	修正無しで戻る場合
		formTag.dblclick(function(event)
		{
			if($(this).attr('id').match(/contents/))
				return false;
				
			$(this).fadeOut(cf.fadespeed,function(){ $(this).prev().fadeIn(cf.fadespeed); });
		});
		
		//	送信してデータを書き換える場合
		formTag.find("img[id=modify],img[id=back]").click(function(event)
		{
			if($(this).attr('id') == 'modify')
			{
				$(this).parent().fadeOut(cf.fadespeed,function()
				{
					id_str = $(this).attr('id');
					//alert(id_str);
					
					if(id_str.match(/article/))
					{
						if(id_str.match(/title/))
							modify_text = target.myajax({ 'a':'modifytitle','in_title':$(this).find('input[id$=_input]').val(),'in_id':id });
						else if(id_str.match(/contents/))
							modify_text = target.myajax({ 'a':'modifycontents','in_contents':$(this).find('textarea[id$=_textarea]').val(),'in_id':id });
					}
					else if(id_str.match(/comment/))
					{
						if(id_str.match(/title/))
							modify_text = target.myajax({ 'p':'comment_edit','a':'modifytitle','in_title':$(this).find('input[id$=_input]').val(),'in_id':id });
						else if(id_str.match(/contents/))
							modify_text = target.myajax({ 'p':'comment_edit','a':'modifycontents','in_contents':$(this).find('textarea[id$=_textarea]').val(),'in_id':id });
					}
					
					$(this).prev().html(modify_text);
					$(this).prev().fadeIn(cf.fadespeed);
					
				});
			}
			else if($(this).attr('id') == 'back')
			{
				$(this).parent().fadeOut(cf.fadespeed,function()
				{
					$(this).prev().fadeIn(cf.fadespeed);
				});
			}
		});
	}
	
})(jQuery);

(function($)
{  
	$.fn.makeform = function(config)
	{
		config = $.extend(
		{
			spam_str:'えろまんが',
			bg_color:'#F0F0F0',
			focus_bg_color:'#FFFFCC',
			margin:'0 0 10px 0',
			padding:'3px 0 3px 5px'
		}
		,config);
		
		var target = $(this);
		
		target.submit(function()
		{
			empty = false;
			
			$(this).find(':text,:password,textarea').each(function()
			{
				if($(this).val() == $(this).attr('alt') && $(this).attr('state') == 'need')
				{
					rename = $(this).val().replace(/[ 　]+/,'');
					
					alert('空欄があります');
					empty = true;
					return false;
				}
				
				if($(this).val().replace(/[ 　]+/,'') == '' && $(this).attr('state') == 'need')
				{
					alert('空欄があります！');
					empty = true;
					return false;
				}
				
				if($(this).val() == $(this).attr('alt') && $(this).attr('state') != 'need')
				{
					$(this).val('');
				}
				
				if($(this).attr('name').match(/mail/) && $(this).val() != '' && $(this).val() != $(this).attr('alt'))
				{
					if(!$(this).val().match(/^\S+@\S+\.\S+$/))
					{
						alert('メールアドレスに不備があります');
						empty = true;
						return false;
					}
				}
				
				if($(this).attr('name').match(/date/) && $(this).val() != '' && $(this).val() != $(this).attr('alt'))
				{
					if(!$(this).val().match(/^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}$/))
					{
						alert('日付に不備があります');
						empty = true;
						return false;
					}
				}
				
				if($(this).attr('name') == 'pass' && $(this).val() != '' && $(this).val().length != 4)
				{
					alert('パスワードは４文字の英数字でお願いします');
					empty = true;
					return false;
				}
			});
			
			$(this).find('select').each(function()
			{
				if($(this).val() == '' && $(this).attr('state') == 'need')
				{
					alert('選択していない項目があります');
					empty = true;
					return false;
				}
			});
			
			if(empty)
				return false;
		});
		
		target.find(':text,:password,textarea').each(function()
		{
			$(this).css({'border':'1px solid #AAAAAA','background-color':config.bg_color,'margin':config.margin,'padding':config.padding});
			
			if($(this).val() == '' && $(this).attr('alt') != '')
			{
				$(this).css({'color':'#969696'});
				$(this).val($(this).attr('alt'));
			}
			
			$(this).focus(function()
			{
				if($(this).val() == $(this).attr('alt'))
				{
					$(this).css({'color':'#000000'});
					$(this).val('');
				}
				
				$(this).css({'background-color':config.focus_bg_color});
			});
			
			$(this).blur(function()
			{
				if($(this).val() == $(this).attr('alt') || $(this).val() == '')
				{
					$(this).css({'color':'#969696'});
					$(this).val($(this).attr('alt'));
				}
				
				$(this).css({'background-color':config.bg_color});
			});
		});
		
		target.find('select').each(function()
		{
			if(typeof($(this).attr('alt')) == 'string')
			{
				$(this).prepend('<option value="">' + $(this).attr('alt') + '</option>');
				
				if(typeof($(this).attr('def')) == 'string')
				{
					$(this).val('');
				}
			}
				
			$(this).css({'margin':config.margin});
		});
		
		target.prepend('<input name="spam" type="hidden" value="' + config.spam_str + '" />')
	};
	
})(jQuery);

$(document).ready(function()
{
	$("img[class='url'],span[id='action'],#blog_title,.text_paste").hover(
		function(){ $(this).css('cursor','pointer'); },
		function(){ $(this).css('cursor','default'); }
	);
	
	$("img[class='url']").click(
		function(){ jump_url = $(this).attr('id'); location.href=jump_url; }
	);
	
	$("div[class='retweet_button']").hover(
		function(){ $(this).css('background-color','#00F'); },
		function(){ $(this).css('background-color','#09F'); }
	);
	
	$('#new_article_form').makeform();
	$("form[id='delete_confirm']").submit(function() { return confirm('本当に削除しますか？'); });
});

function panelToggle(name_a,name_b)
{
	//alert($(name_b).html());
	if($(name_a).css('display') == 'none')
		$(name_b).fadeOut(100,function() { $(name_a).fadeIn('slow'); } );
	else
		$(name_a).fadeOut(100,function() { $(name_b).fadeIn('slow'); } );
		
}