document.domain='qq.com';

var gCgiOss = window.location.protocol + "//log.minigame.qq.com/cgi-bin/qqgame_web_report";
var gCgiGetSnsAds = window.location.protocol + "//lobby.minigame.qq.com/cgi-bin/qqgame_get_sns_ads";
var gCgiGetProfile = window.location.protocol + "//lobby.minigame.qq.com/cgi-bin/qqgame_get_profile"
var gAdsList = [];
var gCurShowIndex = 0;
var gAppID = 0;
var gAdsAppID = 0;
var gIsVip = 0;
var gIsShareShow = 0;
var gIsInviteShow= 0;
var gIsOpenVipShow = 0;

function ShowShareHtml()
{
	// 隐藏游戏
	$(".iframe_box").hide();

	var doc_obj = $(document);
	var top = doc_obj.scrollTop() + (doc_obj.height() - 230)/2 - 10;
	var left = doc_obj.scrollLeft() + (doc_obj.width() - 460)/2;
	
	var url= window.location.protocol + "//qqgameplatcdn.qq.com/social_hall/sns_share.html?appid=" + gAppID;
	
	var html = "";
	html += "<div class=\"classDivShare\" style=\"position:absolute;width:467px;height:219px;top:" +  top + "px;left:" + left + "px;z-index:99;\">";
	html += "<iframe id=\"idIframeShare\" width=\"467\" height=\"219\" frameborder=\"0\" scrolling=\"no\" allowTransparency=\"true\" src=\"" + url + "\">";
	html += "</iframe>";
	html += "</div>";
	
	$("body").append(html);
};

function ChangeAds()
{
	gCurShowIndex++;	
	if (gCurShowIndex >= gAdsList.length)
		gCurShowIndex = 0;
				
	$("#idAdsMinImg").attr("src", window.location.protocol + "//qqgameplatcdn.qq.com/social_hall/app_frame/images/sns_ads/act_min_" + gAdsAppID + "_"
	                            + gAdsList[gCurShowIndex]["smallimgid"]);
								
	$("#idAdsMaxImg").attr("src", window.location.protocol + "//qqgameplatcdn.qq.com/social_hall/app_frame/images/sns_ads/act_max_" + gAdsAppID + "_"
	                            + gAdsList[gCurShowIndex]["bigimgid"]);
								
};

function Show()
{
	$("#idBody").addClass('jQ_snsbody');
	$(".jQ_actBar").show();
	
	if (0 >= gIsVip)
	{
		$("#idARev").hide()
		$("#idAVip").show();
	}
	else
	{
		$("#idARev").show();
		$("#idAVip").hide();
	}

	ChangeAds();
	setInterval(ChangeAds, 10000);
};

function GetProfileCallBack(result)
{
	if (result["result"] != 0)
	{
		return;
	}
	
	gIsVip = result["gamevip"];	
	
	Show();
};

function GetProfile()
{
	$.getJSON(gCgiGetProfile + "?type=json&callback=?", null, GetProfileCallBack);	
};

function GetSnsAdsCallBack(result)
{
	if (result["result"] != 0 || result["isopen"] == 0 || result["list"].length == 0 )
	{
		return;
	}		
	
	// 获取广告数据
	gAppID = result["appid"];		// 有些游戏用的是gaid，所以这做修正
	gAdsAppID = result["ads_appid"];	// 广告的appid可能是默认的0
	gAdsList = result["list"];	
	gCurShowIndex = 0;

	// 拉取用户信息
	GetProfile();
};

function GetSnsAds()
{
	gCgiGetSnsAds = gCgiGetSnsAds + "?type=json&callback=?&appid="+gAppID;
	$.getJSON(gCgiGetSnsAds, null, GetSnsAdsCallBack);	
};

function Invite()
{
	if (gIsInviteShow || gIsShareShow || gIsOpenVipShow) return;

	// 隐藏游戏
	$(".iframe_box").hide();
	
	gIsInviteShow = 1;
	
	var api = window.GameAPI;
	var opt = {};
	api.InviteEx(gAppID, 
	             opt, 
				 function()
				 { 
					gIsInviteShow = 0; 
					// 显示游戏
					$(".iframe_box").show();
				 }, 
				 function()
				 { 
					gIsInviteShow = 0; 
					// 显示游戏
					$(".iframe_box").show();					
				});
	
};

function Share()
{	
	if (gIsInviteShow || gIsShareShow || gIsOpenVipShow) return;
	
	gIsShareShow = 1;
	
	ShowShareHtml();
};

function CloseShareDlg()
{
	// 显示游戏
	$(".iframe_box").show();
	
	$(".classDivShare").remove();
	
	gIsShareShow = 0;
};

function GetAppID()
{
	var api = window.GameAPI;
    var params = api.Aux.get_url_param();
	if (typeof(params['appid']) == "undefined" )
	{
		return -1;
	}
	
    gAppID = api.Aux.str2int(params['appid']);	
	return 0;
};

function ClickMaxImg()
{
	// 在新窗口中打开图片链接
	window.open(gAdsList[gCurShowIndex]["bigimglinkurl"]);

	// 打印点击下拉广告oss日志
	$.getJSON(gCgiOss + "?type=json&callback=?&rid=9&rint=" + gAppID, null, function(){});	
};

function OpenVipBuy()
{
	if (gIsInviteShow || gIsShareShow || gIsOpenVipShow) return;

	gIsOpenVipShow = 1;
	//window.open("http://pay.qq.com/qqgame/index.shtml?aid=ied.qqgame");
	// 隐藏游戏
	$(".iframe_box").hide();
	
	var api = window.GameAPI;
	api.OpenGameVIP(gAppID, function()
	{
		gIsOpenVipShow = 0;
		
		// 显示游戏
		$(".iframe_box").show();
	},
	"VIP.WEB.HALL.TOP",
	"VIP.WEB.HALL.TOP");
};

$(function()
{
	// 没有appid，直接返回
	if (GetAppID())
	{
		return;
	}

	top.window.CloseShareDlg = CloseShareDlg;

	$("#idInvite").click(Invite);
	$("#idShare").click(Share);	
	$("#idAMaxImg").click(ClickMaxImg);
	$("#idAVip").click(OpenVipBuy);
	$("#idARev").click(OpenVipBuy);

	//运营位显示与隐藏
	var actTimer;
	var actLure = $('.jQ_actLure');
	var actView = $('.jQ_actView');
	actLure.mouseover(function(){
		actTimer = setTimeout(function(){
			actView.show();
			// 打印下拉广告oss日志
			$.getJSON(gCgiOss + "?type=json&callback=?&rid=8&rint=" + gAppID, null, function(){});
		},800);
	});
	actLure.mouseout(function(){
		clearTimeout(actTimer);
		actView.hide();
	});
	actView.mouseover(function(){
		$(this).show();
	});
	actView.mouseout(function(){
		$(this).hide();
	});
	
	//获取运营位资源
	GetSnsAds();
});
