$(function(){
	$('a[rel*=confirm]').click(function(){
		if (confirm('削除してもよろしいですか？')) {
			location.href=$(this).attr('href');
		}
		return false;
	});

	$('.popup').each(function(){
		var distance = 10;
		var time = 250;
		var hideDelay = 500;
		var hideDelayTimer = null;
		var beingShown = false;
		var shown = false;
		var trigger = $('.trigger', this);
		var hint = $('.hint', this).css('opacity', 0);
		$([trigger.get(0), hint.get(0)]).focus(function () {
			if (hideDelayTimer) clearTimeout(hideDelayTimer);
				if (beingShown || shown) {
					return;
				}
				beingShown = true;
				hint.css({
					top: 0,
					left: $(this).attr('offsetWidth') + 15 + 'px',
					display: 'block'
				}).animate({
					left: '-=' + distance + 'px',
					opacity: 1
				}, time, 'swing', function() {
					beingShown = false;
					shown = true;
				});
				
				return false;
			}).blur(function () {
				if (hideDelayTimer) clearTimeout(hideDelayTimer);
					hideDelayTimer = setTimeout(function () {
						hideDelayTimer = null;
						hint.animate({
						left: '+=' + distance + 'px',
						opacity: 0
					}, time, 'swing', function () {
						shown = false;
						hint.css('display', 'none');
				});
			}, hideDelay);
			return false;
		});
	});
});

function toggleSelector(i) {
	$('#compare-box ul li').each(function(num){
		if (i == num) {
			$(this).addClass('active');
		} else {
			$(this).removeClass('active');
		}
	});
	
	 $('.timeline-selector').each(function(num){
	 	if (i == num) {
	 		$(this).css('display', 'block');
	 	} else {
	 		$(this).css('display', 'none');
	 	}
	 });
}

function getPageScroll() {
	var xScroll, yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) {
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;	
	}
	
	return new Array(xScroll,yScroll);
}

function getPageHeight() {
	var windowHeight;
	
	if (self.innerHeight) {
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) {
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) {
		windowHeight = document.body.clientHeight;
	}
		
	return windowHeight;
}

function notification(message) {
	var div = document.createElement('div');
		div.setAttribute('id', 'notification');
		div.setAttribute('style', 'display: none');
	
	document.body.appendChild(div);
	
	$('#notification').html(message);
	$('#notification').css('top', getPageScroll()[1]);
	$('#notification').css('left', $(window).width() / 2 - ($('#notification').width() / 2));
	$('#notification').fadeIn(500, function(){
		setTimeout(function(){
			$('#notification').fadeOut(300);
		}, 1000);
	});
}

function createIcon(image_url){
	icon = new GIcon();
	icon.image = image_url;
	icon.iconSize = new GSize(20, 34);
	icon.shadow = '/images/shadow50.png';
	icon.shadowSize = new GSize(37, 34);
	icon.iconAnchor = new GPoint(20, 34);
	icon.infoWindowAnchor = new GPoint(20, 5);
	return icon;
}

function createMarker(point, html, icon) {
	var marker = new GMarker(point, icon);
	GEvent.addListener(marker, 'click', function() {
		marker.openInfoWindowHtml(html);
		map.setCenter(marker.getLatLng());
	});
	return marker;
}

function loadGMap(lng, lat, zoom) {
	$('#map').css('display', 'block');
	var map = new GMap2(document.getElementById('map'));
		map.setCenter(new GLatLng(lat, lng), zoom);
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl(true));
	var marker = new GMarker(map.getCenter());
		map.addOverlay(marker);
	return map;
}

function loadEditableGMap(lng, lat){
	$('#map').css('display', 'block');
	var map = new GMap(document.getElementById('map'));
		map.centerAndZoom(new GPoint(lng, lat), 1);
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl(true));
		
	var icon = new GIcon();
		icon.image = "/images/cross.png";
		icon.iconSize = new GSize(50, 50);
		icon.iconAnchor = new GPoint(25, 25);
	
	var marker = new GMarker(map.getCenterLatLng(), icon);
		map.addOverlay(marker);
	
	GEvent.addListener(map, 'move', function(){
		var lng = map.getCenterLatLng().x;
		var lat = map.getCenterLatLng().y;
		map.clearOverlays();
		marker = new GMarker(map.getCenterLatLng(), icon);
		map.addOverlay(marker);
		$('#longitude').val(lng);
		$('#latitude').val(lat);
	});
}
