var currentItems = null;
var currentValue = null;
var currentMainValue = null;
var currentSubValue = null;
var currentSubSubValue = null;
	//popupSelectWindow.innerHTML = '';
	//popupSelectWindow.style.visibility = 'hidden';
function changeSubSelect(mi,si)
{
	var popupSelectWindow = $('popupSelectWindow');
	var mainValue = currentItems[mi].value;
	var subValue = currentItems[mi].items[si].value;
	var mainTitle = currentItems[mi].title;
	var subTitle = currentItems[mi].items[si].title;
	$('select_button_'+currentValue).value = mainTitle + ' - ' + subTitle + ' ↓';
	$(currentValue).value  = mainTitle + ' - ' + subTitle + ' ↓';
	$(currentMainValue).value = mainValue;
	$(currentSubValue).value = subValue;
	if($(currentSubSubValue))
	$(currentSubSubValue).value = '';
	popupSelectWindow.style.visibility = 'hidden';
	$('select_button_'+currentValue).disabled = '';
}
function changeSubSubSelect(mi,si,ssi)
{
	var popupSelectWindow = $('popupSelectWindow');
	var mainValue = currentItems[mi].value;
	var subValue = currentItems[mi].items[si].value;
	var subsubValue = currentItems[mi].items[si].items[ssi].value;
	var mainTitle = currentItems[mi].title;
	var subTitle = currentItems[mi].items[si].title;
	var subsubTitle = currentItems[mi].items[si].items[ssi].title;
	$('select_button_'+currentValue).value = mainTitle + ' - ' + subTitle + ' - ' + subsubTitle + ' ↓';
	$(currentValue).value  = mainTitle + ' - ' + subTitle + '-' + subsubTitle + ' ↓';
	$(currentMainValue).value = mainValue;
	$(currentSubValue).value = subValue;
	$(currentSubSubValue).value = subsubValue;
	popupSelectWindow.style.visibility = 'hidden';
	$('select_button_'+currentValue).disabled = '';
}
function popSubSubSelect(index,si,subbuttonid)
{
	var popupSelectWindow = $('popupSelectWindow');
	popupSelectWindow.style.visibility = 'hidden';
	var subsubPopopObject = $('_sub_sub_items_popup_');
	for(i=0;i < currentItems[index].items[si].length;i++)
	{
		var subItem = $('_sub_link_'+currentValue+'_'+i);
		if(i == si) subItem.className = 'selected';
		else subItem.className = 'unselected';
	}
	
	var subsubItems = currentItems[index].items[si].items;
	//alert(subsubItems.length);
	var html = '<table>';
	for(i=0;i<subsubItems.length;i++)
	{
		if(i % 4 == 0) html += '<tr>';
		html += '<td style="padding:4px 8px;"><a class="unselected" id="_sub_sub_link_'+currentValue+'_'+i+'" href="#' + subbuttonid + '" onclick="javascript: changeSubSubSelect('+index+','+si+','+i+ ');">'+subsubItems[i].title+'</a></td>';
		if(i + 1 % 4 == 0) html += '</tr>';
	}
	html += '</table>';
	subsubPopopObject.innerHTML = html;
	popupSelectWindow.style.visibility = 'visible';
}
function changeMainSelect(index,buttonId)
{
	var popupSelectWindow = $('popupSelectWindow');
	popupSelectWindow.style.visibility = 'hidden';
	var subPopupObject = $('_sub_items_popup_');
	for(i=0; i<currentItems.length; i++)
	{
		var mainItem = $('_link_'+currentValue+'_'+i);
		if(i == index) mainItem.className = 'selected';
		else mainItem.className = 'unselected';
	}
	
	var subItems = currentItems[index].items;
//alert(subItems.items);
	var html = '<table>'; 
	for (var i=0;i<subItems.length;i++) {
		if(i % 4 == 0) html += '<tr>';
		if(subItems[i].items.length > 0){
		html += '<td style="padding:4px 8px;"><a class="unselected" id="_sub_link_'+currentValue+'_'+i+'" href="#' + buttonId + '" onclick="javascript:popSubSubSelect('+index+','+i+ ',\''+buttonId + '\');">'+subItems[i].title+'</a></td>';
		}
		else{
		html += '<td style="padding:4px 8px;"><a class="unselected" id="_sub_link_'+currentValue+'_'+i+'" href="#' + buttonId + '" onclick="javascript:changeSubSelect('+index+','+i+ ');">'+subItems[i].title+'</a></td>';
		}
		if(i + 1 % 4 == 0) html += '</tr>';
	}
	html += '</table>';
	subPopupObject.innerHTML = html;
	popupSelectWindow.style.visibility = 'visible';
}

function showMainSelect(items,valueName,mainValue,subValue,subsubValue)
{
	var popupSelectWindow = $('popupSelectWindow');
	currentItems = items;
	currentValue = valueName;
	
	currentMainValue = mainValue;
	currentSubValue = subValue;
	currentSubSubValue = subsubValue;
	var valueObject = $(mainValue);
	var buttonObject = $('select_button_' + valueName);
	var buttonId = '\'select_button_' + valueName + '\'';
	Element.makePositioned(buttonObject);
	
	var left = buttonObject.offsetLeft + buttonObject.offsetWidth;
	var top = buttonObject.offsetTop;
	if(arguments[5] && arguments[5] == 'hasParentPop')
	{
		left = left + $('popupWindow').offsetLeft;
		top = top + $('popupWindow').offsetTop;
	}
	//window.alert(arguments[1]);
	var value = valueObject.value;
	var selectIndex = 0;
	var topItemSize  = items.length;

	popupSelectWindow.style.left = left + 'px';
	popupSelectWindow.style.top = top + 'px';
	
	var html = '<table><tr><td valign="top" style="border-right:solid 1px #cccccc;"><table><tr><td valign="top" style="border-right:solid 1px #cccccc;"><table>'; 
	for (var i=0; i<topItemSize; i++) { 
		if(items[i].value == value) selectIndex  = i;
		
		if(i % 4 == 0) html += '<tr>';
		html += '<td style="padding:2px 8px;"><a class="unselected" class="unselected" id="_link_'+valueName+'_'+i+'" href="#popupSelectWindow" onclick="javascript:changeMainSelect(' + i + ',' + buttonId + ');">' + items[i].title + '</a></td>';
		if(i + 1 % 4 == 0) html += '</tr>';
	}
	html += '</table></td><td valign="top"><div id="_sub_items_popup_"></div>';
	html += '</table></td><td valign="top"><div id="_sub_sub_items_popup_"></div>';
	html += '</td></tr></table>';
	popupSelectWindow.innerHTML = html;
	popupSelectWindow.style.visibility = 'visible';

	//buttonObject.disabled = 'true';
}

function showImageUpload(id)
{
	var upfile = $('upfile_'+id);
	var showUpload = $('showUpload_'+id);
	showUpload.style.width= '480px';
	showUpload.style.height= '60px';
	showUpload.style.FRAMEBORDER=0;
	showUpload.style.scrolling='no';
	upfile.style.visibility = 'visible';
}

function showImageUploadTwoPic()
{
	var upfile = $('upfile');
	var showUpload = $('showUpload');
	showUpload.style.width= '480px';
	showUpload.style.height= '60px';
	showUpload.style.FRAMEBORDER=0;
	showUpload.style.scrolling='no';
	upfile.style.visibility = 'visible';
}

function changeSelect(index)
{
	var popupSelectWindow = $('popupSelectWindow');
	var value = currentItems[index].value;
	var title = currentItems[index].title;
	$('select_button_'+currentValue).value = title+' ↓';
	$(currentValue).value = value;
	popupSelectWindow.style.visibility = 'hidden';
	$('select_button_'+currentValue).disabled = '';
}

function setPopupWindow(object)
{
	try{
		Element.makePositioned(object);
	}catch(e){}
//	var left = object.offsetLeft;
//	var top = object.offsetTop; 
	
	var popupObject = $('popupWindow');
//	popupObject.style.left = left + 'px';
//	popupObject.style.top = top + 'px';
}

function closePopupWindow()
{ 
	var objUpfile = $("upfile");
	var popupObject = $('popupWindow');
	try
	{
		popupObject.innerHTML = '';
		if($('popupSelectWindow'))
		{
			$('popupSelectWindow').style.visibility = 'hidden';
		}
		popupObject.style.visibility = 'hidden';
		objUpfile.style.visibility = 'hidden';
	}catch(e){}
	popupObject = null;
}

function showSelect(items,valueName)
{
	currentItems = items;
	currentValue = valueName;
	
	var popupSelectWindow = $('popupSelectWindow');	
	var valueObject = $(valueName);
	var buttonObject  = $('select_button_' + valueName);
	
	setPopupWindow(buttonObject);
	
	var values = valueObject.value.split('&&');
	var selectIndex = 0; 
	var topItemSize  = items.length;

//	popupSelectWindow.style.left = left + 'px';
//	popupSelectWindow.style.top = top + 'px';
	
	var html = '<table>'; 
	for (var i=0; i<topItemSize; i++) { 
		if(items[i].value == values[0]) selectIndex  = i;
		
		if(i % 4 == 0) html += '<tr>';
		html += '<td style="padding:2px 8px;"><a class="unselected" id="_link_'+valueName+'_'+i+'" href="#" onclick="javascript:changeSelect(' + i + ');">' + items[i].title + '</a></td>';
		if(i + 1 % 4 == 0) html += '</tr>';
	}
	html += '</table>';

	popupSelectWindow.innerHTML = html;
	popupSelectWindow.style.visibility = 'visible';

	buttonObject.disabled = 'true';
}

function showPopupWindow(popupUrl,valueName)
{
	popupObject = $('popupWindow');
	var html = '<table cellpadding="0" cellspacing="0"><tr><td class="formtitle">';
	html += '<a class="formtitle" onclick="javascript:closePopupWindow();" style="cursor:hand;cursor:pointer;">关闭</a>';
	html += '</td></tr><tr><td colspen="2" class="formbody">';
	html += '<div id="formbody"></div>';
	html += '</td></tr></table>';
	popupObject.innerHTML = html;
	
	var urlparts = popupUrl.split('?');
	if(urlparts.length > 1)
		new Ajax.Updater('formbody',urlparts[0],{method:'GET',evalScripts: true,parameters:urlparts[1],onComplete:setPopupWindowPos});
	else
		new Ajax.Updater('formbody',popupUrl,{method:'GET',evalScripts: true,onComplete:setPopupWindowPos});
	
	popupObject.style.visibility = 'visible';
	
	if(valueName != null) setCookie('dn',valueName);
}

function setCookie(key,value)
{
	document.cookie = key + '=' + value;
}

function getCookie(key)
{
	var str = document.cookie;
	var idx = str.indexOf(key+'=');
	if(idx < 0) return null;
	
	var end = str.indexOf(';');
	var value = str.substring(idx, end-idx);
	return value.replace(key+'=','');
}

function setPopupWindowPos()
{
	var popupWindow = $('popupWindow');
	//alert(window.innerWidth + '|' + window.innerHeight);

if(window.navigator.userAgent.indexOf("MSIE")>=1){
		var left = (document.body.scrollWidth - popupObject.getWidth())/2;
		var top = document.documentElement.scrollTop + 100;
}else{
		var left = (window.innerWidth - popupObject.getWidth())/2;
		var top = (window.innerHeight -  popupObject.getHeight())/2 + window.scrollY;
}
	popupObject.style.left = left + 'px';
	popupObject.style.top = top + 'px';
}

function ajaxSubmitForm(formName)
{
	var hasError = false;
	var items = Form.getInputs(formName);
	for (var i=0;i<items.length;i++){ 
		if(validFormItem(items[i],true) != null) hasError = true;
	}
	if(hasError) return;
	try{
		publish_article();
	}catch(e){}
	
	var formObject = $(formName);
	var submitUrl = formObject.action;
	var submitItems = Form.serialize(formObject);
	//alert(submitUrl);
	new Ajax.Updater('formbody',submitUrl,{method:'POST',parameters:submitItems,evalScripts: true});
}

function ajaxSubmitFormGetResult(formName,resultName)
{
	var hasError = false;
	var items = Form.getInputs(formName);
	for (var i=0;i<items.length;i++){ 
		if(validFormItem(items[i],true) != null) hasError = true;
	}
	if(hasError) return;
	try{
		publish_article();
	}catch(e){}
	
	var formObject = $(formName);
	var submitUrl = formObject.action;
	var submitItems = Form.serialize(formObject);
	new Ajax.Updater(resultName,submitUrl,{method:'POST',parameters:submitItems,evalScripts: true});
}

function iframeAutoFit()
{
	try
	{
		if(window!=parent)
		{
			var frameObject = parent.document.getElementById("popup_fream");
        	var h1=0, h2=0;
			frameObject.parentNode.style.height = frameObject.offsetHeight +"px";
        	frameObject.style.height = "10px";
        	if(document.documentElement&&document.documentElement.scrollHeight)
        	{
           		h1=document.documentElement.scrollHeight;
        	}
        	if(document.body) h2 = document.body.scrollHeight;

        	var h=Math.max(h1, h2);
        	if(document.all) {h += 4;}
        	if(window.opera) {h += 1;}
				frameObject.style.height = frameObject.parentNode.style.height = h +"px";
		}
	} catch (ex){}
}
function imageFlashOnChange(id,previewId){
	var obj = $(id);
	var objShow = $(previewId);
	if(obj.value.length < 4) return;
	var ext = obj.value.substring(obj.value.length - 4);
	
	if(ext == '.swf'){
		objShow.innerHTML = '';
		//objShow.innerHTML = '<object id="player" classid=\'clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\' codebase=\'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0\'><param name=\'movie\' value="'+obj.value+'" /><param name=\'quality\' value=\'high\' /><embed src="'+obj.value+'" quality=\'high\' pluginspage=\'http://www.macromedia.com/go/getflashplayer\' type=\'application/x-shockwave-flash\'></embed></object>';
	}else if(ext == '.gif' || ext == '.png' || ext == '.jpg'){
		objShow.style.height = objShow.style.width = '100px';
		//objShow.innerHTML = '<img id="showMedia" width="100" height="100" src= "file:///'+ obj.value + '" />';
		objShow.innerHTML = '<img id="'+id+'showMedia" style="width:100px;height:100px;" />';// src= file:///"'+ obj.value + '" />';
		$(id+"showMedia").src= "file:///"+obj.value;
	}
	else
	{
		// TODO:错误
	}
}
function onKeyTypeSelectChange(id){
	var obj = $(id);
	var objView = $('view_' + obj.id);
	
	obj.value = objView.value;
}