(function($) {
    $.fn.heightAdjust = function(row, child) {
        this.each(function(){
            var elems = $(child, this);
            var nelems = elems.length;
            var heights = new Array(nelems);
            var setHeight = function(){
                for (var i = 0; i < nelems; i++) {
                    elems[i].style.height = "";
                    heights[i] = $(elems[i]).height();
                }
                var max = 0;
                for (var i = 0; i < nelems; i++) {
                    if(0 == i % row){
                        max = Math.max.apply(Math, heights.slice(i, i + row));
                    }
                    heights[i] = max;
                }
                for (var i = 0; i < nelems; i++) {
                    elems[i].style.height = heights[i] + "px";
                }
            }
            setHeight();
            if($("#font-checker").length == 0){
                $("body").append('<div id="font-checker" style="position:absolute;left:-9999px;top:0;">&nbsp;</div>');
            }
            var baseSize = $("#font-checker").height();
            var checkSize = 0;
            setInterval(function() {
                checkSize = $("#font-checker").height();
                if(baseSize !== checkSize){
                    setHeight();
                    baseSize = checkSize;
                }
            }, 1000);
        });

    }
})(jQuery);

(function($) {
    $.fn.rollover = function() {
		var elems = this;
		var nelems = elems.length;
		var off = "_off";
		var on = "_on";
        var preLoadImg = new Object();
		for (var i = 0; i < nelems; i++) {
            preLoadImg[elems[i]] = new Image();
            preLoadImg[elems[i]].src  = elems[i].src.replace(off, on);
			elems[i].onmouseover = function(){
				 this.src = this.src.replace(off, on);
			}
			elems[i].onmouseout = function(){
				 this.src = this.src.replace(on, off);
			}
		}
    }
})(jQuery);


function twitterCallback2(twitters) {
    var statusHTML = [];
    for (var i=0; i<twitters.length; i++){
        if(twitters[i].user.scereen_name != ''){
            var username = twitters[i].user.screen_name;
            var status = twitters[i].text.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g, function(url) {
                return '<a href="'+url+'">'+url+'</a>';
            }).replace(/\B@([_a-z0-9]+)/ig, function(reply) {
                return  reply.charAt(0)+'<a href="http://twitter.com/'+reply.substring(1)+'">'+reply.substring(1)+'</a>';
            });
            statusHTML.push(status);
            break;
        }
    }
    if(statusHTML != ""){
        document.getElementById('twitter_update').innerHTML = statusHTML.join('');
    } else {
        document.getElementById('twitter_update').innerHTML = "読み込むことが出来ませんでした。";
    }
}

function twitterload() {
	var json = $.ajax({
		type: "GET",
		url: "/assets/cache/twitter.json",
		data: null,
		async: false
	}).responseText;
	eval(json);
}

function relative_time(time_value) {
    var values = time_value.split(" ");
    time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
    var parsed_date = Date.parse(time_value);
    var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
    var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
    delta = delta + (relative_to.getTimezoneOffset() * 60);

    if (delta < 60) {
        return 'less than a minute ago';
    } else if(delta < 120) {
        return 'about a minute ago';
    } else if(delta < (60*60)) {
        return (parseInt(delta / 60)).toString() + ' minutes ago';
    } else if(delta < (120*60)) {
        return 'about an hour ago';
    } else if(delta < (24*60*60)) {
        return 'about ' + (parseInt(delta / 3600)).toString() + ' hours ago';
    } else if(delta < (48*60*60)) {
        return '1 day ago';
    } else {
        return (parseInt(delta / 86400)).toString() + ' days ago';
    }
}

$(function() {
    $("a[target='_blank']", "#container").each(function(){
        if ($("img", this).length == 0) {
            $(this).append('<img src="/assets/shared/img/icon-window.png" alt="別ウインドウで開きます。" class="window" />');        
        }
    });

    $("img[src *= _off.]").rollover();
	$('ul.pullmenu').hide();
	$('img.menu_class').click(function () {
		$('ul.pullmenu').slideToggle('fast');
	});
	
	$("ul#nav ul.subnav").parent().append("<span></span>"); //Only shows drop down trigger when js is enabled (Adds empty span tag after ul.subnav*)  
	
	$("ul#nav li span").click(function() { //When trigger is clicked...  
		//Following events are applied to the subnav itself (moving subnav up and down)  
		$(this).parent().find("ul.subnav").slideDown('fast').show(); //Drop down the subnav on click 
		$(this).parent().hover(function() {  
		}, function(){  
		$(this).parent().find("ul.subnav").slideUp('fast'); //When the mouse hovers out of the subnav, move it back up  
	});
		//Following events are applied to the trigger (Hover events for the trigger)  
	}).hover(function() {  
		$(this).addClass("subhover"); //On hover over, add class "subhover"  
	}, function(){  //On Hover Out  
		$(this).removeClass("subhover"); //On hover out, remove class "subhover"  
	});
	
	$("#breadcrumb li").hover(function(){
        $(this).children('ul').stop();
        $(this).children('ul').css({display:'block', height:'auto', opacity:'0.5'});
        $(this).children('ul').animate({opacity:'1'},100);
        $(this).children('ul').fadeIn('fast');
	}, function(){
		$(this).children('ul').fadeOut(400);
	});

	$('ul.entry-meta li.category span').each(function(){
		var _category = $(this).text();
		if(_category == "執筆"){
			$(this).css("background-color", "#6c85ad");
		} else if (_category == "クリップ"){
			$(this).css("background-color", "#6cad98");
		} else if (_category == "講演"){
			$(this).css("background-color", "#a56cad");
		} else if (_category == "ネットイヤーグループ"){
			$(this).css("background-color", "#ad926c");
		}
	});

    $('div.hfeed div.hentry:last').addClass('last-entry');

    $('#tab #tab-01 h3').addClass("current");
    $('#tab #tab-02 table').hide();
    $('#tab h3').click(function(){
        $(this).addClass('current');
        $(this).parents("div").siblings("div").children("h3").removeClass('current');
        $(this).parents("div").siblings("div").children("table").hide();
        $(this).siblings("table").show();
    });

    $('ul.pagenav').each(function(){
        if($("li", this).length == 1){
            $(this).hide();
        }
    });

    $("a[rel='popup']").click(function(){
        window.open(this.href, 'popup', 'width=620, height=600, scrollbars=yes');
        return false;
    });

    $("ul.weare", "#professional").hover(function(){
        $(this).css("opacity", "0.5");
    }, function(){
        $(this).css("opacity", "1");
    });

    $("ul.weare", "#professional").click(function(){
        location.href = "http://" + location.hostname + "/service/professional/";
    });

	// IE fix
	if (jQuery.browser.msie) {
		if (jQuery.browser.version <= 8){
			$("#breadcrumb li:last-child").css({'background':'none', 'padding-right':'0'});
	  		$("#sitemap .section:last-child, #footwrap div#privacy li:last-child").css("margin-right","0");
	  		$("#tweets div.section:last-child").css("float","right");
	  		$("#sidebar .section ul.submenu li:first-child a").css("border-top","0");
            $(".callus h2:first-child").css("background", "none").css("padding-top", "0");;
	  	}
	}
	$("table.stripe tbody tr:nth-child(2n+2)").addClass("even");
	$("table.stripe tbody tr > *:first-child").addClass("first");
	$("table.stripe thead tr > *:first-child").addClass("first");
});

$.event.add(window, "load", function(){
    $("#ngc_togetter").html('<iframe name="togetter" id="frm_togetter" width="448" height="480" src="http://togetter.com/iframe/26815?bc=ffffff"></iframe>');
    $('ul.leader-list').heightAdjust(4, "li");
    $('div.logolist ul').heightAdjust(3, "li");
    $('div.layout-box').heightAdjust(2, "div.layout-content");
});



