
//切换广告位
function _adAREA(P,O){
	var A = ADSAREA;
	
	O.length=0;
	
	for(var B in A){
		if(P == A[B]['parent']){
			O.options[O.length] = new Option( A[B]['name'], A[B]['id'] );
		}
	}
}

//插入JS代码
function _inScript(s){
	if(s=="script"){
		Mo("#script").value( '\nVAD.write(\'<scr\'+\'ipt type="text/javascript" src="" charset=""></scr\'+\'ipt>\');', true );
	}else{
		Mo("#script").value( "\nVAD."+s.replace("()",'("")'), true );
	}
}

//广告形式
function doForm(){
	var o = Mo("input[name=form]").value();
	
	Mo("#ad_form_0,#ad_form_1,#ad_form_script").hide();
	
	switch( o ){
		
		//代码广告
		case 1:
		
			Mo("#ad_form_script").show();
		
		break;
		
		//自定义
		case 2:
		
		
		break;
		
		//普通广告
		default:
		
		
		break;
	
	}
	
	Mo("#ad_form_"+o).show();
	
}


//HTML转JS
function _adHTML(){
	Mo("#Re").value( "VAD.$V(\""+Mo("#oResult").value().replace(/\\/g,"\\\\").replace(/\//g,"\\/").replace(/\'/g,"\\\'").replace(/\"/g,"\\\"").split('\r\n').join("\");\nVAD.$V(\"")+"\");" );
}

//图片上传回调
function doSize( o ){
	if( Mo("#width").size() == 0 ){
		return;
	}
	
	var image = o["image"];	
	if( image["width"] && image["height"] ){
		Mo("#width").value( image["width"] );
		Mo("#height").value( image["height"] );
	}
}

//图片上传回调
function doThumb( o ){
	
	var image = o["image"];	
	if( image["width"] && image["height"] ){
		Mo("input[name='config[THUMB-WIDTH]']").value( image["width"] );
		Mo("input[name='config[THUMB-HEIGHT]']").value( image["height"] );
	}
	
	Mo("input[name='config[THUMB-FILE]']").value( o['value'] );
}

//输出广告位信息
function getAdInfo( id ){
	
	if( Mo.Validate.Number(id) && ADSAREA[id] ){
		Mo('#ad_info').html( '类型：'+ SHOW[ADSAREA[id]['show']] + ' - 尺寸：'+ ADSAREA[id]['size'] + ' - 数量：'+ ADSAREA[id]['number'] + ' - 标识：'+ ADSAREA[id]['key'] );
	}else{
		Mo('#ad_info').html( '' );	
	}
	
	//自定义
	if( Mo.Validate.Number(id) && ADSAREA[id]['show'] == 13 ){
		Mo("#ad_form").hide();
		Mo("#ad_form_2").show();
		Mo("#ad_form_0,#ad_form_1,#ad_form_script").hide();
		
		Mo("#ad_temp").html( Mo.String( ADSAREA[id]['config']['SHOW-TEMPLATE'] ).htmlEncode() );
		
		Mo("#ad_data").html( doData( ADSAREA[id]['config']['SHOW-TEMPLATE'] ) );
		
	}else{
		Mo("#ad_form").show();
		Mo("#ad_form_2").hide();
		doForm();
	}
	
	//滑出广告
	if( ADSAREA[id]['show'] == 8 ){
		Mo("#bind_slide").show();
	}else{
		Mo("#bind_slide").hide();	
	}
	
}

/////////////////////////////////

/*
	自定义广告
	s	模板
*/
function doData( s ){
	
	//提取标签
	var r = s.match(/\{(\w+):([^\}.]*)\}/igm);
	
	//标签主体
	var subject   = function( s ){ return s.match(/(\w+):([^\}.]*)/gim); }
	
	//分解标签
	var decompose = function( s ){ return s.match(/(\w+):([^\}.]*)/); }
	
	var html = '<table cellpadding="0" cellspacing="1" class="frame">';
	
	for( var i = 0; i < r.length; i++ ){
		
		var tag = decompose( r[i] );
		
		var mark = subject(r[i]);
		
		var name = 'config['+ mark +']';
		
		if( html.indexOf( name ) > -1 ) continue;
		
		html += '<tr class="green"><td><strong>'+ tag[2] +'：</strong></td><td>';
		
		switch( tag[1] ){
		
			case "TEXT":
				html += '<input type="text" name="'+name+'" size="60" value="'+ ( DATA[mark] ? DATA[mark] : '' ) +'" />';
			break;
		
			case "FILE":
				var id = Mo.random(10);
				var ex = DATA[mark] ? DATA[mark].replace(/.*\./,"").toLowerCase() : null;
				html += '<input type="text" name="'+name+'" id="'+id+'" size="60" value="'+ ( DATA[mark] ? DATA[mark] : '' ) +'" /><button type="button" onclick="doFile(\''+id+'\');" class="button">上传</button>';
				html += ex && Mo.Array( Serv.Upload.Picture ).indexOf( ex ) > -1 ? '<br /><img id="'+id+'_img" src="'+ DATA[mark] +'" />' : '';
			break;
			
		}
		
		html += '</td></tr>';
		
	}
	
	html += '</table>';
	
	return html;

}

function doFile( id ){
	Mo.store._file_ = id;
	
	//显示表单
	Serv.Upload.Show( '_file_' );
	
	//新文件
	Serv.Upload.Select( '_file_' );
}

//上传回调
function doCall( data ){
	Mo('#'+Mo.store._file_).value( data['value'] );
	Mo('#'+Mo.store._file_+'_img').attr({"src": data['value'] });
}
