﻿function loadJsCssFile(fileName,fileType,where){
	var fileRef;
	if(fileType=="js"){
		fileRef = document.createElement("script");
		fileRef.setAttribute("type","text/javascript");
		fileRef.setAttribute("src",fileName);	
	}	
	else if(fileType=="css"){
		fileRef.document.createElement("link");
		fileRef.setAttribute("rel","stylesheet");
		fileRef.setAttribute("type","text/css");
		fileRef.setAttribute("href",fileName);	
	}
	if(typeof fileRef!="undefined"){
		if(where=="head"){
			document.getElementsByTagName("head")[0].appendChild(fileRef);	
		}else{
			document.getElementsByTagName("body")[0].appendChild(fileRef);	
		}
	}
}

$(document).ready(function(){
	if(window.location != 'http://www.bygamer.com/')
		getGames();
	DaoHangCss();
	
	initButtonStyle();//初始按钮,调用该方法
	
	if(document.getElementById("looyuJs")){
		loadJsCssFile("http://newpower.looyu.com/j.jsp?c=35810&f=82471","js","head");	
	}
});

function initButtonStyle(){
	$(".btn3_mouseout")
		.hover(
			function(){$(this).addClass("btn3_mouseover")},
			function(){$(this).removeClass("btn3_mouseover")}
		)
		.focus(function(){this.blur();});
	$(".btn4_mouseout")
		.hover(
			function(){$(this).addClass("btn4_mouseover")},
			function(){$(this).removeClass("btn4_mouseover")}
		)
		.focus(function(){this.blur();});
	$(".btn5_mouseout")
		.hover(
			function(){$(this).addClass("btn5_mouseover")},
			function(){$(this).removeClass("btn5_mouseover")}
		)
		.focus(function(){this.blur();});
	$(".btn6_mouseout")
		.hover(
			function(){$(this).addClass("btn6_mouseover")},
			function(){$(this).removeClass("btn6_mouseover")}
		)
		.focus(function(){this.blur();});
	$(".btn0_mouseout")
		.hover(
			function(){$(this).addClass("btn0_mouseover")},
			function(){}
		)
		.focus(function(){this.blur();});
}

function DaoHangCss(){
	var s = location.href.toLowerCase();
	var flag = 0;
	var path = ["buy-gold","buy-powerleveling","buy-item","buy-cdkey","faq","contactus","aboutus","trackorder"];
	var data = ["Currency","Powerleveing","Item","CDKey","faq","ContactUs","AboutUs","TrackOrder"];
	var _class = ["hoverCurrency","hoverPowerleveing","hoverItem","hoverCDKey","hoverHelpCenter","hoverContactUs","hoverAboutUs","hoverTrackOrder"];
	for(var i=0; i<path.length; i++){
		if(s.indexOf(path[i])>0){
			$("#"+data[i]).addClass(_class[i]);	
			flag=1;
			break;
		}
	}
	if(flag==0){
		$("#Home").addClass("hoverHome");
	}
}

function getGames(){
	$.get("/ajax/common.cfm?action=games",function(data){
		success("headGame",data,"Select Game");
		if(document.getElementById("quickGame"))
			success("quickGame",data,"select the game");
	});	
}

function getServers(gid){
	if(gid == "0"){
		clear("headServer"," All Servers ");	
		return;
	}
	
	$.ajax({
		type: "GET",
		url: "/ajax/common.cfm",
		data: "action=servers&gid="+gid,
		beforeSend: function(){
			loading("headServer","loading...");
		},
		success: function(data){
			success("headServer",data," All Servers ");
		}
	});
}

function getQuickServers(gid){
	clear("quickProduct","select the product");
	if(gid == "0"){
		clear("quickServer","select the server");	
		return;
	}
	
	$.ajax({
		type: "GET",
		url: "/ajax/common.cfm",
		data: "action=servers&gid="+gid,
		beforeSend: function(){
			loading("quickServer","loading...");
		},
		success: function(data){
			success("quickServer",data,"select the server");
		}
	});
}

function getSpecialServers(gid){
	if(gid == "0"){
		clear("specialServers","-- Please Choose Your Game --");	
		return;
	}
	
	$.ajax({
		type: "GET",
		url: "/ajax/common.cfm",
		data: "action=servers&gid="+gid,
		beforeSend: function(){
			//loading("productType","loading...");
		},
		success: function(data){
			success("specialServers",data,"-- Please Choose Your Game --");
			showFastOrder(gid,$("#ptype").val());
		}
	});
}

function getProduct(f){
	var gid = parseInt(f.quickGame.value);
	var sid = parseInt(f.quickServer.value);
	
	if(!gid || !sid){
		clear("quickProduct","select the product");	
		return;
	}
	
	$.ajax({
		type: "GET",
		url: "/ajax/common.cfm",
		data: "action=products&gid="+gid+"&sid="+sid,
		beforeSend: function(){
			loading("quickProduct","loading...");
		},
		success: function(data){
			var dom = document.getElementById("quickProduct");
			while(dom.options.length > 0){
				dom.remove(0);
			}
			var jStr = eval("(" + data + ")");
			var opt = document.createElement("OPTION");
			opt.text = "select the product";
			opt.value = "0";
			dom.options.add(opt);
			opt.selected = true;
			
			if(jStr != null){
				var opt = null;
				$.each(jStr.DATA, function(i,n){
					opt = document.createElement("OPTION");
					opt.value = n[0];
					opt.text = opt.title = n[1] + " ($" + n[2].toFixed(2) + ")";
					dom.options.add(opt);   
				});
			}
		}
	});
}

function success(id,msg,s){
	
	var dom = document.getElementById(id);
	while(dom.options.length > 0){
		dom.remove(0);
	}
	var jStr = eval("(" + msg + ")");
	var opt = document.createElement("OPTION");
	opt.text = s;
	opt.value = "0";
	dom.options.add(opt);
	opt.selected = true;
	
	if(jStr != null){
		var opt = null;
		var count = 0;
		$.each(jStr.DATA, function(i,n){
			opt = document.createElement("OPTION");
			opt.value = n[0];
			opt.text = opt.title = n[1];
			dom.options.add(opt);
			count++;
		});
		if(count == 1){
			//opt.selected = true;
			//dom.onchange();//只有一个条数据的时候默认选中,并且自动触发该下拉列表的onchange事件
		}
	}
}

function loading(id,s){
	var dom = document.getElementById(id);
	while(dom.options.length>0)
	{
		dom.remove(0);
	}
	var opt = document.createElement("OPTION");
	opt.text = s;
	opt.value = "0";
	dom.options.add(opt);
}

function clear(id,s){
	var dom = document.getElementById(id);
	while(dom.options.length>0)
	{
		dom.remove(0);
	}
	var opt = document.createElement("OPTION");
	opt.text = s;
	opt.value = "0";
	dom.options.add(opt);
}

function checkHeadForm(f){
	var game = parseInt(f.headGame.value);
	var server = parseInt(f.headServer.value);
	var type = parseInt(f.headType.value);
	if(type == 3 || type ==4){
		if(!game)
			return;
	}
	else{
		if(!game || !server){
			return;
		}
	}
	f.submit();
}

function checkQuickForm(f){
	var game = parseInt(f.quickGame.value);
	var server = parseInt(f.quickServer.value);
	var p = parseInt(f.quickProduct.value);
	if(!game || !server || !p){
		return;
	}
	f.checkpriceid.value = p;
	f.submit();
}

function checkType(f){
	var type = parseInt(f.headType.value);
	if(type == 3 || type == 4){
		f.headServer.options[0].selected = true;	
	}
}

function getIncludeCartInfo(symbol,rate,innerObj){
	jQuery.get("/ajax/ajax_cartInfo.cfm?symbol="+symbol+"&rate="+rate,function(res){
		innerObj.append(res);
	});
}
