﻿$(document).ready(function(){
	if($('div.special_site_block div.xpress-post-header').eq(0).html() != '') {
		var n;
		var nn = $('div.special_site_block div.xpress-post-header').size();
		$title = $('div.special_site_block div.xpress-post-title h2');
		$entry = $('div.special_site_block div.xpress-post-entry');
		for(n = 0; n < nn; n++) {
			var title = $($title[n]).text();
			var link = $($title[n]).find('a').attr('href');
			var date = $($entry[n]).find('span.date').text();
			var text = $($entry[n]).find('p').text().substring(0,50);
			var img = $($entry[n]).find('img').attr('src');
			img = '<a href="'+link+'"><img src="'+img+'" width="100" height="100" alt="'+title+'" /></a>';
			text = text.replace(/ この投稿の.*/, "");
			$('div.special_site_block div.xpress-post-header').eq(n).html('');
			$('div.special_site_block div.xpress-post-entry').eq(n).html(img+'<h3><span>■</span><a href="'+link+'">'+title+'</a>'+newTopic(date, 10)+'</h3><p>'+text+'...（'+date+'）<a href="'+link+'" class="readmore">Read More</a></p>');
		}
	}
	if($('div.tricks_block div.xpress-post-header').eq(0).html() != '') {
		var n;
		var nn = $('div.tricks_block div.xpress-post-header').size();
		$title = $('div.tricks_block div.xpress-post-title h2');
		$entry = $('div.tricks_block div.xpress-post-entry');
		for(n = 0; n < nn; n++) {
			var link = $($title[n]).find('a').attr('href');
			var date = $($entry[n]).find('span.date').text();
			var text = $($entry[n]).find('p').text().substring(0,30);
			var title = $($title[n]).text();
			var img = $($entry[n]).find('img').attr('src');
			img = '<a href="'+link+'"><img src="'+img+'" width="100" height="100" alt="'+title+'" /></a>';
			text = text.replace(/ この投稿の.*$/, "");
			$('div.tricks_block div.xpress-post-header').eq(n).html('');
			$('div.tricks_block div.xpress-post-entry').eq(n).html(img+'<h3><span>■</span><a href="'+link+'">'+title+'</a>'+newTopic(date, 10)+'</h3><p>'+text+'...</p>');
		}
	}
	if($('div#products div.xpress-post-header h2').size() == 1 && location.href.indexOf('p=') > -1) {
		var products = $('div#products div.xpress-post-header h2').html();
		$('div#products div.xpress-post-header h2').html(products + '<a href="/products/" class="back"></a>');
	}
	if($('div#gallery div.xpress-post-header h2').size() == 1 && location.href.indexOf('p=') > -1) {
		var products = $('div#gallery div.xpress-post-header h2').html();
		$('div#gallery div.xpress-post-header h2').html(products + '<a href="/gallery/" class="back"></a>');
	}
	var special_site = $('div#special div.xpress-post-header h2').html();
	if(special_site != '') {
		$('div#special div.xpress-post-header h2').html(special_site + '<a href="/modules/special/" class="back"></a>');
	}
	var tricks = $('div#tricks div.xpress-post-header h2').html();
	if(tricks != '') {
		$('div#tricks div.xpress-post-header h2').html(tricks + '<a href="/modules/tricks/" class="back"></a>');
	}
	if ($('p.xpress_pings_status small').text().match('トラックバック / ピンバックは現在受け付けていません。')) {
		$('div#xpress-pings-block').hide();
	}
	$('div#tricks div.xpress-post-entry img').eq(0).hide();
	$('div#special div.xpress-post-entry img').eq(0).hide();
	var coment_please = $('div#respond h3').text() + '　　<span>コメントは管理人の承認の後に表示されます。表示には時間がかかる場合があります。</span>';
	$('div#respond h3').html(coment_please);
	var tb = $('p.xpress_pings_status small a').attr('href');
	if(tb != '') {
		$('p.xpress_pings_status small').html('ご自分のサイトからこのページにトラックバックを送れます。 [ '+tb+' ] ');
	}
	resize_div();
	return false;
});

$(function(){
	$("input#s").click(function(){
		$(this).val('');
		return false;
	});
	$("#contact_table input#check").click(function(){
		if($('input#contact_name').val() == '') {
			$('#name_check').html('&nbsp;←ご入力下さい。');
		} else if(!mail_address($('input#contact_mail').val())) {
			$('#name_check').html('');
			$('#mail_check').html('&nbsp;←正しくご入力下さい。');
		} else if($('textarea#contact_desc').val() == '') {
			$('#mail_check').html('');
			$('#desc_check').html('&nbsp;↓ご入力下さい。');
		} else {
			var input = document.createElement('input');
			input.setAttribute('type', 'hidden');
			input.setAttribute('name', 'func');
			input.setAttribute('value', 'check');
			this.form.appendChild(input);
			$(this.form).submit();
		}
	});
	$("#contact_table input#post").click(function(){
		var input = document.createElement('input');
		input.setAttribute('type', 'hidden');
		input.setAttribute('name', 'func');
		input.setAttribute('value', 'send');
		this.form.appendChild(input);
		$(this.form).submit();
	});
});

function resize_div() {
	var c = $('#ContentsBody').height();
	var r = $('#RightColumn').height();
	if (c < r - 120) {
		$('#ContentsBody').css('min-height' , (r-120)+'px');
	}
}

function newTopic(date, nt) {
	var today = new Date();
	var time = date.split("/");
	var c = new Date(time[0], time[1]-1, time[2], 0);
	if (today - c < 60 * 60 * 1000 * 24 * nt) { 
		return '<em>New!</em>';
	} else {
		return '';
	}
}

function mail_address(m) {
	if (!m.match(/^[A-Za-z0-9]+[\w-]+@[\w\.-]+\.\w{2,}$/)){
		return false;
	} else {
		return true;
	}
}

function qs(){
	var str = location.search;
	var dec = decodeURIComponent;
	var par = new Array, itm;
	if(typeof(str) == 'undefined') return par;
	if(str.indexOf('?', 0) > -1) str = str.split('?')[1];
	str = str.split('&');
	for(var i = 0; str.length > i; i++){
		itm = str[i].split("=");
		if(itm[0] != ''){
			par[itm[0]] = typeof(itm[1]) == 'undefined' ? true : dec(itm[1]);
		}
	}
	return par;
}
