// Javascript for Karacoro

//list border-bottom:none
jQuery.noConflict();
jQuery(document).ready(function(){

  	//list underline last-item.
	jQuery(".menu-reader li:last-child")
		.css("border-bottom", "none");
	jQuery("#top-contents #information dl dd:last-child span")
		.css("border-bottom", "none");
	jQuery(".menu-front li:last-child")
		.css("border-bottom", "none");
	jQuery(".menu-shopping li:last-child")
		.css("border-bottom", "none");
	jQuery(".menu-backnumber li:last-child")
		.css("border-bottom", "none");
	jQuery(".menu-facility li:last-child")
		.css("border-bottom", "none");
	jQuery(".menu-corporate li:last-child")
		.css("border-bottom", "none");
	jQuery("#content .page #form-area table tr:last-child")
		.css("border-bottom", "none");
	jQuery("#content .page #form-area table tr:last-child th")
		.css("border-bottom", "none");
	jQuery("#content .page #form-area table tr:last-child td")
		.css("border-bottom", "none");
	jQuery("#content .page #form-area table input[type=radio]")
		.css("border", "none");
	jQuery("#content div.category-kobore:last-child")
		.css("border-bottom", "none");
	jQuery("#content div.category-healthinfo .entry-content table tr td:first-child")
		.css({ fontSize:"0.85em", width:"80px"});

	//Custom Scrollbar
	if (jQuery("#mcs_container").size() > 0 ){
    	jQuery("#mcs_container").mCustomScrollbar("vertical",900,"easeOutCirc",1.05,"auto","yes");
	}
	if (jQuery("#mcs_container_2").size() > 0){
		jQuery("#mcs_container_2").mCustomScrollbar("vertical",900,"easeOutCirc",1.05,"auto","yes");
	}
	if (jQuery("#mcs_container_3").size() > 0){
		jQuery("#mcs_container_3").mCustomScrollbar("vertical",900,"easeOutCirc",1.05,"auto","yes");
	}
	
	//Twitter
	var tweeturl="https://twitter.com/status/user_timeline/karacorostaff.json?count=3&callback=?";
	jQuery.getJSON(tweeturl, function(data){
	   // JSONデータ読み込み
	   jQuery.each(data, function(i, item){
		  // URLにテキストがある場合はリンク設置
		  var txt = item.text
			 .replace(/(https?:\/\/[-a-z0-9._~:\/?#@!$&\'()*+,;=%]+)/ig,'<a href="$1">$1</a>')
			 .replace(/@+([_A-Za-z0-9-]+)/ig, '<a href="https://twitter.com/$1">@$1</a>')
			 .replace(/#+([_A-Za-z0-9-]+)/ig, '<a href="https://search.twitter.com/search?q=$1">#$1</a>');
		  // 日時編集
		  var update = formatDate(item.created_at);
		  jQuery("<li></li>")
			 .addClass(i%2 ? "even" : "odd")
			 .html(txt + " (" + update + ")")
			 .appendTo("#info-update");
	   });
	});
	
	//image align
	jQuery.fn.verticalMiddle = function(options){
		var op = jQuery.extend({
			visibility: 'visible',
			height    : 9999
		}, options || {});
	
		$(this).each(function(){
			var h = jQuery(this).height();
			if (h < op.height) {
				var p = (op.height - h)/2;
				jQuery(this).css({'position':'relative','top':p});
			}
			if (op.visibility == 'hidden') {
				jQuery(this).css('visibility','visible');
			}
		});
		
	};

  	//shopping image vartical align.
	jQuery('#itempage div.itemimg a img').verticalMiddle({height: 308});
	jQuery('#itempage div.itemsubimg a img').verticalMiddle({height: 138});
	jQuery('.sbox p.sbox-photo a img').verticalMiddle({height: 152});

	if (jQuery("div.itemsubimg img").size() == 0 ){
    	jQuery("div.itemsubimg").css("display", "none");
	}
	
	//jQuery Pagination Plugin.
	initPagination();
	
	//Google Maps
	if (!(jQuery("#address-text").size() == 0 )){

		var latlng = new google.maps.LatLng(35.689488,139.691706);
		var myOptions = {
		  zoom: 12,
		  center: latlng,
		  mapTypeId: google.maps.MapTypeId.ROADMAP
		}
		var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

		var geocoder = new google.maps.Geocoder();
		var addr = jQuery("#address-text").text();
		
		geocoder.geocode({
			address: addr
			},
			function(results, status) {
			    if (status == google.maps.GeocoderStatus.OK) {
			
				      // 結果の表示範囲。結果が１つとは限らないので、LatLngBoundsで用意。
				      var bounds = new google.maps.LatLngBounds();
				
				      for (var i in results) {
				        if (results[i].geometry) {
				
				          // 緯度経度を取得
				          var latlng = results[i].geometry.location;
				
				          // 住所を取得(日本の場合だけ「日本, 」を削除)
				          var address = results[0].formatted_address.replace(/^日本, /, '');
				
				          // 検索結果地が含まれるように範囲を拡大
				          bounds.extend(latlng);
				
						  // マーカー表示
						  var marker = new google.maps.Marker({
						      map: map,
						  	  position: latlng
						  });
						
						
					     }
				      }
					  
					  map.setZoom(14);
					  map.setCenter(latlng);
				
				
				}else{alert("地図の取得に失敗しました。geocoder.geocode.code=" + status);}
			});
			
			
	}

});


/*
### formatting strings of date ###
Sat Jan 22 14:12:52 +0000 2011 -> 2011.01.22
*/
function formatDate(str){
	var retstr = "";
	var week = { Jan:'01', Feb:'02', Mar:'03', Apr:'04', May:'05', Jun:'06', Jul:'07', Aug:'08', Sep:'09', Oct:'10', Nov:'11', Dec:'12' };
	var temp = str.split(" ");
	
	retstr = temp[5] + '.' + week[temp[1]] + "." + temp[2];
	
	return retstr;
}

//jQuery Pagination Plugin.
function pageselectCallback(page_index, jq){
	var new_content = jQuery('#post-pagination .page-box:eq('+page_index+')').clone();
	jQuery('#res').empty().append(new_content);
	return false;
}

function initPagination() {
	var num_entries = jQuery('#post-pagination .page-box').length;
	// Create pagination element
	jQuery("#Pagination").pagination(num_entries, {
		  num_edge_entries: 5,
		  num_display_entries: 1,
		  callback: pageselectCallback,
		  items_per_page:1,
		  current_page:1,
		  next_text:"&nbsp;",
		  prev_text:"&nbsp;"
	});
	jQuery('#Pagination .pagination a').click();
}

