window.addEvent('domready', function(){
	setToUrl();
	resetResult();
	var curi = new URI(window.location.href);
	var param = curi.get('data');
	var mid = param.no;
	if(mid != null) {
		doGetSingleMaterial();	
	}
});
function resetResult() {
	if($('resultTable').hasClass('hdStyle'))
		$('resultTable').removeClass('hdStyle');
	if(!$('noResult').hasClass('hdStyle'))
		$('noResult').addClass('hdStyle');	
}
function noResult() {
	if(!$('resultTable').hasClass('hdStyle'))
		$('resultTable').addClass('hdStyle');
	if($('noResult').hasClass('hdStyle'))
		$('noResult').removeClass('hdStyle');
}
function doGetSingleMaterial() {
	var mid = $('hd_material_no').get('value');
	Material.getSingleMaterial(mid, function(response){
		if(response.resultCode == "00000") {
			var title = response.title;
			var category = response.category;
			var bdate = response.builddate;
			var viewcount= response.viewcount;
			var refdoc = response.refdoc;
			var fckurl = response.fckurl;
			bdate = dateFormat(bdate, ".");
			$('category').set('text', category);
			$('title').set('text', title);
			$('builddate').set('text', bdate);
			$('builddate2').set('text', bdate);
			$('viewcount').set('text', viewcount);
			fullContent(fckurl);
			fullDownloadData(refdoc);
		}else if(response.resultCode == "00010") {
			noResult();
		}else {
			alert('讀取頁面失敗');
			window.location.href = "index.jsp";
		}
	});
}
function dateFormat(str, separ) {
	return str.substr(0,4)+separ+str.substr(4,2)+separ+str.substr(6,2);
}
function fullDownloadData(data) {
	var len = data.length;
	var mainTB = $('downloadTB');
	var curl = window.location.href;
	if(curl.indexOf("?") != 1) {
		curl = curl.substring(0, curl.indexOf("?"));
	}
	resetDownloadTB();
	if(len > 0) {
		if((mainTB != null) && (mainTB.hasClass)) {
			mainTB.removeClass('hdStyle');
		}
//		<td height="16" colspan="2" align="left" valign="top" class="line_bg">&nbsp;</td>
		tr_top = mainTB.rows[0];
//		var tr_sep1 = new Element('tr');
		var tr_sep1 = mainTB.insertRow(1);
		var td_sep = new Element('td', {
			'height' : '16',
			'colspan' : '2',
			'align' : 'left', 
			'valign' : 'top',
			'class' : 'line_bg'
		});
		td_sep.inject(tr_sep1);
//		tr_sep1.injectAfter(tr_top);
		for(var i=0;i<len;i++) {
			var len2 = mainTB.rows.length;
			var tr = mainTB.insertRow(len2);
//			var tr = new Element('tr');
			var fid = data[i].fid;
			var foname = data[i] .foname;
//			if(foname.indexOf(".") != -1) {
//				foname = foname.substring(0, foname.indexOf("."));
//			}
			var header_td = new Element('td', {
				'width' : '3%',
				'height' : '25',
				'align' : 'center',
				'valign' : 'middle'
			});
			var img = new Element('img', {
				'src' : 'images/arrow04.jpg',
				'width' : '8',
				'height' : '9'
			});
			img.inject(header_td);
			header_td.inject(tr);
			
			var data_td = new Element('td', {
				'width' : '97%',
				'height' : '25',
				'align' : 'left',
				'valign' : 'middle'
			});
			var path = "info.jsp";
			var dpage = "getfile.do?source="+fid;
			var durl = curl.replace(path, dpage);
			var data_link = new Element('a', {
				'href' : durl,
				'class' : 'gary12Copy',
				'text' : foname
			});
			data_link.inject(data_td);
			data_td.inject(tr);
//			tr.injectAfter(tr_sep1);
		}
//		var tr_sep2 = new Element('tr');
		len = mainTB.rows.length;
		var tr_sep2 = mainTB.insertRow(len);
		var td_sep2 = new Element('td', {
			'height' : '16',
			'colspan' : '2',
			'align' : 'left', 
			'valign' : 'top',
			'class' : 'line_bg'
		});
		td_sep2.inject(tr_sep2);
//		tr_sep2.inject(mainTB);
	}else {
		if((mainTB != null) && (!mainTB.hasClass)) {
			mainTB.addClass('hdStyle');
		}
	}
}
function fullContent(url) {
	var mainTD = $('content');
	if(mainTD != null) {
		var fme = new IFrame({
			'src' : url,
			'frameborder' : '0',
			'marginheight' : '0',
			'marginwidth' : '0',
			'scrolling' : 'auto',
			'align' : 'top',
			'width' : '100%',
			'height' : '1000',
			'events' : {
				'load' : function() {
//					alert('this.height : ' + this.contentDocument.body.offsetHeight);
					dyniframesize(this);
				}
			}
		});
		fme.inject(mainTD);
	}else {
		return;
	}
}
function resetContent() {
	var mainTD = $('content');
	if(mainTD != null) {
		mainTD.empty();
	}else {
		return;
	}
}
function resetDownloadTB() {
	var mainTB = $('downloadTB');
	var len = mainTB.rows.length;
	for(var i=1;i<len;i++) {
		mainTB.deleteRow(1);
	}
	if(!mainTB.hasClass('hdStyle')) {
		mainTB.addClass('hdStyle');
	}
}
function getParameter(url, key) {
	if((url.indexOf("?") == -1) || (url.indexOf(key) == -1)) {
		return null;
	}else {	
		var result = "";	
		var sa = url.split(key);
		if(sa[1].indexOf("&") == -1) {
			result = sa[1].replace("=", "");
		}else {
			result = sa[1].replace("=", "");
			result = result.substring(0, result.indexOf("&"));
		}
		return result;
	}
}
function setToUrl() {
	var op = getParameter(window.location.href, "op");
	if(op != null) {
		var toUrl = "";
		switch(op) {
			case '001':
				toUrl = "about/about.jsp";
				break;
			case '002' : 
				toUrl = "about/about_1.jsp";
				break;
			case '003' :
				toUrl = "index.jsp";
				break;
			case '100':
				toUrl = "e_focus/e_focus_0.jsp";
				break;
			case '101':
				toUrl = "e_focus/e_focus_1.jsp";
				break;
			case '102' :
				toUrl = "e_focus/e_focus_2.jsp";
				break;
			case '103' :
				toUrl = "e_focus/e_focus_3.jsp";
				break;
			case '200' :
				toUrl = "e_service/e_service.jsp";
				break;
			default :
				toUrl = "index.jsp";
		}	
	}else {
		var toUrl = "index.jsp";	
	}
	$('toUrl').set('href', toUrl);
}
var getFFVersion=navigator.userAgent.substring(navigator.userAgent.indexOf("Firefox")).split("/")[1];
var FFextraHeight=getFFVersion>=0.1? 16 : 0;

function dyniframesize(iframenameObj) {
  var pTar = null;
//  if (document.getElementById){
//    pTar = document.getElementById(iframename);
//  }else {
//    eval('pTar = ' + iframename + ';');
//  }
  pTar = iframenameObj;

  if (pTar && !window.opera){
    //begin resizing iframe
    pTar.style.display="block"
    if (pTar.contentDocument && pTar.contentDocument.body.offsetHeight){
      //ns6 syntax
      pTar.height = pTar.contentDocument.body.offsetHeight+FFextraHeight;
    }else if (pTar.Document && pTar.Document.body.scrollHeight){
      //ie5+ syntax
      pTar.height = pTar.Document.body.scrollHeight;
    }

  }

}
					