/*
 * setting
 */
var animation_speed = 1000;
var switch_speed = 3000;
var timer;

/*
 * constract
 */
var now_key = 1;
var is_end = false;

$(function(){
	/*
	 * url setting
	 */
	var shop_info = new Array();
	shop_info['billiard_title'] = {
		'url':'http://www.bagus-99.com/billiard/index.html',
		'img':'./common/image/top_01-01.jpg',
	};
	shop_info['billiard'] = {
		'url':'http://www.bagus-99.com/billiard/index.html',
		'img':'./common/image/top_01-02.jpg',
	};
	shop_info['netcafe_title'] = {
		'url':'http://www.bagus-99.com/netcafe/index.html',
		'img':'./common/image/top_02-01.jpg',
	};
	shop_info['netcafe'] = {
		'url':'http://www.bagus-99.com/netcafe/index.html',
		'img':'./common/image/top_02-02.jpg',
	};
	shop_info['simulationgolf_title'] = {
		'url':'http://www.bagus-99.com/simulationgolf/index.html',
		'img':'./common/image/top_03-01.jpg',
	};
	shop_info['simulationgolf'] = {
		'url':'http://www.bagus-99.com/simulationgolf/index.html',
		'img':'./common/image/top_03-02.jpg',
	};
	shop_info['karaoke_title'] = {
		'url':'http://www.bagus-99.com/karaoke/index.html',
		'img':'./common/image/top_04-01.jpg',
	};
	shop_info['karaoke'] = {
		'url':'http://www.bagus-99.com/karaoke/index.html',
		'img':'./common/image/top_04-02.jpg',
	};
	shop_info['restaurant_title'] = {
		'url':'http://www.bagus-99.com/restaurant/index.html',
		'img':'./common/image/top_07-01.jpg',
	};
	shop_info['restaurant'] = {
		'url':'http://www.bagus-99.com/restaurant/index.html',
		'img':'./common/image/top_07-02.jpg',
	};
	shop_info['darts_title'] = {
		'url':'http://www.bagus-99.com/darts/index.html',
		'img':'./common/image/top_06-01.jpg',
	};
	shop_info['darts'] = {
		'url':'http://www.bagus-99.com/darts/index.html',
		'img':'./common/image/top_06-02.jpg',
	};
	shop_info['Bplace_title'] = {
		'url':'http://www.bagus-99.com/Bplace/index.html',
		'img':'./common/image/top_05-01.jpg',
	};
	shop_info['Bplace'] = {
		'url':'http://www.bagus-99.com/Bplace/index.html',
		'img':'./common/image/top_05-02.jpg',
	};
	shop_info['end'] = {
		'url':null,
		'img':'./common/image/top_08.jpg',
	};

	/*
	 * start
	 */
	setImage();

	/*
	 * function
	 */
	function setImage(){
		var promise = new Array();

		for(var i in shop_info){
			var main_img = $(document.createElement('img')).attr({'src':shop_info[i].img}).addClass(i);
			$('#parts').append(main_img);

			promise[i] = $.Deferred();
			$(main_img).load(function(){
				promise[$(this).attr('class')].resolve();
			});
		}
		$.when(
			promise['billiard'].promise(),
			promise['netcafe'].promise(),
			promise['simulationgolf'].promise(),
			promise['karaoke'].promise(),
			promise['darts'].promise(),
			promise['Bplace'].promise(),
			promise['end'].promise()
		).then(function(ajaxResArgs){
			for(var i in shop_info){
				now_key = i;
				break;
			}
			$('#switch_img_link').attr({'href':getUrl(now_key)});
			$('#main_img').html(getImage(now_key));

			setNextImage();
			hoverImage();
			dispSubMenu();
		},function(){
			msg('failed load images');
		});
	}
	function setNextImage(){
		timer = setInterval(function(){
			if(shop_info[now_key].url){
				var tmp_shop_info = shop_info;
				var tmp_now_key = now_key;
				var tmp_now_val = shop_info[now_key];
				shop_info = new Array();
				
				var is_fst = true;
				for(var i in tmp_shop_info){
					if(is_fst){
						is_fst = false;
						continue;
					}
					shop_info[i] = tmp_shop_info[i];
				}
//				delete shop_info[now_key];
//				shop_info.shift();
				shop_info[tmp_now_key] = tmp_now_val;
				for(var i in shop_info){
					now_key = i;
					break;
				}
			}else{
				is_end = true;
			}

			switchImage();
		}, (animation_speed)+switch_speed);
	}
	function unsetNextImage(){
		clearInterval(timer);
	}
	function switchImage(key){
		if(!key && is_end) return;
		$('#next_img').empty().show().html($('#main_img').html());

		if(!key) key = now_key;

		$('#main_img').html(getImage(key));

		$('#next_img').fadeOut(animation_speed);
	}
	function hoverImage(){
		$('#switch_img_menu .link, #switch_img_sub_menu .link').hover(
			function(){
				unsetNextImage();
				switchImage($(this).attr('id'));
			},
			function(){
				if(is_end){
					timer = setTimeout(function(){
						switchImage(now_key);
					}, (animation_speed)+switch_speed);
					return;
				}
				setNextImage();
			}
		);
	}
/*
	function dispSubMenu(){
		$('#switch_img_menu .sub').click(function(){
			var ele = $('#'+$(this).attr('id')+'_sub');
			if('hidden' == $(ele).css('visibility')){
				$(ele).css({'visibility':'visible'});
			}else{
				$(ele).css({'visibility':'hidden'});
			}
		});
	}
*/
	function dispSubMenu(){
		$('#switch_img_menu .sub').hover(
			function(){
				var ele = $('#'+$(this).attr('id')+'_sub');
				$(ele).css({'visibility':'visible'});
			},
			function(){
				var ele = $('#'+$(this).attr('id')+'_sub');
				$(ele).css({'visibility':'hidden'});
			}
		);
		$('#switch_img_sub_menu > div').hover(
			function(){
				$(this).css({'visibility':'visible'});
			},
			function(){
				$(this).css({'visibility':'hidden'});
			}
		);
		$('#switch_img_sub_menu a').hover(
			function(){
				$(this).css({'opacity':'0.7'});
			},
			function(){
				$(this).css({'opacity':'1'});
			}
		);
	}
	function getImage(class_name){
		return $('#parts .'+class_name).clone(false);
	}
	function getUrl(class_name){
		return shop_info[class_name].url;
	}
});
