var Skiyarn = {
	init: function() {
		var postfix = '-on';
		var path = $.url.attr('path');
		var current = $.url.segment(0);
		Skiyarn.addClassOn(current);
		Skiyarn.setNav(postfix,path);
		Skiyarn.swapImages(postfix);
		if(current === 'hobby' && $.url.segment(1)) {
			Skiyarn.center();
		} else if(current === 'company') {
			Skiyarn.modal();
			Skiyarn.gmap();
		}
	},
	addClassOn: function(current) {
		$('#gnav ul li a').each(function() {
			if($(this).attr('href') == '/'+current+'/') {
				$(this).addClass('on');
			}
		});
	},
	setNav: function(postfix,path) {
		$('#gnav ul li a.on img').each(function() {
			var src = $(this).attr('src');
			var src_on = src.substr(0, src.lastIndexOf('.')) + postfix + src.substring(src.lastIndexOf('.'));
			$(this).attr('src', src_on);
			if($(this).parent().attr('href') == path){
				$(this).parent().click(function(){return false;});
			} else {
				$(this).parent().css('cursor','pointer');
			}
		});
	},
	center: function() {
		$('.hobbylist ul li a img').each(function(){
			$(this).css('paddingTop',(200-this.height)/2);
		});
	},
	gmap: function(area) {
		var area = area.replace('#',''); // kyoto or tokyo
		var ll = {'kyoto':[34.931463,135.744344], 'tokyo':[35.688527,139.787491]};
		var latlng = new google.maps.LatLng(ll[area][0],ll[area][1]);
		var myOptions = {
			zoom: 18,
			center: latlng,
			mapTypeId: google.maps.MapTypeId.ROADMAP
		};
		var map = new google.maps.Map(document.getElementById('map'), myOptions);

		var beachMarker = new google.maps.Marker({
			position: latlng,
			map: map
		});

	},
	modal: function() {
		$.getScript('/scripts/jquery.simplemodal.js', function(){
			$('table tbody tr td p a.map').each(function(){
				$(this).click(function(){
					$('#header-content object').css('visibility','hidden');
					$('#map-modal').modal({
						opacity:50,
						overlayCss:{backgroundColor:"#ffc"}
					});

					Skiyarn.gmap($(this).attr('href'));

					$('.modal-close').each(function(){
						$(this).click(function(){
							$.modal.close();
							$('#header-content object').css('visibility','visible');
						});
					});
				});
			});
		});
	},
	swapImages: function(postfix) {
		$('#header ul li a:not(.on) img, #gnav ul li a:not(.on) img, #banner-oc a img').hover(function(){
			var src = $(this).attr('src');
			var src_on = src.substr(0, src.lastIndexOf('.')) + postfix + src.substring(src.lastIndexOf('.'));
			$(this).attr('src',src_on);
		}, function() {
			var src = $(this).attr('src');
			var src_off = $(this).attr('src').replace(postfix, '');
			$(this).attr('src',src_off);
		});
	}
};

/*
var Pebeo = {
	init: function() {
		Pebeo.setIeImage();
		Pebeo.setMenuLink(current);
		Pebeo.setCurrent(postfix);
		Pebeo.swapImages(current, postfix);
		//dispatch request
		if (f = dispatch[current]) f();
	},
	setIeImage: function() {
		if(jQuery.browser.version < 7) {
			$('.nav ul li a img').each(function() {
				var src = $(this).attr('src');
				src = src.replace('png','gif');
				$(this).attr('src',src);
			});
		}
	},
	setMenuLink: function(current) {
		$('.nav ul li a').each(function() {
			// 例外
			if(current == '/contact/done.php') {
				current = '/contact/';
			} else if (current == '/product/list.php' || current == '/product/detail.php') {
				current = '/product/';
			}

			if($(this).attr('href') == current) {
				$(this).addClass('on');
			}
		});
	},
	setCurrent: function(postfix) {
		$('.nav ul li a.on img').each(function() {
			var src = $(this).attr('src');
			var src_on = src.substr(0, src.lastIndexOf('.')) + postfix + src.substring(src.lastIndexOf('.'));
			$(this).attr('src', src_on);
			$(this).parent().click(function(){return false;});
		});
	},
	top: {
		init:function(){
			$.getScript('/scripts/pure_packed.js', function(){
				$.ajax({
					url: 'http://oc-i.jp/pebeo/announce?view=jsonp&page=1&entries_per_page=3',
					dataType: 'jsonp',
					success: function(data) {
						data = {infos: data};
						var directive = {
							'tbody tr': {
								'info<-infos': {
									'td.title a': function(arg){
										return arg.info.item.announce.shorten(70);
									},
									'td.title a@href+': 'info.announceid',
									'td.date': function(arg){
										return arg.info.item.registdate.replace(/\//g, '.');
									}
								}
							}
						};
						$('#information').render(data, directive);
					}
				});
			});
		}
	},
	product: {
		init:function(){
			var curtab = '.tabs a.on';
			$('.tabs a').click(function(){
				$($(curtab).attr('href')+'-cat').hide();
				$(curtab).removeClass('on');
				$($(this).attr('href')+'-cat').show();
				$(this).addClass('on');
				return false;
			});
		}
	},
	shoplist: {
		init:function(){
			$.getScript('/scripts/shoplist.js', function(){
			});
		}
	},
	contact: {
		init:function() {
			$.getScript('/scripts/jquery.simplemodal.js', function(){
				$('<input type="button" value="修正">').appendTo('form #modal div.action').click(function(){
					Pebeo.contact.confirm.hide();
				});
				$('form #input div.action input[type^=submit]').appendTo('form #modal div.action').click(function() {
					$('form').submit();
				});
				$('<input type="button" value="確認" />').appendTo('form #input div.action').click(function(){
					Pebeo.contact.confirm.show();
				});
			});
		},
		confirm:{
			show:function(){
				Pebeo.contact.confirm.create_table('#input table tr'); // input data
				$('#modal').modal({
					opacity:80,
					overlay:80,
					overlayCss:{
						backgroundColor:"#ccc"
					},
					containerCss: {
						backgroundColor:"transparent",
						width: 700
					}
				});
			},
			hide:function(elements){
				$.modal.close();
				$('#modal table').remove();
			},
			create_table:function(elements){
				$('<table><tbody></tbody></table>').insertAfter('#modal p');
				$(elements).each(function(e){
					var label = $(this).find('th').text();
					$(this).find('td').each(function(){
						var $el = $(this).children()[0];
						var body;
						if($el.tagName == 'INPUT') {
							body = $el.value;
						} else if($el.tagName == 'TEXTAREA') {
							body = $el.value.replace(/\n/g,'<br />');
						};
						$('<tr><th>'+label+'</th><td>'+body+'</td></tr>').appendTo('form #modal table tbody');
					});
				});
			}
		}
	}
};
*/

$().ready(function() {
	Skiyarn.init();
	$('.lb a').lightBox({
		imageLoading: '/img/lightbox/lightbox-ico-loading.gif',
		imageBtnClose: '/img/lightbox/lightbox-btn-close.gif',
		imageBtnPrev: '/img/lightbox/lightbox-btn-prev.gif',
		imageBtnNext: '/img/lightbox/lightbox-btn-next.gif',
		imageBlank: '/img/lightbox/lightbox-blank.gif',
		overlayBgColor: '#ffffcc',
		overlayOpacity: '0.5'
	});
});

