// コンテンツを画面中央に配置
$(window).bind('load resize', function(){
	var left = Math.floor( ($(window).width() - $("#container").width() ) / 2);
	var top= Math.floor( ($(window).height() - $("#container").height() ) / 2);
	$("#container").css({ "left": left, "top": top });
});



$(document).ready(function(){
	var lock, selected, itemW, actT, itemList, head, body, face, btn;

	// ロック解除
	lock = false;

	// 最初の選択アイテム種
	selected = 'head';
	
	// アイテム画像幅(アニメーション移動距離)
	itemW = 162;
	
	// アニメーションスピード
	actT = 250;
	
	// アイテム名の格納先配列
	itemList = [];

	/* ----------------------------------------------------------------------- */
	/* アイテム */
	
	// アイテムクラス
	function items(type){
	
		// 名前
		this.name = type;
		
		// 選択アイテムNo
		this.current = 1;
		
		// アイテム総数
		this.num = $('#' + this.name + ' .flipContent > *').length;
		
		// アイテムの親要素の幅をセット
		$('#' + this.name + ' .flipContent').css('width', this.num * itemW + 'px');
		
		// アイテムの親要素の幅(不使用？)
		this.w = $('#' + this.name + ' .flipContent').width();

		// アイテムリストに名前追加
		itemList[ itemList.length ] = this.name;
		
		return this;
	}
	
	// アニメーション処理
	items.prototype.itemMove = function(param){ $('#' + this.name + ' .flipContent').animate(param, actT); }
	
	// 選択アイテムNoを返す
	items.prototype.itemCheck = function(){ return this.current; }

	// アイテム生成
	head = new items('head');
	face = new items('face');
	body = new items('body');

	/* ----------------------------------------------------------------------- */
	/* フォーム送信 */
	
	function checkForm(){
	
		var bResult = false;
		
		var sTargetName = document.engine.site_name;
		var sTargetURL = document.engine.site_url;

		// 空欄チェック
		// サイト名
		if(sTargetName.value == ''){
			alert('ホームページの名前の入力をお願いいたします。');
			sTargetName.focus();
			return;
		}
		
		if(sTargetName.value.length > 40){
			alert('ホームページの名前は40文字まで入力可能です。');
			sTargetName.focus();
			return;
		}
		
		// サイトURL チェック項目：空欄、http://のみ、7文字以下
		if(sTargetURL.value == '' || sTargetURL.value == 'http://' || sTargetURL.value.length <= 7){
			alert('ホームページのURLの入力をお願いいたします。');
			sTargetURL.focus();
			return;
		}

		// フォーム送信
		document.engine.action = '/event/xmas2009/sub.php';
		document.engine.submit();
		
		/*
		alert(
		'Head:' + document.engine.head_id.value + ' | '
		+ 'Face:' + document.engine.face_id.value + ' | '
		+ 'Body:' + document.engine.body_id.value + ' | '
		+ 'SiteName:' + document.engine.site_name.value + ' | '
		+ 'URL:' + document.engine.site_url.value);
		*/
	}
	
	
	/* ----------------------------------------------------------------------- */
	/* 各ボタンのMouseOver,MouseOutイベント */
	
	$('.selectType .btnS').mouseover(function(){
		if($(this).css('background-position') == '0px -28px'){
			$(this).css('background-position', '0 -56px');
		}
	});
	
	$('.selectType .btnS').mouseout(function(){
		if($(this).css('background-position') == '0px -56px'){
			$(this).css('background-position', '0px -28px');
		}
	});
	
	$('.selectItem .btnS').mouseover(function(){
		if($(this).css('background-position') == '0px 0px'){
			$(this).css('background-position', '0 -56px');
		}
	});
	
	$('.selectItem .btnS').mouseout(function(){
		if($(this).css('background-position') == '0px -56px'){
			$(this).css('background-position', '0px 0px');
		}
	});
	
	$('.btnS').mouseover(function(){
		$(this).css({
			'cursor' : 'pointer'
		});
	});
	
	$('.btnM').mouseover(function(){
		$(this).css({
			'background-position' : '0px -45px',
			'cursor' : 'pointer'
		});
	});
	
	$('.btnM').mouseout(function(){
		$(this).css({
			'background-position' : '0px 0px'
		});
	});
	
	$('.btn').mouseover(function(){
		$(this).css({
			'cursor' : 'pointer'
		});
	});
	
	$('#btnsubmit').mouseover(function(){
		$(this).attr('src', '/event/xmas2009/images/parts/btn_nextstep2_on.jpg');
	});

	$('#btnsubmit').mouseout(function(){
		$(this).attr('src', '/event/xmas2009/images/parts/btn_nextstep2.jpg');
	});

	/* ----------------------------------------------------------------------- */
	/* 各ボタンのClickイベント */
	
	$('#btnprev').click(function(){ naviBtn.prevClick(selected, 'btnprev'); });
	$('#btnnext').click(function(){ naviBtn.nextClick(selected, 'btnnext'); });

	$('#btnhead').click(function(){ selected = 'head'; selectBtn.select('head'); });
	$('#btnface').click(function(){ selected = 'face'; selectBtn.select('face'); });
	$('#btnbody').click(function(){ selected = 'body'; selectBtn.select('body'); });

	// 選択アイテムNoをフォームに格納
	$('#btncheck').click(function(){
		var aResult = [];
		for(iCnt = 0; iCnt < itemList.length; iCnt ++){
			document.engine.elements[itemList[iCnt] + '_id'].value = eval(itemList[iCnt]).itemCheck();
		}
		$('#editDarumaNavi').css('display', 'none');
		$('#editDarumaForm').css('display', 'block');
		
		$('#leftCol #step').css('background-position', '0px -46px');
		$('#editComment img').attr('src', '/event/xmas2009/images/page/img_edit_comment2.gif');
	});

	$('#btnbackstep').click(function(){
		$('#editDarumaNavi').css('display', 'block');
		$('#editDarumaForm').css('display', 'none');
		
		$('#leftCol #step').css('background-position', '0px 0px');
		$('#editComment img').attr('src', '/event/xmas2009/images/page/img_edit_comment1.gif');
	});

	$('#btnsubmit').click(function(){
		checkForm();
	});

	$('#infoAboutClose').click(function(){
		$('#infoAbout').css('display', 'none');
	});

	$('#navi_about').click(function(){
		if($('#infoAbout').css('display') == 'none'){
			$('#infoAbout').css('display', 'block');
		}else{
			$('#infoAbout').css('display', 'none');
		}
	});

	/* ----------------------------------------------------------------------- */
	/* アイテム種類切替ボタン */

	selectBtn = {
		nameList : ['btnhead', 'btnface', 'btnbody'],
		
		// 選択時処理
		select : function(target){
		
			// id名生成用の接頭語
			var prefix = 'btn';
			
			// すべて未選択状態に
			this.clear();
			
			// ターゲットを選択状態に
			$('#' + prefix + target).css('background-position', '0px 0px');
			
			// 選択したアイテムの送りボタン制御
			// current、numをチェック
			var itemLength = eval(target).num;
			var itemCurrent = eval(target).current;

			// next、prevをenable
			for(i in naviBtn.nameList){
				naviBtn.enableClick(naviBtn.nameList[i]);
			}
			
			// current == 1ならprevをdisable
			if(itemCurrent == 1){
				naviBtn.disableClick('btnprev');
			}
			
			// currentがnumと同数ならnextをdisable
			if(itemCurrent == (itemLength)){
				naviBtn.disableClick('btnnext');
			}
		},
		
		// すべて未選択状態に
		clear : function(){
			for(i in this.nameList){
				$('#' + this.nameList[i]).css('background-position', '0 -28px');
			}
		}
	}
	
	/* ----------------------------------------------------------------------- */
	/* アイテム送りボタン */

	naviBtn = {
		nameList : ['btnprev', 'btnnext'],
		
		// ターゲットと対のid名を返す
		oppName : function(name){
			return (name == this.nameList[0])? this.nameList[1] : this.nameList[0];
		},
	
		// Prevボタンクリック時処理
		prevClick : function(target, name){
		
			// ロック中ならリターン
			if(lock) return;
			
			// 送るアイテムが無ければボタンをdesable
			if(eval(target).current <= 1){
				this.disableClick(name);
				return;
			
			// 送るアイテムがあれば逆(next)のボタンをenable
			}else{
				this.enableClick(this.oppName(name));
			}
			
			// 現在のアイテムのx座標
			pos = - (eval(target).current - 1) * itemW;
		
			// 移動先x座標
			param = { left: pos + itemW + 'px' };
			
			// 移動処理
			eval(target).itemMove(param);
			
			// 選択アイテムNoをマイナス
			eval(target).current -= 1;

			// ボタンをロック
			this.lockClick();

			// アイテム送り後のボタン処理
			// 送るアイテムが無ければボタンをdesable
			if(eval(target).current <= 1){
				this.disableClick(name);
				
			// 送るアイテムがあればボタンをenable
			}else{
				this.enableClick(name);
			}
		},

		// Nextボタンクリック時処理
		nextClick : function(target, name){

			// ロック中ならリターン
			if(lock) return;

			// 送るアイテムが無ければボタンをdesable
			if(eval(target).current >= eval(target).num){
				this.disableClick(name);
				return;

			// 送るアイテムがあれば逆(prev)のボタンをenable
			}else{
				this.enableClick(this.oppName(name));
			}
		
			// 現在のアイテムのx座標
			pos = - (eval(target).current - 1) * itemW;

			// 移動先x座標
			param = { left: pos - itemW + 'px' };

			// 移動処理
			eval(target).itemMove(param);

			// 選択アイテムNoをマイナス
			eval(target).current += 1;

			// ボタンをロック
			this.lockClick();

			// アイテム送り後のボタン処理
			// 送るアイテムが無ければボタンをdesable
			if(eval(target).current >= eval(target).num){
				this.disableClick(name);

			// 送るアイテムがあればボタンをenable
			}else{
				this.enableClick(name);
			}
		},
	
		// ボタンロック
		lockClick : function(){
			lock = true;
			setTimeout( this.unlockClick, actT + 50);
		},

		// ボタンロック解除
		unlockClick : function(){
			lock = false;
		},
		
		// ボタン使用不可時処理
		disableClick : function(name){
			$('#' + name).css('background-position', '0 -28px');
		},
		
		// ボタン使用可時処理
		enableClick : function(name){
			$('#' + name).css('background-position', '0px 0px');
			
		}
	}
});

$(window).load(function(){
	$('body').css('visibility', 'visible');
});

