$(document).ready(function()
{
	$('#login_area').load("/loginArea/time_"+Math.random(),function()
	{
		$('form').live("keypress",function(event){
		if(event.keyCode == 13){
			var nx, me = event.target;
			if(me.type == 'text' || me.type == 'password' || me.type == 'checkbox'){
				var i = 0;
				if(!this.key13first){
					var index = 0, stack = [];
					while(i < this.elements.length){
						nx = this.elements[i++];
						if(nx.tabIndex){
							if(nx.tabIndex == 1)this.firstElement = nx;
							if(index < nx.tabIndex)index = nx.tabIndex;
						}else{
							stack.push(nx);
						}
					}
					while(nx = stack.shift())nx.tabIndex = ++index;
					if(!this.firstElement)this.firstElement = this.elements[0];

					i = 0;
					this.key13first = 1;
				}
				while(i < this.elements.length){
					nx = this.elements[i++];
					if(nx.tabIndex == me.tabIndex + 1){
						nx.type == 'button' || nx.type == 'submit' || nx.type == 'image' ? nx.click() : nx.focus();
						break;
					}
				}
				if(i >= this.elements.length)this.firstElement.focus();
			}
		}
	});
		$("#nav_login_btn").css("cursor","pointer").live("click",function()
		{
				var errmsg = '';
				if($("input[name=nav_email]").val()==''){
					errmsg += 'メールアドレスを入力してください\n';
				}
				if($("input[name=nav_password]").val()==''){
					errmsg += 'パスワードを入力してください\n';
				}
				if(errmsg != '')
				{
					alert(errmsg);
				}else{
					var options = {
						url:'/loginArea_doLogin',
						type:'POST',
						dataType:'json',
						success: function(data){
							if(data.err)
							{
								alert(data.err);
							}else{
								//$('#login_area').empty();
								//$('#login_area').load("/loginArea");
								window.location.reload();
							}
						}
					};
					$('#nav_login_form').ajaxSubmit(options);
				}
		 });

		 $("#nav_register_btn").css("cursor","pointer").live("click",function()
		 {
			 window.open("/member/register");
		 });

	});

	$.getJSON("/ajaxGetNav_GameList/time_"+Math.random(), null, function(items){initOptions($('#nav_game_list'), items)});
	$('#nav_game_list').change(function(){
		var value = parseInt($(this).val()), select = $('#nav_partition_list'),
			cache = select.data('cache') || {}, items = value ? cache[value] : [];
		initAllValues();
		items ? initOptions(select, items) :
			$.getJSON("/ajaxGetNav_PartitionList/gid_" + value + "/time_"+Math.random(), null, function(items){
				cache[value] = items;
				select.data('cache', cache);
				initOptions(select, items);
		});

	});
	$('#nav_partition_list').change(function(){
		var key = $('#nav_game_list').val() + '/' + $(this).val(),
			select = $(this), cache = select.data('cache') || {}, $rs = cache[key];
		!this.selectedIndex || $rs ? initAllValues($rs) :
			$.getJSON("/ajaxGetNav_QuoteTotal/gid_"+$("#nav_game_list").val()+"/pid_"+$(this).val()+"/time_"+Math.random(), null, function($rs){
				cache[key] = $rs;
				select.data('cache', cache);
				initAllValues($rs);
			});
	});
	function initOptions(select, items){
		var i = 0, select = select[0];
		if(select){
			select.options.length = 1;
			while(i < items.length)select.options[i + 1] = new Option(items[i].title || items[i].fulltitle, items[i++].id);
		}
	}
	function initAllValues($rs){
		$rs = $rs || {};
		var quote = $rs.quote, total = $rs.total;
		$('#nav_quote').html('<ul>'
				+ '<li>銀行振込/コンピニ<br />' + (quote ? quote[0].price : 0) + '円</li>'
				+ '<li>クレジットカード<br />' + (quote ? quote[1].price : 0) + '円</li>'
				+ '<li>WebMoney<br />' + (quote ? quote[5].price : 0) + 'WM</li>'
			+ '</ul>'
			+ '<div class="clear"></div>');
		$('#nav_total').html('在庫口数<br />' + (total ? total.front_stock : 0) + '口');
	}

	$('#btn_buy').bind("click",function()
	{
		if($('#nav_game_list').val() > 0 && $('#nav_partition_list').val() > 0)
		{
			window.open("/buy/id_"+$('#nav_game_list').val()+"/pid_"+$('#nav_partition_list').val());
		}
	});

	//$('#game_list_left').load("/ajaxGetGameListLeft/time_"+Math.random());
	$('#game_list_bot').load("/ajaxGetGameListBot/time_"+Math.random());
});


function addfavorite(url, title){
	try{
		window.external.addfavorite(url, title);
	}catch (e){
		try{
			window.sidebar.addPanel(title, url, '');
		}catch (e){
			alert('ブラウザがそっぽをご使用くださいctrl + d收藏!');
		}
	}
}
