var data = [];
var index = 0;
var htmlFlag = 0;
var thread = 4;
var pg = 1;
var timeSign;
var tp = '';
var createIframeIndex = 0;
var createStatus = 0;
(function(){
	if($$B.ie){
		if($$B.ie6 || $$B.ie7){
			thread = 2;
		}else{
			thread = 6;
		}
	}else if($$B.firefox){
		thread = 6;
	}else{
		thread = 4;
	}
})();
function myform_submit()
{
	var obj = document.getElementsByTagName("INPUT");
	var cidArray  =  new Array();
	for (var i=0; i<obj.length; i++){
		if(obj[i].type=='checkbox' && obj[i].id.substr(0,3)=='ids' && obj[i].checked==true){
			cidArray.push(obj[i].value);
		}
	}
	if( cidArray.length == 0) return false;
	else return cidArray;
}
function htmlReady( type, pageno)
{
	if(createStatus){
		if(!window.confirm('当前有内容正在生成中！您是否还继续？') ){
			return false;
		}
	}
	var cidArray = [];
	switch( type)
	{
		case 'a':
		case 'c':
			cidArray = myform_submit();
			if( !cidArray){
				alert("请选择待操作的栏目");
				return true;
			}
		break;

		case 'f':
			if( isNaN(parseInt($('fromid').value)) || isNaN(parseInt($('endid').value))){
				alert("开始ID和结束ID必须是数字");
				return false;
			}
			if( parseInt($('fromid').value) > parseInt($('endid').value)){
				alert("开始ID必须小于或者等于结束ID");
				return false;
			}
			cidArray[0] = $('fromid').value;
			cidArray[1] = $('endid').value;
		break;
		
		case 't':
			cidArray[0] = $('time_start').value;
			cidArray[1] = $('time_end').value;
		break;
		
		case 'm':
			cidArray[0] = $('modify_time_start').value;
			cidArray[1] = $('modify_time_end').value;
		break;

		case 'h':
			return createChannelIndex(channelid);
		break;
	}

	disabledButton(true);
	var link = "system.php?sys=module&mod="+mod+"&channelid="+channelid+"&action=html";
	AJAX.request( link,{
		method		: "post",
		parameters	: "type="+type+"&val="+cidArray.join(','),
		onComplete	: function(json){
			var json = evaljson(json);
			if( json.status == 200){
				if( type == 'd'){ 
					alert('静态页面删除完毕');
					disabledButton(false);
					return true;
				};
				index = 0;
				htmlFlag = 0;
				pg = pageno;
				tp = type;
				data = json.data.split(',');
				createIframe( type);
				window.scroll(0,0);
			}else{
				alert("您所选择的范围不存在相关信息\n或者该数据没有指定为生成静态页面");
				disabledButton(false);
			}
		}
	});
}
function createIframe( type)
{
	createIframeIndex++;
	if( thread > data.length) thread = data.length;
	var iframe = [];
	for(var i=0;i<( type == 'c' ? 1 : thread);i++){
		var iframe = document.createElement('iframe');
		iframe.id = 'iframe--'+i+'-'+createIframeIndex;
		iframe.name = 'iframe--'+i+'-'+createIframeIndex;
		iframe.setAttribute('frameborder', 0);
		iframe.setAttribute('border', 0);
		iframe.src = 'system.php?sys=tohtml&action=other&type='+type+'&channelid='+channelid+'&channelselect='+channelselect;
		iframe.style.display = 'none';
		document.body.appendChild(iframe);
	}
}
function clearData()
{
	var dataTemp = new Array();
	for( var i=0 ; i<data.length; i++){
		if( data[i] != 0){
			dataTemp.push(data[i]);	
		}
	}
	if(dataTemp.length > 0){
		index = 0 ;
		data  = dataTemp;
		return true;
	}else{
		return false;
	}
}

function disabledButton(status){
	var o = document.getElementsByTagName('input');
	for(var i=0;i<o.length;i++){
		if(o[i].type.toLowerCase() == 'button' || o[i].type.toLowerCase() == 'submit'){
			o[i].disabled = status;
		}
	}
	if(status == true){
		$$E.addEvent(window, 'beforeunload', waringError);
		createStatus = 1;
	}else{
		$$E.removeEvent(window, 'beforeunload', waringError);
		createStatus = 0;
	}
};
function uncheckedAll(){
	Each(document.getElementsByTagName('input'), function(){
		if(this.type.toLowerCase() == 'checkbox') this.checked = false;
	});
}

function waringError(e){
	var e = e || window.event;
	e.returnValue = "有些静态文件的生成还在进行中，你确定要离开吗？";
}