$(function() {
// グリッド------------------------------
	var hsort_flg = false;

	//setup
	vg = $("#grids").vgrid({
		easeing: "easeOutQuint",
		time: 400,
		delay: 20,
		fadeIn: {
			time: 0,
			delay: 0
		}
	});

/*
	//setup
	vg = $("#grids").vgrid({
		easeing: "easeOutQuint",
		time: 400,
		delay: 20,
		fadeIn: {
			time: 400,
			delay: 20
		}
	});
*/

	$("#grids").ajaxComplete(function(){
		vg.vgrefresh();
	});

	$(".block-thumb").hover( function() {
		//$("#grids").vgrid({ easeing: "easeOutQuint", time: 400,delay: 20, fadeIn: { time: 0, delay: 0 }});
	});

// マウスオーバー処理------------------------------
	$('.block-link span a').click(function () {
		return false;
	});
	
	$('.block-link > div').hover(function() {
		var src =$(this).parent().find(".block-link-btn").find("img");
		src.attr("src",src.attr("src").replace(/^(.+)(\.[a-z]+)$/, "$1_on$2"));
	}, function () {
		var src =$(this).parent().find(".block-link-btn").find("img");
		src.attr("src",src.attr("src").replace(/^(.+)_on(\.[a-z]+)$/, "$1$2"));
		//$(this).fadeTo(500,1);
	});
	
	$('.block-link').click(function() {
		var src =$(this).find(".block-link-btn").find("img");
		src.attr("src",src.attr("src").replace(/^(.+)_on(\.[a-z]+)$/, "$1$2"));
	});

// ------------------------------

	Shadowbox.setup("a[href$='.jpg']");
	Shadowbox.setup("#jitensya");

	//$("#post-idea").fadeTo("slow");
	
});

$(window).load(function () {
  $("#grids").vgrid({ easeing: "easeOutQuint", time: 400,delay: 20, fadeIn: { time: 0, delay: 0 }});
});


var block_1 = 160;
var block_2 = 324;
var block_3 = 488;
var block_4 = 652;
var block_5 = 816;

function ajax_load(category,id,url,block) {
	
	// ajax.load重複を回避
	var winWidth = 0;
	var winHeight = 0;
	winWidth = $(window).width();
	winHeight = $(window).height();
	$("#stop").width(winWidth).height(3000).css("display", "block");

	
	if ( block == "block1x1") {
		var width = block_1;
		var height = block_1;
	}

	if ( block == "block1x2") {
		var width = block_1;
		var height = block_2;
	}

	if ( block == "block2x1") {
		var width = block_2;
		var height = block_1;
	}
	
	$(".block-load").empty().parent().width(width).height(height);
	$("#grids").vgrid({ easeing: "easeOutQuint", time: 400,delay: 20, fadeIn: { time: 0, delay: 0 }});
	$(".block-thumb").css("display","block");
	
	var selecter = "#" + id;
	var selecter = $(selecter);
	//var load = url +"&load=1" + " #load";
	var load = url +" #load";
	
	$(".block-idea").fadeTo(500,0.8);
	selecter.fadeTo(500,1);
	
	selecter.children(".block-link-btn").hide();
	selecter.children(".block-thumb").hide();
	selecter.children(".block-load").load(load, function() {
		$(this).parent().width($(this).children().innerWidth());
		$(this).parent().height($(this).children().innerHeight());

		$(this).css("display","none").hide(function() {
		
		//$("#post-comment").charCount({
		//		allowed: 300,
			//	warning: 250,
			//	counterText: '入力可能文字数：'	
		//});
			
			$(this).fadeIn("slow",function() {
				//var offset = 0;
				var offset = $(this).delay(800).parent().offset();
				//$(this).children().append(offset.top);
				$(this).children().append("<a class=block-close onclick=ajax_close('"+id+"','"+block+"')><img src=/wp-content/themes/x2tokyo_portal/assets/images/btn-close.gif width=16 height=16 alt=x /></a>");
					$('html,body').animate({scrollTop: offset.top}, 600);
					$("#stop").css("display", "none");
					bubbleInfo();
					//bubbleinfo();
					$("#grids").vgrid({ easeing: "easeOutQuint", time: 400,delay: 20, fadeIn: { time: 0, delay: 0 }});
			});
		});
	});
	return false;
}

function ajax_close(id,block) {
	var selecter = "#" + id;
	selecter = $(selecter);

	if ( block == "block1x1") {
		var width = block_1;
		var height = block_1;
	}
	
	if ( block == "block1x2") {
		var width = block_1;
		var height = block_2;
	}

	if ( block == "block2x1") {
		var width = block_2;
		var height = block_1;
	}

	selecter.children(".block-load").empty().parent().width(width).height(height);
	$("#grids").vgrid({ easeing: "easeOutQuint", time: 400,delay: 20, fadeIn: { time: 0, delay: 0 }});
	
	selecter.children(".block-thumb").show();
	$(".block-idea").fadeTo(500,0.5).fadeTo(500,1);
	$(".block-link-btn").fadeTo(2000,1);

	var src =selecter.find(".block-link-btn").find("img");
	src.attr("src",src.attr("src").replace(/^(.+)_on(\.[a-z]+)$/, "$1$2"));

/*
	var offset = selecter.offset();
	selecter.append(offset.top);
	$('html,body').animate({scrollTop: offset.top}, 600);
*/
	vg.vgrefresh();
	return false;
}

//window open ------------------------------
function winopen(url,target) {
	var win = window.open(url,target);
	if (target == "new") {
		win.focus();
	}	
}

//select link ------------------------------
function link_select(url){
  if(url.options[url.selectedIndex].value){
    location.href = url.options[url.selectedIndex].value;
  }
}

//tweet tool tip ------------------------------
function bubbleInfo() {
//$(function () {
    $('.bubbleInfo').each(function () {
        var distance = 10;
        var time = 250;
        var hideDelay = 500;

        var hideDelayTimer = null;

        var beingShown = false;
        var shown = false;
        var trigger = $('.trigger', this);
        var info = $('.popup', this).css('opacity', 0);


        $([trigger.get(0), info.get(0)]).mouseover(function () {
            if (hideDelayTimer) clearTimeout(hideDelayTimer);
            if (beingShown || shown) {
                // don't trigger the animation again
                return;
            } else {
                // reset position of info box
                beingShown = true;

                info.css({
                    top: -100,
                    left: -70,
                    display: 'block'
                }).animate({
                    top: '-=' + distance + 'px',
                    opacity: 0.8
                }, time, 'swing', function() {
                    beingShown = false;
                    shown = true;
                });
            }

            return false;
        }).mouseout(function () {
            if (hideDelayTimer) clearTimeout(hideDelayTimer);
            hideDelayTimer = setTimeout(function () {
                hideDelayTimer = null;
                info.animate({
                    top: '-=' + distance + 'px',
                    opacity: 0
                }, time, 'swing', function () {
                    shown = false;
                    info.css('display', 'none');
                });

            }, hideDelay);

            return false;
        });
    });
//});
}
