(function($){
	if ( window.location.search.indexOf('?') == -1 ){
		if ( window.navigator.appName.indexOf("Microsoft") != -1 ) {
			document.write('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="0" height="0" id="bgm_external" align="middle">\
<param name="allowScriptAccess" value="always" />\
<param name="movie" value="bgm.swf" />\
<param name="quality" value="high" />\
<param name="bgcolor" value="#ffffff" />\
</object>');
		} else {
			document.write('<embed src="bgm.swf" quality="high" bgcolor="#ffffff" width="0" height="0" name="bgm_external" align="middle" allowScriptAccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />');
		}
	}
	
	$(function(){
		if (window.location.search.indexOf('?') == -1){
			var h = $('body').outerHeight();
			var path = (window.location.pathname.indexOf(':') == -1) ? window.location.pathname : 'file://' + window.location.pathname;
			$('body').prepend('<iframe id="bgm_wrapper" src="'+path+'?bgm'+window.location.hash+'" style="position: absolute; top: 0; left: 0; width: 100%; height: 100%;" frameborder="0"></iframe>');
			$('body > div').remove();
			$('html,body').css('overflow-y', 'hidden');
		} else {
			$('a').click(function(){
				
				if ( !$(this).attr('target') || $(this).attr('target') == null) {
					var url = $(this).attr('href');
					if(url.indexOf('#') != -1){
						var path = url.split('#');
						url = path[0] + '?bgm#' + path[1];
					} else {
						url += '?bgm';
					}
					$(this).attr('href', url);
				}
				
				getParentFlash('bgm_external').playClickSound();
			}).mouseover(function(){
				getParentFlash('bgm_external').playMouseOverSound();
			});
			
			$('.fontResizer dd img').click(function(){
				getParentFlash('bgm_external').playClickSound();
			}).mouseover(function(){
				getParentFlash('bgm_external').playMouseOverSound();
			});
			
			$('.inline-box, .inline-blocks > div').exInlineBlock();
			
			$('#bgm_buttons img').click(function(){
				if ($(this).attr('src').match(/on\d+\.jpg$/)){
					getParentFlash('bgm_external').sound_on();
				} else {
					getParentFlash('bgm_external').sound_off();
				}
				if ($(this).attr('src').match(/01\.jpg$/)){
					$('#bgm_buttons img').each(function(){
						var src = $(this).attr('src');
						if(src.match(/01\.jpg$/)){
							$(this).attr('src', src.replace(/01\.jpg$/, '02.jpg'));
						} else if(src.match(/02\.jpg$/)){
							$(this).attr('src', src.replace(/02\.jpg$/, '01.jpg'));
						}
					});
				}
			});
			
			if (!getParentFlash('bgm_external').is_sound_on()){
				$('#bgm_buttons img:eq(1)').trigger('click');
			}
			
			$(document).pngFix();
		}
	});
	
	function getParentFlash(str) {
		if (window.navigator.appName.indexOf("Microsoft") != -1) {
			return parent.window[str];
		} else {
			return parent.document[str];
		}
	}
})(jQuery);
