function bubble(e){
	if(!e){var e=window.event};
	try{e.cancelBubble = true;}catch(e){}
}
function strrgbtohex(color) {
	
	if(color.indexOf("rgb")>-1){
		var c1 = parseInt(color.substring(4, color.indexOf(',')));
		var c2 = parseInt(color.substring(color.indexOf(',')+1, color.lastIndexOf(',')));
		var c3 = parseInt(color.substring(color.lastIndexOf(',')+1,color.length-1));

		return '#' + calc10to16(c1) + calc10to16(c2) + calc10to16(c3);
	}else{
		return color;
	}
}

function editstyle(element, element_key){
	var content = null;
	var del = null;
	var edit = null;
	var menu = null;
	
	try{content = idx('content_box_'+element);}catch(err){}
	try{del = idx('delete_box_'+element);}catch(err){}
	try{edit = idx('edit_box_'+element);}catch(err){}
	try{menu = idx('tool_box_'+element);}catch(err){}
	
	if(menu.style.display=='none'){
		if(old_element!=null) {
			alert('您的另一个模块正在编辑，请先关闭，再进行此模块的编辑。');
			return;
		}
		showHide(0);
		rememberCSS(element);
		edit_mode=true;
		edit.innerHTML = '取消样式编辑';
		if(menu != null) menu.style.display='';
		if(content != null) content.style.display='none';
		if(del != null) del.style.display='none';
//		idx('save_box_'+element).style.display='';
		
		frm.element_name.value = element;
		frm.element_type.value = 0;
		frm.element_key.value = element_key;

//		frm.submittype.value='preview';
//		frm.action='waynprofile.html?wci=view#box_'+element;
//		frm.submit();
	}else{	
		if(frm.submittype.value == 'preview'){
			if(confirm('您的改动还没有保存, 确定取消吗?'))
				document.location="waynprofile.html";
			else
				return;
		}
		edit_mode=false;
		showHide(1);
		restoreCSS(element);
		edit.innerHTML = '样式编辑';
		if(menu != null) menu.style.display='none';
		if(content != null) content.style.display='';
		if(del != null) del.style.display='';
//		idx('save_box_'+element).style.display='none';
		cancel();
	}
	frm.panel.value='0';
	try{e.cancelBubble = true;}catch(e){}
}

function rememberCSS(element){
	if(old_element!=null){
		editstyle(old_element);
	}
	old_element = element;
}
function restoreCSS(element){
	old_element=null;
}
function cancel(){
	//show video element
	try{idx('obj_box_videos').style.visibility = "";}catch(err){}
	try{idx('obj_box_trail_map').style.visibility = "";}catch(err){}
	//hide toolbar
	try{objColorPanel.style.display='none';}catch(e){};
	idx('id_dialog').style.display='none';
}


function element_edit(e,epart,panel){
	if(isdragging) return;
	
	changetab(panel, frm.element_name.value);
	frm.panel.value=panel;
	frm.element_part.value=epart;
	
	loadStyleFromCSS();

	if(epart.indexOf('body')>-1){
		idx('disable_font').style.display='';
		idx('disable_size').style.display='';

		idx('disable_link').style.display='';
		idx('testlink').style.display='';
		idx('testcolumn').style.display='';
		idx('disable_image').style.display='none';
		if (frm.element_name.value=='profile' || frm.element_name.value=='interests'){idx('disable_column').style.display='';}
		else{idx('disable_column').style.display='none';}
	}else{
		idx('disable_font').style.display='none';
		idx('disable_size').style.display='none';
		idx('disable_link').style.display='none';
		idx('testlink').style.display='none';
		idx('disable_column').style.display='none';
		idx('testcolumn').style.display='none';
		idx('disable_image').style.display='';
	}
	if(epart.indexOf('foot')>-1){
		idx('disable_txcolor').style.display='none';
	}else{
		idx('disable_txcolor').style.display='';
	}
	if(sfrm.forall.value==1) idx('xforall').checked=true;

	//hide video element
	//try{idx('obj_box_videos').style.visibility = "hidden";}catch(err){}
	//try{idx('box_trail_map').style.visibility = "hidden";}catch(err){}
	idx('id_dialog').style.left = getDialogX(e, 180)+'px';
	idx('id_dialog').style.top  = getDialogY(e, 160) + 'px';
	
	//idx('idlabel').src = 'images/panel/label_edit_part.gif';
	idx('id_dialog').style.display="";
}

function loadStyleFromCSS(){
	var subs = frm.element_part.value.substring(3,4);
	var tmp='';

	try{
		currentColumnName = idx('box_'+frm.element_name.value).parentNode.parentNode.id;
		sfrm.cur_photo_key.value = eval('js_'+frm.element_name.value + '_'+ subs + '_bg_key');
	}
	catch(err) {}
//	idx('testimg').style.background = "#ffffff";
	idx('testfontbg').style.background = "";
	idx('testcolumn').style.color = "";
	idx('testimg').style.background = "";
	idx('idimage_testbg').style.background = "";

	if(frm.element_type.value==1)
	{
		//load from js container variables
		try{ idx('testimg').style.background = eval('js_'+frm.element_name.value); }catch(e){};
	}else
	{
		/*
		try{
			idx('testimg').style.background = eval('js_' + frm.element_name.value + '_' + subs + '_bg'); 
		}catch(err){
			//try use all value
			try{idx('testimg').style.background = eval('js_all_' + subs + '_bg'); }catch(err){};
		};
		try{idx('idcolor_testbg').value = strrgbtohex(idx('testimg').style.backgroundColor); }catch(err){};
		try{
			if(eval('js_'+frm.element_name.value + '_'+ subs + '_bg_key') > 0){
				frm.cur_photo_key.value = eval('js_'+frm.element_name.value + '_'+ subs + '_bg_key'); 
			}
		}catch(err){};
		*/
	
	}
	/////////////////////////////////
	var preview = null;
	if(frm.panel.value==3) {
		var brd,trans;
		try{
			brd = eval('js_'+frm.element_name.value+'_border');
			trans = eval('js_'+frm.element_name.value+'_transparent');
		}catch(err){
			try{
				brd = eval('js_all_border');
				trans = eval('js_all_transparent');
			}catch(err){trans=''}
		}
		try{
			sfrm.css_border.value = brd;
			idx('border-color').style.border = brd;
			idx('border-color').value = brd.substring(brd.lastIndexOf(' ')+1,brd.length);
			idx('border-width').value = brd.substring(0, brd.indexOf(' '));
			idx('border-style').value = brd.substring(brd.indexOf(' ')+1, brd.lastIndexOf(' '));
			idx('id_dialog').style.opacity = trans;
			idx('id_dialog').style.filter = 'alpha(opacity='+trans+')';
			for (var i=0; i<5; i++){
				if(idx('box_transparent').options[i].value==trans){
					idx('box_transparent').selectedIndex=i;
					break;
				}
			}
		}catch(err){}

	}

	if(frm.panel.value==2) {

		//read column color
		try{idx('testcolumn').style.color = eval('js_' + frm.element_name.value + '_t_c'); }catch(err){
			try{idx('testcolumn').style.color = eval('js_all_t_c'); }catch(err){}
		}
		//read link color
		try{idx('testlink').style.color = eval('js_' + frm.element_name.value + '_l'); }catch(err){
			try{idx('testlink').style.color = eval('js_all_l'); }catch(err){}
		}

		
		//read body color

		try{
			idx('testfontbg').style.background = eval('js_' + frm.element_name.value + '_'+subs+'_bg'); 
		}catch(err){
			//read from all
			try{idx('testfontbg').style.background = eval('js_all_'+subs+'_bg'); }catch(err){};
		}

		//image preview color
		try{
			if(idx('testfontbg').style.background.indexOf('url')>-1) 
				idx('testimg').style.background = idx('testfontbg').style.background;
		}catch(err){}
		

		preview = idx('testfont');
		//font color from element
		try{preview.style.color = eval('js_'+frm.element_name.value+'_'+subs+'_c');}catch(e){
			//from all
			try{preview.style.color = eval('js_all_'+subs+'_c');}catch(e){
				if(subs=='b') preview.style.color = '#000000';
			};
		};

		//from element
		try{preview.style.font = eval('js_'+frm.element_name.value+'_'+subs+'_f');}catch(e){
			//try get font from all
			try{preview.style.font = eval('js_all_'+subs+'_f');}catch(e){
				if(subs=='h')
					preview.style.font = 'bolder 12px/19px 新宋体';
				else
					preview.style.font = 'normal 12px/19px 新宋体';

			};
		};

		idx('idcolor_testfont').value = strrgbtohex(preview.style.color);
		idx('idcolor_testlink').value = strrgbtohex(idx('testlink').style.color);
		idx('idcolor_testbg').value = strrgbtohex(idx('testfontbg').style.backgroundColor);
		if(idx('idcolor_testbg').value=='transparent') idx('idcolor_testbg').value='';
		idx('idcolor_testcolumn').value = strrgbtohex(idx('testcolumn').style.color);
		
		try{
			eval('document.saveForm.css_'+subs+'_background_key').value = eval('js_'+frm.element_name.value+'_'+subs+'_bg_key');
		}catch(err){}

		if(frm.element_part.value=='boxbody'){
			//set form fields
			idx('font-weight').value = preview.style.fontWeight;
			idx('font-style').value = preview.style.fontStyle;
			idx('font-family').value = preview.style.fontFamily;
			idx('font-size').value = preview.style.fontSize;
		//	idx('line-height').value = '/'+preview.style.lineHeight;

			//set buttons
			if(preview.style.fontWeight=='bolder')
				idx('b1').style.backgroundColor = '#ffffff';
			else
				idx('b1').style.backgroundColor = 'buttonface';

			if(preview.style.fontStyle=='italic')
				idx('b2').style.backgroundColor = '#ffffff';
			else
				idx('b2').style.backgroundColor = 'buttonface';

			if(preview.style.textDecoration=='underline')
				idx('b3').style.backgroundColor = '#ffffff';
			else
				idx('b3').style.backgroundColor = 'buttonface';
		}
	}
}

function getDialogX(e, sub){
	return (screenW/2) - sub;
}
function getDialogY(e, sub){
	return ((screenH/2) + document.documentElement.scrollTop) - sub;
}

function setBoxStyle(n,self){
	var strStyle='';
	var obj = idx('testfont');
	
	switch(n){
		case 0:
			if(obj.style.fontWeight == 'bolder'){
				strStyle = 'normal';
				self.style.backgroundColor='buttonface';
			}else{
				strStyle = 'bolder';
				self.style.backgroundColor='#ffffff';
			}

			obj.style.fontWeight = strStyle;
			idx('font-weight').value = strStyle;
			break;
			
		case 1:
			if(obj.style.fontStyle == 'italic'){
				strStyle = 'normal';
				self.style.backgroundColor='buttonface';
			}else{
				strStyle = 'italic';
				self.style.backgroundColor='#ffffff';
			}
			obj.style.fontStyle = strStyle;
			idx('font-style').value = strStyle;
			break;
		case 2:
			if(obj.style.textDecoration == 'underline'){
				strStyle = 'none';
				self.style.backgroundColor='buttonface';
			}else{
				strStyle = 'underline';
				self.style.backgroundColor='#ffffff';
			}
			obj.style.textDecoration = strStyle;
			idx('text-decoration').value = strStyle

			break;
	}
}

function fn_onscroll(){
	var adder = 0;
	adder = (document.documentElement.scrollTop - test)/10;
	test += adder;
	idx('idscrollmenu').style.top = test;
	if(Math.abs(adder)>1) window.setTimeout('fn_onscroll()', 75);
}
document.documentElement.onscroll = function(){
	try{
		test = parseInt(idx('idscrollmenu').style.top);
		window.setTimeout('fn_onscroll()', 500);
	}catch(err){}
}
function scrollMenu(add){
	var w = parseInt(idx('idmenuoptions').style.width);
	w += add;
	if( (add <0 && w > -2) || (add >0 && w < 120) )
	{
		idx('idmenuoptions').style.width = w + 'px';
		window.setTimeout('scrollMenu('+add+')', 5);
	}
}
function scrollActionMenu(direction, target){
	if(direction == 0){
		try{idx('idmenuoptions').style.display = 'none';}catch(err){}
		try{idx('idmenuoptions2').style.display = 'none';}catch(err){}
		try{idx('idmenuoptions3').style.display = 'none';}catch(err){}
	}
	else{
		if(target == 'idmenuoptions') idx(target).style.display = '';
		if(target == 'idmenuoptions2') idx(target).style.width = screenW-70+'px';
		if(target == 'idmenuoptions2') idx(target).style.display = '';
		if(target == 'idmenuoptions3') idx(target).style.display = '';
	}
}
function cancelBuble(e){
	if(!e){var e=window.event};
	try{e.cancelBubble = true;}catch(err){}
}
function showSubmenu(type){
	if(type==1){
		if(idx('id_submenu').style.display=='block'){
			idx('id_submenu').style.display='none';
			idx('id_profile').src=appproxypath+'friends/images/sort_down.gif';
		}else{
			idx('id_submenu').style.display='block';
			idx('id_profile').src=appproxypath+'friends/images/sort_up.gif';
		}
	}else{
		idx('id_submenu').style.display='none';
		idx('id_profile').src=appproxypath+'friends/images/sort_down.gif';
	}
}
function savestyle2(){
	var tmpObj=null;
	var part = frm.element_part.value;
	var element = frm.element_name.value;
	var panel = frm.panel.value;
	var tmpAtr = "";
	sfrm.forall.value = (idx('xforall').checked==true) ? 1 : 0;
	sfrm.element_part.value = part;

	if(frm.element_type.value==1){
		/*
			COUNTAINER ELEMENTS LIKE ( BODY, HEADER, PANEL, MENU )
		*/
		
		if(panel==5){
			//header
			tmpBackground = idx('idcolor_testbg5').value;
			if(sfrm.cur_photo_key.value!='') sfrm.css_header_key.value = sfrm.cur_photo_key.value;
			if(tmpBackground!='') sfrm.css_header.value = tmpBackground;
		}else
		if(panel==4){
			//menu
			tmpObj = idx('idimage_testbg4');
			tmpBackground = idx('idcolor_testbg4').value;
			var tmpLink = idx('idcolor_testlink4').value;

			eval('document.saveForm.css_'+frm.element_name.value).value = tmpBackground;
			eval('document.saveForm.css_'+frm.element_name.value+'_link').value = tmpLink;
		}
		else
		{
			//body & panel
			tmpObj = idx('idimage_testbg');

			if(tmpObj.style.background != ''){
				tmpBackground = idx('idcolor_testbody').value;

				if(tmpObj.style.backgroundImage!='none'){
					tmpAtr = ' ' + idx('imgrepeat').value;
					tmpAtr += ' ' + idx('imgattachment').value;
					tmpAtr += ' ' + idx('imgpositiony').value;
					tmpAtr += ' ' + idx('imgpositionx').value;
				}
				if(frm.element_name.value=='panel') {
					//reset body if panel because they are using the same toobar form
					document.saveForm.css_body.value='';
					document.saveForm.css_body_key.value='';
					document.saveForm.css_body_atr.value='';
				}
				eval('document.saveForm.css_'+frm.element_name.value).value = tmpBackground;
				eval('document.saveForm.css_'+frm.element_name.value+'_key').value = sfrm.cur_photo_key.value;
				eval('document.saveForm.css_'+frm.element_name.value+'_atr').value = tmpAtr;
			}
		}

	}else{

		/*
			NORAML BOX ELEMENTS 
		*/
		switch(parseInt(panel)){

			case 2://fonts
				var tmpFont = "";
				var tmpTextDecoration = "";
				var tmpTextAlign = "";
				var tmpFontColor = "";
				var tmpColumnFontColor = "";
				
				tmpFont += idx('font-style').value;
				tmpFont += ' ' +  idx('font-weight').value;
				tmpFont += ' ' +  idx('font-size').value;
				if(parseInt(idx('font-size').value)>0) tmpFont += '/' +  parseInt(parseInt(idx('font-size').value) * 1.65) + 'px';
				tmpFont += ' ' +  idx('font-family').value;

				//clean if equal to default font value from css
				if(tmpFont == 'normal normal 12px/18px 新宋体') tmpFont = '';

				tmpTextDecoration = idx('text-decoration').value;
				tmpFontColor = idx('idcolor_testfont').value;
				tmpColumnFontColor = idx('idcolor_testcolumn').value;
				if(part.indexOf('head')>-1){
					sfrm.LColumn.value = '';
					sfrm.RColumn.value = '';
					sfrm.css_h_background.value = idx('idcolor_testbg').value;
					sfrm.css_h_color.value = tmpFontColor;
					//if(sfrm.cur_photo_key.value!='') sfrm.css_h_background_key.value = sfrm.cur_photo_key.value;
					sfrm.css_h_background_key.value = sfrm.cur_photo_key.value;

				}else
				if(part.indexOf('foot') > -1){
					sfrm.css_footer.value = idx('idcolor_testbg').value;
					//if(sfrm.cur_photo_key.value!='') sfrm.css_footer_key.value = sfrm.cur_photo_key.value;
					sfrm.css_footer_key.value = sfrm.cur_photo_key.value;
				}else{
					sfrm.css_b_background.value = idx('idcolor_testbg').value;
					sfrm.css_b_color.value = tmpFontColor;
					sfrm.css_b_font.value = tmpFont;
					sfrm.css_b_txdecor.value = tmpTextDecoration;
					sfrm.css_link.value = idx('idcolor_testlink').value;
					sfrm.css_t_color.value = tmpColumnFontColor;
				}
				break;

			case 3:
				sfrm.css_border.value = idx('border-width').value+' '+idx('border-style').value+' '+idx('border-color').value;
				sfrm.css_transparent.value = idx('box_transparent').value;
				break;
		}

	}	
	
//	frm.submittype.value='preview';
//	frm.action='waynprofile.html?wci=view#box_'+element;
//	frm.submit();
}

function editcontent(element, el_key, iFrameKey){
//	if(confirm('您确定编辑这个模块的内容吗?')){
	return false;
		switch(element){
			case 'interests':
			case 'favorites':
			case 'details':
				document.location = 'wayn.html?wci=editprofile&advprof=1';
				break;
			case 'aboutme':
				editPersonalNote();
				break;
			case 'profile':
				document.location = 'wayn.html?wci=editdetails&advprof=1';
				break;
			case 'locations':
				document.location = 'wayntrips.html?wci=trips';
				break;
			case 'competition':
				document.location = 'waynprofile.html?wci=competition';
				break;	
			default:
				document.location = 'waynprofile.html';
				break;
		}
//	}
}
function loading(){
	try{
		if (navigator.userAgent.indexOf("MSIE") >= 0){
			document.frames("imgresults").document.body.innerHTML="<html><body style='background:#ffffff;'><br/><br/><br/><center><b style='color:lightBlue'>please wait...</b></center></body></html>";
		}else{
			frames['imgresults'].document.body.innerHTML = "<html><body style='background:#ffffff;'><br/><br/><br/><center><b style='color:lightBlue'>please wait...</b></center></body></html>";
		}
	}catch(err){}
}
function browseImages(e){
	if(!e){var e=window.event};
	photo_type = '';
	loading();
	
	switch (frm.element_part.value){
		case 'head':
		photo_type = 'head';
		break;
		case 'body':
		photo_type = 'body';
		break;
		case 'panel':
		photo_type = 'panel';
		break;
		case 'boxhead':
		photo_type = 'boxhead';
		break;
		case 'boxfoot':
		photo_type = 'boxfoot';
		break;
	}
	idx('imgresults').src='imgBrowser.asp?photo_type='+photo_type+'&rnd='+Date.parse(new  Date());

	//idx('imgheadtext').innerHTML = 'photo for '+frm.element_name.value;
	idx("id_dialog").style.display='none';
	idx("image_panel").style.left = getDialogX(e, 225) + 'px';
	idx("image_panel").style.top = getDialogY(e, 275) +'px';
	idx("image_panel").style.display="";

	try{e.cancelBubble = true;}catch(e){}
}

function addpic() {
	loading();
	idx('imgresults').src='add_pic.asp?photo_type='+photo_type+'&rnd='+Date.parse(new  Date());
}
function searchPic() {
	loading();
	idx('imgresults').src='imgBrowser.asp?photo_type='+photo_type+'&picTitle='+idx('picTitle').value;
}
function closeImgBrowser() {
//	show(0,'image_panel');
	idx('image_panel').style.display='none';
	show_drag(1,'id_dialog');
	idx("id_dialog").style.display='';
}
function show_drag(visible, id){
	if(visible==0) 
		idx(id).style.display='none';
	else 
		idx(id).style.display='';
}
function deleteBox(element_name){
	if(idx('box_'+element_name).parentNode.parentNode.childNodes.length < 2){
		alert("此列还剩一个模块，不能再移除了！");
		return false;
	}
	if(confirm('您确定要移除这个模块吗?')){
		//test if removed element is not last element in column
		frm.submittype.value = 'del';
		frm.element_name.value = element_name;
		frm.action='dataProcess/editCont/delElement.asp';
		frm.submit();
	}
	
}
function choseSkin(skin_key, member_level){
  	document.location = 'changeskin.asp?preview_skin=' + skin_key;
}
function setTransparency(val){
	if(val==''){
		idx('id_dialog').style.opacity = '100';
		idx('id_dialog').style.filter = 'alpha(opacity=100)';
	}else{
		idx('id_dialog').style.opacity = '.'+val;
		idx('id_dialog').style.filter = 'alpha(opacity='+val+')';
	}
}


