var imgObj;
var aw = 20;
var ah = 20;
//var realwidth,realheight;
function checkImg(theURL,winName){
  if (typeof(imgObj) == "object"){
    if ((imgObj.width != 0) && (imgObj.height != 0)){
		realsize = getthumbsize(imgObj.width,imgObj.height);
		OpenFullSizeWindow(theURL,winName, ",width=" + realsize[0] + ",height=" + realsize[1]);
	 }else{
      setTimeout("checkImg('" + theURL + "','" + winName + "')", 100);
	 }
  }
}
function OpenFullSizeWindow(theURL,winName,features) {
  var aNewWin, sBaseCmd;
  sBaseCmd = "toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no";
  if (features == null || features == ""){
    imgObj = new Image();
    imgObj.src = theURL;
    checkImg(theURL, winName)
  }
  else{
    aNewWin = window.open(theURL,winName, sBaseCmd + features);
	 aNewWin.moveTo((window.screen.width-realsize[0]-aw)/2, (window.screen.height-realsize[1]-ah)/2); 
    aNewWin.focus();
  }
}
function getthumbsize(w,h) {
	var rsize = new Array();
	if (w>window.screen.width || h>window.screen.height)
	{
		if (w/h>=window.screen.width/window.screen.height) {
			rsize[0]=window.screen.width;
			rsize[1]=(window.screen.width)*h/w;
		}else{
			rsize[0]=(window.screen.height)*w/h;
			rsize[1]=window.screen.height;
		}
	}else{
		rsize[0]=w+aw;
		rsize[1]=h+ah;
	}
	return rsize;
}// End

// 弹出窗口，调用后请使用body 的resize属性
function winOpen(url, name, scroll, resize) {
	if(url) window.open(url, name, 'width=100,height=100,scrollbars='+scroll+',resizable='+resize);
}

function showMemoTxt(sId) {
	cdWin = window.open(sId, "cdWin", "toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=700,height=400,left=35,top=85");
}

function showMemo(){
	Dialog.info('<div id="placeholder" style="float:center;line-height:150%;margin:4px;width:98%;height:360px;overflow:auto;"></div><div class="close" onclick="exitMemo();"></div>'
						 ,{ windowParameters: {className:'alphacube',resizable: true, width:600, height: 400,title:"my memo"} 
						 }
						 );
	var url = web_dir+'ajax.server.php';
	var pars = 'todo=mm';
	var myAjax = new Ajax.Updater(
	'placeholder',
	url,
	{
	method: 'get',
	parameters: pars
	});
}

function exitMemo(){
	Dialog.closeInfo(); 
}

/*edit tip*/
function cambia(nn,actual) {
	elem = $(nn);
	elem.innerHTML = "<input maxlength=\"100\"type=\"text\" value=\"" + actual.innerHTML + "\" style=\"width:600px;\" onkeypress=\"return guardaEnter(this,event,'" + nn + "')\" onblur=\"return guardaBlur(this,'" + nn + "')\" />";
	elem.firstChild.focus();
}

function guardaEnter(campo,evt,idfld) {
	evt = (evt) ? evt : window.event;
	if (evt.keyCode == 13 && campo.value!="") {
		campo.value = campo.value.replace("/\|/","");

		var url = web_dir+'ajax.server.php';
		var pars = "todo=tip&text="+encodeURIComponent(campo.value);
		var demoAjax = new Ajax.Request(
			url,
			{method: 'post', parameters: pars}
			);

		elem.innerHTML = "<span ondblclick=\"cambia('" + idfld + "',this);\" onmouseover=\"this.className='editable'\" onmouseout=\"this.className=''\">" + campo.value + "</span>";
		return false;
	} else {
		return true;
	}
}

function guardaBlur(campo,idfld) {
	if (campo.value!="") {
		campo.value = campo.value.replace("/\|/","");

		var url = web_dir+'ajax.server.php';
		var pars = "todo=tip&text="+encodeURIComponent(campo.value);
		var demoAjax = new Ajax.Request(
			url,
			{method: 'post', parameters: pars}
			);

		elem.innerHTML = "<span ondblclick=\"cambia('" + idfld + "',this);\" onmouseover=\"this.className='editable'\" onmouseout=\"this.className=''\"></span>";
		elem.firstChild.innerHTML = campo.value;
		return false;
	}
}

function readCookie(name){
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		};
		return null;
	};
function createCookie(name,value,days){
		var expires = "";
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			expires = "; expires="+date.toGMTString();
		};
		document.cookie = name+"="+value+expires+"; path=/";
	};

// JavaScript Document
function SlideBox(container, frequency, direction) {
	if (typeof(container) == 'string') {
		container = document.getElementById(container);
	}
	this.container = container;
	this.frequency = frequency;
	this.direction = direction;
	this.films = [];
	var divs = this.container.getElementsByTagName('div');
	for (var i = 0; i < divs.length; i++) {
		if (divs[i].className == 'slideFilm') {
			divs[i].onmouseover = function(self){return function(){self._mouseover()};}(this);
			divs[i].onmouseout = function(self){return function(){self._mouseout()};}(this);
			this.films[this.films.length] = divs[i];
		}
	}
	this._playTimeoutId = null;
	this._slideTimeoutId = null;
	this._slidable = true;

	this._loop();
}

SlideBox.prototype = {
	_loop : function() {
		var sb = this;
		this._playTimeoutId = setTimeout(function(){sb._slide()}, this.frequency);
	},

	_slide : function() {
		var sb = this;
		var _slide = function() {
			if (!sb._slidable) return;
			var c = sb.container;
			if (sb.direction == 'top') {
				if (c.scrollTop < c.offsetHeight-2) {
					c.scrollTop += 2;
				} else {
					clearInterval(sb._slideTimeoutId);
					sb._loop();
					var ul = c.getElementsByTagName('ul')[0];
					ul.appendChild(c.getElementsByTagName('li')[0]);
					c.scrollTop = 0;
				}
			} else if (sb.direction == 'left') {
				if (c.scrollLeft < c.offsetWidth-2) {
					c.scrollLeft += 2;
				} else {
					clearInterval(sb._slideTimeoutId);
					sb._loop();
					var ul = c.getElementsByTagName('ul')[0];
					ul.appendChild(c.getElementsByTagName('li')[0]);
					c.scrollLeft = 0;
				}
			}
		}
		this._slideTimeoutId = setInterval(_slide, 10);
	},

	_mouseover : function() {
		this._slidable = false;
	},

	_mouseout : function() {
		this._slidable = true;
	}
}

//for remind message

function findPosX(obj) {
	var curleft = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curleft += obj.offsetLeft;
			obj = obj.offsetParent;
		}
	} else if (obj.x) curleft += obj.x;
	return curleft;
}
function findPosY(obj) {
	var curtop = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curtop += obj.offsetTop;
			obj = obj.offsetParent;
		}
	} else if (obj.y) curtop += obj.y;
	return curtop;
}
function showLiveDiv(liveDiv,fixDiv,Words){
	try{
		var div = document.getElementById(liveDiv);
		if(!div){
			div = document.createElement("div");
			div.id = liveDiv;
			div.style.position = "absolute";
			document.body.appendChild(div);
		}
		var content = 
			'<table width="148" height="78" border="0" cellpadding="0" cellspacing="0" background="'+web_dir+common_dir+'img/rmbg.gif">\
			<tr>\
			<td height="4"></td>\
			</tr>\
			<tr>\
			<td height="74" valign="top"><table width="140" border="0" align="center" cellpadding="0" cellspacing="0">\
			<tr>\
			<td width="140" valign="top" style="font-size:12px;text-align:left;padding:0 3px 0 3px;">'+Words+'</td>\
			</tr>\
			<tr>\
			<td style="text-align:right;padding-right:10px;"><span><a href="javascript:closeLiveDiv('+'\''+liveDiv+'\''+');"><img src="'+web_dir+common_dir+'img/rmclose.gif" width="10" height="10" border="0"></a></span></td>\
			</tr>\
			</table></td>\
			</tr>\
			</table>';
		div.innerHTML = content;
		var parentObj = document.getElementById(fixDiv);
		if (!parentObj){
			div.style.display = 'none';
		}else{
			var divLeft = findPosX(parentObj)-55;
			var divTop = findPosY(parentObj)-80;
			div.style.top = divTop+"px";
			div.style.left = divLeft+"px";
			div.style.display = '';
		}
	}catch(e){}
}

function closeLiveDiv(id){
	try{
		document.getElementById(id).style.display = 'none';	
		createCookie('remind_'+id,'yes',3650);
	}catch(e){}
}