/* ----------------------------------

Novarese

---------------------------------- */

/* ---------- initialize ---------- */

jQuery(
	
	function ($) {

		$.Body = $('body');
		
		$.Window = $(window);
		
		$.Scroll = ($.browser.mozilla || $.browser.msie) ? $('html') : $.Body;
		
		$.Mobile = ($.Body.hasClass('webkit-mobile') || (navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i))),
		
		$.Unsupported = $.Body.hasClass('unsupported-browser');
		
		$.Body
			.Keyboard();
		
		$('[data-controller]').Instantiate();
		
		$('[data-target=_blank]').TargetBlank();
		
	} 
	
);

/* ---------- Events ---------- */

(function($) {

	$.Events = {


		SECTION_ENTER: 'sectionEnter',

		SCROLL_TO: 'scrollTo',

		SCROLL: 'windowScroll',
		SCROLL_ENTER: 'windowScrollEnter',
		SCROLL_LEAVE: 'windwScrollLeave',

		KEY_UP: 'keyUp',
		KEY_DOWN: 'keyDown',
		KEY_LEFT: 'keyLeft',
		KEY_RIGHT: 'keyRight',
		KEY_ESC: 'keyEsc',
		KEY_SPACE: 'keySpace'



	} // Events  

	$.Views = {

	} // Views 

})(jQuery);

/* ---------- speed set ---------- */
(function($) {

	$.vscrollspeed = 800;
	$.hscrollspeed = 800;
	$.hbgscrollspeed = 1200;
	
/* default value -----
$.vscrollspeed = 800;
$.hscrollspeed = 800;
$.hbgscrollspeed = 1200;
----- */

})(jQuery);

/* ---------- XML ---------- */

var ITEMS = new Array();

function ITEM() {
	var that = {};
		that.name = '';
		that.group = '';
		that.category = '';
		that.region = '';
		that.section = '';
		that.url = '';
		that.img = '';
	
	return that;
}

$.get('data.xml', '',
	function (data, status) {
	
		$('item', data).each(function() {
		
			function _group(g) {
				switch (g) {
					case 'WeddingSpace':	g = 'space'; break;
					case 'WeddingDress':	g = 'dress'; break;
					case 'RESTAURANT&HOTEL':	g = 'restaurant'; break;
				};
				return g;
			}
			function _region(r) {
				switch (r) {
					case '関東':			r = 'kan'; break;
					case '北関東':			r = 'kit'; break;
					case '東北':			r = 'toh'; break;
					case '北陸・甲信越':	r = 'hok'; break;
					case '東海':			r = 'tok'; break;
					case '関西':			r = 'ksa'; break;
					case '中国':			r = 'chu'; break;
					case '九州':			r = 'kyu'; break;
				};
				return r;
			}
			function _category(c) {
				var e = '';
				if (c.indexOf('AMANDAN')>-1)	e += 'amandan ';
				if (c.indexOf('MONOLITH')>-1)	e += 'monolith ';
				if (c.indexOf('再生会場')>-1)	e += 'saisei ';
				if (c.indexOf('ホテル・式場')>-1)	e += 'hotel ';
				if (c.indexOf('提携会場')>-1)	e += 'teikei ';
				if (c.indexOf('NOVARESE')>-1)	e += 'novarese ';
				if (c.indexOf('PRIMA')>-1)	e += 'prima ';
				if (c.indexOf('ecruspose')>-1)	e += 'ecruspose ';
				if (c.indexOf('RESTAURANT&HOTEL')>-1)	e += 'restaurant ';
				return e;
			}
			function _section(s) {
				switch (s) {
					case '北海道':	s = 'hokkaido'; break;	case '青森':	s = 'aomori'; break;	case '秋田':	s = 'akita'; break;
					case '岩手':	s = 'iwate'; break;	case '山形':	s = 'yamagata'; break;	case '宮城':	s = 'miyagi'; break;
					case '福島':	s = 'fukushima'; break;	case '新潟':	s = 'nigata'; break;	case '茨城':	s = 'ibaraki'; break;
					case '栃木':	s = 'tochigi'; break;	case '群馬':	s = 'gunma'; break;	case '長野':	s = 'nagano'; break;
					case '富山':	s = 'toyama'; break;	case '石川':	s = 'ishikawa'; break;	case '福井':	s = 'fukui'; break;
					case '岐阜':	s = 'gifu'; break;	case '埼玉':	s = 'saitama'; break;	case '千葉':	s = 'chiba'; break;
					case '東京':	s = 'tokyo'; break;	case '神奈川':	s = 'kanagawa'; break;	case '山梨':	s = 'yamanashi'; break;
					case '静岡':	s = 'shizuoka'; break;	case '愛知':	s = 'aichi'; break;	case '滋賀':	s = 'shiga'; break;
					case '三重':	s = 'mie'; break;	case '奈良':	s = 'nara'; break;	case '大阪':	s = 'osaka'; break;
					case '和歌山':	s = 'wakayama'; break;	case '京都':	s = 'kyoto'; break;	case '兵庫':	s = 'hyogo'; break;
					case '鳥取':	s = 'tottori'; break;	case '岡山':	s = 'okayama'; break;	case '島根':	s = 'shimane'; break;
					case '広島':	s = 'hiroshima'; break;	case '山口':	s = 'yamaguchi'; break;	case '香川':	s = 'kagawa'; break;
					case '徳島':	s = 'tokushima'; break;	case '愛媛':	s = 'ehime'; break;	case '高知':	s = 'kochi'; break;
					case '大分':	s = 'oita'; break;	case '宮崎':	s = 'miyazaki'; break;	case '福岡':	s = 'fukuoka'; break;
					case '佐賀':	s = 'saga'; break;	case '長崎':	s = 'nagasaki'; break;	case '熊本':	s = 'kumamoto'; break;
					case '鹿児島':	s = 'kagoshima'; break;	case '沖縄':	s = 'okinawa'; break;
				};
				return s;
			}

			_item = new ITEM();
			_item.name = $('name', this).text();
			_item.group = _group($('group', this).text());
			_item.category = _category($('category', this).text());
			_item.region = _region($('region', this).text());
			_item.section = _section($('section', this).text());
			_item.url = $('url', this).text();
			_item.img = $('imgsrc', this).text();
			
			ITEMS.push(_item);
		});
		
		//$.locator();
		//$.imageslider(".main_view");
	}
);


/* ---------- instantiate ---------- */

(function($) {

	$.fn.Instantiate = function(settings) {

		var config = {};

		if (settings) $.extend(config, settings);

			this.each(function() { 

					var $self = $(this),
							$controller = $self.attr('data-controller');

					if ($self[$controller])
						$self[$controller]();

			});

	}

})(jQuery);


/* ---------- Scrollable ---------- */

(function($) {


	$.fn.Scrollable = function(settings) {

		var config = { threshold: -100, offset_scroll: 6, offset_intertia: .15 };

		if (settings) $.extend(config, settings);

		this.each(function() {

				var $self = $(this),
					$id = $self.attr('id');

				config.threshold = 0;

				if ($.Unsupported) {
					$self.css({backgroundAttachment:'scroll'});
				
				} else if ($.Mobile!==null) {
					$self.css({backgroundAttachment:'scroll'});
				
				} else {

					$.Window
						.bind('scroll',
							function(e){

								if ( $.inview($self,{threshold:config.threshold}) ) {

									if (!$self.hasClass('_active')){

										$self.addClass('_active');

										if (settings.is_nav)
											$.Body.triggerHandler($.Events.SECTION_ENTER,$id);

										$self.triggerHandler($.Events.SCROLL_ENTER);

									}

										_scroll_background();

									$self.triggerHandler($.Events.SCROLL,$.distancefromfold($self,{threshold:config.threshold}) - config.threshold);

								}else{

									if ($self.hasClass('_active')){

										$self.removeClass('_active');
										
										$self.triggerHandler($.Events.SCROLL_LEAVE);

									}

								}


					});

				}
				
				function _scroll_background() {
				
					var _id = $self.attr("id"),
						_e = document.getElementById(_id),
						_style = _e.currentStyle || document.defaultView.getComputedStyle(_e,null),
						_bg_left = "";
						
						if (document.all) _bg_left = _style.backgroundPositionX;
						else _bg_left = _style.backgroundPosition.split(" ")[0];
					
					bpos = _bg_left + " " + (-($.distancefromfold($self,{threshold:config.threshold}) - config.threshold) * config.offset_intertia) + 'px';
					
					$self.css({'backgroundPosition':bpos});

				}

				/*if (config.auto_scroll)
					_scroll_background();*/

		});
		
		return this;
		
	} //Scrollable
	
	$.fn.Top = function() {

		this.each(function() { 

			var $self = $(this),
				$id = $self.attr('id'),
				$bg = $self.find('.bg'),
				_threshold = 0;

			$self
				.Scrollable({threshold: _threshold,is_nav:true,offset_intertia:.02})
				.bind($.Events.SCROLL,on_scroll)

			function on_scroll(e,distance) {
				var _style = $.getcomputedbgpos("contents-top"),
					bpos = _style.left + 'px ' + (100 + $.Window.height()/2-distance/3) + 'px';
				
				$bg.css({'backgroundPosition':bpos});
			}

		});

		return this;
	
	}// Top
	
	$.fn.Space = function() {
	
		this.each(function() { 

			var $self = $(this),
				$id = $self.attr('id'),
				$bg = $self.find('.bg'),
				_threshold = -500;
			
			$self
				.Scrollable({threshold: _threshold,is_nav:true})
				.bind($.Events.SCROLL,on_scroll)

			function on_scroll(e,distance) {
				var _style = $.getcomputedbgpos("contents-space"),
					bpos = _style.left + 'px ' + (240 + $.Window.height()/2-distance/3) + 'px';
				
				$bg.css({'backgroundPosition':bpos});

			}

		});

		return this;
	
	}// Space
	
	$.fn.Dress = function() {

		this.each(function() { 

			var $self = $(this),
				$id = $self.attr('id'),
				$bg = $self.find('.bg'),
				_threshold = -200;

			$self
				.Scrollable({threshold: _threshold,is_nav:true})
				.bind($.Events.SCROLL,on_scroll)

			function on_scroll(e,distance) {
				var _style = $.getcomputedbgpos("contents-dress"),
					bpos = _style.left + 'px ' + (100 + $.Window.height()/2-distance/3) + 'px';
				
				$bg.css({'backgroundPosition':bpos});
			}

		});

		return this;
	
	}// Dress
	
	$.fn.Restaurant = function() {

		this.each(function() { 

			var $self = $(this),
				$id = $self.attr('id'),
				$bg = $self.find('.bg'),
				_threshold = -500;

			$self
				.Scrollable({threshold: _threshold,is_nav:true})
				.bind($.Events.SCROLL,on_scroll)

			function on_scroll(e,distance) {
				var _style = $.getcomputedbgpos("contents-restaurant"),
					bpos = _style.left + 'px ' + (250 + $.Window.height()/2-distance/3) + 'px';
				
				$bg.css({'backgroundPosition':bpos});
			}

		});

		return this;
	
	}// Restaurant
	
	$.fn.Locator = function() {

		this.each(function() { 

			var $self = $(this),
				$id = $self.attr('id'),
				$bg = $self.find('.bg'),
				_threshold = 0;

			$self
				.Scrollable({threshold: _threshold,is_nav:true})
				.bind($.Events.SCROLL,on_scroll)

			function on_scroll(e,distance) {
				$bg.css("display","block");
			}

		});

		return this;
	
	}// Locator

})(jQuery);


/* ---------- Global Nav ---------- */

(function($) {

	$.fn.GlobalNav = function() {
	
		if ($.Mobile===null) {
		    $(window).scroll(function(){
				var scrollTop = $(window).scrollTop();
				if(scrollTop != 0) {
					$('.nav-main:first').stop().animate({'marginTop':'10px'},200);
					$('#global').stop().animate({'height':'120px'},400);
				}
				else {
					$('.nav-main:first').stop().animate({'marginTop':'20px'},200);
					$('#global').stop().animate({'height':'160px'},400);
				}
			});
		}
		
		this.each(function() {
			
			var $self = $(".nav-main"),
				$ul = $('<ul/>').appendTo($self),
				$sections = $('[data-nav]'),
				_sections = new Array(),
				$navs = new Array(),
				_active = 0;


			if (!$.Unsupported) { 
				$sections.each(
					function(i){
						_sections.push($(this));
						if(i===0) {
							$('<li/>').addClass("nav" + i).appendTo($ul).DotNav({id:$(this).attr('id'),name:$(this).attr('data-nav')});
							$('<li/>').addClass("nav" + (i+1)).appendTo($ul).DotNav({id:$(this).attr('id'),name:$(this).attr('data-nav')});
						}
						else $('<li/>').addClass("nav" + (i+1)).appendTo($ul).DotNav({id:$(this).attr('id'),name:$(this).attr('data-nav')});
					})
			}

			$.Body
				.bind($.Events.SECTION_ENTER,
					function(e,id){

						$sections.each(
							function(i){
								if ($(this).attr('id')==id)
									_active = i;

							})

					})
				.bind($.Events.KEY_RIGHT,
					function(e){
						_active++;
						if (_active>$sections.length-1)
							_active=$sections.length-1;
						_seek();
				})
				.bind($.Events.KEY_LEFT,
					function(e){
						_active--;
						if (_active<0)
							_active=0;
						_seek();
				})

			function _seek() {
				$.Body.triggerHandler($.Events.SCROLL_TO,_sections[_active].attr('id'));
			}
		});
		
		

		return this;

	}// GlobalNav


	$.fn.DotNav = function(settings) {
		
		var config = {};

		if (settings) $.extend(config, settings);

		this.each(function() { 

			var $self = $(this),
				$a = $('<a/>'),
				$id = config.id;


			$a
				.attr('href',"#/"+config.name.split(' ').join('_'))
				.appendTo($self)
				.bind('click',
					function(e){

						$.Body.triggerHandler($.Events.SCROLL_TO,$id);

						e.preventDefault();

					})

			$span = $('<span/>').html(config.name).appendTo($a);
			
			$self
				.attr('data-id',$id);


			$.Body
				.bind($.Events.SECTION_ENTER,
					function(e,id){

						if (id==$id)
							$self.addClass('active')
						else
							$self.removeClass('active')


					});

		});

		return this;
		
	} // DotNav

})(jQuery);


/* ---------- Vertical Scroll ---------- */

(function($) {

	$.fn.SiteScroll = function() {

		this.each(function() { 

			var $self = $(this);

			$.Body
				.bind($.Events.SCROLL_TO,
					function(e,id){

						var $element = $('#'+id),
							$header = $element.find('header'),
							_align = $element.attr('data-align'),
							_offset = $element.attr('data-scrolloffset') ? parseInt($element.attr('data-scrolloffset')) : 50,
							_top = $element.offset().top;

						if ($header.length>0 && _align!="top") { 

							_top = $header.offset().top  + $header.height()/2 - $.Window.height()/2;

							if (_top > $header.offset().top)
								_top = $header.offset().top - 50
						}

						if (_align=="center" && $element.height()>$.Window.height()) {

							_top = $element.offset().top + ($element.height()-$.Window.height())/2

						}
						
						if (_top===$(window).scrollTop()) $.sidescrollreset();
						else $.Scroll
							.stop()
							.animate(
								{ 'scrollTop': _top },
								$.vscrollspeed,
								'easeInOutQuart',
								$.sidescrollreset
							)

					})

		});
		
		
		$.Window
			.bind('resize',
				function() {
					
					if ($.Mobile===null) _screenreset();
					$.sidescrollreset();
					
				});
		
		$(document).ready(function() {
			$('#overlay').height($(document).height());
			$('#loading').html($('#loading').html() + '<br><img src="lib/img/loading.gif">');
			$('#loading').css('margin-top',$(window).scrollTop() + 200 + 'px');
			
			//iPhone
			if ($.Mobile!==null) {
				$('#top').css('background-image','url(lib/img/mob_bg_top.jpg)');
				$('#space').css('background-image','url(lib/img/mob_bg_space.jpg)');
				$('#dress').css('background-image','url(lib/img/mob_bg_dress.jpg)');
				$('#restaurant').css('background-image','url(lib/img/mob_bg_restaurant.jpg)');
				$('#top .bg, #space .bg, #dress .bg, #restaurant .bg').css('background-image','none');
				$('#global').css('width','1050px');
			}
		});
		$.Window
			.bind('load',
				function() {
					
					_screenreset();
					$.sidescrollreset();
					$.locator();
					$.imageslider(".main_view");
					$('#overlay').fadeOut('slow');
					$('#loading').remove();
				});
		
		
		function _screenreset() {
			var _left = $("#top").offset().left,
				_ctop = $.getcomputedbgpos("contents-top"),
				_space = $.getcomputedbgpos("contents-space"),
				_dress = $.getcomputedbgpos("contents-dress"),
				_restaurant = $.getcomputedbgpos("contents-restaurant");
			
			if ( !($.browser.msie && $.browser.version==='6.0') ) $(window).scrollTop(0,0);
			
			$("#contents-top").css({"backgroundPosition":_left + "px " + _ctop.top + "px"}).show();
			$("#space, #dress, #restaurant").css({"backgroundPosition":_left + "px 0"});
			$("#contents-space").css({
				"marginLeft":"0px",
				"backgroundPosition":(_left + 440) + "px 280px"
			});
			
			$("#contents-dress").css({
				"marginLeft":"0px",
				"backgroundPosition":(_left + 160) + "px 200px"
			});
			$("#contents-restaurant").css({
				"marginLeft":"0px",
				"backgroundPosition":(_left + 435) + "px 280px"
			});
			$('#header-nav .nav-main').show();
		}
		
		return this;

	} // SiteScroll
	
	$.sidescrollreset = function() {
		var _space = $.getcomputedbgpos('space'),
			_space_key = $.getcomputedbgpos('contents-space'),
			_dress = $.getcomputedbgpos('dress'),
			_dress_key = $.getcomputedbgpos('contents-dress'),
			_restaurant = $.getcomputedbgpos('restaurant'),
			_restaurant_key = $.getcomputedbgpos('contents-restaurant'),
			_base_left = $("#top").offset().left;
		
		_space_key.left = _space_key.left > 0 ? _space_key.left : _space_key.left + 2000;
		_dress_key.left = _dress_key.left > 0 ? _dress_key.left : _dress_key.left + 2000;
		_restaurant_key.left = _restaurant_key.left > 0 ? _restaurant_key.left : _restaurant_key.left + 2000;
		
		
		$.sidescrollresetmove('space',_space,_space_key,_base_left);
		$.sidescrollresetmove('dress',_dress,_dress_key,_base_left);
		$.sidescrollresetmove('restaurant',_restaurant,_restaurant_key,_base_left);
		
		return false;
	}
	$.sidescrollresetmove = function(id,wrap,box,left) {
		var m = $('#contents-' + id).css('marginLeft').replace(/px/,'');
		if (m<0) {
			$('#contents-' + id).stop().animate({
				"marginLeft":"0px",
				"backgroundPosition":box.left + "px " + box.top + "px"
			},$.hscrollspeed,'easeInOutQuart');
			$('#' + id).stop().animate({"background-position":left + "px " + wrap.top + "px"},$.hbgscrollspeed,'easeInOutQuart');
		}
	}

})(jQuery);


/* ---------- Target Blank ---------- */

(function($) {

	$.fn.TargetBlank = function() {

		this.each(function() { 

			var $self = $(this);

			$self
			.attr('target','_blank')
			.bind('click',on_click);

			function on_click(e){

			}

		});

		return this;

	}

})(jQuery);


/* ---------- Keyboard ---------- */

(function($) {


	$.fn.Keyboard = function(settings) {

		var config = {};

		if (settings) $.extend(config, settings);

			this.each(function() { 

				var $self = $(this);

				$(document)
				.bind('keydown',on_keydown);
				
				function on_keydown(e) {
					
					var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
					
					switch(key) {

						
						case 27: //escape

							$.Body.triggerHandler($.Events.KEY_ESC);

						break;
						
						case 32: //space

							$.Body.triggerHandler($.Events.KEY_SPACE);

						break;
						
						case 38: //top
							
							$.Body.triggerHandler($.Events.KEY_UP);

						break;

						case 39: //right

							$.Body.triggerHandler($.Events.KEY_RIGHT);
							e.preventDefault();

						break;
						
						case 40: ///bottom

							$.Body.triggerHandler($.Events.KEY_DOWN);

						break;

						case 37: //left

							$.Body.triggerHandler($.Events.KEY_LEFT);

						break;

					}//switch

				}//keydown

			}); 

			return this;

	}


})(jQuery);


/* ---------- Worker ---------- */

(function($) {

	$.distancefromfold = function($element, settings) {
		if (settings.container === undefined || settings.container === window) {
			var fold = $(window).height() + $(window).scrollTop();
		} else {
			var fold = $(settings.container).offset().top + $(settings.container).height();
		}
		return (fold + settings.threshold) - $element.offset().top ;
	};
	
	$.belowthefold = function($element, settings) {
		if (settings.container === undefined || settings.container === window) {
			var fold = $(window).height() + $(window).scrollTop();
		} else {
			var fold = $(settings.container).offset().top + $(settings.container).height();
		}
		return fold <= $element.offset().top - settings.threshold;
	};
	
	$.rightoffold = function($element, settings) {
		if (settings.container === undefined || settings.container === window) {
			var fold = $(window).width() + $(window).scrollLeft();
		} else {
			var fold = $(settings.container).offset().left + $(settings.container).width();
		}
		return fold <= $element.offset().left - settings.threshold;
	};

	$.abovethetop = function($element, settings) {
		if (settings.container === undefined || settings.container === window) {
			var fold = $(window).scrollTop();
		} else {
			var fold = $(settings.container).offset().top;
		}
		return fold >= $element.offset().top + settings.threshold  + $element.height();
	};
	
	$.leftofbegin = function($element, settings) {
		if (settings.container === undefined || settings.container === window) {
			var fold = $(window).scrollLeft();
		} else {
			var fold = $(settings.container).offset().left;
		}
		return fold >= $element.offset().left + settings.threshold + $element.width();
	};
	
	$.inview = function($element, settings) {
		return ($.abovethetop($element,settings)!=true && $.belowthefold($element,settings)!=true)
	};


	$.extend($.expr[':'], {
		"below-the-fold" : "$.belowthefold(a, {threshold : 0, container: window})",
		"above-the-fold" : "!$.belowthefold(a, {threshold : 0, container: window})",
		"right-of-fold"  : "$.rightoffold(a, {threshold : 0, container: window})",
		"left-of-fold"   : "!$.rightoffold(a, {threshold : 0, container: window})"
	});
	
})(jQuery);


/* ---------- Side Scroll ---------- */

(function($) {

	$.fn.SideScroll = function(settings) {
		
		var $self = $(this),
			$id = $self.attr("id"),
			_pageID = $self.attr("data-id"),
			$parent = $("#" + _pageID),
			$a = $("#" + $id + " .nav-" + _pageID + " a[href!='#map']"),
			_width = 1050,
			_effect = "easeInOutQuart";
		
		$a.each(function(i) {
			
			$(this).click(function() {
				
				var _num = $(this).attr("href").charAt($(this).attr("href").length - 1),
					_parent_style = $.getcomputedbgpos(_pageID),
					_self_style = $.getcomputedbgpos($id),
					_base_left = $("#top").offset().left,
					_move = (_num * _width) * -1;
					
				
				//kick the key image out of sight
				_self_style.left = _self_style.left < 0 ? _self_style.left : _self_style.left - 2000;
				
				$self.stop().animate({
					"marginLeft":_move + "px",
					"backgroundPosition":_self_style.left + "px " + _self_style.top + "px"
				},$.hscrollspeed,_effect);
				$parent.stop().animate({"backgroundPosition":_move + _base_left + "px " + _parent_style.top + "px"},$.hbgscrollspeed,_effect);
				
				
				return false;
			})
			
		})
		
		//back button
		$("#" + $id + " .back a").each(function() {
			
			$(this).click(function() {
				
				var _parent_style = $.getcomputedbgpos(_pageID),
					_self_style = $.getcomputedbgpos($id),
					_base_left = $("#top").offset().left;
				
				//put the key image back to the spot
				_self_style.left = _self_style.left + 2000;
				
				$self.stop().animate({
					"marginLeft":"0px",
					"backgroundPosition":_self_style.left + "px " + _self_style.top + "px"
				},$.hscrollspeed,_effect);
				$parent.stop().animate({"background-position":_base_left + "px " + _parent_style.top + "px"},$.hbgscrollspeed,_effect);
				
				return false;
			})
		})
	
	}// SideScroll

})(jQuery);


/* ---------- Tool Functions ---------- */

(function($) {

	$.getcomputedbgpos = function(id) {
		
		var e = document.getElementById(id),
			style = e.currentStyle || document.defaultView.getComputedStyle(e,null),
			val = { left:0, top:0 };
		
		if (document.all) {
			val.left = style.backgroundPositionX.replace(/px/i,"") - 0;
			val.top = style.backgroundPositionY.replace(/px/i,"") - 0;
		}
		else {
			val.left = style.backgroundPosition.split(" ")[0].replace(/px/i,"") - 0;
			val.top = style.backgroundPosition.split(" ")[1].replace(/px/i,"") - 0;
		}
		
		return val;
	};
	
	
	$.imageslider = function(id) {
		
		var _timers = new Array(),
			_len = $(id).size();
		
		//set images
		for (var i=0; i<ITEMS.length; i++) {
			var _this = ITEMS[i],
				$link = '<a href="' + _this.url + '" target="_blank" onclick="javascript:_gaq.push([\'_trackEvent\', \'outGoing\', \'Ex\', \'' + _this.name + '\']);"><img src="' + _this.img + '" width="380" title="' + _this.name + '"></a>';
			
			if (_this.group==='restaurant') _imageset('#restaurant1 .image_reel',$link);
			if (_this.group==='space') {
				if (_this.category.indexOf('amandan')>-1) _imageset('#space1 .image_reel',$link);
				if (_this.category.indexOf('monolith')>-1) _imageset('#space2 .image_reel',$link);
				if (_this.category.indexOf('saisei')>-1) _imageset('#space3 .image_reel',$link);
				if (_this.category.indexOf('hotel')>-1) _imageset('#space4 .image_reel',$link);
				if (_this.category.indexOf('teikei')>-1) _imageset('#space5 .image_reel',$link);
			}
		}
		function _imageset(id,li) {
			var html = $(id).html();
			$(id).html(html + li);
		}
		
		
		$(id).each(function(i) {
			
			var $id = $(this).attr('data-id'),
				$paging = '#' + $id + ' .paging',
				$icons = '#' + $id + ' .btn',
				$reel = '#' + $id + ' .image_reel',
				
				_sum = $($reel + ' img').size(),
				_width = $(_window = '#' + $id + ' .window').width(),
				_reelwidth = _width * _sum,
				_name;
			
			//create paging dot
			var _dot = '';
			for (var n=1; n<_sum+1; n++) {
				_dot += '<a href="#"><span>' + n + '</span></a>';
			}
			$($icons).append(_dot);
			
			//initialize
			var $active = $($icons + ' a:first'),
				_triggerid = parseInt($active.text()) - 1,
				_pos = _triggerid * _width;
			
			$active.addClass('active');
			$($reel).css({ 'width':_reelwidth });
			$($paging + ' .name').text($($reel + ' img:eq(' + _triggerid + ')').attr('title'));

			//functions
			var _rotate = function(txt) {
			
				_triggerid = parseInt($active.text()) - 1;
				_pos = _triggerid * _width;
				
				$($icons + ' a').removeClass('active');
				$active.addClass('active');
				
				$($paging + ' .name').text($($reel + ' img:eq(' + _triggerid + ')').attr('title'));
				
				$($reel).animate({ left: _pos * -1 }, 500, "easeInOutQuart" );
			}
			var _rotateswitch = function() {
			
				_timers[i] = setInterval(function(){
					
					if (_triggerid < _sum-1 ) $active = $($icons + ' a.active').next();
					else $active = $($icons + ' a:first');
					
					_rotate();
				
				}, 4000);
			}
			var _rotateclear = function() {
				for (var c=0; c<_len; c++) {
					clearInterval(_timers[c]);
				}
			}
			
			//slide stops when cursor over the image
			$($reel + ' a').hover(function() {
				clearInterval(_timers[i]);
			}, function() {
				_rotateswitch();
			});	
			
			//paging links
			$($icons + ' a').click(function() {
				$active = $(this);

				_rotateclear();
				_rotate();
				_rotateswitch();
				
				return false;
				
			});
			
			//attach functions to links
			$($paging + ' .map-locator a:first, #' + $id + ' .back a:first').click(function() {
				_rotateclear();
				return false;
			});
			
			if (i==0) {
				$("a[href='#space1']").click(function() {
					_rotateclear();
					_rotateswitch();
				});
			}
			
			if (i==1) {
				$("a[href='#space2']").click(function() {
					_rotateclear();
					_rotateswitch();
				});
			}
			if (i==2) {
				$("a[href='#space3']").click(function() {
					_rotateclear();
					_rotateswitch();
				});
			}
			if (i==3) {
				$("a[href='#space4']").click(function() {
					_rotateclear();
					_rotateswitch();
				});
			}
			if (i==4) {
				$("a[href='#space5']").click(function() {
					_rotateclear();
					_rotateswitch();
				});
			}
			if (i==5) {
				$("a[href='#restaurant1']").click(function() {
					_rotateclear();
					_rotateswitch();
				});
			}
			
		});
		
	
	}// Image Slider
	
	$.locator = function() {
	
		function _show(e,id) {
			$(e).addClass('blank').css('display','none');
			$(e + '[class*="' + id + '"]').css('display','block');
		}
		
		function _scroll(e,box) {
			var _max = $(box).height() - $(e).height(),
				_isScroll = 'null';
			
			//initialize
			$(e).css('margin-top',0);
			
			if (_max<0) {
				$('#map .scroll-btn').css('display','block');
				$('#map .scroll-btn a:eq(0)').mousedown(function() {
					_isScroll = setInterval(function() {
						var m = $('#map-list').css('margin-top').replace(/px/,'') - 0 - 1;
						if (_max<m) $('#map-list').css('margin-top',m + 'px');
						else $('#map-list').css('margin-top',_max + 'px');
					},10);
					return false;
				}).mouseup(function() {
					clearInterval(_isScroll);
				}).click(function() {
					return false; 
				});
				$('#map .scroll-btn a:eq(1)').mousedown(function() {
					_isScroll = setInterval(function() {
						var m = $(e).css('margin-top').replace(/px/,'') - 0 + 1;
						if (m<0) $(e).css('margin-top',m + 'px');
						else $(e).css('margin-top','0');
					},10);
					return false;
				}).mouseup(function() {
					clearInterval(_isScroll);
				}).click(function() {
					return false; 
				});
			}
			else {
				$('#map .scroll-btn').css('display','none');
			}
		}
		
		var _region = {
		
			set: function(e,id) {
				$(e).attr('class',id);
			},
			clear: function(e) {
				$(e).attr('class','');
			}
		};
		
		var _section = {
		
			set: function(e,id) {
				$(e).attr('class',id);
			},
			clear: function(e) {
				$(e).attr('class','');
			}
		};
		
		var _title = {
		
			h2:	function(e,id) {
				$(e + ' h2').attr('class','map-' + id);
			},
			region: function(e) {
				$(e + '[class*="map-kan"]:visible:first').removeClass('blank');
				$(e + '[class*="map-kit"]:visible:first').removeClass('blank');
				$(e + '[class*="map-toh"]:visible:first').removeClass('blank');
				$(e + '[class*="map-hok"]:visible:first').removeClass('blank');
				$(e + '[class*="map-tok"]:visible:first').removeClass('blank');
				$(e + '[class*="map-ksa"]:visible:first').removeClass('blank');
				$(e + '[class*="map-chu"]:visible:first').removeClass('blank');
				$(e + '[class*="map-kyu"]:visible:first').removeClass('blank');
			}
		};
		
		
		//set all links
		for (var i=0; i<ITEMS.length; i++) {
		
			//for Locator list
			var _this = ITEMS[i],
				$list = $('<li/>').addClass(_this.region + '_'  + _this.group),
				$link = $('<a/>').attr({ 'href':_this.url, 'target':'_blank', 'rel':_this.section }).text(_this.name),
				$span = '';
			
			$list.append($link);
			
			$link.hover(function() {
				_section.set('#locator .map span',$(this).attr('rel'));
			},function(){
				_section.clear('#locator .map span');
			});
			$link.click(function() {
				_gaq.push(['_trackEvent', 'outGoing', 'Exmaxp', _this.name ]);
			});
			
			$('#locator-list').append($list);
			
			
			//for Map list
			$list = $('<li/>').addClass(_this.group + ' ' + _this.category + 'map-' + _this.region + ' blank');
			$link = $('<a/>').attr({ 'href':_this.url, 'target':'_blank', 'rel':_this.section });
			$span = $('<span/>').addClass(_this.region).text(_this.name);
			
			$link.append($span);
			$list.append($link);
			
			$link.hover(function() {
				_region.set('#map .map div',$('span',this).attr('class'));
				_section.set('#map .map span',$(this).attr('rel'));
			},function(){
				_region.clear('#map .map div');
				_section.clear('#map .map span');
			});
			$link.click(function() {
				_gaq.push(['_trackEvent', 'outGoing', 'Exmaxp', _this.name ]);
			});
			
			$('#map-list').append($list);
		}
		
		
		//initialize
		_show('#locator-list li','kan_space');
		
		//main nav
		$('#locator .nav-locator a').each(function() {
		
			$(this).click(function() {
			
				$('#locator .nav-locator a').removeClass('cr');
				$('#locator .nav-locator-sub a').removeClass('cr');
				$(this).addClass('cr');
				$('#locator .nav-locator-sub a:first').addClass('cr');
				
				var r = $(this).attr('href').replace(/#/,'');
				
				_region.set('#locator .map div',r);
				_show('#locator-list li',r + '_space');
				
				return false;
			
			});
		});
		
		//sub nav
		$('#locator .nav-locator-sub a').each(function() {
		
			$(this).click(function() {
			
				$('#locator .nav-locator-sub a').removeClass('cr');
				$(this).addClass('cr');
				
				var g = $(this).attr('href').replace(/#/,'');
				var r = $('#locator .nav-locator a[class*="cr"]').attr('href').replace(/#/,'');
				_show('#locator-list li',r + '_' + g);
				
				return false;
			
			});
		});
		
		//map button
		function _setoverlay() {
			$('#overlay').width($(window).width()).height($(document).height());
			$('#overlay').fadeIn('slow');
			$('#overlay, #map .close').click(function() {
				$('#map').css('display','none');
				$('#overlay').fadeOut('slow');
				return false;
			});
		}
		function MapSet(id) {
			_setoverlay();
			_show('#map-list li',id);
			_title.h2('#map',id);
			
			var _left = $(window).scrollLeft(),
				_top = $(window).scrollTop(),
				_width = $(window).width(),
				_height = $(window).height();
			
			_left += (_width - 1050) / 2;
			_top += (_height - 550) / 2;
			$('#map').css({
				'left':_left,
				'top':_top
			}).fadeIn('slow');
			
			_title.region('#map-list li');
			_scroll('#map-list','#map .scroll');
		}
		
		$('.map-locator').each(function() {
		
			var $id = $(this).attr('data-id');
			
			$('a',this).click(function() {
				MapSet($id);
				return false;
			});
		});
	
	}// Locator

})(jQuery);
