/*/////////common/////////////*/
$.extend(String, {
	format : function(format){
		var args = jQuery.makeArray(arguments).slice(1);
		return format.replace(/\{(\d+)\}/g, function(m, i) {
			return args[i];
		});
	}
});

;jQuery(function($) {
    var $window = $(window);
    var $body = $(document.body);
    var $footer = $('#footer'), footerHeight = $footer.height(), bodyHeight, windowHeight;
    var resizeBody;
    var isFixedLayout = false;
    if($.browser.msie && $.browser.version <= 7){
        var resizeBodyWidth = function(e){
            if($window.width() < 960){
                $body.width(960);
            }else if($body.width() == 960){
                $body.css('width', 'auto');
            }
        };
        $window.resize(resizeBodyWidth);
        $window.load(resizeBodyWidth);
    }
    resizeBody = function(e){
        windowHeight = $window.height();
        bodyHeight = $body.height();
        if(isFixedLayout){
            bodyHeight += footerHeight;
        }
        if(windowHeight <= bodyHeight){
            $footer.css({
                position: 'static'
            });
            isFixedLayout = false;
        }else{
            $footer.css({
                position: 'absolute',
                bottom: 0
            });
            isFixedLayout = true;
        }
    }
    
    $window.resize(resizeBody);
    $window.load(resizeBody);
    ($.window = $.window || {}).resizeBody = resizeBody;
    
});

var MAIN_DOMAIN = 'qplus.com',
	setCookie = function(name, value, domain, path, hour) {
		if (hour) {
			var today = new Date();
			var expire = new Date();
			expire.setTime(today.getTime() + 3600000 * hour);
		}
		window.document.cookie = name + "=" + value + "; " + (hour ? ("expires=" + expire.toGMTString() + "; ") : "") + (path ? ("path=" + path + "; ") : "path=/; ") + (domain ? ("domain=" + domain + ";") : ("domain=" + domainPrefix + ";"));
		return true;
	},
	getCookie = function(name) {
		var r = new RegExp("(?:^|;+|\\s+)" + name + "=([^;]*)");
		var m = window.document.cookie.match(r);
		return (!m ? "" : m[1]);
	},
	removeCookie = function(name, domain, path) {
		window.document.cookie = name + "=; expires=Mon, 26 Jul 1997 05:00:00 GMT; " + (path ? ("path=" + path + "; ") : "path=/; ") + (domain ? ("domain=" + domain + ";") : ("domain=" + domainPrefix + ";"));
	},
	setNick = function(cbs) {
		var url = 'http://cgi.qplus.com/qplusown/get_nick?retype=2&domain=qplus.com&callback=?',
			encodeHtmlSimple = function(sStr){
				sStr = sStr.replace(/&amp;/g,"&");
				sStr = sStr.replace(/&gt;/g,">");
				sStr = sStr.replace(/&lt;/g,"<");
				sStr = sStr.replace(/&quot;/g,"\"");
				sStr = sStr.replace(/&#39;/g,"'");
				return sStr;
			},
			encodeHtml = function(sStr) { 
				return sStr.replace(/[&'"<>\/\\\-\x00-\x09\x0b-\x0c\x1f\x80-\xff]/g, function(r){ 
					return "&#"+r.charCodeAt(0)+";";
				}).replace(/ /g, "&nbsp;").replace(/\r\n/g, "<br />").replace(/\n/g, "<br />").replace(/\r/g, "<br />"); 
			};
		
		$.getJSON(url, null, function(data) {
			if(data.retcode == 0) {//登陆
				var nick = encodeHtmlSimple(data.result.nick + '');
				$('#user_nick').text(nick);
				$('#user_nick').attr('title', nick);
				
				cbs && cbs.onLogin && cbs.onLogin();
				//callback && callback();
			} else if(data.retcode == 100000) {//未登录
				cbs && cbs.onNotLogin && cbs.onNotLogin();
			}
		});
	},
	setAvator = function() {
		var getUin = function() {
				return getCookie('uin').replace(/\D+/g, '');
			},
			src = 'http://face1.qplus.com/cgi/svr/face/getface?type=1&uin=' + getUin();
		
		$('.avator').html('<img src="'+src+'" alt="头像" />');
		//$('.avator')[0].appendChild(img);
		//img.src = src;
	},
	formSubmit = function() {//POST方式跨域提交表单
		var form1, 
			timer = 'jQuery_1021_iframe' + (+new Date),
			getForm = function(){
				if (form1) return form1;
				var body = $('body');
				$('<iframe id=' +timer+ ' name=' +timer+ '></iframe>').appendTo( body ).hide();
				return form1 = $('<form></form>').appendTo(body).attr('target', timer).hide();
			};
		
		return function(ur, parmas, callback) {
			var timer2 = 'jQuery_1021_callback' + (+new Date);
			window[timer2] = callback;
			var form1 = getForm();
			form1.empty();
			parmas.retype = parmas.retype || 3;
			$.extend(parmas, {
				"callback": 'parent.' + timer2
			});
			for (var i in parmas) {
				$('<input name='+ i +' value='+ parmas[i] +'></input>').appendTo(form1);
			}
			form1.attr({method: 'post', action: ur});
			form1.submit();
		}
	}(),
	openLoginBox = function(e) {
		e.preventDefault();
		
		var mask = $('#mask'),
			loginBox = $('#loginBox'), 
			close = $('#closeBtn'), 
			win = $(window), 
			body = $('body'), 
			doc = $(document), 
			html, 
			height;
			
		if (mask.length == 0) {
			mask = $('<div id="mask" class="mask"></div>');
			mask.css("opacity",0.4).appendTo(body);
		}

		if (loginBox.length == 0) {
			var loginBox = $('<div id="loginBox" class="loginBox"></div>');
			
			html = '<div class="loginBoxTop"></div>\
						<div class="loginBoxMid">\
							<div class="loginBoxContainer">\
								<div class="loginBoxHeader">\
									<div class="loginText">登录</div>\
									<div title="关闭" id="closeBtn" class="closeBtn">×</div>\
								</div>\
								<iframe width="100%" height="300" frameborder="0" scrolling="no" name="login_frame" id="login_frame" src="http://ui.ptlogin2.qplus.com/cgi-bin/login?appid=682013003&hide_title_bar=1&s_url='+location.href+'"></iframe>\
							</div>\
						</div>\
					<div class="loginBoxBottom"></div>';

			loginBox.html(html);
			loginBox.appendTo(body);

			close = $('#closeBtn');
		}

		height = Math.max(win.height(),doc.height());
		mask.css("height",height+"px").show();
		loginBox.show();

		close.click(function() {
			mask.hide();
			loginBox.hide();

			doc.unbind();
			win.unbind();
		});

		doc.keydown(function(e) {
			if (e.keyCode == 27) {
				mask.hide();
				loginBox.hide();

				doc.unbind();
				win.unbind();
			}
		});
		
		win.resize(function() {
			if (mask.css("dispaly") != "none") {
				height = Math.max(win.height(),doc.height());
				mask.css("height",height+"px").show();
			}
		});
	},
	ptlogin2_onResize = function(w, h) {
		$('#login_frame').css(height, h);
	};

document.domain = MAIN_DOMAIN;

$('.login').length && $('.login').click(openLoginBox);

$('.logout').length && $('.logout').click(function(e) {
	e = e || window.event;
	e.preventDefault();
	removeCookie("skey", MAIN_DOMAIN);
	removeCookie("uin", MAIN_DOMAIN);
	window.location.reload();
});
