/**
 * Filename: adachi.js
 * Website: Adachi Hospital
 * Required: jQuery
 *
 * Author: Kosuke Nakatani
 * Licensed under the MIT License: http://www.opensource.org/licenses/mit-license.php
 *	
*/

jQuery(function() {
	// Variable
	var _messageHtml = '<div id="message">'
						+ '<img src="/imgs/message.gif" alt="" width="346" height="387" />'
						+ '</div>';
	var _backgroundHtml = '<div id="background">'
					+ '<img src="/imgs/background01.jpg" alt="" width="900" height="750" />'
					+ '<img src="/imgs/background02.jpg" alt="" width="900" height="750" />'
					+ '<img src="/imgs/background03.jpg" alt="" width="900" height="750" />'
					+ '</div>';
	var _bgContentsHtml = '<div class="bgContents"></div>';
	var _loaderHtml = '<div id="loader"><img src="/imgs/loader.gif" alt="" width="64" height="64" /></div>';
	var _posArray = new Array();
	var _contentsHeight = 0;
	var _total = jQuery('.contents').size() - 1;

	// Change Background
	function changeBackground() {
		var _bgImg = jQuery('#background img'),
			_current = 0,
			_time = 2000,
			_delay = 5000,
			_total = _bgImg.size() - 1;
				
		function loop() {
			getCurrent();
			_bgImg.eq(_current).delay(_delay).fadeIn(_time, function() {
				_bgImg.eq(_current - 1).hide();
				loop();
			});
		}
		function getCurrent() {
			if (_current >= _total) _current = -1;
			_current++;
			_bgImg.eq(_current - 1).css('z-index','0');
			_bgImg.eq(_current).css('z-index','5');
		}

		_bgImg.hide();
		_bgImg.eq(_current).show();
		_bgImg.eq(_current).delay(_delay).fadeIn(_time, loop);
	}
	
	// contentsSlider
	function contentsSlider() {
		var _controller = jQuery('#controller a'),
			_bgContents = jQuery('.bgContents'),
			_innerContents = jQuery('.innerContents'),
			_current = 0,
			_time = 300;
		
		// Click Handler
		_controller.click(function() {
			var _id = jQuery(this).attr('id');
			
			if (_id == "next") {
				if (_current != _total) {
					_current++;
					jQuery('#mainContents').stop().animate({
						'marginLeft': _posArray[_current]
					}, _time, function() {
						onActiveHandler(_current);
					});
				}
			} else {
				if(_current != 0) {
					_current--;
					jQuery('#mainContents').stop().animate({
						'marginLeft': _posArray[_current]
					}, _time, function() {
						onActiveHandler(_current);
					});
				}
			}
			
			function onActiveHandler(_current) {
				if (!jQuery.support.opacity) {
					_bgContents.css('visibility', 'hidden').eq(_current).css('visibility', 'visible');
					_innerContents.css('visibility', 'hidden').eq(_current).css('visibility', 'visible');
				} else {
					_bgContents.css('visibility', 'hidden').eq(_current).fadeTo(0, 0).css('visibility', 'visible').fadeTo(_time, 1);
					_innerContents.css('visibility', 'hidden').eq(_current).fadeTo(0, 0).css('visibility', 'visible').fadeTo(_time, 1);
				}
			}
			
			return false;
		});
		
		// RollOver Handler
		jQuery('.contents').hover(
			function() {
				var _index = jQuery(this).index();
				if (_current != _index) {
					if(!jQuery.support.opacity) {
						_bgContents.eq(_index).css("visibility", "visible");
						_innerContents.eq(_index).css("visibility", "visible");
					} else {
						_bgContents
							.eq(_index)
							.css("visibility", "visible")
							.fadeTo(0, 0)
							.animate({
								'opacity': 1
							},
							{
								'duration': _time,
								'queue': false
							});
						_innerContents
							.eq(_index)
							.css("visibility", "visible")
							.fadeTo(0, 0)
							.animate({
								'opacity': 1
							},
							{
								'duration': _time,
								'queue': false
							});
					}
				}
			},
			function() {
				var _index = jQuery(this).index();
				if (_current != _index) {
					if(!jQuery.support.opacity) {
						_bgContents.eq(_index).css("visibility", "hidden");
						_innerContents.eq(_index).css("visibility", "hidden");
					} else {
						_bgContents
							.eq(_index)
							.animate({
								'opacity': 0
							},
							{
								'duration': _time,
								'queue': false
							}, function() {
								jQuery(this).css("visibility", "hidden");
							});
						_innerContents
							.eq(_index)
							.animate({
								'opacity': 0
							},
							{
								'duration': _time,
								'queue': false
							}, function() {
								jQuery(this).css("visibility", "hidden");
							});
					}
				}
			}
		);
	}
	
	// Resize Handler
	function onResizeHandler() {
		var _clientHeight = document.documentElement.clientHeight,
			_setHeight = 0,
			_zoomX = jQuery(window).width() / 1200,
			_zoomY = jQuery(window).height() / 750,
			_zoom = Math.max(_zoomX, _zoomY);
		scaleImage(_zoom);
		
		function scaleImage(_zoom) {
			jQuery("#background img").css({
				width: _zoom * 1200,
				height: _zoom * 750
			});
		}
		
 		// Height
 		if (_contentsHeight > _clientHeight) {
 			var _bgiHeight = jQuery("#background img").height();
 			jQuery('html').css('overflowY', 'auto');
 			if (_bgiHeight > _contentsHeight) {
 				_setHeight = _bgiHeight;
	 		} else {
	 			_setHeight = _contentsHeight;
	 		}
	 		jQuery('#mainContents').css('height', _setHeight);
			if(!jQuery.support.opacity && !jQuery.support.style && typeof document.documentElement.style.maxHeight == "undefined") {
				jQuery(window).scroll(function() {
				// Footer Fixed for IE6
					var	_posScrollY = jQuery(document).scrollTop();
					var	_posY = (_clientHeight + _posScrollY) - 45;
					if (_posY < _setHeight) {
						jQuery('#footer').css('top', _posY);
					}
				});
			}
		
			// Footer Fixed for IE6
			if(!jQuery.support.opacity && !jQuery.support.style && typeof document.documentElement.style.maxHeight == "undefined") {
				jQuery('#footer').css('top', _clientHeight - 45);
			}
 		} else {
 			jQuery('html').css('overflowY', 'hidden');
 			jQuery('#mainContents').css('height', _clientHeight);
 			
			// Footer Fixed for IE6
			if(!jQuery.support.opacity && !jQuery.support.style && typeof document.documentElement.style.maxHeight == "undefined") {
				var _posY = _clientHeight - 45;
				jQuery('#footer').css('top', _posY);
			}
 		}

	}	
	jQuery(window).bind('resize', function() {
		var _timer = null;
		if (_timer) clearTimeout(_timer);
		_timer = setTimeout(onResizeHandler, 10);
	});
	
	
	// Sound Modern Player
	function modernPlayer(_soundUrl) {
		var _soundObj = new Audio(_soundUrl);
		var _soundFlg = true;
		
		jQuery('#sound a').click(function() {
			var _id = jQuery(this).attr('id');
			if (_id == "soundPlay") {
				if (!_soundFlg) {
					_soundFlg = true;
					btnSound(_soundFlg)
					_soundObj = new Audio(_soundUrl);
					_soundObj.play();
				}
			} else {
			_soundFlg = false;
				btnSound(_soundFlg)
				_soundObj.pause();
			}
			
			return false;
		});
		btnSound(_soundFlg)
		_soundObj.play();
	}
	// Sound Analog(IE) Player
	function analogPlayer(_soundUrl) {
		var _player = '';
		var _soundFlg = true;
		
		jQuery('#sound a').click(function() {
			var _id = jQuery(this).attr('id');
			if (_id == "soundPlay") {
				if (!_soundFlg) {
					_soundFlg = true;
					btnSound(_soundFlg)
					makePlayer();
				}
			} else {
				_soundFlg = false;
				btnSound(_soundFlg)
				jQuery('bgsound').remove();
				_player = '';
			}
			return false;
		});
		
		function makePlayer() {
			_player = jQuery('<bgsound/>').attr({
				src: _soundUrl,
				loop: 1,
				autostart: true
			});
			jQuery('body').append(_player)
		}
		
		btnSound(_soundFlg)
		makePlayer();
	}
	function btnSound(_soundFlg) {
		if (_soundFlg) {
			jQuery('#soundPlay').hide()
			jQuery('#soundStop').show();
		} else {
			jQuery('#soundPlay').show()
			jQuery('#soundStop').hide();
		}
	}
	
	// Initialize
	function init() {
		// Add Html
		jQuery('body').append(_loaderHtml);
		jQuery('#mainContents').prepend(_messageHtml);
		jQuery('body').prepend(_backgroundHtml);
		jQuery('.contents').each(function() {
			jQuery(this).prepend(_bgContentsHtml);
			var _marginLeft = 240 - jQuery(this).css('marginLeft').replace("px","");
			_posArray.push(_marginLeft + 'px');
		});
		
		// Get Max Height
		jQuery('.innerContents').each(function() {
			var _thisHeight = jQuery(this).height() + 100;
			if (_contentsHeight < _thisHeight) _contentsHeight = _thisHeight;
		})
		
		// Hidden
		jQuery('#background, #mainContents, #controller').css('visibility', 'hidden');
		onResizeHandler();
	}
	init();
	
	// onLoadHanlder
	jQuery(window).load(function() {
		jQuery('#loader').remove();
			jQuery('#message')
				.fadeTo(0, 0)
				.css('visibility', 'visible')
				.fadeTo(500, 1)
				.delay(2000)
				.fadeTo(500, 0, showStart);
		
		// Show Start
		function showStart() {
			jQuery('#message').fadeOut(500, function() {
				jQuery(this).remove();
				jQuery('.bgContents').css('visibility', 'visible');
				if(!jQuery.support.opacity) {
					jQuery('#mainContents, #controller, #sound').css('visibility', 'visible');
				} else {
					jQuery('#mainContents, #controller, #sound')
						.fadeTo(0, 0)
						.css('visibility', 'visible')
						.fadeTo(500, 1);
				}
				changeBackground();
				jQuery('#background')
					.fadeTo(0, 0)
					.css('visibility', 'visible')
					.fadeTo(500, 1, function() {
						jQuery(this).delay(5000).show(0, function() {
							if(!jQuery.support.opacity) {
								jQuery('.bgContents').css('visibility', 'hidden').eq(0).css('visibility', 'visible');
								jQuery('.innerContents').css('visibility', 'hidden').eq(0).css('visibility', 'visible');
							} else {
								jQuery('.contents').each(function(e) {
									console.log(e);
									if (e != 0) {
										jQuery('.bgContents').eq(e).animate({
											'opacity': 0
										}, 300, function() {
											jQuery(this).css("visibility", "hidden");
										});
										jQuery('.innerContents').eq(e).animate({
											'opacity': 0
										}, 300, function() {
											jQuery(this).css("visibility", "hidden");
										});
									}
								});
							}
							
							// Sound Start
							if (!jQuery.support.checkOn) { // Chrome
								var _soundUrl = '/sound/piano.mp3';
								modernPlayer(_soundUrl);
							} else if (jQuery.support.checkOn && jQuery.support.noCloneEvent) { // FF, Opera
								var _soundUrl = '/sound/piano.ogg';
								modernPlayer(_soundUrl);
							} else if (!jQuery.support.noCloneEvent && jQuery.support.opacity) {
								alert("IE9");
								var _soundUrl = '/sound/piano.mp3';
								modernPlayer(_soundUrl);
							} else if (!jQuery.support.opacity) {
								var _soundUrl = '/sound/piano.mp3';
								analogPlayer(_soundUrl);
							} else {}
				
							contentsSlider();
						});
				});
			});
		}
	});
});
