$(function(){

/* Setting */
	$('.accordion .block').not('.view').viewContents('h2', 500);



/* Roll Over Image */
  var postfix = '_o';
  $('.rollover a img').not('[src*="'+ postfix +'."]').each(function() {
    var img = $(this);
    var src = img.attr('src');
    var src_on = src.substr(0, src.lastIndexOf('.'))
               + postfix
               + src.substring(src.lastIndexOf('.'));
    $('<img>').attr('src', src_on);
    img.hover(
      function() {
        img.attr('src', src_on);
      },
      function() {
        img.attr('src', src);
      }
    );
  });

/* Scroll Up */
  $('.scrollup a').click(function(){
    window.scrollTo(0,0);
    return false;
  });

/* Flat Heights */
  var sets = [], temp = [];
  $('div.flat-height-2 > div').each(function(i) {
    temp.push(this);
    if (i % 2 == 1) {
      sets.push(temp);
      temp = [];
    }
  });
  if (temp.length) sets.push(temp);
  $.each(sets, function() {
    $(this).flatHeights();
  });


/* Slide Show */
  var count=0;
  var num=$('.photos img').length;
  var ck=true;
  $('.photos').css('position', 'relative');
  $('.photos img').css('position', 'absolute');
  $('.photos img').click(function(){
    var ck=false;
    imageSwap();
  });
  var timer=setInterval(imageSwap, 3000);
  function imageSwap(){
    var ph1=num-1-count%num;
    var ph2=num-1-(count+1)%num;
    $('.photos img').eq(ph1).fadeOut('3000');
    $('.photos img').eq(ph2).fadeIn('3000');
    count++;
    if(count>num-2 && ck){
      clearInterval(timer);
    }
  }
  
/* Google Map */
	$.fn.gMap = function gMap(cLat,cLng,mLat,mLng,zm) {
		var mapdiv = $(this).get(0);
		var mapOptions = {
			zoom: zm,
			size: new google.maps.Size(600, 400),
			center: new google.maps.LatLng(cLat,cLng),
			mapTypeId: google.maps.MapTypeId.ROADMAP,
			mapTypeControl: false,
			scaleControl: true,
			navigationControl: true,
			scrollwheel: false
		};
		var map = new google.maps.Map(mapdiv, mapOptions);
		var markerOptions = {
			map: map,
			position: new google.maps.LatLng(mLat,mLng)
		};
		var marker = new google.maps.Marker(markerOptions);
	}

/* Unaccessed Link */
  $('ul.sitemap a[href^="#"]').css('color', '#B3B3B3');

});

$.fn.extend({

/* View Contents */
	viewContents: function(heading, speed){
		$(this).each(function(){
			$(this).children(heading).after('<a class="open"><img src="/img/bt-open.png" alt="本文を開く" /></a><a class="close"><img src="/img/bt-close.png" alt="本文を閉じる" /></a>')
			$(this).append('<a class="close button"><img src="/img/bt-close.png" alt="本文を閉じる" /></a>');
			$(this).css('margin','0');
			$('.close').css('display', 'none');
			var view_element=heading+', .open, .close';
			$(this).children().not(view_element).css('display', 'none');
			$('.open').click(function(){
				$(this).siblings().not(view_element).slideDown(speed);
				$(this).parent('div').find('.open').css('display', 'none');
				$(this).parent('div').find('.close').css('display', 'block');
				return false;
			});
			$('.close').click(function(){
				$(this).siblings().not(view_element).slideUp(speed);
				$(this).parent('div').find('.close').css('display', 'none');
				$(this).parent('div').find('.open').css('display', 'block');
				return false;
			});
		});
	}
	
});

