

$(document).ready(
	function(){
		$('.c').corner();
		$('.inner').corner({tl:{radius:6},tr:{radius:6},bl:{radius:6},br:{radius:6}});
		$('.c2').corner({tl:{radius:16},tr:false,bl:false,br:{radius:16},antiAlias:true,autoPad:true,validTags:["div"]});

		
		
		//メニューの表示
		$("#menu1").fadeIn(1000);
		
		$("#lsT").click(function(e){
			$("#shopmap").fadeOut(1000);
			$("#menu2").fadeOut(1000);
			$("#inf").hide();
			$("#news").fadeOut(1000);
			$("#camera").show();
		});

		$("#ls0").click(function(e){
			$("#shopmap").fadeOut(1000);
			$("#menu2").fadeOut(1000);
			$("#inf").hide();
			$("#news").fadeIn(1000);
			$("#camera").show();
		});

		//ショップの表示
		$("#ls1").click(function(e){
			$("#inf").hide();
			$("#news").hide();
			$("#shopmap").fadeOut(1000);
			$("#menu2").fadeIn(1000);
			$("#shop1").show();
			$("#shop2").show();
			$("#shop3").show();
			$("#shop4").show();
			$("#shop5").show();
			$("#shop6").show();
			$("#shop7").show();
			$("#shop8").show();
			$("#shop9").show();
			abd="<div id='logo'></div><div id='adress'></div><div id='tel'></div><div id='comment'></div>";
			$("#about").html(abd);
	    	abd="<div id='sl'><select id='ml' size='10'></select></div><div id='ar'><div id='upa'></div><div id='righta'></div><div id='downa'></div></div>";
			$("#mlist").html(abd);
			$("#mlist").hide();			
			$("#camera").hide();
				    
		});
		
		//レンタルサイクルの表示
		$("#ls2").click(function(e){
			$("#news").hide();
			$("#mlist").hide();
			$("#camera").show();
			$("#menu2").hide();
			$("#shopmap").hide();
			showRentalCycle();
		});
		
		//カスタムホイールの表示
		//サイクリングマップの表示
		$("#ls3").click(function(e){
			$("#news").hide();
			$("#inf").hide();
			$("#menu2").fadeOut(1000);
			$("#camera").hide();
			showCycleMap();
		});
		$("#ls4").click(function(e){
			window.open('./garally.html');
		});
		//linkの表示
		$("#ls5").click(function(e){
			$("#news").hide();
			$("#inf").hide();
			$("#camera").show();
			$("#shopmap").fadeOut(1000);
			$("#menu2").fadeOut(1000);
			$("#mlist").hide();
			$("#camera").show();
			showLink();
		});		
		//aboutの表示
		//mapの表示
		$("#shop1").click(function(){
				$("#menu1").fadeIn(1000);
				maplist={};
				setmap(1);
			}
		);
		$("#shop2").click(function(){
				$("#menu1").fadeIn(1000);
				setmap(2);
			}
		);
		$("#shop3").click(function(){
				$("#menu1").fadeIn(1000);
				setmap(3);
			}
		);
		$("#shop4").click(function(){
				$("#menu1").fadeIn(1000);
				setmap(4);
			}
		);
		$("#shop5").click(function(){
				$("#menu1").fadeIn(1000);
				setmap(5);
			}
		);
		$("#shop6").click(function(){
				$("#menu1").fadeIn(1000);
				setmap(6);
			}
		);
		$("#shop7").click(function(){
				$("#menu1").fadeIn(1000);
				setmap(7);
			}
		);
		$("#shop8").click(function(){
				$("#menu1").fadeIn(1000);
				setmap(8);
			}
		);
		$("#shop9").click(function(){
				$("#menu1").fadeIn(1000);
				setmap(9);
			}
		);

		//mapを隠す		
		$("#cl").click(function(){
				$("#shopmap").fadeOut(1000);
			}
		);
		
		$("#aright").live("click",(function(){
			photoIndex++;
			showadMap(photoIndex);
		}));

		$("#aleft").live("click",(function(){
			photoIndex--;
			showadMap(photoIndex);
		}));
		
		$("#slist").live("click",(function(){
			addList();
		}));

		$("#upa").live("click",(function(){
			upAr();
		}));
		$("#righta").live("click",(function(){
			rightAr();
		}));
		$("#downa").live("click",(function(){
			downAr();
		}));
		
	
		
	});
	
//観光写真のインデックス
var photoIndex=0;

//map インスタンス
var map;
var directionsDisplay = new google.maps.DirectionsRenderer();
var directionsService = new google.maps.DirectionsService();



//サイクルマップ用のリスト
var maplist={};

var longitudec;
var latitudec;
var idc;
var namec;


function nv(ob){
	if(ob){
		return ob.nodeValue;
	}else{
		return "";
	}
}


$(function() {
	$(".sPhoto").css("opacity","0.7");
	$(".sPhoto").hover(function () {
		$(this).stop().animate({
			opacity: 1.0
		}, "slow");
	}
	,
	function () {
		$(this).stop().animate({
			opacity: 0.7
			}, "slow");
	}
	);
}); 

function showRentalCycle(){
	$("#cn").html("");
	$("#inf").fadeIn(1000);
	
	$("#cn").css("background-image","url('./img/rental.png')"); 
}

function showLink(){
	$("#menu2").hide();
	$("#shopmap").hide();
	$("#inf").fadeIn(1000);
	$("#cn").css("background-image","" ); 
	var fr="";
	fr=fr+"<div class='lnk'><a href ='http://www.blimpautomotive.com/' target='_blnak'><img src='./img/link/blimp.png'></img></a>&nbsp;";
	fr=fr+"<a href ='http://www.keinocycles.com/' target='_blnak'><img src='./img/link/keino.png'></img></a>&nbsp;";
	fr=fr+"<a href ='http://www.cals-speedshop.com/' target='_blnak'><img src='./img/link/calslogo.png'></img></a></div>";

	fr=fr+"<div class='lnk'><a href ='http://www.corazon.ne.jp/electra/' target='_blnak'><img src='./img/link/electra.png'></img></a>&nbsp;";
	fr=fr+"<a href ='http://www.harfees.com/' target='_blnak'><img src='./img/link/harfees.png'></img></a>&nbsp;";
	fr=fr+"<a href ='http://d.hatena.ne.jp/mrgyazawa/' target='_blnak'><img src='./img/link/mrg.png'></img></a></div>";
	fr=fr+"<div class='lnk'><a href ='http://www.gee-mc.com/' target='_blnak'><img src='./img/link/gee.png'></img></a>&nbsp;";
	fr=fr+"<a href ='http://www.bicycleshop-rush.com/' target='_blnak'><img src='./img/link/rush_logo.gif'></img></a></div>";
	
	
	$("#cn").html(fr)
}

function showCycleMap(){
	$("#inf").hide();
	$("#shopmap").css("width", "800px" ); 
	$("#shopmap").css("margin-left", "-280px" ); 
	$("#map").css("width", "700px" ); 
	$("#cl").css("margin-left", "707px" ); 
	$("#iit").css("margin-left", "180px" ); 

	$("#logo").css("background-image","" ); 
	$("#tel").text(""); 
	$("#adress").text(""); 
	$("#comment").html(""); 
	$("#dist").text("");
	maplist={};
	$("#mlist").hide();
	    	abd="<div id='sl'><select id='ml' size='10'></select></div><div id='ar'><div id='upa'></div><div id='righta'></div><div id='downa'></div></div>";
	$("#mlist").html(abd);
	$("#shopmap").fadeIn(1000);

    photoIndex=0;
    showadMap(photoIndex);
}
function showadMap(index){
	jQuery.ajax({
		url : "./mod/getSightDat.php",
		data :"id="+index,
		type : "post",
		success : function(data){
			
			var bd = data.getElementsByTagName("bd");
			var bdc = nv(bd[0].firstChild);
			var longitude = data.getElementsByTagName("longitude");
			longitudec = nv(longitude[0].firstChild);
			var latitude = data.getElementsByTagName("latitude");
			latitudec = nv(latitude[0].firstChild);
			var name = data.getElementsByTagName("name");
			namec = nv(name[0].firstChild);
			var id = data.getElementsByTagName("id");
			idc = nv(id[0].firstChild);

			$("#about").html(bdc);
			
		    var mapdiv = document.getElementById('map');
		    var lg= new google.maps.LatLng(latitudec,longitudec);
		    
		    var myOptions = {
		        zoom: 13,
		        center:lg,
		        mapTypeId: google.maps.MapTypeId.ROADMAP,
		        sclistLongaleControl: true,
		        noClear:false
		    };			
			map = new google.maps.Map(mapdiv, myOptions);
			
		    var image = './img/ree_pin.png';
			var marker = new google.maps.Marker({
			    position: new google.maps.LatLng(latitudec, longitudec),
			    map: map, 
			    icon:image,
			    title: namec
			});
			
			showRoot();
			
    	}
	});
}


function setmap(index){

	var latitude=33.587601;
	var longitude=130.403785;
	var shopname="reeCycle";
	var logo="";
	var wi="";
	var hi="";
	var ad="";
	var tel="";

	var come="福岡は自転車が気持ち良い町です<br />皆様のお越しをお待ちしています";
	
	switch (index){
	case 1:	
		latitude=33.5914;
		longitude=130.379815;
		shopname="everyCycle";
		logo="everycycle.png";
		wi="152px";
		hi="44px";
		ad="福岡県福岡市中央区大手門3-9-12";
		come="ビーチクルザー エレクトラ専門のショップになります。<br/><a href='./evereecycle/index.html' target='_blank'><img src='./img/home.png'/>詳細</a>" ;
		tel="092-752-0003"; 
		break;
	case 2:
		latitude=33.585891;
		longitude=130.393284;
		shopname="コリコリサイクル";
		logo="korikori.png";
		wi="189px";
		hi="25px";
		ad="福岡市中央区大名1丁目2-28";
		tel="092-724-9911"; 
		come="<a href='http://twitter.com/@coricoricycle' target='_blank'><img src='./img/twitter-y.png'/>コリコリサイクルをフォローする</a><br/><a href='./coricori/' target='_blank'><img src='./img/home.png'/>詳細</a>" ;
		break;
	case 3:
		shopname="リサイクリング屋";
		latitude=33.582442;
		longitude=130.419991;
		logo="recyclingya.png";
		wi="196px";
		hi="26px";
		ad="福岡市博多区博多駅前4丁目32-15";
		come="福岡は自転車が気持ち良い町です<br />皆様のお越しをお待ちしています<br/><a href='./recyclingya/' target='_blank'><img src='./img/home.png'/>詳細</a>";
		tel="092-476-3221"; 
		break;

	case 4:
		latitude=33.587601;
		longitude=130.403785;
		shopname="reeCycle";
		logo="reecycle.png";
		wi="152px";
		hi="36px";
		ad="福岡市中央区渡辺通5丁目15-21";
		come="福岡は自転車が気持ち良い町です<br />皆様のお越しをお待ちしています<br/><a href='./ree/' target='_blank'><img src='./img/home.png'/>詳細</a>";
		tel="092-752-0120"; 
		break;
	case 5:
		latitude=33.586335;
		longitude=130.404367;
		shopname="reeCycle中古";
		logo="reecycletyuuko.png";
		wi="162px";
		hi="39px";
		come="中古自転車の販売しています。5800円からいろいろな自転車を取り揃えています。お気軽にお立ち寄りください。"
		ad="福岡市中央区渡辺通5丁目12-1";
		tel="092-752-0088"; 
		break;
	case 6:
		shopname="ナイス";
		latitude=33.573714;
		longitude=130.369225;
		logo="nice.png";
		wi="153px";
		hi="37px";
		ad="福岡市城南区別府2丁目10-20";
		come="皆様のお越しをお待ちしています<br/><a href='./nice/' target='_blank'><img src='./img/home.png'/>詳細</a>";

		tel="092-851-0190"; 
		break;
	case 7:
		shopname="くるくるさいくる";
		latitude=33.591889;
		longitude=130.398177;
		logo="kurukuru.png";
		wi="192px";
		hi="37px";
		come="福岡パルコ７Fでピストを中心に販売しています。<br/><a href='http://shopblog.parco-fukuoka.com/web/shop/shop6211/' target='_blank'><img src='./img/home.png'/>詳細</a>" ;	
		ad="福岡市中央区天神2丁目11-1";
		tel="092-235-7269"; 
		
		break;
	case 8:
		shopname="来来再来";
		latitude=33.587335;
		longitude=130.392166;
		logo="";
		wi="153px";
		hi="37px";
		ad="福岡市中央区大名";
		tel="092-851-0190"; 
		break;
	case 9:
		shopname="パーマネントサイクル";
		latitude=33.875761;
		longitude=130.881532;
		logo="parma.png";
		wi="190px";
		hi="119px";
		ad="北九州市小倉北区江南町3-29";
		tel="093-941-8868"; 
		break;
	};

	$("#shopmap").css("width", "500px" ); 
	$("#map").css("width", "400px" ); 
	$("#shopmap").css("margin-left", "-250px" ); 
	$("#iit").css("margin-left", "42px" ); 
	$("#cl").css("margin-left", "407px" ); 
	$("#dist").text("");
	$("#inf").hide();
	
	//ロゴ表示
	$("#logo").css("width", wi ); 
	$("#logo").css("height", hi ); 
	if (logo!=""){
		$("#logo").css("background-image","url(./img/"+ logo +")" ); 
		$("#logo").text("")
	}else{
		$("#logo").text(shopname); 
		$("#logo").css("background-image","" ); 
	}
	$("#tel").text("tel "+tel); 
	$("#adress").text(ad); 
	$("#comment").html(come); 
	//map初期化
    var mapdiv = document.getElementById('map');
    var lg= new google.maps.LatLng(latitude,longitude);
    var myOptions = {
        zoom: 16,
        center:lg,        
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        scaleControl: true,
        noClear:false
    };

	$("#shopmap").fadeIn(1000);
    map = new google.maps.Map(mapdiv, myOptions);
    var image = './img/ree_pin.png';
	var marker = new google.maps.Marker({
	    position: new google.maps.LatLng(latitude, longitude),
	    map: map, 
	    icon:image,
	    title: shopname
	});

	
	
}

function addList(){
	//同じものが存在する場合には処理しない
	if (typeof maplist["id_"+idc]!="undefined"){
		return;
	}
	//リスト配列に追加
	var listLong=0;
	if (typeof maplist["cnt"]!="undefined"){
		listLong=maplist["cnt"];
		if(listLong==7){
			alert("8個までしか登録できません");
			return;
		}
		listLong++;
	}
	maplist["cnt"]=listLong;
	maplist["latitude_"+listLong]=latitudec;
	maplist["longitude_"+listLong]=longitudec;
	maplist["id_"+idc]=listLong;
//select に追加
	$("#mlist").show();
	$('#ml').append($('<option>').attr({ value: listLong }).text(namec));
	showRoot();
}

function showMark(){
	var listLong=0;
	if (typeof maplist["cnt"]!="undefined"){
		listLong=maplist["cnt"];
	}
//マーク追加
    for (i = 0; i <= listLong ; i++){
    		if( maplist["id_"+idc]!=i){
				var marker = new google.maps.Marker({
			    	position: new google.maps.LatLng(maplist["latitude_"+i], maplist["longitude_"+i]),
			    	map: map
				});
			};
	}
}

function showRoot(){

	var listLong=0;
	if (typeof maplist["cnt"]!="undefined"){
		listLong=maplist["cnt"];
	}
	

	latitude=33.586335;
	longitude=130.404367;

	var start = new google.maps.LatLng(latitude, longitude);
	var end = new google.maps.LatLng(latitude, longitude);



	var waypts = [];
    for (i = 0; i <= listLong ; i++){
	    waypts.push({
	        location:new google.maps.LatLng(maplist["latitude_"+i], maplist["longitude_"+i]),
	        stopover:true
	    });
	}
 
	directionsDisplay.setMap(map);

	var request = {
	    origin: start, 
	    destination: end,
	    waypoints: waypts,
	    travelMode: google.maps.DirectionsTravelMode.WALKING 
	};
	directionsService.route(request,function(response, status) {
		if (status == google.maps.DirectionsStatus.OK) {
		    directionsDisplay.setDirections(response);
             var journey = 0;  
             var legs=response.routes[0].legs
             for (var i in legs)  
				{  
				    journey += legs[i].distance.value;  
				}  
             $("#dist").text((journey / 1000)+"km");  
		}
	});


}
function rightAr(){
	var sel = document.getElementById("ml");  
	ind=sel.selectedIndex;
	if (ind==-1){return;}
	if (maplist["cnt"]==0){
		delete maplist["cnt"];
	}else{
		maplist["cnt"]=maplist["cnt"]-1;
	}

	
	for (keyString in maplist) {
		if (keyString.substring(0,3)=="id_" && maplist[keyString]==ind ){
			delete maplist[keyString];
			break;
		}
	}	
	
	for (keyString in maplist) {
		if (keyString.substring(0,3)=="id_" && maplist[keyString]>ind ){
			maplist[keyString]=maplist[keyString]-1;
		}
	}
	for (i = ind; i < sel.length-1 ; i++){
		maplist["latitude_"+i]=maplist["latitude_"+(i+1)];
		maplist["longitude_"+i]=maplist["longitude_"+(i+1)];
	}

	delete maplist["latitude_"+(sel.length-1)];
	delete maplist["longitude_"+(sel.length-1)];

	$('#ml option:selected').remove();

	showRoot();
}

function upAr(){
	if( maplist.length<=1){return;}
	var sel = document.getElementById("ml");  
	if(sel.selectedIndex==0){return;}
	

	ind=sel.selectedIndex;
	if (ind==-1){return;}
   	dd=sel.options[ind].text;
   	sel.options[ind].text = sel.options[ind-1].text;
   	sel.options[ind-1].text = dd;

   	dv=sel.options[ind].value;
   	sel.options[ind].value = sel.options[ind-1].value;
   	sel.options[ind-1].value = dv;

   	dd=maplist["latitude_"+ind];
   	maplist["latitude_"+ind]=maplist["latitude_"+(ind-1)];
   	maplist["latitude_"+(ind-1)]=dd;
   	
   	dd=maplist["longitude_"+ind];
   	maplist["longitude_"+ind]=maplist["longitude_"+(ind-1)];
   	maplist["longitude_"+(ind-1)]=dd;
	
	
	for (keyString in maplist) {
		if (keyString.substring(0,3)=="id_" && maplist[keyString]==ind ){
			k1=keyString;
			break;
		}
	}


	for (keyString in maplist) {
		if (keyString.substring(0,3)=="id_" && maplist[keyString]==ind-1 ){
			k2=keyString;
			break;
		}
	}
	dd=maplist[k1];
	maplist[k2]=maplist[k1];
	maplist[k1]=dd;
	
	$("#ml").val(dv);

	
	showRoot();
}
function downAr(){
	if( maplist.length<=1){return;}
	var sel = document.getElementById("ml");  
	
	if(sel.selectedIndex==(maplist.length-1)){return;}
	ind=sel.selectedIndex;
	if (ind==-1){return;}
	
   	dd=sel.options[ind+1].text;
   	sel.options[ind+1].text =sel.options[ind].text ;
   	sel.options[ind].text = dd;

   	dv=sel.options[ind].value;
   	sel.options[ind].value=sel.options[ind+1].value ;
   	sel.options[ind+1].value = dv;

   	dd=maplist["latitude_"+(ind+1)];
   	maplist["latitude_"+(ind+1)]=maplist["latitude_"+ind];
   	maplist["latitude_"+ind]=dd;
   	
   	dd=maplist["longitude_"+(ind+1)];
   	maplist["longitude_"+(ind+1)]=maplist["longitude_"+ind];
   	maplist["longitude_"+ind]=dd;
	
	
	for (keyString in maplist) {
		if (keyString.substring(0,3)=="id_" && maplist[keyString]==ind ){
			k2=keyString;
			break;
		}
	}


	for (keyString in maplist) {
		if (keyString.substring(0,3)=="id_" && maplist[keyString]==ind+1 ){
			k1=keyString;
			break;
		}
	}
	dd=maplist[k1];
	maplist[k2]=maplist[k1];
	maplist[k1]=dd;
	$("#ml").val(dv);
	showRoot();

}
