$('body').attr('id', 'js')


$(function() {
	var initMenu = function(i, num) {
		var img = $("#menu"+i)
		var org = img.attr('src')
		num = num || (i<10 ? '0' : '')+i
		var hover = 'images/icons_act_'+num+'.png'
		img.hover(
			function() { img.attr('src', hover) },
			function() { img.attr('src', org) }
		)
		$("#apDiv"+(i-1)).hover(
			function() { img.mouseover() },
			function() { img.mouseout() }
		)
		var preload = new Image
		preload.src = hover
	}
	for(var i = 2; i < 13; i++) initMenu(i)
	initMenu(13, 14)

	$("a[rel^='prettyPhoto']").prettyPhoto({theme:'facebook'});

	// rmenu
	$('.b-rmenu').each(function(i, rmenu) {
		zIndex = 200
		var items = $('.b-rmenu-item', rmenu).each(function(i, item) {
			var nContent = $($(this).attr('rel'))
			var timeout = null
			var move = function() {
				o = $(item).offset()
				o.left -= $('#allwrap').offset().left 
				
				var left = o.left- (nContent.outerWidth()/2) + ($(item).width()/2)
				var top = o.top
				left = Math.min(left, $(window).width()-nContent.width()-5)

				nContent.css({left: left+'px', top: top+'px', marginLeft: 0})

				nContent.find('img.b-rmenu-content-bg').css({
					marginLeft: o.left - left + $(item).width()/2 - 10
				})
			}
			var hide = function() {
				timeout = setTimeout(function() {
					timeout = null
					nContent.stop().animate({opacity: 0}, 'fast', function() {
						nContent.hide()
					})
				}, 100)
			}

			$(item).click(function() { return false }).hover(function() {
				if (timeout) return clearTimeout(timeout)
				nContent.css({opacity: 0}).show()
				move()
				nContent.css({ zIndex: zIndex++ })
				nContent.stop().animate({opacity: 1}, 'fast')
			}, hide)

			nContent.hover(function() {
				if (timeout) return clearTimeout(timeout)
			}, hide)
			$(window).resize(move)
		})
	})

	// for debug:
	//$(document).toggle(function() { items.css('opacity', 0.5) }, function() { items.css('opacity', 1) })

	$('.b-static').each(function(i, node) {
		var top = $(node).offset().top
		var height = $(window).height()

		var update = function() {
			$(node).stop().animate({top: $(window).height()-height+top+$(window).scrollTop()+'px'}, 'fast')
		}

		$(window).scroll(update).resize(update)
		update()

		var scrollNode
		var getScrollNode = function(last) {
			if (scrollNode) return scrollNode

			if ($('html').scrollTop()) return scrollNode = $('html')
			if ($('body').scrollTop()) return scrollNode = $('body')

			$('html,body').scrollTop(1)
			if (!last) {
				scrollNode = getScrollNode(true)
				$('html,body').scrollTop(0)
			}
			return scrollNode
		}
		
		var getScrollTop = function() {
			var scrollTop = 0
			$('html,body').each(function() {
				scrollTop = Math.max(scrollTop, $(this).scrollTop())
			})
			return scrollTop
		}
			

		$('.b-static-up', node).click(function() {
			if (getScrollTop()) getScrollNode().stop().animate({scrollTop: 0}, 'fast')
			return false
		})

		$('.b-static-down', node).click(function() {
			getScrollNode().stop().animate({scrollTop: '+='+$(window).height()}, 'fast')
			return false
		})

		$('.b-static-music', node).each(function(i, button) {
			// preload 
			$(new Image).attr('src', '/images/static/music_on_active.png')
			$(new Image).attr('src', '/images/static/music_on_passive.png')

			var nPassive = $('.b-static-passive img', button)
			var nActive = $('.b-static-active img', button)
			
			var sound

			soundManager.url = '/soundManager/swf/'
			soundManager.onready(function() {
				if (!soundManager.supported()) {
					top += $(button).height()
					$(button).hide()
					update()
					return
				}
				sound = soundManager.createSound({
					id: 'mySound',
					url: '/soundManager/r.mp3',
					autoLoad: false,
					autoPlay: false,
					loops: 0,
					onload: function() {  },
					volume: 50
				})

				var play = function() {
					$.cookie('musicoff', '', {path: '/'})
					nActive.attr('src', '/images/static/music_on_active.png')
					nPassive.attr('src', '/images/static/music_on_passive.png')
	
					if (sound) return sound.play()
				}
	
				var pause = function() {
					$.cookie('musicoff', 'yes'	, {path: '/'})
					nActive.attr('src', '/images/static/music_off_active.png')
					nPassive.attr('src', '/images/static/music_off_passive.png')
					if (sound) sound.pause()
				}

				if (!$.cookie('musicoff')) play()

				$(button).click(function() {
					$.cookie('musicoff') ? play() : pause()
					return false
				})
			})
		})
	})
 });
