/**
 * <script type="text/javascript" src="/search/script/datasheetBox.js">
 * 		datasheetBox.img='/images/content/eefocus/datasheet_logo.gif';
 *      datasheetBox.text='请输入元器件型号!';
 *      datasheetBox.target='http://www.eefocus.com/datasheet/';
 *      datasheetBox.init(); 
 * </script>
 * @param {Object} $sid
 */
var datasheetBox = {
	datasheet:true,
	img:'http://www.eefocus.com/images/content/eefocus/datasheet_logo.gif',
	text:'请输入元器件型号!',
	title:'元器件快速查询',
	target:'http://www.eefocus.com/datasheet/',
	
	diagram:true,
	diagramTitle:'电路方案',
	diagramTarget:'http://www.cndzz.com/',
	
    g: function($sid){
        return document.getElementById($sid);
    },
    
    init: function(){
        var tpl = this.tpl();
        document.write(tpl);
		
		//加载ajax类
		if(this.diagram){
			var dg = document.createElement('script'); 
			dg.type = 'text/javascript'; 
			dg.async = true;
			dg.src = '/search/script/api.php';
			
			var s = document.getElementsByTagName('script')[0]; 
			s.parentNode.insertBefore(dg, s);
    	}
	},
	
	start:function(){
		if (this.g('datasheetBoxSearchInput').value == this.text) {
            this.g('datasheetBoxSearchInput').value = '';
            this.g('datasheetBoxSearchInput').style.color = 'black';
            return;
        }
        if (this.g('datasheetBoxSearchInput').value == '') {
            this.g('datasheetBoxSearchInput').value = this.text;
            this.g('datasheetBoxSearchInput').style.color = 'gray';
            return;
        }
	},
    
    check:function(){
        if (this.text == this.g('datasheetBoxSearchInput').value || this.g('datasheetBoxSearchInput').value == '') {
            alert("请填写搜索关键字！");
            return false;
        }
        return true;
    },
    
    tpl: function(){
        var tpl = '';
		if(this.datasheet){
	        tpl += '<fieldset id="datasheetBox">';
			tpl += '	<legend>'+ this.title +'</legend>';
			tpl += '	<form id="SearchFormBox" name="SearchFormBox" method="get" action="'+ this.target +'" target="_blank" onsubmit="return datasheetBox.check();">';
			tpl += '		<img src="'+ this.img +'"style="margin:5px 0;"/><br />';
			tpl += '		<input id="datasheetBoxSearchInput" name="keyword" type="text" autocomplete="off" gtbfieldid="1" value="'+this.text+'" accesskey="s" onfocus="datasheetBox.start();" onblur="if(this.value==\'\')datasheetBox.start();" style="color:gray;" class="search_input">';
			tpl += '		<input type="submit" name="Submit" value="搜 索"class="search_btn"/>';
			tpl += '    </form>';
			tpl += '</fieldset>';
		}
		if(this.diagram){
			tpl += '<fieldset id="diagramBox">';
			tpl += '	<legend>'+ this.diagramTitle +'</legend>';
			tpl += '	<ul id="diagramList"></ul>';
			tpl += '	<p><a href="'+this.diagramTarget+'" target="_blank">更多>></a></p>';
			tpl += '</fieldset>';
        }
        return tpl;
    },
	
	setlink: function(val){
		var html='';
		var vals=eval(val);
		
		for (var i in vals) {
			html += '	<li><a href="'+vals[i]['link']+'" target="_blank">'+vals[i]['title']+'</a></li>';
		}
		document.getElementById('diagramList').innerHTML=html;
	}
}
