﻿
var keepingYoung = new Object();
keepingYoung.servicesBaseUrl = "http://keepyoung.ellechina.com/api/";
keepingYoung.currentUserStatus = new Object();


function popupDialog(content, w, h, useHtml) {
	var contentHtml = "";

	if (useHtml) {
		contentHtml = "<table><tr><td class=\"dialogContent\">"+content+"</td></tr></table>";
	}
	else {
		contentHtml = "<iframe scrolling=\"no\" width=\"" + w + "\" height=\"" + h + "\" frameborder=\"0\" src=\"" + content + "\" />";
	}

	$.modal(contentHtml);
};

function fillPageButtons(selector, pageindex, pagecount, clickHandler, pageSize) {

	if (pageSize == null || typeof (pageSize) == "undefined") pageSize = 5;
	
	var targetDiv = $(selector);
	targetDiv.html("");

	if (pagecount == 1) return;
	//pageSize = 5;
	var firstPage = Math.floor(pageindex / pageSize) * pageSize;
	if (firstPage == pageindex) {
		firstPage -= pageSize;
	}
	if (pageindex == 1) firstPage = 0;

	//alert(pageindex + " , " + pagecount + " , " + firstPage);

	if (pageindex > 1) {
		targetDiv.append("<a href=\"" + formatString(clickHandler, pageindex-1) + "\" class=\"button\">&lt;前一页</a>");
	}

	if (firstPage + 1 > 1) {
		//targetDiv.append("<a href=\"" + formatString(clickHandler, firstPage) + "\">...</a>");
	}

	for (var i = 0; i < pageSize; i++) {
		var thisPage = i + 1 + firstPage;

		if (thisPage > pagecount) break;

		if (thisPage == pageindex) {
			targetDiv.append("<b>" + thisPage + "</b>");
		}
		else {
			targetDiv.append("<a href=\""+formatString(clickHandler,thisPage)+"\">" + thisPage + "</a>");
		}
	}

	if (thisPage < pagecount) {
		//targetDiv.append("<a href=\"" + formatString(clickHandler, firstPage + pageSize+1) + "\">...</a>");
	}

	if (pageindex < pagecount) {
		targetDiv.append("<a href=\"" + formatString(clickHandler, pageindex + 1) + "\" class=\"button\">后一页&gt;</a>");
	}

};

function formatString() {
	var result = arguments[0];

	for (var i = 1; i < arguments.length; i++) {
		result = result.replace("{" + (i - 1) + "}", arguments[i]);
	}

	return result;
}




$(document).ready(function() {



    swfobject.embedSWF("ui/swf/DataProxy.swf", "dataProxySwf", "1", "1", "9.0.0", ""
	    , {}
	    , { wmode: "Transparent", allowScriptAccess: "always" });


    checkFlashVersion();

    setInterval(updateBrowserTitle, 500);

});

function updateBrowserTitle() {
	document.title = "找寻身心年轻的秘密";
};


function loadUserStatus() {
	$.dataProxy(keepingYoung.servicesBaseUrl +"user", {}, "loadUserStatus_loaded", "GET", true);
};


function loadUserStatus_loaded(data) {
	keepingYoung.currentUserStatus = data;
	
	if (data.IsAnonymous) {
		$("#userStatus").html("<a href=\"http://www.ellechina.com/action/register\" target=\"_blank\"><img src=\"ui/img/nav/reg.jpg\" alt=\"立即注册\" /></a><a href=\"http://keepyoung.ellechina.com/sso?returnUrl=" + document.location + "\"><img src=\"ui/img/nav/login.jpg\" alt=\"会员登录\" /></a>");
		var txtNameSelected = $("#txtName");

		//if (txtNameSelected.length > 0) {
		//	txtNameSelected[0].disabled = true;
		//	txtNameSelected[0].style.backgroundColor = "#DDDDDD";
		//}
	}
	else {
		var txtNameSelected = $("#txtName");
		
		if (txtNameSelected.length > 0) {
			txtNameSelected[0].value = data.Username;
		}
		$("#userStatus").html("您好，" + data.Username + "　<a href=\"http://keepyoung.ellechina.com/logout?returnUrl=" + document.location + "\"><img src=\"ui/img/nav/logout.jpg\" alt=\"安全退出\" /></a>");
	}
	
};


function showDialogNeedLogin() {
	popupDialog("本活动仅限<b>www.ellechina.com</b>注册用户参加！请先<a href=\"http://keepyoung.ellechina.com/sso?returnUrl=" + document.location + "\">登录</a>", 350, 100, true);
}

function getRndNumber(min, max) {
	var result = Math.floor(Math.random() * max + 1);
	if (result < min) result = min;
	if (result > max) result = max;


	return result;
};
function randKey() {

	return;
	
	swfobject.embedSWF("ui/swf/Goldkey.swf", "theGoldKeySwf", "288", "179", "9.0.0", ""
		    , {}
		    , { wmode: "Transparent", allowScriptAccess: "always" });
	
	
	var tx = getRndNumber(0, 500);
	var ty = getRndNumber(100, 300);

	//alert(tx + "," + ty);
	var theGoldKeyDiv = $("#theGoldKey")[0];
	theGoldKeyDiv.style.display = "";
	theGoldKeyDiv.style.left = tx + "px";
	theGoldKeyDiv.style.top = ty + "px";
};

function updateFlashPV() {
    if (window.frames["frmFlashpv"] != null) {
        window.frames["frmFlashpv"].document.location.reload();
    }
}

function getFlashVersion() {
    var f = "-1", n = navigator;
    try {
        if (n.plugins && n.plugins.length) {
            for (var ii = 0; ii < n.plugins.length; ii++) {
                if (n.plugins[ii].name.indexOf('Shockwave Flash') != -1) {
                    f = n.plugins[ii].description.split('Shockwave Flash ')[1];
                    break;
                }
            }
        }
        else if (window.ActiveXObject) {
            for (var ii = 10; ii >= 2; ii--) {
                try {
                    var fl = eval("new ActiveXObject('ShockwaveFlash.ShockwaveFlash." + ii + "');");
                    if (fl) { f = ii + '.0'; break; }
                }
                catch (e) { }
            }
        }
        if (f == "-1") return f;
        else return parseFloat(f.substring(0, f.indexOf(".") + 2));
    }
    catch (e) {
        return -1;
    }
}

function checkFlashVersion() {
    var version = getFlashVersion();
    if (version < 9) {
        var msg = "";
        if (version == -1) {
            msg = "您的浏览器还没有安装Flash插件，请先安装。";
        }
        else {
            msg = "您的浏览器Flash版本低于9.0，请先升级。";
        }
        msg += "Flash插件安装地址：<a href='http://get.adobe.com/flashplayer/?promoid=BUIGP' target='_blank'>http://get.adobe.com/flashplayer/?promoid=BUIGP</a>";
        popupDialog(msg, 350, 100, true);
    }
}
