﻿/*---------------------------------+

すきなばしょ
design by Isao Watanabe 
up date:2010/01/07
Copyright (c) 2010 Sukina Basho. All Rights Reserved.

+----------------------------------*/


var i = 1;
var cnt;


function setContentScroll(){
	var size = jQuery(window).height();
	var scrollArea = jQuery('div#contents');
	var elmHeight = scrollArea.height();
	var cw = jQuery('div#contentsWrapper').css('top');
	var wh = size - cw.match(/\w/);
	var elmHeight = scrollArea.height();
	scrollArea.css({'overflow' : 'hidden'});
	if(size < elmHeight){
		scrollArea.css({'height' : wh + 'px'});
		jQuery('div#scrollBar').css('display','block');
	}else if(size > elmHeight){
		scrollArea.css({'height' : wh + 'px'});
		jQuery('div#scrollBar').css('display','none');
	}
	scrollInit();
}

function scrollInit(){
	var ckSc = jQuery('body').attr('id');
	if(ckSc == 'top'){
			cnt = $('div.eLayout').length;
			jQuery('<div/>').attr('id','btnScrollUp').append(jQuery('<img/>').attr('src', path + '/images/common/btn_scroll_up.png').attr('alt','上へ移動').attr('title','上へ移動').attr('width','32').attr('height','34')).appendTo('body');
			jQuery('<div/>').attr('id','btnScrollDown').append(jQuery('<img/>').attr('src', path + '/images/common/btn_scroll_down.png').attr('alt','上へ移動').attr('title','下へ移動').attr('width','32').attr('height','34')).appendTo('body');
			jQuery('div#btnScrollUp').bind('click', function(){scrollLogic(-4);});
			jQuery('div#btnScrollDown').bind('click', function(){
				i++;
				scrollLogic(4);
				$('<div/>').load('/page/' + i + ' div#contentsList div.eLayout', function(){
					$(this).appendTo('div#contentsList');
				});
				/*
				要素が存在しない場合は
				jQuery("#id").size() == 0

				要素が存在する場合は
				jQuery("#id").size() > 0 
				*/
				$('div.hide div.navigation').empty();
				$('<div/>').load('/page/' + i + ' div.hide div.navigation a', function(){
					$(this).appendTo('div.hide div.navigation');
					if(jQuery('div.hide div.navigation a').size() > 0){
						var x = $('div.hide div.navigation a:first').attr('href');
						//alert(x);
					}else{
						return false;
					}
				});

				setEventLis(cnt);				
			});
			jQuery('div#contents').mousewheel(function(e,delta){
				i++;
				if(delta > 0){
					return false;
				}else if(delta < 0){
					$('<div/>').load('/page/' + i + ' div#contentsList div.eLayout', function(){
						$(this).appendTo('div#contentsList');
					});
					$('<div/>').load('/page/' + i + ' div.hide div.navigation', function(){
						$(this).appendTo('div.navigation');
					});
				}
			});
	}
	
	jQuery(document).bind('keydown', function(e){
		if(40 == e.keyCode){
			scrollLogic(4);
		}else if(38 == e.keyCode){
			scrollLogic(-4);
		}		
	});
	
	jQuery('div#contents').mousewheel(function(e,delta){
		if(delta > 0){
			scrollLogic(-5);
		}else if(delta < 0){
			scrollLogic(5);
			setEventLis(cnt);
		}
	});
	
	jQuery('div#scrollBar').bind('drag', scrollDrag);
	
}

function setEventLis(cnt){
	var totalEntryCnt = $('div.eLayout').length;
	var NoEventList = totalEntryCnt - cnt;
	for(var n=0; n<NoEventList; n++){
		var x = cnt + n
		$('div.eLayout').eq(x).attr('id', 'map' + x);
		$('#map' + x).bind('mouseover', function(){
			jQuery(this).addClass('choiceList');
			var setMap = new setGmap();
			setMap.ChengeMap(this);
		});
		
		jQuery('#map' + x).bind('mouseout', function(){
			jQuery(this).removeClass('choiceList');
		});
							
	}

}

function scrollLogic(m){ 
	var wh = jQuery('div#contents').height();
	var st = jQuery('div#contents').scrollTop();
	var sh = jQuery('div#contents').attr('scrollHeight') - wh + 100;
	
	var ss = st + m * 30;	
	if (ss > sh) {
		ss = sh;
	} else if (ss < 0) {
		ss = 0;
	}

	jQuery('div#contents').scrollTop(ss);
	jQuery('div#scrollBar').stop().animate({top:Math.round((wh - jQuery('div#scrollBar').height()) / sh * ss)},100);		
}

function scrollDrag(e){
		var y = e.offsetY;
		var wh = jQuery(window).height();
		var m = wh - jQuery(e.dragProxy).height() + 15;
		var sh = jQuery('div#contents').attr("scrollHeight") - wh;
		
		if (y > m) {
			y = m;
		} else if (y < 0) {
			y = 0;
		}
		
		jQuery(e.dragProxy).css({top:y});
		jQuery('div#contents').scrollTop(Math.round(sh / m * y));
}

