function $(oid)
{
	return (typeof oid == "object")?oid:document.getElementById(oid);
}

//Media Link
function playmedia(strID,strType,strURL,intWidth,intHeight)
{ 
	var objDiv=document.getElementById(strID);
	if (!objDiv) return false;
	if (objDiv.style.display!='none')
	{
		objDiv.innerHTML='';
		objDiv.style.display='none';
	}
	else
	{
		objDiv.innerHTML=makemedia(strType,strURL,intWidth,intHeight);
		objDiv.style.display='block';
	}
}

//Media Build
function makemedia (strType,strURL,intWidth,intHeight) //显示 media
{
	var strHtml;
	switch(strType) {
		case 'wmp':
			strHtml="<object width='"+intWidth+"' height='"+intHeight+"' classid='CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6'><param name='url' value='"+strURL+"'/><embed width='"+intWidth+"' height='"+intHeight+"' type='application/x-mplayer2' src='"+strURL+"'></embed></object>";
			break;
		case "wmv":
			strHtml='<div style="height:6px;overflow:hidden"></div><object classid="clsid:22D6F312-B0F6-11D0-94AB-0080C74C7E95" codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,0,02,902" type="application/x-oleobject" standby="Loading..." width="'+intWidth+'" height="'+intHeight+'"><param name="FileName" VALUE="'+strURL+'" /><param name="ShowStatusBar" value="-1" /><param name="AutoStart" value="true" /><embed type="application/x-mplayer2" pluginspage="http://www.microsoft.com/Windows/MediaPlayer/" src="'+strURL+'" autostart="true" width="'+intWidth+'" height="'+intHeight+'" /></object>';
			break;
		case 'swf':
			strHtml="<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' width='"+intWidth+"' height='"+intHeight+"'><param name='movie' value='"+strURL+"'/><param name='quality' value='high' /><embed src='"+strURL+"' quality='high' type='application/x-shockwave-flash' width='"+intWidth+"' height='"+intHeight+"'></embed></object>";
			break;
		case 'rm':
			strHtml="<object classid='clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA' height='"+intHeight+"' width='"+intWidth+"' viewastext><param name='_ExtentX' value='12726'><param name='_ExtentY' value='8520'><param name='AUTOSTART' value='0'><param name='SHUFFLE' value='0'><param name='PREFETCH' value='true'><param name='NOLABELS' value='0'><param name='CONTROLS' value='ImageWindow'><param name='CONSOLE' value='_master'><param name='LOOP' value='0'><param name='NUMLOOP' value='0'><param name='CENTER' value='0'><param name='MAINTAINASPECT' value='"+strURL+"'><param name='BACKGROUNDCOLOR' value='#000000'></object><br><object classid='clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA' height='32' id='Player' width='"+intWidth+"' VIEWASTEXT><param name='_ExtentX' value='18256'><param name='_ExtentY' value='794'><param name='AUTOSTART' value='-1'><param name='SHUFFLE' value='0'><param name='PREFETCH' value='0'><param name='NOLABELS' value='0'><param name='CONTROLS' value='controlpanel'><param name='CONSOLE' value='_master'><param name='LOOP' value='0'><param name='NUMLOOP' value='0'><param name='CENTER' value='0'><param name='MAINTAINASPECT' value='0'><param name='BACKGROUNDCOLOR' value='#000000'><param name='SRC' value='"+strURL+"'></object>";
			break;
		case 'mp3':
			strHtml="<object width='400' height='65' classid='CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6'><param name='url' value='"+strURL+"'/><embed width='400' height='65' type='application/x-mplayer2' src='"+strURL+"'></embed></object>";
			break;
		case 'wma':
			strHtml="<object width='400' height='65' classid='CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6'><param name='url' value='"+strURL+"'/><embed width='400' height='65' type='application/x-mplayer2' src='"+strURL+"'></embed></object>";
			break;
		case 'media':
			strHtml="<object width='"+intWidth+"' height='"+intHeight+"' classid='CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6'><param name='url' value='"+strURL+"'/><embed width='"+intWidth+"' height='"+intHeight+"' type='application/x-mplayer2' src='"+strURL+"'></embed></object>";
			break;
		case "qt":
				strHtml="<embed src='"+strURL+"' autoplay='true' Loop='false' controller='true' playeveryframe='false' cache='false' scale='TOFIT' bgcolor='#000000' kioskmode='false' targetcache='false' pluginspage='http://www.apple.com/quicktime/'>";
				break;
	}
	return strHtml;
}
 
function isEmail(s) //测试是否是合法的email
{
	var re = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
	return (re.test(s));
}

function str_replace(search, replace, str) //字符串替换
{
	var regex = new RegExp(search, "g");
	return str.replace(regex, replace);
}

function setcookie(name,value,t) //设置cookie值，t为cookie保存时间（分）
{
	var cookieexp = 12*30*24*60*60*1000; //默认1年过期
	var cookiestr=name+"="+value+";";
	var expires = "";
	var d = new Date();
	var t2=(!t)?cookieexp:t*60*1000;
	d.setTime( d.getTime() + t2);
	expires = "expires=" + d.toGMTString()+";";
	document.cookie = cookiestr+ expires+' path=/';
}

function getcookie(name) //获得cookie值
{
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) return "";
	if ( start == -1 ) return "";
	var end = document.cookie.indexOf( ";", len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}

function createAJAX() //创建ajax对象
{
	var xmlhttp;
	try{xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");}
	catch (e1)
	{
		try{xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");	}
		catch (e2){xmlhttp = null;}
	}
	if (!xmlhttp)
	{
		if (typeof XMLHttpRequest != "undefined"){xmlhttp = new XMLHttpRequest();}
		else return false;
	}
	return xmlhttp;
}

function resize_image() //调整图片大小
{
	if (!max_image_width) return;
	var debug = 0;
	if (debug) alert(max_image_width);
	var imgs = document.getElementsByTagName('img');
	for(i=0;i<imgs.length;i++)
	{
		var img = imgs[i];
		if (parseInt(img.width) <= max_image_width) continue;
		var orig = { width : parseInt(img.width), height: parseInt(img.height) };
		var p = max_image_width/orig.width;
		if (debug) alert("p="+p);
		img.style.width = max_image_width +"px";
		img.style.height = parseInt(orig.height*p)+"px";
		img.width = max_image_width ;
		img.height = orig.height*p;
		
		img.title = "点击看大图\n"+orig.width+"x"+orig.height+"\n"+img.title;
		if (debug) alert( img.width+" "+img.height);
		if (img.parentNode.tagName.toLowerCase() != "a")
		{
			img.style.cursor = "hand";
			img.onclick=function ()
			{
				window.open(this.src);
			}
		}
	}
}

function open_child(obj) //打开/关闭子节点 (日志归档)
{
	if (obj && obj.nextSibling && obj.nextSibling.style)
	{
		obj.nextSibling.style.display = (obj.nextSibling.style.display == "")?"none":"";
	}
}

window.loaded = false;

function on_load() //html加载完成(不包括图片)之后执行
{
	if (window.loaded) return;
	window.loaded = true;
	load_blogdata();
	resize_image();
	if (window.id == "")
	{
		showcount();
	}
	else if (window.id == "gb")
	{
		document.write("<script src='"+get_blog_url()+"js/check_form.php?type=guestbook'></script>");
	}
	else
	{
		document.write("<script src='"+get_blog_url()+"js/check_form.php?type=remark'></script>");
	}
}

function load_blogdata() // 载入上一篇博客，下一篇博客，最近发表，最新评论数据
{
	var content = "";
	if ($('nextblog')) content+="nextblog|";
	if ($('previousblog')) content+="previousblog|";
	if ($('lastblog')) content+="lastblog|";
	if ($('lastremark')) content+="lastremark|";
	if ($('hotestblog')) content+="hotestblog|";
	if ($('archive')) content+="archive|";
	if ($('calendar')) content+="calendar|";
	
	if (content == "") return;
	var ajax = createAJAX();
	ajax.open("GET",get_blog_url()+"js/load.php?content="+content+"&id="+window.id,true);
	ajax.onreadystatechange = function ()
	{
		if(ajax.readyState == 4 && ajax.status == 200 && ajax.responseText != "")
		{
			var s = ajax.responseText;
			try
			{
				eval(s);
				if ($('nextblog')) $('nextblog').innerHTML = nextblog;
				if ($('previousblog')) $('previousblog').innerHTML = previousblog;
				if ($('lastblog')) $('lastblog').innerHTML = lastblog;
				if ($('lastremark')) $('lastremark').innerHTML = lastremark;
				if ($('hotestblog')) $('hotestblog').innerHTML = hotestblog;
				if ($('calendar')) $('calendar').innerHTML = calendar;
				if ($('archive')) $('archive').innerHTML = archive;
			}
			catch(e) {}
		}
	}
	ajax.send(null);
}

function get_blog_url() // 得到真实的博客根地址
{
	var old_url = window.blogurl;
	if (!old_url)
	{
		alert("模板错误!请在header.html文件中加入 blogurl 值!");
		return;
	}
	return old_url.replace(get_domain(old_url),get_domain(window.location.toString()));
}

function get_domain(s)  //从URL中取出 域名
{
	s = s.replace(/http:\/\//ig,"");
	var arr = s.split("/");
	return arr[0];
}

function do_showcount(ids) //首页显示阅读次数
{
	var url = get_blog_url()+"count.php";
	var ajax = createAJAX();
	ajax.open("GET",url+"?do=get&ids="+ids,true);
	ajax.onreadystatechange = function ()
	{
		if(ajax.readyState == 4 && ajax.status == 200 && ajax.responseText != "")
		{
			var s = ajax.responseText;
			//alert("ajax response: "+s);
			var arr = s.split("|");
			var d,arr2,o;
			for(i = 0; i < arr.length; i++)
			{
				d = arr[i];
				if (d == "") continue;
				arr2 = d.split(")");
				o = $('count_'+arr2[0]);
				if (!o) continue;
				o.innerHTML = arr2[1];
			}
		}
	}
	ajax.send(null);
}

function showcount() //首页显示阅读次数
{
	var arr = document.getElementsByTagName("span");
	var ids = "";
	for(var i=0;i<arr.length;i++)
	{
		var cc = arr[i].id;
		if (!cc || cc.indexOf("count_") == -1) continue;
		cc = cc.replace("count_","");
		ids += cc + "|";
	}
	do_showcount(ids);
}