function trim(str)
{
	str=str.replace(/(^ +)|( +$)/g, '')
	return(str)
}

// LTrim(string) : Returns a copy of a string without leading spaces.
function ltrim(str)
{
	var whitespace = new String(" \t\n\r");
	var s = new String(str);
	if (whitespace.indexOf(s.charAt(0)) != -1) {
		var j=0, i = s.length;
		while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
		j++;
		s = s.substring(j, i);
	}
	return s;
}

//RTrim(string) : Returns a copy of a string without trailing spaces.
function rtrim(str)
{
	var whitespace = new String(" \t\n\r");
	var s = new String(str);
	if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
		var i = s.length - 1;       // Get length of string
		while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
		i--;
		s = s.substring(0, i+1);
	}
	return s;
}

function getGetParameter(name) 
{ 
	var paramStr=location.search; 
	if(paramStr.length==0)return null; 
	if(paramStr.charAt(0)!="?")return null; 
	paramStr=unescape(paramStr); 
	paramStr=paramStr.substring(1); 
	if(paramStr.length==0)return null; 
	var params=paramStr.split("&"); 
	for(var i=0;i<params.length;i++){ 
		var parts=params[i].split("=",2); 
		if(parts[0]==name) { 
			if(parts.length<2||typeof(parts[1])=="undefined"||parts[1]=="undefined"||parts[1]=="null")return ""; 
			return parts[1]; 
		} 
	} 
	return null; 
} 

function change(obj,n,cname,num)
{
	var name = cname;
	var new_div = obj+"List";

	if(obj == 'zine'){
		var more = document.getElementById('zine0_more');
		if(n == 0){
			more.style.display = "block";
		}else{
			more.style.display = "none";
		}
	}
	
	for(var i =0;i<num;i++){
		if(i == n){		
			document.getElementById(obj+i).className = name;
			document.getElementById(obj+"List"+i).style.display = "block";

			if(obj == "my"){
				document.getElementById(obj+"_scrollbar"+i).style.display = "block";
			}
		}else{
			document.getElementById(obj+i).className = "";
			document.getElementById(obj+"List"+i).style.display = "none";
			if(obj == "my"){
				document.getElementById(obj+"_scrollbar"+i).style.display = "none";
			}
		}
	}
}

function selectTab(object, prefix, number)
{
	for(i =0 ; i < number; i++){
		document.getElementById(prefix + i).className = "off";
		document.getElementById(document.getElementById(prefix + i).id + "List").style.display = "none";
	}
	object.className = "on";
	document.getElementById(object.id + "List").style.display = "block";
}

function changeTab(prefix,sum,show_number)
{
	for(i =0 ; i < sum; i++){
		document.getElementById(prefix + i + "List").style.display = "none";
	}
	document.getElementById(prefix + show_number + "List").style.display = "block";
}

//选择我的书架
function selectMyBookShelf(object, prefix, number, isLogin, user_id)
{
	selectTab(object,prefix,number);

	//处理我的书架
	if (prefix == "chenage" || object.id == "chenage2"){
		if (isLogin){
			ajaxGetMyZine(user_id);
		}else{
			document.getElementById('selThree').style.display = "block";
		}
	}
}

//动态读取我的书架
function ajaxGetMyZine(user_id)
{
	document.getElementById("chenage2List").innerHTML = "请稍候，数据加载中...";	
	var operation = "my.php";
	var pars = "action=get_my_zine&user_id="+user_id;
	var ajax = new Ajax.Updater(
		"chenage2List",
		operation,
		{
			method:"get",
			parameters:pars
		}
	);
}

function check_search(ele, frm)
{
	var f = document.getElementById(ele);

	if(trim(f.value) == "" || trim(f.value)=="关键字..."){
		alert('请输入搜索内容');
	}else{
		if (frm){
			document.getElementById(frm).submit();
			}else {
			document.getElementById('search_f').submit();
		}
	}
}


function InputCount(obj,limit_length)
{
	var len=0;
	var pass = true;

	for(var i=0;i<obj.value.length;i++){
		if(obj.value.charCodeAt(i)>255){
			len = len+2;
		}else{
			len++;
		}

		if (len>limit_length){
			obj.value = obj.value.substring(0, i);
			obj.blur();
			alert('最多只允许输入'+(limit_length/2)+'个汉字');
			pass = false;
			break;
		}
	}

	if (pass == true){
		//VchangeClass(info_id, 'reg_info','');
	}
}

function clearTimes()
{
	if(tims){
		window.clearInterval(tims);
	}
}

function slides()
{

	var chang_arr = new Array();
	chang_arr[0] =  new Array('zine',2);
	chang_arr[1] =  new Array('club',2);
	for(i=0;i<chang_arr.length;i++){
		k = document.getElementById('n'+i).value
		var num = chang_arr[i][1]+1;
		if(k > 2){
			k = 0;

			change(''+chang_arr[i][0]+'',k,'on',''+num+'');
		}else{
			change(''+chang_arr[i][0]+'',k,'on',''+num+'');
			k++;
		}
		document.getElementById('n'+i).value = k
	}
}

//判断游览器版本
var Browser = {
'isIE' : (navigator.userAgent.indexOf('MSIE') >= 0) && (navigator.userAgent.indexOf('Opera') < 0),
'isFirefox' : navigator.userAgent.indexOf('Firefox') >= 0,
'isOpera' : navigator.userAgent.indexOf('Opera') >= 0
};

function selectRand(iFirstValue,iLastValue){
	var iChoces = Math.abs(iLastValue - iFirstValue) + 1;
	return Math.floor(Math.random() * iChoces + iFirstValue);
}

function  openWindows(url)   {
	bschitchat=window.open(url,'bschitchat','fullscreen=yes,toolbar=no,Status=no,scrollbars=yes,resizable=yes');
	return;
}

//收藏杂志
function favorite_issue(issue_id){

	var  pars = "action=favorite_issue&issue_id="+issue_id+"&ran="+Math.random();
	var myAjax = new Ajax.Request(
	'/detail.php',
	{
		method: 'get',
		parameters: pars,
		onComplete: function(response)
		{
			switch (response.responseText){
			case '0':
				alert("你之前已经成功收藏了该杂志！");
				break;
			case '1':
				alert("恭喜！您已成功收藏这本杂志！");
				break;
			case '-1':
				alert("收藏不成功！");
				break;
			case '-2':
				if (confirm("要登录后才可以收藏，是否要登录？")){
					window.location = '/login.php?return_url='+window.location.href;
				}
				break;
			}
		}
	}
	);
}

//用flash+javscript实现网页上的文本复制,通杀所有浏览器
function copyToClipboard(textit) {
	if (window.clipboardData) {
		window.clipboardData.setData("Text",textit);
	} else {
		var flashcopier = 'flashcopier';
		if(!document.getElementById(flashcopier)) {
			var divholder = document.createElement('div');
			divholder.id = flashcopier;
			document.body.appendChild(divholder);
		}
		document.getElementById(flashcopier).innerHTML = '';
		var divinfo = '<embed src="/views/normal/flash/_clipboard.swf" FlashVars="clipboard='+textit+'" width="0" height="0" type="application/x-shockwave-flash"></embed>';
		document.getElementById(flashcopier).innerHTML = divinfo;
	}
	alert('链接复制成功，您可以粘贴在聊天工具里告诉朋友！')
}

function openBuilder()
{
    window.open("http://mag.blog.sina.com.cn/into_diy.php?flag=1&"+Math.random(), "one","fullscreen=yes,location=no, menubar=no, resizable=yes,scrollbars=yes, status=no, titlebar=no, toolbar=no");
}

var _et=0;var _pt=0;var _pr=1;
function _S_pSc(){
	_et++;
	var s=window.location.href;
	var re=/(\d+)\/(\d+)\/(\d+)/gi;
	if (!re.test(s)) return;
	re.exec(s);
	var cid=RegExp.$1;var mid=RegExp.$2;var iid=RegExp.$3;
	var p=0;
	while ((_et+_pt)/_et<_pr){
		new Ajax.Request('/zine/online/'+cid+'/'+mid+'/'+iid+'.html?page='+p+'&lpage='+currPage+'&ltime=1&report=1&c_id='+cid+'&m_id='+mid+'&i_id='+iid+'&ran='+Math.random(),{method:'get'});
		_pt++;
	}
	
}

//从detail.js转移过来的COOKIE相关函数
function getCookie( name ) {
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
		return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ';', len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}

function is_int(num){
	return num == parseInt(num);
}

function setCookie( name, value, expires, path, domain, secure ) {
	var today = new Date();
	today.setTime( today.getTime() );
	if ( expires ) {
		if (is_int(expires)){
			expires = expires * 1000 * 60 * 60 * 24;
			var expires_date = new Date( today.getTime() + (expires) );
		} else {
			var expires_date = expires;
		}
	}

	document.cookie = name+'='+escape( value ) +
	( ( expires ) ? ';expires='+expires_date.toGMTString() : '' ) + //expires.toGMTString()
	( ( path ) ? ';path=' + path : '' ) +
	( ( domain ) ? ';domain=' + domain : '' ) +
	( ( secure ) ? ';secure' : '' );
}

function deleteCookie( name, path, domain ) {
	if ( getCookie( name ) ) document.cookie = name + '=' +
	( ( path ) ? ';path=' + path : '') +
	( ( domain ) ? ';domain=' + domain : '' ) +
	';expires=Thu, 01-Jan-1970 00:00:01 GMT';
}


function in_array(value, array)
{
	for (i in array) {
		if (array[i] == value) {
			return true;
			
			}
		
		}
	return false;
	}
/**
 * 一维数组内容唯一
 *
 */
function array_unique(array)
{
	
	var unique_array  = new Array();
	var array_str;
	while (true) {
		
		unique_array.push(value = array.shift());
		if (array.length == 0)
		{
			break;
		}
		array_str = "," + array.join(",") +  ",";
		re = new RegExp("," + value + ",");
		array_str = array_str.replace(re, ",");
		array = array_str.substr(1,array_str.length-2).split(",");
		}
	return unique_array;
	}
/**
 * 弹出窗口相关
 *
 *
 */
 
 //分析URL是否存在分类ID,如果是就返回ID,否则返回FALSE
 function getCategoryId()
 {
	url = window.location.href;
	if (matched = url.match(/\/zine\/online\/(\d+)\//)) {
		return matched[1];
		}
	if (matched = url.match(/\/zine\/(\d+)\//)) {
		return matched[1];
		}
	if (matched = url.match(/category_id=(\d+)/)) {
		return matched[1];
		}
	return false;
 }

/**
 * 快速排序
 * 数据形式为二维数组,按二维中的第二个元素进行比较
 * order的可用值为:asc , desc
 */
function quicksort(array, order) {
	//默认为顺序
	if (!order || order.toLowerCase() != "desc") {
		var order = "asc";
	} else {
		var order = "desc";
	}
    if (array.length <= 1) return array;
    var key = array[0];
    var left_arr = new Array();
    var right_arr = new Array();
	if (order == "asc") {
   	 for (i=1; i<array.length; i++){
    	   if (array[i][1] <= key[1])
            left_arr.push(array[i]);
        else
            right_arr.push(array[i]);
    	}
	} else {
		for (i=1; i<array.length; i++){
        	if (array[i][1] >= key[1])
        	    left_arr.push(array[i]);
        	else
        	    right_arr.push(array[i]);
    	}
	}
    left_arr = quicksort(left_arr, order);
    right_arr = quicksort(right_arr, order);
    return left_arr.concat(new Array(key), right_arr);
}

function get_date()
{
	date = new Date();
	year = date.getFullYear();
	month = date.getMonth() + 1;
	day = date.getDate()
	return new Array(year, month, date);
	}



//记录catgory_id 的使用次数,格式为 cid_1:times;cid_2:times;.....
if (cid = getCategoryId()) {
	//单天记录
	//today = get_date().join("");
	date = new Date();
	//从1970年到今天的天数
	today = Math.ceil(date.getTime()/1000/3600/24);
	//COOKIE 名
	today = "c_" + today;
	cookie = getCookie(today)
	if (cookie == null) {
		setCookie(today, cid + ":1", 100, "/");
		} else {
		cookie = cookie.split(";");
		for (i=0; i<cookie.length; i++) {
			cookie_item = cookie[i].split(":");
			if (cookie_item[0] == cid) {
				times = new Number(cookie_item[1]) + 1;
				cookie[i] = cid + ":" + times;
				break;
				}
			}
		if (i == cookie.length) {
			cookie[cookie.length] = cid + ":1";
			}
		
		setCookie(today, cookie.join(";"), 100, "/");
		}
	}
	
	
//记录浏览过的期刊
if (/\/zine\/online\//.test(window.location.href)) {
	match_result = window.location.href.match(/\/(\d+)\.html/);
	issue_id = match_result[1];
	date = new Date();
	//从1970年到今天的天数
	today = Math.ceil(date.getTime()/1000/3600/24);
	//COOKIE 名
	cookie_name = "read_issue_" + today;
	if (null == (cookie = getCookie(cookie_name))) {
		setCookie(cookie_name, issue_id, 100, "/");
		} else {
			if (!in_array(issue_id, cookie.split(","))) {
				setCookie(cookie_name, cookie + "," + issue_id, 100, "/");
				}
		}
	}
	
//删除所有浏览期刊记录
function delIssueBrowseRecord()
{
	var max_records = 20;
	var date = new Date();
	var delete_date;
	//从1970年到今天的天数
	var today = Math.ceil(date.getTime()/1000/3600/24);
	for (var i=0; i<max_records; i++) {
		delete_date = today - i;
		deleteCookie("read_issue_"+delete_date, "/");
		}
	}
	
//浮动窗口显示内容开始	


//内容窗口的宽度
function getWindowWidth()
{

//	alert(document.body);
	if(document.body)
		return document.body.offsetWidth;
	return 1024;
	}

var initPage={'service_center':'','issue':'','review':'','commend':''};

//用户点击分页卡
function clickPage(pre_name, page, sum)
{
	//AJAX处理
	var  pars;
	var act;
	switch (page) {
		case 2:
			pars = "action=service_center";
			act = 'service_center';
			break;
		case 3:
			pars = "action=issue";
			act = 'issue';
			break;
		case 4:
			pars = "action=review";
			act = 'review';
			break;
		default :
			pars = "action=commend";
			act = 'commend';
	}
	
	//改变选项卡的样式
	for (i=1; i<=sum; i++) {
		
		//document.getElementById(pre_name + i).className = "normal";
		}
	//document.getElementById(pre_name + page).className = "selected";
	
	
	var myAjax = new Ajax.Request(
	'/popup.php',
	{
		method: 'get',
		parameters: pars,
		onComplete: function(response)
		{
			document.getElementById("guide_content_inner").innerHTML = response.responseText;
			//"popup_content"
		}
	}
	);	
}
//显示内容
function showContent()
{
	new Ajax.Request('/popup.php?action=open',{method:'get'});
	
	document.getElementById("guide_main").style.display = "block";
	document.getElementById("guide").style.width = "276px";//"400px";
	document.getElementById("guide").style.height = "180px";//"200px";
	clickPage('DivTitle', 1, 4)
	document.getElementById("guide_logo").style.display = "none";
	
	if (document.getElementById("iframe_guide")){
		document.getElementById("iframe_guide").style.visibility = "";
	}
	
	oExpires = new Date(2099, 12, 31);
	setCookie('guide_open', 1, 10000, '/');
}

//隐藏内容
function hiddenContent()
{
	document.getElementById("guide_main").style.display = "none";
	document.getElementById("guide").style.width = "74px";
	document.getElementById("guide").style.height = "112px";
	
	document.getElementById("guide_logo").style.display = "block";
	if (document.getElementById("iframe_guide")){
		document.getElementById("iframe_guide").style.visibility = "hidden";
	}


		//改变LOGO的logo为阅读后的LOGO
		sina_logo = window.document.getElementById("sina_logo");
		//alert(sina_logo.src)
		sina_logo.src = "/views/normal/images/guide_logo.gif";
		//alert(sina_logo.src);
	
	oExpires = new Date(2099, 12, 31);
	setCookie('guide_open', 0, 10000, '/');

}

//根据关键字屏蔽弹出广告
function thisPageIsDisabled()
{
	var disabled_url_keys = new Array(
		"iframe\.php",
		"iframes\.php",
		"from=bbs",
//		"category\.php\\u003Fcategory_id=14",
		"/zine/(online/)\?\\d+/125/\\d+\\.html",
		"/zine/(online/)\?\\d+/906/\\d+\\.html"
		);
	for (i=0; i<disabled_url_keys.length ; i++)
	{
		reg = new RegExp(disabled_url_keys[i]);
		if (reg.exec(window.location.href))
		{
			return true;
		}
	}
	return false;
}

//根据关键字允许广告
function isAllowDomain()
{
	var allow_domains = new Array(
		"mag\.sina\.com\.cn"
		);
	for (i=0; i<allow_domains.length ; i++)
	{
		reg = new RegExp(allow_domains[i]);
		if (reg.exec(window.location.href))
		{
			return true;
		}
	}
	return false;

}

function movePopup()
{
	var o = document.getElementById("guide");
	if (o){
		o.style.top = document.documentElement.clientHeight - o.style.pixelHeight+document.documentElement.scrollTop - 20;

		ifrm = document.getElementById('iframe_guide');

		if (ifrm){
			cookie_name = 'guide_open';
			isShow = ((tmp = getCookie(cookie_name)) == '1' || tmp == null) ? true: false ;

			if (isShow) {
				ifrm.style.width = '276px';
				ifrm.style.height = '180px';
				ifrm.style.right = '0px';
			}

			ifrm.style.top = o.style.top;
			ifrm.style.display = 'block';
		}
	}
	setTimeout("movePopup()",50);
}

function showGuide() {	
	//当窗口宽度大于1000时出显示窗口
	if (getWindowWidth() > 800 && !thisPageIsDisabled() && isAllowDomain()) {
		cookie_name = 'guide_open';
		isShow = ((tmp = getCookie(cookie_name)) == '1' || tmp == null) ? true: false ;
		
		//要显示的LOGO
		sina_logo = "/views/normal/images/guide_logo.gif";		
		
	//显示浮动窗口
	//以下是为了兼容 xhtml1.0 html4以下 / ie6 ie7 ff  之间多种交错情况 
	document.write("<div style='clear:both'></div>");
		if( typeof document.compatMode != 'undefined' && document.compatMode != 'BackCompat'){
			
			if (navigator.appVersion.match(/MSIE [0-6]\./)) {
	//margin-top:expression(document.documentElement.clientHeight-this.style.pixelHeight+document.documentElement.scrollTop);
				document.write("<iframe frameborder=0 id='iframe_guide' style='z-index:9998;border:0;position:absolute;visibility:hidden'></iframe>");
				document.writeln('<DIV  style="z-index:9999;right:0;bottom:20px; position:absolute; " id="guide" >');
				movePopup();
			}else{
				
				document.writeln('<DIV  style="z-index:9999;right:0;bottom:20px;position:fixed; " id="guide" >');
			}
		} else {
			if (navigator.appVersion.match(/MSIE [0-7]\./)) {
				
				document.writeln('<DIV  style="z-index:9999;right:0;bottom:20px;position:absolute; top:expression(eval(document.body.scrollTop)+eval(document.body.clientHeight)-this.style.pixelHeight);" id="guide" >');
			} else {
				
			document.writeln('<DIV  style="z-index:9999;right:0;bottom:20px;POSITION:fixed;" id="guide" >');
			}
		}
		
		//窗口内容
		if (typeof(mag_notice) == 'undefined' || mag_notice == '') {
			mag_notice = '没有公告';
		}
		
		document.write('<!--guide start-->\n	<!--guide_main start-->\n	<div id="guide_main">\n		<!--guide_title start--->\n		<div id="guide_title">\n			<div id="guide_title_L"></div>\n			<div id="guide_title_M">\n			  <div id="guide_title_M_tip">精彩推荐\n			  </div>\n				<div id="guide_title_M_close">\n					<a title="关闭" onclick="hiddenContent();return false;" href="#"><img src="/views/normal/images/guide_title_M_close.gif" width="14" height="13" border="0" /></a>\n				</div>\n			</div>\n			<div id="guide_title_R"></div>\n		</div>\n		<!--guide_title end--->\n		<!--guide_content start--->\n		<div id="guide_content">\n			<div id="guide_content_inner">\n\n		     </div>\n			<div id=\"guide_marqueetxt\"><span class=\"gtitle\">网站公告：</span><span><marquee onmouseout=\"this.start()\" onmouseover=\"this.stop()\" scrollamount=3>' + mag_notice + '</marquee></span></div>		</div>\n		<!--guide_content end--->\n	</div>\n	<!--guide_main end-->\n	<!--guide_logo start-->\n	<div id="guide_logo">\n		<a onfocus="this.blur();" onclick="showContent();" title="点击查看精彩推荐">\n			<img src="'+ sina_logo +'" border="0" id="sina_logo" />\n		</a>\n	</div>\n	<!--guide_logo end-->	\n');
	// width="' + width + '" height="' + height + '"
		
	document.writeln('</DIV>');

	if (isShow) {
		document.getElementById("guide").style.width = "137px";//"400px";117
		document.getElementById("guide").style.height = "158px";//"200px";128
	} else {
		document.getElementById("guide").style.width = "74px";//"400px";
		document.getElementById("guide").style.height = "112px";//"200px";
	}
		
		
	if (isShow) {
		clickPage('DivTitle', 1, 4);
		showContent();
	}
	
	}
}

function getAuthCode(id){
	obj = document.getElementById(id);
	var	img_src = '/index.php?action=auth_code';
	if (typeof(obj) == 'object' && obj.src != img_src)
	{
		obj.src = img_src;
	}
}

function reportVisitData(){
	var visitlog = 'visitlog';
	var now = new Date();
	var d = new Date(now.getFullYear(),now.getMonth()+1,0);
	var exp = new Date(d.getFullYear(),d.getMonth(),d.getDate(),23,59,59);
	
	var str = getCookie(visitlog);
	var new_days = 0;
	if (str == null)
	{
		days = new_days = 1;
	} else {
		arr = str.split('_');
		days = arr[0];
		post_date = new Date();
		post_date.setTime(arr[1]);
		if (post_date.getDate()<now.getDate()){
			new_days = parseInt(days)+1;
		}
	}

	if (new_days>0){
		setCookie(visitlog,new_days+'_'+now.getTime(),exp);
		if (new_days == 1 || new_days >= 2)
			new Ajax.Request('/stat.php?action=visit&days='+new_days,{method: 'get'});
	}
}

reportVisitData();
showGuide();