(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(){
						$('#contents-bg').height($('#contents').height());
					});
				}, function(){
					$(this).parents('.presenter-info').children(':not(h3)').hide('slide').end().prev('img').hide('slide', function(){
						$('#contents-bg').height($('#contents').height());
					});
				});
			});
		}
	});
	jQuery(function($){
		$('#header-main').insertAfter('#footer .page-links');
		$.getJSON('/gateway/is_campaign', function(json){
			if (!$('body').is('.campaign-page') && (json.campaign || location.search.match(/check/))) {
				$('#header .main-navigations #salon-link').replaceWith('<li id="campaign-link" ><a href="/campaign" title="普及キャンペーン指定サロン">普及キャンペーン指定サロン</a></li>');
				$('#footer .main-navigations #footer-salon-link').replaceWith('<li id="footer-campaign-link" ><a href="/campaign" title="普及キャンペーン指定サロン">普及キャンペーン指定サロン</a></li>');
		        $.getJSON('/campaign/linkcheck', function(json){
		            if (!json.check) {
						$('#area-link,#back-link,#footer .page-links li:first-child').remove();
		            }
		        });
			
			}
		});
		$('#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();
		$('#footer .page-links a[href="#header"]').click(function(){
			$('html, body').animate({
				scrollTop: $("#header").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(){
		var content = $('#contents'),
		    contentHeight = content.height(),
			contentPosition = content.offset(),
		    contentsBg = $('<div id="contents-bg"/>').height(contentHeight);

		content.css({position:'absolute'}).after(contentsBg).css({
			left:contentPosition.left,
			top:contentPosition.top,
			zIndex:10
		});
		$('<div id="bgscreen"/>').width(939).height(contentHeight).css({
			position:'absolute',
			left:parseInt(contentPosition.left)+39+'px',
			top:contentPosition.top,
			background:'#000000',
			zIndex:5
		}).appendTo('#contents-bg').fadeTo(0, 0.8);
		$('<div id="map-japan"/>').appendTo('#contents-bg');
		swfobject.embedSWF('/images/japan.swf', 'map-japan', '288', '302', '10.0.0', '/images/expressInstall.swf', null, {wmode:'transparent'});
		$('#map-japan').css({position:'absolute',zIndex:6});
		$('#owner-info>div,#salon-info>div').vCenter();
		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());
		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])));
		}
/*
		$('#salon-image-loader').css({
			width:$('#about-salon').width(),
			height:$('#about-salon').height(),
			position:'absolute',
			top:$('#about-salon').position().top,
			left:$('#about-salon').position().left
		});
		swfobject.embedSWF('/images/salonImageLoader.swf', 'loader-movie', '850', '285', '10.0.0', '/images/expressInstall.swf');
*/
	});
})()
