<?php

/*
*	Copyright VeryIDE,2007-2010
*	http://www.veryide.com/
*
*	2011/6/5 Lay
*/

require '../../include/loader.php';
html_start("广告API - VeryIDE");
?>

<!--body start-->
	<?php
	
    //form
	require 'naver.php';
	
	Cached :: loader($appid,"table.adsarea");
	?>
<!--main-->

	<?php   

	//缓存更新时间
	$rnd = @filemtime(VI_ROOT.$_G['project']['cache'].$appid."/script.adsarea.js");

	echo loader_script(array(VI_BASE."cache/".$appid."/script.adsarea.js?rnd=".$rnd),$_G['product']['charset'],$_G['product']['version']);    
	?>
    
	<?php   
    echo loader_script(array(VI_BASE."js/mo.color.js"),"utf-8",$_G['product']['version']);    
    ?>

    <script type="text/javascript">	

	function _adAREA(P,O){
		var A = ADSAREA;
		O.length=1;
		for(var B in A){
			if(P == A[B]['parent']){
				O.options[O.length] = new Option( A[B]['name'], A[B]['id'] );
			}
		}
	}
	
	function getCode(){
		
		var reg = /config\[(.+)\]/m;
		var cfg = '{ ';		//保留后面的空格
		
		Mo("*").each(function( ele, index ){
			
			if( this.name && reg.test( this.name ) ){
				
				cfg += '"'+ (this.name.replace( reg, '$1' )) +'" : '+' "'+ this.value +'" ,';
				
			}
		
		});
		
		cfg = cfg.substr(0,cfg.length-1);
		
		cfg += '}';
		
		//////////////////////////
		
		var tags = Mo.Serialize( Mo("form").item(0) , {} );
		
		/*参数*/
		var par = "";

		/*父分类*/
		par += "&parent=" + tags["parent"];

		/*子分类*/
		par += "&type=" + tags["type"];
	
		/*形式*/
		par += "&show=" + tags["show"];
	
		/*子分类*/
		par += "&number=" + tags["number"];

		/*宽度*/
		par += "&width=" + tags["width"];
	
		/*高度*/
		par += "&height=" + tags["height"];
	
		/*配置*/
		par += "&config=" + encodeURIComponent(cfg);
	
		/*关键字*/
		par += "&key=" + decodeURIComponent(Mo("#key").value());
	
		/*编码*/
		par += "&charset=" + tags["charset"];
		
		/*API地址*/
		var url = "<?php echo VI_HOST;?>module/<?php echo $appid;?>/content/api.php?"+ par;
		
		var code = '<sc'+'ript type="text/javascript" src="'+ url +'" charset="'+Mo("#charset").value()+'"></sc'+'ript>';
			
		/*取得要运行的代码*/
		var theTempIFrame=document.createElement("IFRAME");
			  theTempIFrame.id="iframe";
			  theTempIFrame.name="iframe";
			  theTempIFrame.frameBorder="0";
			  theTempIFrame.style.width="100%";
			  theTempIFrame.style.height="150px";
			  theTempIFrame.src = "adsview.preview.php?action=api&" + par;
			  
			  Mo("#opener").item(0).href = theTempIFrame.src;
		
		/*创建一个隐藏的IFRAME*/	
		Mo("#give").html("");
		Mo("#give").append(theTempIFrame);
		
		Mo("#code").value(code);
	}
	
	</script>
    
    
	<div id="box">
	    <form action="" method="post" name="from1" onsubmit="getCode();return false;">
        
        <table cellpadding="0" cellspacing="0" class="form">
        
        <tr>
        	<td colspan="2" class="section"><strong>引用设置</strong></td>                
        </tr>

		<tbody>
            <tr>
                <td width="60"><strong>选择分类：</strong></td>
                <td>
					<select name="parent" id="parent" style="width:195px;" onchange="_adAREA(Mo('select[name=parent]').value(),Mo('#type').item(0));">
					<option value="0">不限</option>

					<?php
					//var_dump($_CACHE['adsview']['adsarea']);
					foreach($_CACHE['adsview']['adsarea'] as $key){
						if($key["parent"]=="0"){
							echo '<option value="'.$key["id"].'">'.$key["name"].'</option>';
						}
					}
					?>

					</select>
					-
					<select name="type" id="type" style="width:195px;">
					<option value="0">不限</option>
					<?php
					foreach($_CACHE['adsview']['adsarea'] as $key){
						if($key["parent"]==$parent && $parent){
							echo '<option value="'.$key["id"].'">'.$key["name"].'</option>';
						}
					}

					?>

					</select>
                </td>
            </tr>            
                
			<?php
            
            //载入控制文件
            include("include.area.php");
            
            ?>        
         
        	<tr>
            	<td><strong>关键字：</strong></td>
                <td>
                	<input name="key" type="text" class="text" id="key" value="" size="35" fix_name="分类排序" fix_number="no" />
                    <br />
                    参数 key 接收此处关键字  
                    <script type="text/javascript">Mo.Tools("Tip","针对广告进行标题和标签搜索");</script>
                </td>
            </tr>
         
        	<tr>
            	<td><strong>输出编码：</strong></td>
                <td>
                	<select name="charset" id="charset">
                    	<option value="">自动</option>
                    	<option value="gbk">GBK</option>
                    	<option value="utf-8">UTF-8</option>
                    </select>
                </td>
            </tr>
                        
            <tr class="nobr">
                <td><strong></strong></td>
                <td>
                    <input type="submit" name="Submit" value="生成代码" class="submit" />
                </td>
                
            </tr>
        </tbody>
        
        </table>
            
		</form>
                
        <table cellpadding="0" cellspacing="0" class="form">
        
            <tr>
                <td colspan="2" class="section"><span><a href="#" id="opener" target="_blank">新窗口预览&raquo;</a></span><strong>广告预览</strong></td>                
            </tr>
            
            <tr>
                <td>
                </td>
                <td id="give">
                </td>
            </tr>
            
            <tr>
                <td colspan="2" class="section"><strong>引用代码</strong></td>                
            </tr>
            
            <tr>
                <td>
                </td>
                <td>
                    <textarea name="code" id="code" rows="5" style="width:99%;"></textarea>
                </td>
            </tr>
        
        </table>
	</div>
	<script type='text/javascript'>	
		getCode();
		
		Mo("#parent").value("<?php echo $row["parent"];?>");	
		
		_adAREA( Mo("#parent").value(), Mo("#type").item(0) );
		
		Mo("#type").value("<?php echo $row["type"];?>");
		
    </script>
	
<!--body end-->

<?php html_close();?>