var isIE = (document.all) ? true : false;
var isIE8 = navigator.userAgent.indexOf('MSIE 8.0') != -1;
var isChrome = navigator.userAgent.indexOf('Chrome') != -1;
var isSafari = navigator.userAgent.indexOf('AppleWebKit') != -1

var $$ = function (id) {
	return "string" == typeof id ? document.getElementById(id) : id;
};
//返回function类型，可以再创建对象,创建对象后初始化initialize
var Class = {
	create: function() {
		return function() { this.initialize.apply(this, arguments); }   //应用initialize并赋所有参数arguments
	}
}
//转移参数
var Extend = function(destination, source) {
	for (var property in source) {
		destination[property] = source[property];
	}
}
//转移执行对象
var Bind = function(object, fun) {
	return function() {
		return fun.apply(object, arguments);   //object应用fun，并赋所有参数
	}
}
//转移执行对象并赋参数
var BindAsEventListener = function(object, fun) {
	var args = Array.prototype.slice.call(arguments).slice(2);     //arguments调用Array数组对象的slice方法
	return function(event) {
		return fun.apply(object, [event || window.event].concat(args));
		//只有两个参数时，参数返回event，即在className中的e就是event
	}
}

var CurrentStyle = function(element){
	return element.currentStyle || document.defaultView.getComputedStyle(element, null);
}
//是否数组
var isArray = function( obj ) {
	return Object.prototype.toString.call(obj) === "[object Array]";
}
//是否有效DIV
var isDiv = function( obj ) {
	if ($$(obj) == "[object HTMLDivElement]")          //IE8 、Firefox 
	return $$(obj) == "[object HTMLDivElement]";
	else
	return $$(obj) == "[object]";
}
//在数组或字符串中是否包含字符
var IndexOf = function(array,elt){
	if(array.indexOf){
		return array.indexOf(elt);
	}else{
		var res = [];
		for (var i = 0, len = array.length; i < len; i++) {
			if (array[i].div1 === elt) return i;
		};
		return -1;
	}
};
//添加事件焦点
function addEventHandler(oTarget, sEventType, fnHandler) {
	if (oTarget.addEventListener) {
		oTarget.addEventListener(sEventType, fnHandler, false);
	} else if (oTarget.attachEvent) {
		oTarget.attachEvent("on" + sEventType, fnHandler);
	} else {
		oTarget["on" + sEventType] = fnHandler;
	}
};
//移除事件焦点
function removeEventHandler(oTarget, sEventType, fnHandler) {
    if (oTarget.removeEventListener) {
        oTarget.removeEventListener(sEventType, fnHandler, false);
    } else if (oTarget.detachEvent) {
        oTarget.detachEvent("on" + sEventType, fnHandler);
    } else { 
        oTarget["on" + sEventType] = null;
    }
};



/*程序开始执行*/


var className = Class.create();
className.prototype = {
  //初始化执行
  initialize: function(options) {
	
	this.SetOptions(options);
	this.boxDiv=this.options.boxDiv;           //div盒，用于检测事件区域
	this.defaultDiv=this.options.defaultDiv;   //默认DIV
	this.historyDiv=this.options.historyDiv;   //历史显示DIV
	this.Menu=this.options.Menu;               //导航栏div组
	//设置事件
	this.Set();
	//默认显示设置
	this.Start();
  },
  SetOptions: function(options) {
	this.options = { 
		Menu:		[],
		boxDiv:     "MenuBox",
		defaultDiv: "",
		historyDiv: ""
	};
	Extend( this.options, options || {} );
  },
  //给导航栏添加触发事件
  Set: function() {
    addEventHandler($$(this.boxDiv), "mouseout", Bind(this, this.returnDefault));
    for (var i = 0, len = this.Menu.length; i < len; i++) {
      if (typeof(this.Menu[i].div1)==="string"){
  	    addEventHandler($$(this.Menu[i].div1), "mouseover", BindAsEventListener(this, this.divShow,this.Menu[i].div1));
	  }
    }
  },
  //默认导航栏触发事件
  Start: function(){	
	this.onShow(this.defaultDiv,"on");
	this.historyDiv=this.defaultDiv;
  },
  //导航栏触发事件
  divShow: function(e, thisFocus, touch){
    if(this.historyDiv!=""){
	  this.onShow(this.historyDiv,"off");
	}
	this.onShow(thisFocus,"on");
	this.historyDiv=thisFocus;    
  },
  //根据当前DIV显示导航栏
   onShow: function(thisdiv,static) {
   var Classname,Display;
     if (static==="on"){
	   Classname="menu";
	   Display="block";
	 }
	 else{
	   Classname="menu";
	   Display="none";
	 }
	  for (var i = 0, len = this.Menu.length; i < len; i++) {
			if (this.Menu[i].div1 === thisdiv){
			  //$$(this.Menu[i].div1).className=Classname;
			  $$(this.Menu[i].div2).style.display=Display;
			}
	  };
  },
  //返回设定默认导航栏
  returnDefault: function(){
    //当离开的对象在MenuBox之内 并且进入的对象不在MenuBox之内时显示默认导航栏
    if (this.returnMenuBox(event.fromElement)&&!this.returnMenuBox(event.toElement)){
	  this.divShow(event, this.defaultDiv, "");
	}
	//防止冒泡  当离开导航栏时触发内层的离开事件但不触发MenuBox的离开事件
    event.stopPropagation ? event.stopPropagation() : (window.event.cancelBubble = true);
  },
  //返回当前DIV是否在MenuBox之内
  returnMenuBox: function(thisdiv){
    var all = $$(this.boxDiv).getElementsByTagName('*'); 
    for (var i = 0, n = all.length; i < n; ++i){   
	  if (all[i]==thisdiv)
	    return true;
	}  
	return false;
  },
  //停止
  Stop: function() {
  alert("测试成功");
  }
};
