var souluvilleMain;
$(document).ready(function(){
	//背景設定
	$("img.bgmaximage").maxImage({isBackground: true,overflow: 'auto'});
	//
	souluvilleMain = soulvilleInit();
	$.address.init(addressInit).change(addressChange);
});
function addressInit(){}
function addressChange(event) {
	var names = $.map(event.pathNames,function(n) { return n.substr(0, 1).toUpperCase() + n.substr(1); }).concat(event.parameters.id ? event.parameters.id.split('.') : []);
	var links = names.slice();
	switch(links[0]){
		case "Top":
		souluvilleMain.changeContent("top");
		break;
		case "Concept":
		souluvilleMain.changeContent("concept");
		break;
		case "Collection":
		souluvilleMain.changeContent("collection");
		if(links[1] && $("#" + links[1]).size() >0 ){
			souluvilleMain.showSubContent("#content-collection","#" + links[1]);
		}
		break;
		case "Dealers":
		souluvilleMain.changeContent("dealers");
		break;
	}
}
function soulvilleInit(){
	//背景画像プリロード
	preload();
	function preload(){
		var preload1 = new Image();preload1.src = "img/bg_top.jpg";
		var preload2 = new Image();preload2.src = "img/bg_concept.jpg";
		var preload3 = new Image();preload3.src = "img/bg_collection.jpg";
		var preload4 = new Image();preload4.src = "img/bg_dealers.jpg";
	}
	//ナビメニュー初期化
	var navnit ={
		bttop:{
			normal:null,
			hover:null,
			click:function(){if($(this).attr('selected')){return;}changeContent("top");}
		},
		btconcept:{
			normal:"img/naviConcept.png",
			hover:"img/naviConceptSelected.png",
			click:function(){if($(this).attr('selected')){return;}changeContent("concept");}
		},
		btcollection:{
			normal:"img/naviCollection.png",
			hover:"img/naviCollectionSelected.png",
			click:function(){if($(this).attr('selected')){return;}changeContent("collection");}
		},
		btdealers:{
			normal:"img/naviDealers.png",
			hover:"img/naviDealersSelected.png",
			click:function(){if($(this).attr('selected')){return;}changeContent("dealers");}
		},
		btblog:{
			normal:"img/naviBlog.png",
			hover:"img/naviBlogSelected.png",
			click:null
		},
		btcontact:{
			normal:"img/naviContact.png",
			hover:"img/naviContactSelected.png",
			click:null
		}
	}
	$(".navimenu").each(function(){
		$("a",this).each(function(){
			var init = navnit[$(this).attr('id')];
			if(init){
				if(init.hover){
					$(this).hover(
						function(){$('img',this).attr('src',init.hover);},
						function(){if($(this).attr('selected')){return;}$('img',this).attr('src',init.normal);}
					);
				}
			}
		});
	});
	//初期表示
	currentContnet = null;
	changeContent("top");
	//コレクション動作初期化
	//---------------------------------------------
	var menuLimitRight = 0;
	collectionMenuInit();
	function collectionMenuInit(){
		var moveStart = false;
		var moveDist = 0;
		var defPos = 0;
		var accell = 0;
		var draged = false;
		$(".collectionMenu").each(function(){
			$("li>a",this).each(function(){
				var ref = $(this).attr('href');
				ref = ref.substr(1,ref.length-1);
				$(this).click(function(){if(draged) return false;});
			}).hover(function(){
			$(".hover",this).css("visibility","visible");
			},function(){
			$(".hover",this).css("visibility","hidden");
			});
		});
	}
	function collectionMenuPositionSet(){
		
		var imgwidth = Math.round(($(window).width() -300) / $(".collectionMenu li").size());
		$(".collectionMenu li").css("width",imgwidth+"px");
		$(".collectionMenu li").css("height",$(window).height()+"px");
		$(".collectionMenu li img").css("height","100%");
		$(".collectionMenu li img").css("left",Math.round((imgwidth - $(".collectionMenu li img").width()) /2));
		
	}
	$(window).load(function(){
		collectionMenuPositionSet();
		$(window).resize(collectionMenuPositionSet);
	});
	//コレクションアイテム初期化
	collectionItemInit();
	function collectionItemInit(){
		$(".collectionItem").each(function(){
			var targetItemPhoto = $(".itemphoto",this);
			var targetCollectionItemThumbs = $("ul.thumbList a",this);
			$("ul.thumbList a",this).hover(
			function(){$(".hover",this).css("visibility","visible");},
			function(){if($(this).attr("selected") == "true"){return;}$(".hover",this).css("visibility","hidden");}
			).each(function(){
				$(this).attr("photo",$(this).attr("href"));
				$(this).attr("href","javascript:void(0);");
				$(this).attr("selected","false");
			}).click(function(){
				targetItemPhoto.attr("src",$(this).attr("photo"));
				targetCollectionItemThumbs.attr("selected","false");
				$(".hover",targetCollectionItemThumbs).css("visibility","hidden");
				$(".hover",this).css("visibility","visible");
				$(this).attr("selected","true");
			}).mousedown(function(){return false;});
		});
	}
	//指定されたコレクションアイテムを初期表示状態にする
	function collectionItemViewInit( targetCollectionItem ){
		$("ul.thumbList a",$(targetCollectionItem)[0]).each(function(){
			$(".hover",this).css("visibility","hidden");
			$(this).attr("selected","false");
		});
		var targetItemPhoto = $(".itemphoto",$(targetCollectionItem)[0]);
		$("ul.thumbList a:first",$(targetCollectionItem)[0]).each(function(){
			targetItemPhoto.attr("src",$(this).attr("photo"));
			$(".hover",this).css("visibility","visible");
			$(this).attr("selected","false");
		});
	}
	//ディーラー初期化
	dealersInit();
	function dealersInit(){
		$("#content-dealers").each(function(){
			$(".addresslist li").each(function(){
				var dealerItem = this;
				$(".dealers_btmap",this).each(function(){
					$(this).attr("mapurl",$(this).attr("href"));
					$(this).attr("href","javascript:void(0);");
					$(this).removeAttr("target");
					var urlstr = $(this).attr("mapurl");
					urlstr.match(/([?])(.*)/i);
					var obj = eval("({\""+(RegExp.$2).replace(/=/g,"\":\"").replace(/&/g,"\",\"") + "\"})");
					var ll = new google.maps.LatLng(obj.ll.split(",")[0], obj.ll.split(",")[1]);
					$(this).click(function(){
						$("#content-dealers .shop_name").fadeIn();
						$("#dealers_map").fadeIn();
						if(!dealermap) gmapinitialize();
						dealermap.setZoom(15);
						dealermap.panTo(ll);
						$("#content-dealers .shop_name img").attr("src",$("h2 img",dealerItem).attr("src"));
						$("#content-dealers .addresslist li h2").fadeTo("fast",1);
						$("#content-dealers .addresslist li address").fadeTo("fast",1);
						$("#content-dealers .addresslist li .selectedbg").fadeOut();
						$(".selectedbg",dealerItem).fadeIn();
						$("h2",dealerItem).fadeTo("fast",0.5);
						$("address",dealerItem).fadeTo("fast",0.5);
					});
				});
			});
		});
		collectionItemViewInit();
	}
	function collectionItemViewInit(){
		$("#dealers_map").css("display","none");
		$("#content-dealers .shop_name").css("display","none");
		$("#content-dealers h2,#content-dealers address").fadeTo("fast",1);
		$("#content-dealers .selectedbg").hide();
	}
	//---------------------------------------------
	
	function changeContent( targetContent ){
		if(currentContnet){
			currentContnet.css("display","none");
		}
		collectionItemViewInit();
		switch(targetContent){
			case "top":
				contentTopInit();
				currentContnet = $("#content-top");
			break;
			case "concept":
				contentConceptInit();
				currentContnet = $("#content-concept");
			break;
			case "collection":
				contentCollectionInit();
				currentContnet = $("#content-collection");
				collectionMenuPositionSet();
				$(".collectionMenu").css("left","300px");
				showSubContent($("#content-collection"),null);
			break;
			case "dealers":
				contentDealersInit();
				currentContnet = $("#content-dealers");
			break;
		}
		currentContnet.hide();
		currentContnet.fadeIn();
	}
	function showSubContent( baseContent, subContent ){
		$(".subcontent",$(baseContent)[0]).each(function(){ $(this).css("display","none"); });
		$(subContent,$(baseContent)[0]).each(function(){
			collectionItemViewInit(subContent);
			$(this).css("display","block");
			//$(this).hide();
			//$(this).fadeIn();
			//$(this).css("left", $(window).width() + "px");
			//$(this).animate({left:"0px"},{duration:500,easing:"swing"});
		});
	}
	function contentTopInit(){
		$("#content-top").css("display","block");
		$(".bgmaximage").attr("src","img/bg_top.jpg");
	}
	function contentConceptInit(){
		$("#content-concept").css("display","block");
		$(".bgmaximage").attr("src","img/bg_concept.jpg");
	}
	function contentCollectionInit(){
		$("#content-collection").css("display","block");
		$(".bgmaximage").attr("src","img/bg_collection.jpg");
	}
	function contentDealersInit(){
		collectionItemViewInit();
		$("#content-dealers").css("display","block");
		$(".bgmaximage").attr("src","img/bg_dealers.jpg");
	}
	
	return {changeContent:changeContent,showSubContent:showSubContent}
}

var dealermap;
function gmapinitialize() {
  var myLatlng = new google.maps.LatLng(36.561088,136.655623);
  var myOptions = {
    zoom: 15,
    center: myLatlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  }
  dealermap = new google.maps.Map(document.getElementById("dealers_map"), myOptions);
  $("#content-dealers .addresslist li").each(function(){
		var urlstr = $("a",this).attr("mapurl");
		urlstr.match(/([?])(.*)/i);
		var obj = eval("({\""+(RegExp.$2).replace(/=/g,"\":\"").replace(/&/g,"\",\"") + "\"})");
		var ll = new google.maps.LatLng(obj.ll.split(",")[0], obj.ll.split(",")[1]);
		var marker = new google.maps.Marker({position:ll,map:dealermap});
	});
}
