/*$Id: shell.js,v 1.29 2004/06/30 08:08:43 fifa2k Exp $*/
function sprintf(){
	if (!arguments || arguments.length < 1 || !RegExp){
		return;
	}
	var str = arguments[0];
	var re = /([^%]*)%('.|0|\x20)?(-)?(\d+)?(\.\d+)?(%|b|c|d|u|f|o|s|x|X)(.*)/;
	var a = b = [], numSubstitutions = 0, numMatches = 0;
	while (a = re.exec(str)){
		var leftpart = a[1], pPad = a[2], pJustify = a[3], pMinLength = a[4];
		var pPrecision = a[5], pType = a[6], rightPart = a[7];
		
		//alert(a + '\n' + [a[0], leftpart, pPad, pJustify, pMinLength, pPrecision);

		numMatches++;
		if (pType == '%'){
			subst = '%';
		}
		else{
			numSubstitutions++;
			if (numSubstitutions >= arguments.length){
				alert('Error! Not enough function arguments (' + (arguments.length - 1) + ', excluding the string)\nfor the number of substitution parameters in string (' + numSubstitutions + ' so far).');
			}
			var param = arguments[numSubstitutions];
			var pad = '';
			if (pPad && pPad.substr(0,1) == "'") pad = leftpart.substr(1,1);
			else if (pPad) pad = pPad;
			var justifyRight = true;
			if (pJustify && pJustify === "-") justifyRight = false;
			var minLength = -1;
			if (pMinLength) minLength = parseInt(pMinLength);
			var precision = -1;
  		if (pPrecision && pType == 'f') precision = parseInt(pPrecision.substring(1));
			var subst = param;
			if (pType == 'b') subst = parseInt(param).toString(2);
			else if (pType == 'c') subst = String.fromCharCode(parseInt(param));
			else if (pType == 'd') subst = parseInt(param) ? parseInt(param) : 0;
			else if (pType == 'u') subst = Math.abs(param);
			else if (pType == 'f') subst = (precision > -1) ? Math.round(parseFloat(param) * Math.pow(10, precision)) / Math.pow(10, precision): parseFloat(param);
			else if (pType == 'o') subst = parseInt(param).toString(8);
			else if (pType == 's') subst = param;
			else if (pType == 'x') subst = ('' + parseInt(param).toString(16)).toLowerCase();
			else if (pType == 'X') subst = ('' + parseInt(param).toString(16)).toUpperCase();
		}
		str = leftpart + subst + rightPart;
	}
	return str;
}

xyShellData = new Array();
xyShellData.extTable = Array(
",.jpg,.jpeg,.bmp,.png,.gif,",
",.mid,.midi,.wav,.mp3,.mpa,.mpga,",
",.swf,",
",.avi,.dat,.mpg,.mpeg,.mov,",
",.htm,.html,.txt,",
",.mht,"
//",.htm,html,.txt",
//",.doc,.rtf,"
);
xyShellData.command = Array(
	"openWin(\"/derup/page/picture.html#%ID%\", \"640x480\", \"\", true)",
	"openWin(\"/derup/page/media.html#%ID%|%EXT%\", \"320x140\", \"\", true)",
	"openWin(\"/derup/page/media.html#%ID%|%EXT%\", \"640x480\", \"\", true)",
	"openWin(\"/derup/page/media.html#%ID%|%EXT%\", \"320x268\", \"\", true)",
	"openWin(\"/derup/page/doc.php/%ID%\", \"800x560\", \"status=yes,resizable=yes,location=yes,menubar=yes,toolbar=yes,scrollbars=yes\", true)",
	"openWin(\"/derup/page/doc.php/%ID%/1.mht\", \"800x560\", \"status=yes,resizable=yes,location=yes,menubar=yes,toolbar=yes,scrollbars=yes\", true)"
//	"openWin(\"/derup/page/doc.php/%ID%/txt\", \"640x480\")",
//	"openWin(\"/derup/page/doc.php/%ID%/doc\", \"640x480\")"
);

function xyShell(){
}

xyShell.prototype.openWin = function(url, size, opt, center){
	opt = opt || ",status=yes,resizable=yes";
  center = center || false;
	var a = size.split("x");
	var fmt = sprintf("width=%dpx,height=%dpx ,%s", a[0], a[1], opt);
  var w = (screen.availWidth > 1280) ? 1280 : screen.availWidth;
  var h = (screen.availHeight > 1024) ? 1024 : screen.availHeight;
  if(true == center){
    var l = (w - a[0]) / 2;
    var t = (h - a[1]) / 2 - 60;
    fmt += ", left=" + l + "px, top=" + t + "px";
  }
	window.open(url, "_blank", fmt);
}

xyShell.prototype.openFavor = function(){
  this.openWin("/derup/page/favor.php", "530x340", "resizable=no,scrolls=no");
}

xyShell.prototype.sendToFavor = function(loc, item, title){
	title = title || "";
	if("" == title){
		var pathInfo = loc.pathInfo;
    var url = "";
		if("" != pathInfo.li){
			url += "|lib-" + pathInfo.li ;
			if("/" != pathInfo.fpi ){
				url += pathInfo.fpi.substr(0, pathInfo.fpi.length - 1);
			}
			if("folder" == item.userData.type){
        if("" != pathInfo.pi && 0 != pathInfo.pi){url += "/" + pathInfo.pi;}
				url += "/" + item.userData.id;
        name = "ÎÄ¼þ¼Ð - " + item.userData.ti;
			}else{
        name = item.userData.ti;
				url += "|res-" + item.userData.id;
			}
		}else if("lib" == item.userData.type){
      name = "×Ó¿â - " + item.userData.ti;
			url += "|lib-" + item.userData.id;
		}
    url = url.replace(/\//g, "$");
		url = "/derup/page/classic/main2.html?" + pathInfo.bi + url;
	}else{
		var name = title;
		var url = loc;
	}
  var s = "/derup/page/favor.php?act=new&name=" + 
  UrlConv(name) + "&url=" + UrlConv(url);
  this.openWin(s, "320x400");
}

xyShell.prototype.downExecute = function(id){
	this.openWin("/derup/page/download.html#" + id +"|execute", "380x80", ",status=yes,resizable=yes", true);
}

xyShell.prototype.download = function(list){
	this.openWin("/derup/page/download.html#" + list, "380x80", ",status=yes,resizable=yes", true);
}

xyShell.prototype.pptInsert = function(id){
	this.openWin("/derup/page/download.html#" + id +"|pptInsert", "380x80", ",status=yes,resizable=yes", true);
}

xyShell.prototype.openSearch = function(){
  this.openWin("/derup/page/search.php", "794 x 432", 
		"status=yes,resizable=yes,location=yes,menubar=yes,toolbar=yes,scrollbars=yes", true);
}

xyShell.prototype.openEdit = function(id){
  this.openWin("/derup/page/edit.php?ruid=" + id , "794 x 432", 
		"status=yes,resizable=yes,location=yes,menubar=yes,toolbar=yes,scrollbars=yes", true);
}

xyShell.prototype.down = function(id){
}

xyShell.prototype.replace = function(s, id, ext){
	s = s.replace("%ID%", id);
	s = s.replace("%EXT%", ext);
	return s;
}

xyShell.prototype.showDetail = function(id){
    this.openWin("/derup/page/detail.php/" + id, "794 x 432", 
		"status=yes,resizable=yes,location=yes,menubar=yes,toolbar=yes,scrollbars=yes");
}

xyShell.prototype.showComment = function(id){
    this.openWin("/derup/page/comment.php/" + id, "794 x 432", 
		"location=1,toolbar=1,menubar=1," +
    "status=1,resizable=yes,scrollbars=yes,left=0px,top=0px");
}

xyShell.prototype.login = function(){
    this.openWin("/derup/page/classic/login.php", "280 x 150");
}

xyShell.prototype.logout = function(){
	 var url = "/derup/user/logout.php?url=" + UrlConv(top.location.href);
	 top.location = url;
}

xyShell.prototype.open = function(id, ext){
	var idx = this.test(ext);
  if(-1 == idx && "pack" != ext){
    this.downExecute(id);
  }else if("pack" == ext){
    this.download(id);
  }else{
    var exec = "this." + this.replace(xyShellData.command[idx], id, ext);
    eval(exec);
  }
}
xyShell.prototype.test = function(ext){
	var l = xyShellData.extTable.length;
	var retval = -1;
	var reg = new RegExp("," + ext + ",","");
	for(var i = 0;i < l;i ++){
		if(reg.test(xyShellData.extTable[i])) break;
	}
	if(i < l){
		retval = i;
	}
	return retval;
}

function UrlConv(url){
  url = url + "";
  var m_url = url;
  var outstring = "";
  var x1 = 0;
  for(x1 = 0 ; x1 < (m_url.length) ; x1++){
    chr = m_url.substr(x1,1);
    if(chr=='&'){
      outstring += "%26";
      continue;
    }
    if(chr=='?'){
      outstring += "%3F";
      continue;
    }
    if(chr==':'){
      outstring += "%3A";
      continue;
    }
    if(chr=="#"){
      outstring += "%23";
      continue;
    }
    outstring += chr;
  }
  return outstring;
}

