(function(){
	var rm = window['rimoh_salon'] = {};
	jQuery.extend(rm, {
		getImageSrc: function(selector){
			if ($(selector)[0].nodeName.toLowerCase() == 'img') {
				return $(selector).attr('src');
			}
		}
	});
	jQuery.fn.extend({
		vCenter: function(){
			var maxHeight = 0;
			$(this).each(function(){
				if (maxHeight < $(this).height()) maxHeight = $(this).height();
			});
			return $(this).each(function(){
				var mt = (maxHeight-$(this).height())/2+'px';
				$(this).css({
					marginTop:mt
				});
			});
		},

		showRate: function(){
			return $(this).each(function(){
				$(this).append($('<div class="rate-star"/>').width($(this).attr('rel')*14).height(14));
			});
		},

		showRecm: function(){
			return $(this).each(function(){
				$(this).toggle(function(){
					$(this).parents('.presenter-info').children().show('slide').end().prev('img').show('slide', function(){
						if ($.browser.msie) {
							$('#wrapper').removeAttr('style').height($('#wrapper').height());
							$('#header').css({bottom:0});
						}
					});
				}, function(){
					$(this).parents('.presenter-info').children(':not(h3)').hide('slide').end().prev('img').hide('slide', function(){
						if ($.browser.msie) {
							$('#wrapper').removeAttr('style').height($('#wrapper').height());
							$('#header').css({bottom:0});
						}
					});
				});
			});
		},

		addBookmark: function(){
			var isIEmac = false; /*@cc_on @if(@_jscript&&!(@_win32||@_win16)&& (@_jscript_version<5.5)) isIEmac=true; @end @*/
			var title = document.title, href = location.href ,key = navigator.userAgent.toLowerCase().indexOf('mac') != -1 ? 'Command/Cmd' : 'Ctrl';
			return $(this).each(function(){
				$(this).click(function(){
					try {
						if (window.sidebar) {
							window.sidebar.addPanel(title, href, '');
						}
						else if ($.browser.msie && window.external) {
							if ($.browser.version >= 8.0) {
								window.external.AddToFavoritesBar(href, title);
							}
							else {
								window.external.AddFavorite(href, title);
							}
						}
						else if (window.opera) {
							$(this).attr({
								href:href,
								title:title,
								rel:'sidebar'
							});
							return true;
						}
						else {
							alert('『'+key+' + D』を押してブックマークに追加してください。 ');
						}
						return false;
					} catch(e) {
						alert(e);
					}
				});
			});
		}
	});
	jQuery(function($){
		$('#about-salon img').after('<span id="gradient-overlay"/>');
		var fadeImage = function(){
			var url = $(this).attr('href');
			$('#style-image-display').html('<div id="fader"/>');
			$('#style-image-display #fader').css({
				width:'397px',height:'245px',background:'url('+url+') no-repeat center center'
			}).fadeTo(0, 0).fadeTo(1000, 1, function(){
				$('#style-image-display').css({
					backgroundImage:'url('+url+')'
				});
			});
		}
		$('a.style-image').click(function(){
			var that = this;
			if ($('#style-image-display #fader').length) {
				$('#style-image-display #fader').queue(function(){fadeImage.call(that)})
			}
			else {
				fadeImage.call(that);
			}
			return false;
		});
		$('#comments a.popup').nyroModal({
			endShowContent: function(elm){
				$('button', $(elm.content)).click(function(){
		    		$(this).parents('form').ajaxSubmit({
		    			dataType: 'json',
		    			success:function(json){
			    			if (json) {
			    				$('#nyroModalContent').html(json.form);
			    			}
			    		}
		    		}).end().attr('disabled', true);
				});
			}
		});
		$('.comment-header .rating').showRate();
		$('.page-links a[href="#header"]').click(function(){
			$('html, body').animate({
				scrollTop: $("#header-navi").offset().top
			}, 2000);
			return false;
		})
		$('.recommendation>img, .recommendation>.presenter-info *').hide();
		$('.recommendation')
			.find('.recm-1')
				.children()
					.show()
				.end()
				.prev('img')
					.show()
				.end()
			.end()
			.find(':not(.recm-1) h3')
				.append('<a href="#" class="disp-sw linkbutton">内容を表示</a>')
				.show()
				.children('a').showRecm()
			.end();
	});
	jQuery(window).load(function(){
		$('#add-bookmark a').addBookmark();
		$('#owner-info>div,#salon-info>div').vCenter();
		$('#salon-info .westside').css({
			marginRight:$('#salon-info .eastside').width()+'px'
		});
		if ($('#about-salon img').length) {
			if ($('#about-salon img').height() > $('#about-salon .eastside').height()) {
				$('#about-salon img, #about-salon .eastside').vCenter();
			}
			$('#gradient-overlay').height($('#about-salon img').height()).css({
				display:'block', position:'absolute', top:(($('#about-salon img').css('marginTop') == 'auto' ? 0 : parseFloat($('#about-salon img').css('marginTop')))+parseFloat($('#about-salon img').position().top))+'px', left:($('#about-salon img').width()-100)+'px', zIndex:11
			});
			$('#about-salon .eastside').width(831-$('#about-salon>img').width());
		}
		$('#comment-tree-pane').height($('#double-column .eastside').height()-$('#comments .header').height());
		if ($.browser.msie && $.browser.version <= 7.0) {
			$('#wrapper').height($('#wrapper').height()).removeAttr('style');
		}
		var showmap = function(location){
			var salonmap = new GMap2($('#salon-map').get(0));
			salonmap.addControl(new GSmallMapControl());
			salonmap.addControl(new GMapTypeControl());
			salonmap.setCenter(location, 18);
			var marker = new GMarker(location);
			GEvent.addListener(marker, 'click', function(){
				marker.openInfoWindowHtml('<div style="color:#000;"><h3>'+$('#salon-info .westside h3').text()+'</h3><p>'+$('#access address').html()+'</p></div>');
			})
			salonmap.addOverlay(marker);
		},
		ll = $('#salon-map').attr('rel').split(','), geoCode, h = location.href;
		if (ll.length == 1) {
    		geoCode = function(address){
    			var geocoder = new GClientGeocoder();
    			geocoder.getLatLng(
    				address,
    				function(point){
    					if (point) {
    						showmap(point);
    					}
    				}
    			);
    		};
    		$.getJSON('/gateway/getAddress/'+h.substr(h.lastIndexOf('/')+1), function(json){
    			geoCode(json.address);
    		});
		}
		else {
			showmap(new GLatLng(parseFloat(ll[0]), parseFloat(ll[1])));
		}
	});
})()

