var login_name='';
var user_name = getcookie('username');
var user_pass = getcookie('user_pass');
var tb_leadin = getcookie('tb_leadin');
var tb_appkey = getcookie('tb_appkey');
var tb_return_url = getcookie('tb_return_url');
var trueName = decodeURIComponent(getcookie('trueName')); 

function $(obj) {
	return document.getElementById ? document.getElementById(obj) : null;
}

function base_login_welcome(){
	user_name = getcookie('username');
	user_pass = getcookie('user_pass');
	trueName = decodeURIComponent(getcookie('trueName')); 
	if(user_name && user_pass) {
		if(trueName != 0) {
		   login_name = trueName;
		} else {
		   login_name = user_name;
		}
		login_welcome('welcome_div', login_name);
	}
}

if(user_name && user_pass) {
	if(trueName != 0) {
	   login_name = trueName;
	} else {
	   login_name = user_name;
	}
	
	//$('header_top_div').innerHTML = '<a href="http://agent.pupai.cn">联盟用户</a><a href="http://www.pupai.cn/tuangou.html" target="_blank">团购优惠</a><a href="http://www.pupai.cn/article_cat.php?id=3">帮助中心</a>';
	if(user_name.indexOf('renren.com') >= 0) {
		login_welcome_renren('welcome_div', login_name);
	} else if(user_name.indexOf('tb@pupai.cn') >= 0 && tb_leadin=='1') {
		//login_welcome('welcome_div', "淘宝会员：" + login_name);
		$('welcome_div').innerHTML = "<span id=\"J_TaobaoBar\"></span> [<a href=\"http://www.pupai.cn/user.php\" class=\"red\">会员中心</a>] [<a href=\"javascript:;\" onmousedown=\"logout()\" class=\"red\">退出</a>]";
		Taobao.init({ 
			nickName: login_name, 
			appKey: tb_appkey 
		}); 
	} else {
		login_welcome('welcome_div', login_name);
	}
} else {
	if (tb_leadin=='1'){
		$('welcome_div').innerHTML = "<span id=\"J_TaobaoBar\"></span>";
		Taobao.init({ 
			appKey: tb_appkey 
		}); 
		//$('welcome_div').innerHTML = '您好，欢迎来淘宝合作商家！[<a href="http://login.taobao.com/member/login.jhtml?f=top&redirectURL=http://container.api.taobao.com/container?appkey='+tb_appkey+'&redirectURL='+tb_return_url+'" class="red">请登录</a>][<a href="http://member1.taobao.com/member/newbie.htm" target="_blank" class="red">免费注册</a>]';
	}else {
		function iframe_login(){
			$('welcome_div').innerHTML = '您好，欢迎光临PUPAI普派。[<a class="submodal-836-394" href="http://www.pupai.cn/user.php?act=iframe_login">登录</a>][<a href="http://www.pupai.cn/user.php?act=register" target="_blank" class="red">免费注册</a>]';
			}
		if(document.all) {
			window.attachEvent("onload", iframe_login);
		} else {
			window.addEventListener("load", iframe_login, false);
				}
	}
}

function login_welcome(div, login_name) {
	$(div).innerHTML = '您好，<a href="http://www.pupai.cn/user.php" class="red">' + login_name + '</a>, 欢迎您回来![<a href="javascript:;" onmousedown="logout()" class="red">退出</a>]';
}

function login_welcome_renren(div, login_name) {
	if (document.all)
	{
	    window.attachEvent("onload", function() {
			Loader('http://static.connect.renren.com/js/v1.0/FeatureLoader.jsp');
			setTimeout(function() {
				XN_RequireFeatures(["EXNML"], function() { 
					XN.Main.init("440934b270e4481899b0aa450208e6af", "http://www.pupai.cn/connect/renren.php");
					XN.Main.get_sessionState().waitUntilReady(function() {
						XN.Main.apiClient.users_getInfo([trueName], ['name'], function(result, ex) {
							if (!ex) {
								if(result && result.length > 0) login_name = result[0]['name'];
							}
						});
					});
				});
				$(div).innerHTML = '您好，<a href="http://www.pupai.cn/user.php" class="red">' + login_name + '</a>, 欢迎您回来![<a href="javascript:;" onmousedown="logout()" class="red">退出</a>]';
			}, 4000);
		});
	}
	else
	{
	    window.addEventListener("DOMContentLoaded", function() {
			Loader('http://static.connect.renren.com/js/v1.0/FeatureLoader.jsp');
			setTimeout(function() {
					XN_RequireFeatures(["EXNML"], function() { 
						XN.Main.init("440934b270e4481899b0aa450208e6af", "http://www.pupai.cn/connect/renren.php");
						XN.Main.get_sessionState().waitUntilReady(function() {
							XN.Main.apiClient.users_getInfo([trueName], ['name'], function(result, ex) {
								if (!ex) {
									if(result && result.length > 0) login_name = result[0]['name'];
							    }
							});
						});
					});
				$(div).innerHTML = '您好，<a href="http://www.pupai.cn/user.php" class="red">' + login_name + '</a>, 欢迎您回来![<a class="red" href="javascript:;" onmousedown="logout()">退出</a>]';
			}, 4000);
		}, false);
	}
}

function logout() {
	if(user_name.indexOf('renren.com') >= 0) {
		XN.Connect.logout(function(){logouting();});return false;
	} else {
		window.location.href="http://www.pupai.cn/user.php?act=logout";
		return false;
	}
}

function logouting() {
	//alert('Hello World');
	window.location.href="http://www.pupai.cn/user.php?act=logout";
}
/* popul */
function addEvent(obj, evType, fn){
 if (obj.addEventListener){
    obj.addEventListener(evType, fn, false);
    return true;
 } else if (obj.attachEvent){
    var r = obj.attachEvent("on"+evType, fn);
    return r;
 } else {
    return false;
 }
}
function removeEvent(obj, evType, fn, useCapture){
  if (obj.removeEventListener){
    obj.removeEventListener(evType, fn, useCapture);
    return true;
  } else if (obj.detachEvent){
    var r = obj.detachEvent("on"+evType, fn);
    return r;
  } else {
    alert("Handler could not be removed");
  }
}
function getViewportHeight() {
	if (window.innerHeight!=window.undefined) return window.innerHeight;
	if (document.compatMode=='CSS1Compat') return document.documentElement.clientHeight;
	if (document.body) return document.body.clientHeight; 
	return window.undefined; 
}
function getViewportWidth() {
	var offset = 17;
	var width = null;
	if (window.innerWidth!=window.undefined) return window.innerWidth; 
	if (document.compatMode=='CSS1Compat') return document.documentElement.clientWidth; 
	if (document.body) return document.body.clientWidth; 
}

/* Gets the real scroll top */
function getScrollTop() {
	if (self.pageYOffset) // all except Explorer
	{
		return self.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop) // Explorer 6 Strict
	{
		return document.documentElement.scrollTop;
	}
	else if (document.body) // all other Explorers
	{
		return document.body.scrollTop;
	}
}
function getScrollLeft() {
	if (self.pageXOffset) // all except Explorer
	{
		return self.pageXOffset;
	}
	else if (document.documentElement && document.documentElement.scrollLeft) // Explorer 6 Strict
	{
		return document.documentElement.scrollLeft;
	}
	else if (document.body) // all other Explorers
	{
		return document.body.scrollLeft;
	}
}

var gPopupMask = null;
var gPopupContainer = null;
var gPopFrame = null;
var gReturnFunc;
var gPopupIsShown = false;
var gDefaultPage = "http://image.pupai.cn/2010new/np_new/images/load.gif";
var gHideSelects = false;
var gReturnVal = null;

var gTabIndexes = new Array();
var gTabbableTags = new Array("A","BUTTON","TEXTAREA","INPUT","IFRAME");
if (!document.all) {
	document.onkeypress = keyDownHandler;
}

function initPopUp() {
	
	var theBody = document.getElementsByTagName('BODY')[0];
	var popmask = document.createElement('div');
	popmask.id = 'popupMask';
	var popcont = document.createElement('div');
	popcont.id = 'popupContainer';
	
	popcont.innerHTML = '' +
		'<div id="popupInner">' +
			'<div id="popupTitleBar">' +
					'<img src="http://image.pupai.cn/images/close.gif" onclick="hidePopWin(false);" id="popCloseBox" />' +
			'</div>' +
			'<iframe src="'+ gDefaultPage +'" scrolling="no" frameborder="0" allowtransparency="true" id="popupFrame" name="popupFrame" width="100%" height="100%"></iframe>' +
		'</div>';	
		
	theBody.appendChild(popmask);
	theBody.appendChild(popcont);
	
	gPopupMask = document.getElementById("popupMask");
	gPopupContainer = document.getElementById("popupContainer");
	gPopFrame = document.getElementById("popupFrame");	
	var brsVersion = parseInt(window.navigator.appVersion.charAt(0), 10);
	if (brsVersion <= 6 && window.navigator.userAgent.indexOf("MSIE") > -1) {
		gHideSelects = true;
	}
	
	var elmsd = document.getElementsByTagName('a');	
	for (i = 0; i < elmsd.length; i++) {
		
		if (elmsd[i].className.indexOf("submodal") == 0) {				
			elmsd[i].onclick = function(){				
				var width = 400;
				var height = 270;
				var params = this.className.split('-');
				if (params.length == 3) {
					width = parseInt(params[1]);
					height = parseInt(params[2]);
				}
				showPopWin(this.href,width,height,null); return false;
			}
		}
	}
}
function inPopUp(){	
	if(document.all) {
				window.attachEvent("onload", initPopUp);
			} else {
				window.addEventListener("load", initPopUp, false);
					}
}
window.setTimeout("inPopUp();", 600);				
function showPopWin(url, width, height, returnFunc, showCloseBox) {
	
	if (showCloseBox == null || showCloseBox == true) {
		
		document.getElementById("popCloseBox").style.display = "block";
	} else {
		document.getElementById("popCloseBox").style.display = "none";
	}
	
	gPopupIsShown = true;
	disableTabIndexes();
	gPopupMask.style.display = "block";
	gPopupContainer.style.display = "block";
	centerPopWin(width, height);
	
	var titleBarHeight = parseInt(document.getElementById("popupTitleBar").offsetHeight, 10);

	gPopupContainer.style.width = width + "px";
	gPopupContainer.style.height = (height+titleBarHeight) + "px";
	
	setMaskSize();
	gPopFrame.style.width = parseInt(document.getElementById("popupTitleBar").offsetWidth, 10) + "px";
	gPopFrame.style.height = (height) + "px";
	gPopFrame.src = url;
	
	gReturnFunc = returnFunc;
	if (gHideSelects == true) {
		hideSelectBoxes();
	}
	window.setTimeout("setPopTitle();", 600);
}

var gi = 0;
function centerPopWin(width, height) {
	if (gPopupIsShown == true) {
		if (width == null || isNaN(width)) {
			width = gPopupContainer.offsetWidth;
		}
		if (height == null) {
			height = gPopupContainer.offsetHeight;
		}
		
		var theBody = document.getElementsByTagName("BODY")[0];
		//theBody.style.overflow = "hidden";
		var scTop = parseInt(getScrollTop(),10);
		var scLeft = parseInt(theBody.scrollLeft,10);
	
		setMaskSize();
		
		var titleBarHeight = parseInt(document.getElementById("popupTitleBar").offsetHeight, 10);
		
		var fullHeight = getViewportHeight();
		var fullWidth = getViewportWidth();
		
		gPopupContainer.style.top = (scTop + ((fullHeight - (height+titleBarHeight)) / 2)) + "px";
		gPopupContainer.style.left =  (scLeft + ((fullWidth - width) / 2)) + "px";
	}
}
addEvent(window, "resize", centerPopWin);
addEvent(window, "scroll", centerPopWin);
window.onscroll = centerPopWin;
function setMaskSize() {
	var theBody = document.getElementsByTagName("BODY")[0];
			
	var fullHeight = getViewportHeight();
	var fullWidth = getViewportWidth();
	
	if (fullHeight > theBody.scrollHeight) {
		popHeight = fullHeight;
	} else {
		popHeight = theBody.scrollHeight;
	}
	
	if (fullWidth > theBody.scrollWidth) {
		popWidth = fullWidth;
	} else {
		popWidth = theBody.scrollWidth;
	}
	
	gPopupMask.style.height = popHeight + "px";
	gPopupMask.style.width = popWidth + "px";
}

function hidePopWin(callReturnFunc) {
	gPopupIsShown = false;
	var theBody = document.getElementsByTagName("BODY")[0];
	theBody.style.overflow = "";
	restoreTabIndexes();
	if (gPopupMask == null) {
		return;
	}
	gPopupMask.style.display = "none";
	gPopupContainer.style.display = "none";
	if (callReturnFunc == true && gReturnFunc != null) {
		gReturnVal = window.frames["popupFrame"].returnVal;
		window.setTimeout('gReturnFunc(gReturnVal);', 1);
	}
	gPopFrame.src = gDefaultPage;
	// display all select boxes
	if (gHideSelects == true) {
		displaySelectBoxes();
	}
}

function setPopTitle() {
	return;
	if (window.frames["popupFrame"].document.title == null) {
		window.setTimeout("setPopTitle();", 10);
	} else {
		document.getElementById("popupTitle").innerHTML = window.frames["popupFrame"].document.title;
	}
}

function keyDownHandler(e) {
    if (gPopupIsShown && e.keyCode == 9)  return false;
}

function disableTabIndexes() {
	if (document.all) {
		var i = 0;
		for (var j = 0; j < gTabbableTags.length; j++) {
			var tagElements = document.getElementsByTagName(gTabbableTags[j]);
			for (var k = 0 ; k < tagElements.length; k++) {
				gTabIndexes[i] = tagElements[k].tabIndex;
				tagElements[k].tabIndex="-1";
				i++;
			}
		}
	}
}

function restoreTabIndexes() {
	if (document.all) {
		var i = 0;
		for (var j = 0; j < gTabbableTags.length; j++) {
			var tagElements = document.getElementsByTagName(gTabbableTags[j]);
			for (var k = 0 ; k < tagElements.length; k++) {
				tagElements[k].tabIndex = gTabIndexes[i];
				tagElements[k].tabEnabled = true;
				i++;
			}
		}
	}
}

function hideSelectBoxes() {
  var x = document.getElementsByTagName("SELECT");

  for (i=0;x && i < x.length; i++) {
    x[i].style.visibility = "hidden";
  }
}

function displaySelectBoxes() {
  var x = document.getElementsByTagName("SELECT");

  for (i=0;x && i < x.length; i++){
    x[i].style.visibility = "visible";
  }
}
