  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-3098678-1']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

var direct_active_flag=0;
var force_shadow_flag=0;

$(document).ready(function(){
	$("#glayLayer").click(function(){
		if( force_shadow_flag==0 ){
			$(this).hide()
			$("#overLayer").hide()
			$("#listLayer").hide()
		}
	})
	if($.browser.msie && $.browser.version<7){
		$(window).scroll(function(){
			$("#glayLayer").css('top',$(document).scrollTop())
			$("#overLayer").css('top',($(document).scrollTop()+$(window).height()/2) +"px")
			$("#listLayer").css('top',($(document).scrollTop()+$(window).height()/2) +"px")
		})
	}
	resize_footer();

	$("#side_l").hover(function(){
		remove_text();
	});

	$("#side_r").hover(function(){
		remove_text();
	});

	$("#direct_mes").click(function(){
		if( direct_active_flag==0 ){
			active_direct();
		}
	});
});

$( window ).resize(function(){
	resize_footer();
});

function resize_footer(){
	var height = $(window).height();
	var h = 620;
	if( height<580 ){
		h = height-40;
	}
	$("#listLayer").css("height",h);

	var top  = Math.floor(($(window).height() - h) / 2);
	var left = Math.floor(($(window).width() - 560) / 2);
	$("#listLayer").css("top",top);
	$("#listLayer").css("left",left);

	var top  = Math.floor(($(window).height() - 160) / 2);
	var left = Math.floor(($(window).width() - 300) / 2);
	$("#loading").css("top",top-50);
	$("#loading").css("left",left);
}


function cat_open( id ){
	var flag = true;
	if( $("#cat_g"+id).css("display")!="none" ){
		flag = false;
	}
	$("ul.cat").hide();
	if( flag ){
		$("#cat_g"+id).show();
	}
}

function show_info( id ){
	var flag = true;
	if( $("#info"+id).css("display")!="none" ){
		flag = false;
	}
	$("div.info_body").hide();
	if( flag ){
		$("#info"+id).show();
	}
}

function show_news( id ){
	var flag = true;
	if( $("#news"+id).css("display")!="none" ){
		flag = false;
	}
	if( flag ){
		$("#news"+id).show();
	} else {
		$("#news"+id).hide();
	}
}

function show_side_news( id ){
	var flag = true;
	if( $("#side_news"+id).css("display")!="none" ){
		flag = false;
	}
	if( flag ){
		$("#side_news"+id).show();
	} else {
		$("#side_news"+id).hide();
	}
}

function download( key ){
	$("#output_type").val( key );
	$("#download_form").submit();
}

function go_download(){
	$("#download_start").hide();
	$("#download_end").show();
}

function download_old(){
	document.getElementById("download_form").submit();
}


function item_pinger(){
	var date = new Date();
	var item_id = "0";

	var anc = location.hash;
	if( anc.substring(0,2)=="#i" ){
		item_id = anc.substring(2);
	}
	if( anc.substring(0,2)=="#d" ){
		item_id = anc.substring(2);
	}
	if( item_id=="0" ){
		return false;
	}

	var params = "mod=page&act=pinger&item_id="+item_id+"&t="+date.getTime();

	$.ajax({
			type: "POST",
			url: "index.php",
			data: params,
			success: function(){
				//alert( "success" );
			}
		});
}

function update_tweet(){
	var date = new Date();
	var params = "mod=util&act=tweet&t="+date.getTime();

	$.ajax({
			type: "POST",
			url: "index.php",
			data: params,
			success: function(){

			}
		});
}

function open_zoom( url , name ){
	$("#zoom_image").attr("src",url);
	$("#zoom_name").html( name );
	$("#glayLayer").show();
	$("#listLayer").hide();

	$("#zoom_image").bind('load', function() {
		$("#overLayer").show();
		var left = Math.floor(($(window).width() - $("#zoom_image").width()-60) / 2);
		var top  = Math.floor(($(window).height() - $("#zoom_image").height()-200) / 2);
		$("#overLayer")
			.css({
				"top": top,  
				"left": left,  
				"opacity": 0  
			})
			.animate({opacity: "1"},{queue: true, duration: 500, easing: "linear", complete: function(){}});
	});
}

function close_zoom(){
	$("#glayLayer").hide();
	$("#overLayer").hide();
}

function open_book_owner(){
	if( $("#search_owner_list").css("display")=="none" ){
		$("#search_owner_list").show();
		$("#open_bo_caption").html("作家一覧を閉じる");
	} else {
		$("#search_owner_list").hide();
		$("#open_bo_caption").html("作家一覧から選ぶ");
	}
}

function update_bo_names(){
	var checks=[];
	var tmp = "";
	$("[name='search_owner_id[]']:checked").each(function(){
		checks.push(this.value);
		tmp += "「"+$("#owner_name"+this.value).val()+"」";
		tmp += "　";
	});
	if( tmp=="" ){
		tmp = "全て";
	}
	$("#bo_names").html(tmp);
	
}

function open_book_notice( item_id ){
	if( $("#notice"+item_id).css("display")=="none" ){
		$("#notice"+item_id).show();
	} else {
		$("#notice"+item_id).hide();
	}
}

function see_text( id ){
	$("div.recommend_box div.text").hide();
	$("div.recommend_box div.date").hide();
	$("div.recommend_box div.img img").width(50).height(50).css("top","0px").css("left","0px");
	$("#text"+id).show();
	$("#date"+id).show();
	$("#img"+id).width(100).height(100).css("top","-50px").css("left","-25px");
}

function remove_text(){
	$("div.recommend_box div.text").hide();
	$("div.recommend_box div.date").hide();
	$("div.recommend_box div.img img").width(50).height(50).css("top","0px").css("left","0px");
}

function open_cat_list( cat_id , owner_id ){
	$("#glayLayer").show();
	$("#overLayer").hide();

	var params = "mod=item&act=catlist&owner_id="+owner_id;
	$.ajax({
			type: "GET",
			url: "index.php",
			data: params,
			success: function(res){
				$("#listLayer").show();
				$("#listLayer").html(res);
				$("#listLayer")
					.css({
						opacity: 0  
					})
					.animate({opacity: "1"},{queue: true, duration: 500, easing: "linear", complete: function(){}});

			}
		});
}

function open_owner_list( cat_id , owner_id ){
	$("#glayLayer").show();
	$("#overLayer").hide();

	var params = "mod=item&act=ownerlist&cat_id="+cat_id;
	$.ajax({
			type: "GET",
			url: "index.php",
			data: params,
			success: function(res){
				$("#listLayer").show();
				$("#listLayer").html(res);
				$("#listLayer")
					.css({
						opacity: 0  
					})
					.animate({opacity: "1"},{queue: true, duration: 500, easing: "linear", complete: function(){}});

			}
		});
}

function open_donate_cat_list( cat_id , owner_id ){
	$("#glayLayer").show();
	$("#overLayer").hide();

	var params = "mod=donate&act=catlist&owner_id="+owner_id;
	$.ajax({
			type: "GET",
			url: "index.php",
			data: params,
			success: function(res){
				$("#listLayer").show();
				$("#listLayer").html(res);
				$("#listLayer")
					.css({
						opacity: 0  
					})
					.animate({opacity: "1"},{queue: true, duration: 500, easing: "linear", complete: function(){}});

			}
		});
}

function open_donate_owner_list( cat_id , owner_id ){
	$("#glayLayer").show();
	$("#overLayer").hide();

	var params = "mod=donate&act=ownerlist&cat_id="+cat_id;
	$.ajax({
			type: "GET",
			url: "index.php",
			data: params,
			success: function(res){
				$("#listLayer").show();
				$("#listLayer").html(res);
				$("#listLayer")
					.css({
						opacity: 0  
					})
					.animate({opacity: "1"},{queue: true, duration: 500, easing: "linear", complete: function(){}});

			}
		});
}

function send_direct(){
	if(direct_active_flag==0){
		return;
	}
	var mes = $("#direct_mes").val();
	if( mes=="" ){
		return;
	}

	$("#direct_on").hide();
	$("#direct_loading").show();

	$.ajax({
			type: "POST",
			url: "index.php",
			data: {
				"mod": "main",
				"act": "direct",
				"mes": mes
			},
			success: function(res){
				$("#direct_loading").hide();
				$("#direct_off").show();
			}
		});
}

function active_direct(){
	direct_active_flag=1;
	$("#direct_mes").val("").css("color","black").css("font-size","12px");
}

function open_sample( book_id ){
	window.open("index.php?mod=book&act=samplepdf&book_id="+book_id,"sample"+book_id,"width=390,height=300,menubar=no,toolbar=no,location=no,status=no,directories=no,scrollbars=no");
}

function call_sample_pdf( id ){
	var dt = new Date();
	$.ajax({
			type: "GET",
			url: "index.php",
			data: {
				"mod": "book",
				"act": "samplepdf",
				"book_id": id,
				"t": dt.getTime()
			},
			success: function(){
				$("#pdf_waiting_status").hide();
				$("#pdf_complete_status").show();
			}
		});
}

function toggle_layer( id ){
	$("#"+id).toggle();
}

function send_payment( order_id , conv_store_id , cost ){
	force_shadow_flag = 1;
	$("#glayLayer").show();
	$("#loading").children("p").html("決済処理中です<br/>そのまましばらくお待ち下さい");
	$("#loading").show();

	$.ajax({
			type: "POST",
			url: "index.php",
			data: {
				"mod":"mypage",
				"act":"conv",
				"id":order_id,
				"conv_store_id":conv_store_id,
				"cost":cost
			},
			success: function(res){
				if( res=="OK" ){
					$("#glayLayer").hide();
					$("#loading").hide();
					$("#before_page").hide();
					$("#after_page").show();
				} else {
					$("#loading").children("p").html("決済処理が失敗しました。<br/><a href='index.php?mod=mypage&act=payment&id="+order_id+"'>[もう一度試す]</a>");
				}
			}
		});
}

function update_item_list(){
	v = "";
	for( var i=1 ; i<4 ; i++ ){
		if($("#status_check"+i).attr('checked')){
			v = v + "1";
		} else {
			v = v + "0";
		}
	}
	$("#status_checks").val(v);
	$("#update_form").submit();
}

function send_paypal(){
	$("#glayLayer").show();
	$("#loading").children("p").html("PayPalのサイトに移動中です<br/>そのまましばらくお待ち下さい");
	$("#loading").show();
	$('#paypal_form').submit();
}

