/*** Javascript ***/
$(function () {
	var is_ie = $.browser.msie;
	var onalert = null;
	var tabContainers = $('div.tabs > ul');
	$('div.tabs h2.tabnav a').click(function () {
		tabContainers.hide().filter(this.hash).show();
		$('div.tabs h2.tabnav a').removeClass('selected');
		$(this).addClass('selected');
		return false;
	}).filter(':first').click();

	setInterval(function() {
		$('#timeDiv').html(getMyTime());
	},100);

	$('.sidebar li>a,.sidebar dl a').tipsy({gravity: 'e'});
	$('.articleflow a').tipsy({gravity: 's'});
	$('.ad a').tipsy({gravity: 'e'});

	getcenter('#proc');

	$('#proc').bind("ajaxSend", function(){
		$(this).html(" connecting ……");
		$(this).show();
	}).bind("ajaxComplete", function(){
		$(this).html('handle end ……').show().fadeOut(1000);;
	});
});
function getcenter(e){
	size = getPageSize();
	pos = getScrollTop();
	vleft = parseInt((size[0] - $(e).width())/2 + pos[0]);
	vtop = parseInt((size[1] - $(e).height())/2 + pos[1]);
	$(e).css({left:vleft + 'px',top:vtop + 'px'});
}
function getScrollTop(){
	var yScrolltop;
	var xScrollleft;
	if (self.pageYOffset || self.pageXOffset) {
		yScrolltop = self.pageYOffset;
		xScrollleft = self.pageXOffset;
	} else if (typeof(document.documentElement) != "undefined" && typeof(document.documentElement.scrollTop) != "undefined" || typeof(document.documentElement.scrollLeft) != "undefined" ){	 // Explorer 6 Strict
		yScrolltop = document.documentElement.scrollTop;
		xScrollleft = document.documentElement.scrollLeft;
	} else if (typeof(document.body) != "undefined") {// all other Explorers
		yScrolltop = document.body.scrollTop;
		xScrollleft = document.body.scrollLeft;
	}
	arrayPageScroll = new Array(xScrollleft,yScrolltop);
	return arrayPageScroll;
}
function getPageSize(){
	var de = document.documentElement;
	var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
	var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
	arrayPageSize = new Array(w,h);
	return arrayPageSize;
}



function updateseccode() {
	var rand = Math.random();
	$('#seccodeimage').html('<img id="seccode" onclick="updateseccode()" src="' + Rurl + '/seccode.php?update=' + rand + '" class="codeimg" alt="单击图片换张图片" />');
}


function copylink()
{
	var href = document.location.href;
	clipboardData.setData('Text', href);
	alert(href + " copied to Clipboard");
}



function redirect(url) {
	window.location.replace(url);
}




function pmwin(action, param , script) {
	if(script == null){
		script = 'pm';
	}
	if(userid == 0){
		alert('please login!');return false;
	}
	if(action == 'open') {
		var clientWidth = document.body.clientWidth;
		var clientHeight = document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight;
		var scrollTop = document.body.scrollTop ? document.body.scrollTop : document.documentElement.scrollTop;
		var ucwidth = 800;
		var ucheight = clientHeight * 0.9;

		if(!$('#uclayer').is(':visible')) {
			div = $(document.createElement('div'));
			div.attr('id','uclayer');

			div.css(
				{width:ucwidth + 'px',
				height:ucheight + 'px',
				position:'absolute',
				'z-index':'999',
				left:((clientWidth - ucwidth) / 2) + 'px',
				top:((clientHeight - ucheight) / 2 + scrollTop) + 'px'});
//alert();
			div.show();
			div.html('<div style="width: 800px; background: #666666; margin: 5px auto; text-align: left">' +
				'<div style="width: 800px; height: ' + ucheight + 'px; padding: 1px; background: #FFFFFF; border: 1px solid #7597B8; position: relative; left: -6px; top: -3px">' +
				'<div onmouseover="pmwindrag(this)" style="cursor: move; position: relative; left: 0px; top: 0px; width: 800px; height: 30px; margin-bottom: -30px;"></div>' +
				'<a href="###" onclick="pmwin(\'close\')" style="position: absolute; right: 20px; top: 15px">[×]</a>' +
				'<div id="pmwinmask" style="margin-top: 30px; position: absolute; width: 100%; height: 100%; display: none"></div><iframe id="pmframe" name="pmframe" style="width:' + ucwidth + 'px;height:100%" allowTransparency="true" frameborder="0"></iframe></div></div>');

			$('#append_parent').html(div);
		}

		if(!param) {
			pmframe.location = Rurl+'/'+script+'.php';
		} else {
			pmframe.location = Rurl+'/'+script+ '.php?' + param;
		}
	} else if(action == 'close') {
		$('#uclayer').hide();
	}
}
function pmwindrag(e){
	$.getScript(Rurl+'/includes/javascript/jquery.easydrag.js', function(){
		$("#uclayer").easydrag(true);
	});
}


function dheader(url,time){
	time = time ? time*1000 : 0;
	setTimeout("window.location.href = '" + url + "'; ",time);
}

function pmchecknew() {
	$('#proc').unbind();
	$('#pmnotice').load(Rurl+'pm.php?checknewpm=' + Math.random());
}
function isUndefined(variable) {
	return typeof variable == 'undefined' ? true : false;
}


function getcookie(name) {
	var cookie_start = document.cookie.indexOf(name);
	var cookie_end = document.cookie.indexOf(";", cookie_start);
	return cookie_start == -1 ? '' : unescape(document.cookie.substring(cookie_start + name.length + 1, (cookie_end > cookie_start ? cookie_end : document.cookie.length)));
}
function checkform() {

	if ($('#username')) {
		if ($('#username').val() == '') {
			alert("请输入您的名字.");
			return false;
		}
	}
	if ($('#message').val() == '')	{
		alert("请输入内容.");
		return false;
	}
	if (((postminchars != 0 && $('#message').val().length < postminchars) || (postmaxchars != 0 && $('#message').value.length > postmaxchars))) {
		alert("您的评论内容长度不符合要求。\n\n当前长度: "+$('#message').value.length+" 字节\n系统限制: "+postminchars+" 到 "+postmaxchars+" 字节");
		return false;
	}
	$('#submit').disabled = true;
	return true;
}
function addquote(obj,strAuthor){
	var text = $('#'+obj+' .combox').html();
	text = text.replace(/alt\=(\"|)([^\"\s]*)(\"|)/g,"> $2 <");
	text = text.replace(/\<[^\<\>]+\>/g,"\n");
	text = text.replace(/ +/g," ");
	text = text.replace(/\n+/g,"\n");
	text = text.replace(/^\n*/gm,"");
	text = text.replace(/^\s*/gm,"");
	text = text.replace(/\n*$/gm,"");
	text = text.replace(/\s*$/gm,"");
	text = text.replace(/&lt;/g,"<");
	text = text.replace(/&gt;/g,">");
	text = text.replace(/&nbsp;&nbsp;/g,"  ");
	text = text.replace(/&amp;/g,"&");
	$('#message').val("[quote="+strAuthor+"]"+text+"[/quote]");
	$("#message").focus();
}
/*
 得到下拉DIV框架元素
 @param obj 点击对象
 @param divwidth  div宽度
 @param movepos 移动点 默认为右面偏移量
  正数是向左便宜,负数向右偏移
*/
function getDownDiv(obj,divwidth,movepos,id){
	movepos = movepos?movepos:0;
	var div = $(document.createElement('div'));
	var offset = $(obj).offset();
	id = id ? id : 'movablelayer';
	div.attr('id',id);
	divwidth && div.css('width',divwidth+'px');
	div.css({'left':offset.left - movepos + 'px',
		'top':offset.top + obj.offsetHeight + 4 + 'px'});
	return div;
}
function showDownTarget(obj,target,pos){
	var posval = (pos == 'left') ? $('#'+target).width() - obj.offsetWidth:0;
	var offset = $(obj).offset();
	$('#'+target).css({
	'position':'absolute',
	'z-index':'999',
	'left':offset.left - posval + 'px',
	'top':offset.top + obj.offsetHeight + 4 + 'px'
	}).slideToggle('slow');
}
function closemover(){
	//mover.remove();
	$('#append_parent').empty();
}

function addfavorite(obj,aid){
	if(userid == 0){
		alert('请登录后继续操作!');return false;
	}
	$.post(Rurl+'/post.php?action=favorite&articleid='+aid,function(s){
		var div = getDownDiv(obj,'200');
		div.html(s);
		$('#append_parent').html(div);
		setTimeout("$('#append_parent').empty()", 3000);
	});
}

function getMyTime() {
	var date = new Date();
	var h = date.getHours();
	var m = date.getMinutes();
	var s = date.getSeconds();
	var timeStr = '现在是:' + date.getFullYear() + '年' + date.getMonth() + '月' + date.getDate() + '日 ';
	timeStr += h < 12 ? '上午' : (h < 6 ? '下午' : '晚上');
	timeStr += (h % 12 < 10 ? '0' : '') + (h < 12 ? h : (h - 12)) + '时';
	timeStr += (m < 10 ? '0' : '') + m + '分';
	timeStr += (s < 10 ? '0' : '') + s + '秒';
	return timeStr;
}