$(document).ready(function(){
	activateRollOver();
});


/*
* 特定の接尾語が付いたの画像にロールオーバーアクションをつける。
*/
function activateRollOver(){
	$('img[src*="_up\."]').bind('mouseover', function(){
			$(this).attr("src", function(){ 
					return $(this).attr('src').replace("_up\.", "_ov\.");
			});
	});
	
	$('img[src*="_up\."]').bind('mouseout', function(){
			$(this).attr("src", function(){ 
					return $(this).attr('src').replace("_ov\.", "_up\.");
			});
	});
}

/*
* SHOP一覧の網掛けロールオーバーアクション
*/
function activateShopListRollover() {
	$("div.shop").bind("mouseover", function(){
		$(this).addClass("shop_hover");
	});

	$("div.shop").bind("mouseout", function(){
		$(this).removeClass("shop_hover");
	});
	
	$("div.shop").bind("click", function(){
		var href = $("dl dt a", this).eq(0).attr("href");
		 location.href = href || "#";
	});
}


/*
* 記事リストのベースライン調整
*/
function fixArticleExcerptBaseline() {

	$("div.article>p.excerpt, div.main-article>p.excerpt").each(function(index, element){
		var offset = $(this).offset();
		var lineHeight = 22;
		var glidVOffset = 8;
		
		var mod = 22 - offset.top%lineHeight;
		
		//ベースラインがGridに会うように調整
		$(this).css("marginTop", parseInt($(this).css("marginTop"))+mod-glidVOffset);
		
	});
}

/*
* TOPページの日付
*/

function displayDateStamp() {
	
	var date = new Date();
	
	var m = date.getMonth()+1;
	var d = date.getDate();
	var day = getJapaneseDayFormat(date.getDay());
	
	var html = '<div class="date-stamp"><p><span class="month">'+ m +'<span class="small">月</span></span><span class="date">'+d+'<span class="small">日</span></span><span class="day">'+day+'</span></p></div>'
	
	$("#header").append(html);
	
}

function getJapaneseDayFormat(num) {
	var formats = ['日曜日', '月曜日', '火曜日', '水曜日', '木曜日', '金曜日', '土曜日'];
	return formats[num];
}




/*
* Twitter
*/

var twitter = {
	
	_json: null,
	
	_addLink:  function(text){
	
		var url = "((https?)(:\/\/[-_.!~*\'()a-zA-Z0-9;\/?:\@&=+\$,%#]+))";
		var urlRe = new RegExp(url, "igm");
			
		text = text.replace(urlRe, '<a href="$1" target="_blank">$1</a>');

		var hashRe = /#([A-Za-z0-9_]+)/igm;
		text = text.replace(hashRe, '<a href="http://twitter.com/search?q=%23$1" target="_blank">#$1</a>');
	
		var userRe = /@([A-Za-z0-9_]+)/igm;
		text = text.replace(userRe, '@<a href="http://twitter.com/$1" target="_blank">$1</a>');
	
		return text;
	},
	
	draw: function(elements){
		
		if(!elements || !this._json[0]){ return; }
		
		var result = this._json[0];
		//IEでエラーが出るためタイムゾーンオフセットを取り外す。
		var dateString = String(result.created_at).replace(/(\+\d*\s)/, "");
		var date = new Date(dateString);
		//手動でタイムゾーンオフセット追加。
		date.setHours(date.getHours() + 9);
		var text = result.text;

		var year = date.getFullYear();
		var month = date.getMonth() + 1;
		month = (Number(month) < 10) ? "0"+month : month;
		var day = date.getDate();
		day = (Number(day) < 10) ? "0"+day : day;
		var hours = date.getHours();
		hours = hours < 10 ? "0"+hours : hours;
		var minutes = date.getMinutes();
		minutes = minutes < 10 ? "0"+minutes : minutes;
		
		var html= "<div class='twitter-header'><p class='date'>"+year+"/"+month+"/"+day+"<br />"+hours+":"+minutes+"</p></div><div class='twitter-body'><p>"+ this._addLink(text) +"</p></div><div class='twitter-footer'></div>"
		
		elements.each(function(index, element){						  
			$(this).html(html);
			$("h2 a", $(this).parent()).attr({
				href: 'http://twitter.com/hillsmarche',
				target: '_blank'
			});
			$("h2", $(this).parent()).append('<br /><a href="http://twitter.com/hillsmarche" target="_blank"><img src="common/images/logo_twitter.gif" /></a>');
			$(".read-more", $(this).parent()).remove();
		});
		
	},
	
	jsonpCallBack: function(json) {

		if(json.error) { return; }
		
		this._json = json;
		
	}
	
}

/* Youtube 埋め込み*/
function embedSmallYoutubeVideo(v) {
	
	var vid = v;
	
	document.write('<div id="youtube_embed_marker"></div>');
	
	var html = '<object width="152" height="114"><param name="movie" value="http://www.youtube.com/v/'+vid+'&hl=ja_JP&fs=1&rel=0"></param><param name="allowFullScreen" value="false"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/'+vid+'&hl=ja_JP&fs=1&rel=0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="false" width="152" height="114"></embed></object>'
	
	 var block = $('#youtube_embed_marker').closest(".article");
	 block.append(html);
	 
	 $('#youtube_embed_marker,p.excerpt', block).remove();
	
	
}





