String.prototype.Trim = function(){return this.replace(/(^\s*)|(\s*$)/g, "");}
function submitSO(){
  var showCategory = search_prompt_flag ? $("txtso")['showCategory'] : '';
  var vr_so_key = $('VRSO_FORM').txtso.value.Trim();
  if(vr_so_key == "" || vr_so_key == $('VRSO_FORM').txtso.defaultValue){
    alert("请输入关键字");
    $('VRSO_FORM').txtso.focus();
    return false;
  }
  var add_cate = showCategory ? '/i_' + encodeURIComponent(showCategory) : '';

  window.location = "http://" + index_domain + "/vr_search" + add_cate + "/keyword_"+encodeURIComponent(vr_so_key);
  return false;
}

function collapse(obj){
  var spans = $('openDetail').getElementsByTagName('span');
  if (spans.length > 0){
    var span = $(spans[0]);
    if (span.hasClassName('ico__expand')){
      span.removeClassName('ico__expand');
      span.addClassName('ico__collapse');

      $$('.js_od_list').each(function (item){
        item.hide();
      });
    }else{
      span.removeClassName('ico__collapse');
      span.addClassName('ico__expand');
      $$('.js_od_list').each(function (item){
        item.show();
      });
    }
  }
  return false;
}

var Highlight = {
	bound: [], 			// 	查找表格的范围
	tables: [],			//	表格集合
	classname: 'hover', //行高亮css class

	bind: function(){

		var _this = this;
		for(var i=0; i<this.tables.length; i++){
			var table = this.tables[i];
			var trs = table.rows;
			for(var j=0; j<trs.length; j++){
				var tr = trs[j];
				if(tr.parentNode.tagName != 'THEAD' && tr.className != 'disabled'){
					tr.onmouseover = function(){ this.className = _this.classname; }
					tr.onmouseout = function(){ this.className = ''; }
				}
			}
		}

	},

	init: function(){

		if(arguments.length == 0 || arguments.length > 1){ this.bound.push(document.body); }
		else if(arguments.length == 1){
			var arr = [];
			//single
			if(typeof(arguments[0]) == 'string'){ arr.push(arguments[0]); }
			//collection
			else if(typeof(arguments[0]) == 'object'){ arr = arguments[0]; }
			alert(typeof(arguments[0]));
			for(var i=0; i<arr.length; i++){
				var node = 	document.getElementById(arr[i]);
				if(node){ this.bound.push(node); }
			}
		}

		for(var i=0; i<this.bound.length; i++){
			var bound = this.bound[i];
			if(bound.tagName == 'TABLE'){ this.tables.push(bound); }
			else{
				var	ts = bound.getElementsByTagName('TABLE');
				if(ts.length != 0){
					for(j=0; j<ts.length; j++){
						this.tables.push(ts[j]);
					}
				}
			}
		}

		if(this.tables.length != 0){
			this.bind();
		}

	}
}


/*
只依据纵向处理固定, 不支持已fixed的的dom, static to fixed
*/
var FixedLayer = function(id){
	if(!id){ return; }
	this.dom = document.getElementById(id);
	if(!this.dom){ return; }

	//设置固定点 只支持小于等与原位
	var origiPos = this.getPOS();
	this.origiPosX = origiPos.x;
	this.origiPosY = origiPos.y;
	this.startPosY = 0;
	this.BisIE6 = this.isIE6();
	this.isFix = false;
	this.marginTop = parseInt(this.getStyle(this.dom, 'marginTop'), 10);
	this.marginBottom = parseInt(this.getStyle(this.dom, 'marginBottom'), 10);
	this.marginLeft = parseInt(this.getStyle(this.dom, 'marginLeft'), 10);
	this.opacity = 100;
	this.bind();

}

FixedLayer.prototype = {

	getPOS: function(node){
		var el = this.dom;
		if(arguments.length == 1){ el = node; }
		var	x = el.offsetLeft,
			y = el.offsetTop,
			scrollLeft = 0,
			scrollTop = 0;
		el = el.offsetParent;

		while (el) {
			x += el.offsetLeft;
			y += el.offsetTop;
			el = el.offsetParent;
		}

		el = this.dom;
		while ((el = el.parentNode) && el.tagName) {
			scrollTop = el.scrollTop;
			scrollLeft = el.scrollLeft;
			if (scrollTop || scrollLeft) {
				x -= scrollLeft;
				y -= scrollTop;
			}
		}

		return {'x': x, 'y': y };
	},

	getDocScroll: function(){

		var	scrollLeft = Math.max(document.documentElement.scrollLeft, document.body.scrollLeft),
			scrollTop = Math.max(document.documentElement.scrollTop, document.body.scrollTop);

		return { top: scrollTop, left: scrollLeft };
	},

	getSize: function(){
		var	w = this.dom.offsetWidth;
			h = this.dom.offsetHeight;

		return { width: w, height: h };
	},

	//占位
	place: function(){
		function insertAfter(newElement, targetElement) {
			var parent = targetElement.parentNode;
			if(parent.lastChild == targetElement) {
				parent.appendChild(newElement);
			} else {
				parent.insertBefore(newElement, targetElement.nextSibling);
			}
		}
		var size = this.getSize();
		this.pose = document.createElement('DIV');
		this.pose.style.width = size.width + 'px';
		this.pose.style.height = size.height + 'px';
		this.pose.style.marginTop = this.marginTop + 'px';
		this.pose.style.marginBottom = this.marginBottom + 'px';
		insertAfter(this.pose, this.dom);

	},

	isIE6: function(){
		var s = "MSIE", u = navigator.userAgent, i = -1;
		if ((i = u.indexOf(s)) >= 0) {
			var v = parseFloat(u.substr(i + s.length));
			if(v == 6){ return true; }
		}

		return false;
	},

	getStyle: function(obj, attribute){
		return obj.currentStyle ? obj.currentStyle[attribute] : document.defaultView.getComputedStyle(obj,false)[attribute];
	},

	attachEvent: function(o, e, func){
		if(window.addEventListener){ o.addEventListener(e, func, false); }
		else if(window.attachEvent){ o.attachEvent('on' + e, func); }
	},

	fix: function(){
		var pos = this.getPOS();
		if(!this.BisIE6){
			this.dom.style.position = 'fixed';
			this.dom.style.top = this.startPosY - this.marginTop + 'px';
			this.dom.style.left = this.origiPosX - this.marginLeft + 'px';
		}else{
			this.dom.style.position = 'absolute';
		}
		this.place();
		this.isFix = true;
	},

	detect: function(){
		var pos = this.getPOS();
		var ofx = this.getDocScroll();
		if(!this.isFix && pos.y <= this.startPosY){
			this.fix();
		}
		if(this.isFix){
			if(ofx.top < this.origiPosY){ this.rep(); }
			if(this.BisIE6){
				this.dom.style.top = this.startPosY + ofx.top - this.marginTop + 'px';
				var _this = this;
				if(this.delay){ clearTimeout(this.delay); }
				if(this.timer){ clearInterval(this.timer); }
				this.opacity = 0;
				this.dom.style.filter = 'Alpha(opacity=0)';
				this.delay = setTimeout( function(){ _this.smooth(); }, 200 );
			}
			var offsetLeft = this.BisIE6 ? ofx.left : 0;
			this.dom.style.left = this.getPOS(this.pose).x + offsetLeft - this.marginLeft + 'px';
		}
	},

	smooth: function(){
		var _this = this;
		this.timer = setInterval(function(){
			_this.opacity += 10;
			_this.opacity = _this.opacity > 100 ? 100 : _this.opacity;
			_this.dom.style.filter = 'Alpha(opacity='+ _this.opacity +')';
			if(_this.opacity == 100){ clearInterval(_this.timer); }
		},15);
	},

	rep: function(){
		this.dom.style.position = 'static';
		if(this.pose){
			this.pose.parentNode.removeChild(this.pose);
		}
		this.isFix = false;
	},

	bind: function(){
		var _this = this;
		this.attachEvent(window, 'scroll', function(){ _this.detect(); });
		this.attachEvent(window, 'resize', function(){ _this.detect(); });
	}
}

function myTab(obj){
  $(obj).observe("click", function(e){
    if (e.target.tagName == 'A'){
      var aTarget = e.target;
      var aList = $(aTarget.parentNode.parentNode).select('a');
      var on_index = 0;
      for(var i=0; i < aList.length; i++){
        if (aList[i] == aTarget){
          on_index = i;break;
        }
      }
      aList.each(function(item){$(item.parentNode).removeClassName('current')});
      $(aList[on_index].parentNode).addClassName('current');
      var tableList = $(aTarget.parentNode.parentNode.parentNode.parentNode.parentNode).select('table');
      tableList.invoke('hide');
      myTableShow(tableList[on_index], $(aTarget).readAttribute('href'));
    }
  });
}

function myTableShow(obj, url){
  change_top50_url(obj.parentNode.parentNode, url);
  var tb = $(obj);
  tb.show();
  if (!tb.hasClassName('nova_completed')){
    setTimeout((function(){return function(){
      if (!tb.hasClassName('nova_completed')){
        var tbds = tb.select('tbody');
        if (tbds){
          var tbd = $(tbds[0]);
          tbd.update('<tr><td class="order">&nbsp;</td><td class="key">载入中...</td><td class="status">&nbsp;</td><td class="updown">&nbsp;</td></tr>');
        }
      }
    }})(), 500);
    //load data;
    nova_request((function(){
      return function(res){
        tb.addClassName('nova_completed');
        if (typeof(res) != 'String'){
          try{
            res = res.toString();
          }catch(e){}
        }
        var tbds = tb.select('tbody');
        if (tbds){
          var tbd = $(tbds[0]);
          tbd.update(res);
        }
      }
    })(), url,{ajax:1});
  }
}

function change_top50_url(obj, url){
  var elems = Element.select(obj, '.extend a');
  Element.writeAttribute(elems[0], 'href', url.split('?').shift());
}

// 渲染Canvas 图表
function renderLineChart(url){
        if(!url){
            return ;
        }
        nova_call(unescape(url),{},function(showData){
            if(showData){
                new LineChart({
                    dataInfo: {
                        startDate: showData[0].range[0],
                        endDate: showData[0].range[1],
                        data: showData[0].data, // 默认初始化播放图表
                        title: showData[0].id
                    },
                    dateRange: 30
                });
                document.querySelectorAll('.date-button')[1].style.background = 'none';
            }
        });
};

Event.observe(window, 'load', function() {
	/**
	* @params	查找表格的范围（可以是表格自身） 即包含表格的节点或表格自身  （1至N）
	* null 		document.body	//不指定范围
	* string 	'A'				//指定一个范围
	* string[]	['A', 'B', 'C']	//指定一个范围序列
	*/
	//eg. 不指定范围  将从 document.body 查找表格
	if (typeof(highlightTable)!= 'undefined'){
		Highlight.init();
	}
	//ie 浮动
	(function(){ new FixedLayer('vrtool'); })();

	var rbody = $('js_beta_10245');
	if (rbody){
	  var tabs = rbody.select('div.viewTab');
	  if (tabs){
	    tabs.each(function(item){myTab(item)});
	  }
	}

});

