$(document).ready(function()
{
	var qkpid = $.cookie('pid-val');
	if(qkpid){selectpid(qkpid,0);}
	$("#games-select").click(function(){selectallgames();});
	$("#server-select").click(function(){
		var qkgid = $('#games-val').val();	
		if(qkgid){selectgid(qkgid);}else{selectallgames();}
	});
	$('.btnqk').toggle(function()
	{
		$('.fastpurchase').show();
		$('#quick').addClass('martop45');	
	},function()
	{
		$('.fastpurchase').hide();	
		$('#quick').removeClass('martop45');
	})
	$('.onfocus').mouseover(function(){
		var imgsrc = $(this).attr('src');
		$(this).attr('src',imgsrc.replace('.','_on.'));
	}).mouseout(function(){
		var imgsrc = $(this).attr('src');
		$(this).attr('src',imgsrc.replace('_on.','.'));
	});	
	$('#semail').focus(function(){
		if($('#semail').val()=='お客様のメールアドレス'){
			$('#semail').val('');
		}
	}).blur(function(){
		if(!$('#semail').val()||$('#semail').val()=='お客様のメールアドレス'){
			$('#semail').val('お客様のメールアドレス');
		}
	});
	$('#index-send').click(function(){				
		if($('#scontent').val()&&$('#semail').val()){
			$.ajax({
				type: "post",
				url: url+'/Index/suggestion',
				data:{scontent:$('#scontent').val(),semail:$('#semail').val(),type:'ajax'},
				dataType: 'json',
				success: function(data){
					if(data!=1){
						alert(data);
					}else{
						$('.suggestion').css('display','none');
						alert('ごコメントをして頂きありがとう御座いました。');
					}
				}						
			})
		}
	});	
   $("#addfav").click(function(){
                var ctrl = (navigator.userAgent.toLowerCase()).indexOf('mac') != -1 ? 'Command/Cmd' : 'CTRL';  
                if (document.all) {  
                    window.external.addFavorite('http://www.rmt-mmo.com', 'RMT-MMO')  
                } else if (window.sidebar) {  
                    window.sidebar.addPanel('RMT-MMO', 'http://www.rmt-mmo.com', "")  
                } else {　
                    alert('追加失敗\n' + ctrl + ' + D をクリックしてお気に入りボックスへ')  
                }  
   });	
})
function refresh_code(event) {
	$('#'+event).attr('src', url+"/Base/verify/"+Math.random());
}
/*
 * Check the existence of members
 */
function checkName(){
	var email = $('#ue').val();
	if(email=='' || !(/^[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,5}$/i).test(email)) {
		alert('メールが間違っている');
		return false;
	} else {
		return true;
	}
}
/*
 * Check the password is correct
 */
function checkPwd(){
	var pwd = $('#up').val();
	if(pwd=='' || !(/^\S{6,16}$/i).test(pwd)) {
		alert('パスワードが間違っている');
		return false;
	} else {
		return true;
	}
}
/*
 * Asynchronous login
 */
function asyn_login() {
	if(checkName()==false || checkPwd()==false) { return false; }
}
/*
 * Asynchronous logout
 */
function asyn_logout() {
	window.location.href = url+'/Member/logout';
}
function texchange(node,val,event){
    		$('#'+node+'_dammy').parent().find('label').remove();
    		if(val=='取引方法を選択してください'||val=='取引場所を選択してください'){
    			$('#'+node+'_dammy').after('<label generated="true" class="error">* '+event+'</label>');
    		}else{
    			$('#'+node+'_dammy').after('<label generated="true" class="success"></label>');
    		}
}
function isnull(node,event){
	var val = jQuery.trim($("input[name="+node+"]").val());
	$("input[name="+node+"]").parent().find('label').remove();
	if(!val.length){
		$("input[name="+node+"]").after('<label generated="true" class="error notice"> '+event+'</label>');
	}else{
		$("input[name="+node+"]").after('<label generated="true" class="success notice"></label>');
	}
}
function isemail(node,event){
	$("input[name="+node+"]").parent().find('label').remove();
	var re = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
	var email = $("input[name="+node+"]").val();
    if(!re.test(email)){
    	$("input[name="+node+"]").after('<label generated="true" class="error notice"> *'+event+'</label>');
    }else{
   		$("input[name="+node+"]").after('<label generated="true" class="success notice"></label>');
    }
}
function valitotal(node){
	$("input[name="+node+"]").parent().find('label').remove();
	var re = /^[0-9]*[1-9][0-9]*$/;
	var val = $("input[name="+node+"]").val();
    if(!re.test(val)){
    	$("input[name="+node+"]").parent().append('<label generated="true" class="error"></label>');
    }else{
   		$("input[name="+node+"]").parent().append('<label generated="true" class="success"></label>');
    }	
}
function selectallgames(){
		$.ajax({
			type	: "POST",
			cache	: false,
			url		: url+"/Base/ajaxGameList",
			success: function(data) {
				$.fancybox(data);
			}
		});	
}
function selectgid(gid){
		$.ajax({
			type	: "POST",
			cache	: false,
			url		: url+"/Base/ajaxServer/gid/"+gid,
			success: function(data) {
				$.fancybox(data);
			}
		});	
}
function selectpid(pid,reload){
	//$.fancybox.close();
	$.cookie('pid-val', pid,{path: '/'});
	$.ajax({
		type	: "POST",
		dataType: 'json',
		url		: url+"/Base/ajaxProduct/pid/"+pid,
		success: function(data) {
			$('#games-select').val(data.gname);
			$('#server-select').val(data.pname);
			$('#top-stock').html(data.buystore);
			$('#top-price').html(data.price);
			$('#games-val').val(data.ggid);
			$('.qk_buy a').attr('href',data.url);
			if(reload){
			$.fancybox(data.html);
			}
		}
	});	
}
var flag=false;
function limitImage(ImgD){
    var areaWidth = 600;
    var areaHeight = 500;
    var image=new Image();
    image.src=ImgD.src;
    if(image.width>0 && image.height>0){
        flag=true;
        if(image.width/image.height>= areaWidth/areaHeight){
            if(image.width>areaWidth){
                ImgD.width=areaWidth;
                ImgD.height=(image.height*areaWidth)/image.width;
            }else{
                ImgD.width=image.width;
                ImgD.height=image.height;
            }
            ImgD.alt=image.width+""+image.height;
        }else{
            if(image.height>areaHeight){
                ImgD.height=areaHeight;
                ImgD.width=(image.width*areaHeight)/image.height;
            }else{
                ImgD.width=image.width;
                ImgD.height=image.height;
            }
            ImgD.alt=image.width+""+image.height;
        }
    }
}

