var HTTP_SERVER = 'http://www.reedcom.jp';
jQuery.fn.loadpage = function(target_url, loading_img_size, loadSuccess){
	var target_obj = this;
	var loading_imgs = { s : "/images/common/loading_s.gif", m : "/images/common/loading_m.gif", l : "/images/common/loading_l.gif" };
	if ( !loading_img_size ) { var loading_img_size = "l"; }
	target_obj.css("height",target_obj.height()+"px");
	target_obj.html("");
	target_obj.css("background","url("+HTTP_SERVER+loading_imgs[loading_img_size]+") no-repeat center");
	jQuery.ajax({
		url: target_url,
		success: function(rspnse){
			target_obj.css("background","none");
			target_obj.css("height","auto");
			target_obj.html(rspnse);
			//target_obj.css("height",target_obj.height()+"px");
			if ( jQuery.isFunction(loadSuccess) ) { loadSuccess(); }
		}
	});
}

jQuery.fn.ajaxform = function(frmSubmit, frmSuccess, frmCancel){
	var frm_pt = jQuery(this);
	var frm_action = frm_pt.attr("action");
	frm_pt.unbind("submit").bind("submit", function(event){
		var inp_data = frm_pt.serialize();
		var pre = ( jQuery.isFunction(frmSubmit) ) ? frmSubmit() : true;
		if ( pre != false ) {
			jQuery.ajax({
				type: "POST",
				url: frm_action,
				data: inp_data,
				success: function(rspnse){ frmSuccess(rspnse); }
			});
		}
		event.preventDefault();
	});
	jQuery("#"+(frm_pt.attr("id"))+" #cancelBtn").unbind("click").bind("click", function(event){
		if ( jQuery.isFunction(frmCancel) ) { frmCancel(); }
		event.preventDefault();
	});
}

jQuery.fn.deleterec = function(delpage, reloadcontent, reloadcontainer){
	this.unbind("click").bind("click", function(event){
		if ( confirm("本当に削除しますか？") ) {
			jQuery.ajax({
				url: delpage+"?delete="+jQuery(this).attr("id"),
				success: function(rspnse){
					jQuery("#"+reloadcontainer).loadpage(reloadcontent);
				}
			});
		}
		event.preventDefault();
	});
}

jQuery.fn.uploaderbox = function(upmod, uniqueid){
	if ( uniqueid ) { var upmod = upmod + "&uid=" + uniqueid; }
	var uploader_cont = $(this);
	uploader_cont.html('<div class="uploaderInnerWrap"></div>');
	var uploader_obj = $("#"+$(this).attr("id")+" .uploaderInnerWrap");
	uploader_obj.css("background","url("+HTTP_SERVER+"/images/common/loading_l.gif) no-repeat center");
	jQuery.ajax({
		url: HTTP_SERVER+"/includes/uploader.php?mod="+upmod,
		success: function(rspnse){
			uploader_obj.css("background","none");
			uploader_obj.html(rspnse);
		}
	});
}

jQuery.fn.pagetitle = function(page_code){
	jQuery("#editPageTitle").show(200);
	jQuery("#editPageTitleBtn").click(function(event){
		jQuery("#page_title").empty();
		jQuery("#editPageTitle").hide(200);
		jQuery("#pageTitleFrmWrapper").show(200);
		jQuery.ajax({ url: "./includes/ajax/mod.page_title.php?"+page_code, success: function(rspnse){ jQuery("#pageTitleInp").val(rspnse); } });
		jQuery("#pageTitleFrm").ajaxform(function(){
				jQuery("#pageTitleFrmWrapper").hide();
				jQuery("#page_title").html('<img src="./images/common/loading_m.gif" />');
			},
			function(rspnse){
				jQuery("#page_title").html(rspnse);
				jQuery(this).pagetitle(page_code);
			},
			function(){
				jQuery("#pageTitleFrmWrapper").hide();
				jQuery("#page_title").html('<img src="./images/common/loading_m.gif" />');
				jQuery.ajax({
					url: "./includes/ajax/mod.page_title.php?"+page_code,
					success: function(rspnse){
						jQuery("#pageTitleFrmWrapper").hide(200);
						jQuery("#page_title").html(rspnse);
						jQuery("#editPageTitle").show(200);
					}
				});
			}
		);
		event.preventDefault();
	});
}

jQuery.preloadImages = function(){
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img />").attr("src", arguments[i]);
  }
}

jQuery.basename = function(path, suffix){
	var b = path.replace(/^.*[\/\\]/g, '');
	if (typeof(suffix) == 'string' && b.substr(b.length-suffix.length) == suffix) {
			b = b.substr(0, b.length-suffix.length);
	}
	return b;
}

jQuery.trim12 = function(str){
	var	str = str.replace(/^\s\s*/, ''),
		ws = /\s/,
		i = str.length;
	while (ws.test(str.charAt(--i)));
	return str.slice(0, i + 1);
}

jQuery.fn.taLimit = function(limit,counter_bx) {
  var txt_area = $(this);
  if(counter_bx) var rcvr = $("#"+counter_bx);
  var lines = txt_area.val().split('\n');
  var DoNotCount = lines.length-1;
  rcvr.text((limit-(txt_area.val().length-DoNotCount))+' left');
  $(this).keyup(function(){
    var lines = txt_area.val().split('\n');
    var DoNotCount = lines.length-1;
    if(txt_area.val().length-DoNotCount > limit) {
    	var climit = ( DoNotCount > 0 ) ? txt_area.val().length-DoNotCount : limit;
    	txt_area.val(txt_area.val().substr(0, climit));
    }
    rcvr.text(limit-(txt_area.val().length-DoNotCount)+' left');
    //if(limit - (txt_area.val().length-DoNotCount) == 0) rcvr.css("color","red");
  });
}

jQuery.fn.popwin = function(popurl,popwinwidth,popwinheight,appval){
	function rePosPopWin(){
		addTopSpc = $(window).scrollTop();
		addLeftSpc = $(window).scrollLeft();
		var popwintopspc = (($(window).height()-popwinheight)/2)+addTopSpc;
		var popwinleftspc = (($(window).width()-popwinwidth)/2)+addLeftSpc;
		$("#popWin").css({"top":popwintopspc+"px","left":popwinleftspc+"px"});
	}
	$(this).click(function(event){
		$("object").each(function(){ $(this).hide(); })
		var winheight = $(window).height();
		var winwidth = $(window).width();
		var popwintopspc = ((winheight-popwinheight)/2)+$(window).scrollTop();
		var popwinleftspc = (winwidth-popwinwidth)/2;
		var winst = $(window).scrollTop();
		var winsl = $(window).scrollLeft();
		var targeturl = ( appval === true ) ? popurl+$(this).val() : popurl;
		$("body").prepend('<div id="popWinBgOverlay"></div><div id="popWin"></div>');
		$("#popWinBgOverlay").css({"width":$(document).width()+"px","height":$(document).height()+"px","top":"0px"});
		$("#popWin").css({"width":popwinwidth+"px","height":popwinheight+"px","top":popwintopspc+"px","left":popwinleftspc+"px"});
		jQuery.ajax({ url:targeturl, success: function(rspnse){ $("#popWin").css("background-image","none").html(rspnse); $.iniHelpPop(); } });
		$("#popWinBgOverlay").click($.exitpopwin);
		$(window).resize(function(){ rePosPopWin(); });
		$(window).scroll(function(){ $(window).scrollTop(winst); $(window).scrollLeft(winsl); });
		event.preventDefault();
	});
}
jQuery.exitpopwin = function(){
		$("object").each(function(){ $(this).show(); })
		$(window).unbind("scroll");
		$("#popWinBgOverlay").remove();
		$("#popWin").remove();
}
jQuery.iniHelpPop = function(){
	var help_popups = $(".popHelp");
	$(".helpIcon").remove();
	help_popups.unbind("hover");
	for ( hp = 0; hp < help_popups.length; hp++ ){
		var help_bx_content = ( !$(help_popups[hp]).attr("id") ) ? '<img src="'+HTTP_SERVER+'/images/common/help_top.png" width="200" height="8" /><br /><div class="tWrap">'+$(help_popups[hp]).html()+'</div><img src="'+HTTP_SERVER+'/images/common/help_bottom.png" width="200" height="8" />' : $(help_popups[hp]).html();
		$(help_popups[hp]).attr("id", "helpPop"+hp).before('<a href="" id="helpPop'+hp+'Icon" class="helpIcon"><img src="'+HTTP_SERVER+'/images/common/help.gif" align="absmiddle" border="0" /></a>').html(help_bx_content);
	}
	$(".helpIcon").hover(
		function(){
			var obj_id = "#"+$(this).attr("id").replace('Icon','');
			var obj_pos = $(this).position();
			$(obj_id).css({"left":(obj_pos.left+24), "top":obj_pos.top}).fadeIn(300);
		},
		function(){
			var obj_id = "#"+$(this).attr("id").replace('Icon','');
			var obj_pos = $(this).position();
			$(obj_id).fadeOut(300);
		}
	).click(function(event){ event.preventDefault(); });
}
jQuery.helpPopWin = function(hpage){ w=window.open(HTTP_SERVER+'/help/popup/'+hpage+'.html','','scrollbars=yes,Width=490,Height=400'); w.focus(); return false; }
//
$(document).ready(function(){
	$.preloadImages(HTTP_SERVER+'/images/common/loading_l.gif',HTTP_SERVER+'/images/common/loading_m.gif',HTTP_SERVER+'/images/common/loading_s.gif');
	$.iniHelpPop();
});
