var ns4 = document.layers;
var ns6 = document.getElementById && !document.all;
var ie4 = document.all;
var offsetX = 10;
var offsetY = 10;
var toolTipSTYLE="";
var useDialog = window.showModalDialog;

function files(name, type, mode, size, date,channelid,encodeName){
	this.name = name;
	this.type = type;
	this.mode = mode;
	this.size = (this.type == 'DIR') ? -1 : size;
	this.date = date;
	this.channelid = channelid;
	this.encodeName = encodeName;
};
function Directory(name){
	this.relatePath = "";
	this.parentPath = "";
	this.urlRoot = "http://your-web-root/";
	this.aFiles = [];
	this.name = name;
	this.ordName = 1;
	this.ordSize = 1;
	this.ordDate = 1;
};
Directory.prototype.add = function(name, type, mode, size, date, channelid, encodeName) {
	this.aFiles[this.aFiles.length] = new files(name, type, mode, size, date ,channelid, encodeName);
};
Directory.prototype.usort = function( ordFunc, ordType ){//ordType = -1 or 1
	switch(ordFunc){
		case 1:
			this.aFiles.sort(orderName);
			this.ordName = -ordType ;
		break;
		case 2:
			this.aFiles.sort(orderSize);
			this.ordSize = -ordType ;
		break;
		case 3:
			this.aFiles.sort(orderDate);
			this.ordDate = -ordType ;
		break;
	}			
	if( ordType == -1 ){ 
		this.aFiles.reverse();
	}
	this.aFiles.sort(orderDir);
};
Directory.prototype.setHTML = function( ordFunc, ordType, channelid ){
	this.usort( ordFunc, ordType );
	var i;
	if ( showType == 0){
		for (i=0; i<this.aFiles.length; i++){
			var channelid= this.aFiles[0].channelid; 
		}
		if(channelid==undefined) channelid=1; 
		var tableStr    = '<table border=0 cellspacing=0 cellpadding=0 width=100% align=center class=tableborder id="mytable">'
					+ '<tr align=center>'
					+ '<td  class=tablerowl width=30><input type="checkbox" onclick="checkall();" id="chkall" /></td>'
					+ '<td  class=tablerowl height=26></td>'
					+ '<td  class=tablerowl><b><span style="cursor:pointer" onClick="' +this.name+ '.setHTML(1, ' +this.ordName+ ')">Name <IMG height=7 src='+config.awp+'images/sma/order_' +( (this.ordName==-1) ? 'desc':'asc' )+ '.gif width=7 border=0></span></b></td>'
					+ '<td  class=tablerowl width=60><b><span style="cursor:pointer" onClick="' +this.name+ '.setHTML(2, ' +this.ordSize+ ')">Size <IMG height=7 src='+config.awp+'images/sma/order_' +( (this.ordSize==-1) ? 'desc':'asc' )+ '.gif width=7 border=0></span></b></td>'
					+ '<td  class=tablerowl width=180><b><span style="cursor:pointer" onClick="' +this.name+ '.setHTML(3, ' +this.ordDate+ ')">Date <IMG height=7 src='+config.awp+'images/sma/order_' +( (this.ordDate==-1) ? 'desc':'asc' )+ '.gif width=7 border=0></span></b></td>'
					+ '<td  class=tablerowl width=120>属性</td>'
					+ '<td  class=tablerowl width=150><b>Action</b></td></tr>'
					+ '<tr align=center>'
					+ '<td  class=tablerowl width=30></td>'
					+ '<td  class=tablerow  width=20 nowrap><img border=0 src='+config.awp+'images/sma/folder_parent.gif width=16 height=16></td>'
					+ '<td  class=tablerow  align=left><a href=system.php?sys=explorer&channelid='+channelid+'&path=' +this.parentPath+ '><b>.. (上级目录)</b></a></td>'
					+ '<td  class=tablerow></td><td class=tablerow></td><td  class=tablerow></td><td  class=tablerow></td>'
					+ '</tr>';
	
		var fname = '';
		for (i=0; i<this.aFiles.length; i++){
			var oFile = this.aFiles[i];
			if(oFile.type == 'DIR'){
				fname = "<a href=system.php?sys=explorer&channelid="+oFile.channelid+"&path=" +urlEncode(this.relatePath+ "/" +oFile.name)+ "><b>" +oFile.name+ "</b>";
			}else{
				fname = "<b>" +oFile.name+ "</b>";
			}
			tableStr   += '<tr align=center class="item" id="item-'+i+'">'
					+ '<td  class=tablerowl width=30><input  onclick="try{event.cancelBubble=true;}catch(e){event.stopPropagation();}" type="checkbox" id="ids_'+i.toString()+'" value="'+i+'" /></td>'
					+ '<td  class=tablerow  width=20 nowrap><img onMouseover="' +this.name+ '.showTooltip(' +i+ ', this)" onMouseout="' +this.name+ '.showTooltip()" border=0 src='+config.awp+'images/sma/' +oFile.type.toLowerCase()+ '.gif width=16 height=16></td>'
				 	+ '<td align=left><a href="'+this.urlRoot + this.relatePath.substr(2) +'/'+ oFile.name+'" target="_blank">' +fname+ '</a></td>'
					+ '<td align=right>' +setLocaleSize(oFile.size)+ '</td>'
					+ '<td  align=right>' +setLocaleDate(oFile.date)+ '</td>'
    				+ '<td>' +oFile.mode+ '</td>'
    				+ '<td align=center>'
			if (oFile.type!='DIR'){ 
					tableStr+= '<a target="_blank" href="'+this.urlRoot + this.relatePath.substr(2) +'/'+ oFile.name+'">查看</a> '
						+ '<a style="cursor:pointer" onClick="return ' +this.name+ '.act(' +i+ ', \'down\');">下载</a> '
						+ '<a style="cursor:pointer" onClick="return ' +this.name+ '.act(' +i+ ', \'move\');">移动</a> '
			}
			if (oFile.type=='PHP' || oFile.type=='TEXT' || oFile.type=='SCRIPT' || oFile.type=='HTML') tableStr+= '<a style="cursor:pointer" onClick="return ' +this.name+ '.act(' +i+ ', \'edit\');">编辑</a> ';
			if(oFile.type == 'IMG'){
					tableStr+= '<a style="cursor:pointer" onClick="return ' +this.name+ '.act(' +i+ ', \'pic\');">修改</a> ';
			}
			tableStr+= '<a style="cursor:pointer" onClick="return ' +this.name+ '.act(' +i+ ', \'delete\');">删除</a> ';
		    tableStr   += '</td></tr>';
		}
		tableStr +="</table>";
	}
	findObj('divDirectoryTable').innerHTML = tableStr;
	gridTableEvent();
};

Directory.prototype.act = function(idx, action){
	var oFile = this.aFiles[idx];
	var returnVal;
	var fileName='';
	var encodeFileName = '';
	var ext     ='';
	var index   = 0;
	if(oFile){
		index = urlEncode(oFile.name).lastIndexOf('.');
		if(oFile.type == 'DIR'){
			fileName = oFile.name;
			encodeFileName = oFile.name;
			ext = '';
		}else{
			if(index > -1){
				fileName = oFile.name.substr(0,index);
				ext = oFile.name.substr(index);
				encodeFileName = oFile.encodeName.substr(0,oFile.encodeName.lastIndexOf('.'));
			}else{
				fileName = oFile.name;
				encodeFileName = oFile.encodeName;
				ext = '';
			}
		}
		flink = "&path=" +encodeURIComponent(this.relatePath)+ "&ext="+encodeURIComponent(ext)+"&file="+encodeFileName;
		switch(action){
			case 'delete' :
				var url = "system.php?sys=explorer&channelid="+oFile.channelid+"&action=delete" +flink;
				if(arguments.length > 2){
					Ajax.request(url,{method:'get',onComplete: function(resp){
						$('exploreTips').innerHTML = "<font color=red><b>"+resp+"</b></font>";
						setTimeout(removeAllFileItem, 200);
					}});
				}else{
					if( window.confirm('确实要删除 ' +oFile.name+ ' 吗？') ){
						window.location.replace(url);
					}
				}
			break;
			case 'down':
				window.location.href ="system.php?sys=explorer&channelid="+oFile.channelid+"&action=down" +flink;
			break;
			case 'edit' :
				qmeCms.dialogView.iframe("编辑文件", "system.php?sys=explorer&channelid="+oFile.channelid+"&action=editor" +flink, {width:800,type:'abs'});
			break;
			case 'move' :
				qmeCms.dialogView.iframe("移动文件", "system.php?sys=explorer&channelid="+oFile.channelid+"&action=remove_start" +flink, {width:340,type:'abs'});
			break;
			case 'pic':
				var url = this.urlRoot + this.relatePath.substr(2) +'/'+ urlEncode(oFile.name) ;
				picEdit(url, web_path, oFile.channelid);
			break;
		}
	}
	return false;
};

if (navigator.appName == 'Netscape'){
	document.captureEvents(Event.MOUSEMOVE);
	document.onmousemove = netscapeMouseMove;
}
var nE ;
function netscapeMouseMove(e) {
	Ne = e ;
} 
Directory.prototype.showTooltip = function(idx, obj){
	var content, image;
	var toolTipSTYLE = findObj('divTooltip').style;
	var oFile = this.aFiles[idx];
	if(idx!=null){
		var o = fetchOffset(obj);
		toolTipSTYLE.left = (parseInt(o.left)  + 20).toString()+'px';
		toolTipSTYLE.top  = (parseInt(o.top) -  100).toString()+'px';
		toolTipSTYLE.visibility = "visible";
		toolTipSTYLE.display = "block";
		if(oFile.type=='IMG' && this.urlRoot!=null){
	  		src = this.urlRoot + this.relatePath + '/' + oFile.name + "?d="+oFile.date.toString();
			image = '<img align=middle border=1 src="' + src + '" onload="if(this.height>200){this.height=200;}" style="MARGIN-LEFT:0px;">';
	  	}else if(oFile.type=='FLASH' && this.urlRoot!=null){
	  		src = this.urlRoot + this.relatePath + '/' + oFile.name;
		  	image = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width=240 height=200>'
				  + '<param name=movie value="' +src+ '"><param name=menu value=false>'
				  + '<embed src="' +src+ '" width=240 height=200 pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" wmode=transparent menu=false></embed>'
				  + '</object>';
	  	}else{
  			image = '<img align=middle src="'+config.awp+'images/big/' + oFile.type.toLowerCase() + '.gif" height=200 width=200 style="MARGIN-BOTTOM:5px;MARGIN-TOP:5px;">';
  		}
		content = "<table border=0 align=left valign=top cellspacing=0 bgColor=#FFFFFF>"
			+ "<tr><td colspan=2 align=left valign=middle>" + image +"</td></tr>"
			+ "</table>";
		findObj('divTooltip').innerHTML = content;
	}else{
		toolTipSTYLE.visibility = "hidden";
		toolTipSTYLE.display = "none";
	}
};
function orderSize(obj1, obj2){
	return obj1.size - obj2.size ;
}
function orderDate(obj1, obj2){
	return obj1.date - obj2.date ;
}
function orderName(obj1, obj2){
	var str1 = new String(obj1.name);
	var str2 = new String(obj2.name);
	if(str1.toLowerCase() > str2.toLowerCase())
		return 1;
	else if(str1.toLowerCase() == str2.toLowerCase())
		return 0;
	else
		return -1;
}
function orderDir(obj1, obj2){
	var str1 = new String(obj1.type);
	var str2 = new String(obj2.type);
	
	if(str1== 'DIR' && str2!='DIR')
		return -1;
	else if(str1!= 'DIR' && str2=='DIR')
		return 1;
	else 
		return 0;
}
function setLocaleDate (nTime){
	var d, s;
	d = new Date();
	d.setTime( nTime*1000 );
	s = d.toLocaleString();
	return(s);
}
function setLocaleSize(nSize){
    var s;
    if( nSize>=1048576 )
    	s = ( parseInt(nSize*10/1048576)/10 ) +" MB"
    else if( nSize>=1024 )
		s = ( parseInt(nSize*10/1024)/10 ) + " KB"
	else if( nSize == -1 ) // DIR
		s = "";
	else
		s =nSize + " B";
	return(s);
}
function urlEncode(url){
	url = url.replace(/%/g, "%25");
	url = url.replace(/ /g, "%20");
	return url;
}

function findObj(theObj, theDoc){
  var i, foundObj;
  if(!theDoc) theDoc = document;
  if(!(foundObj = theDoc[theObj]) && theDoc.all)
  	foundObj = theDoc.all[theObj];
  for (i=0; !foundObj && i < theDoc.forms.length; i++) 
    foundObj = theDoc.forms[i][theObj];
  if(!foundObj && document.getElementById)
  	foundObj = document.getElementById(theObj);
  return foundObj;
}