var browser_tmp = window.navigator.userAgent;
var Browser = {
	MSIE: browser_tmp.match(/MSIE (\d+(?:\.\d+){0,})/),
	Gecko: browser_tmp.indexOf('Gecko') > -1 && browser_tmp.indexOf('KHTML') == -1,
	Safari: browser_tmp.match(/Version\/(\d+(?:\.\d+){0,}) Safari/),
	Opera: browser_tmp.match(/Opera\/(\d+(?:\.\d+){0,})/)
};
browser_tmp = null;

function domready(fn){
	if(Browser.MSIE){//鸟IE
		var timer = setInterval(function(){try{document.documentElement.doScroll("left"); fn(); clearInterval(timer);timer=null;}catch(e){}},50);
	}else{
		document.addEventListener("DOMContentLoaded",fn,true);
	}
}

function $(){
	var elements = new Array();
	for (var i = 0; i < arguments.length; i++) {
		var e = arguments[i];
		e = typeof e == 'object' ? e : document.getElementById(e);
		if (arguments.length == 1)
			return e;
		elements.push(e);
	}
	return elements;
}

var Try = {
  these: function() {
    var returnValue;

    for (var i = 0, length = arguments.length; i < length; i++) {
      var lambda = arguments[i];
      try {
        returnValue = lambda();
        break;
      } catch (e) { }
    }

    return returnValue;
  }
};

function htmlentitie(s){
	return s.replace(
		/&/g, "&amp;").replace(
		/'/g, "&#039;").replace(
		/"/g, "&quot;").replace(
		/</g, "&lt;").replace(
		/>/g, "&gt;");
}

function unhtmlentitie(s){
	return s.replace(
		/&#039;/g, "'").replace(
		/&quot;/g, "\"").replace(
		/&lt;/g, "<").replace(
		/&gt;/g, ">").replace(
		/&amp;/g, "&");
}

String.prototype.trim = function(){
	return this.replace(/(^\s*)|(\s*$)/g, ""); 
}

function get_scrollLeft(){
	return (document.documentElement.scrollLeft || document.body.scrollLeft);
}

function get_scrollTop(){
	return (document.documentElement.scrollTop || document.body.scrollTop);
}

function get_scrollWidth(){
	return Math.max(document.documentElement.scrollWidth || document.body.scrollWidth, get_document_width());
}

function get_scrollHeight(){
	return Math.max(document.documentElement.scrollHeight || document.body.scrollHeight, get_document_height());
}

function get_document_width(){
	return (document.documentElement.clientWidth || document.body.clientWidth);
}

function get_document_height(){
	return (document.documentElement.clientHeight || document.body.clientHeight);
}

function ajax_parameters(o, p){
	var s = '';
	p = p ? p : '';
	
	for(i in o){
		if(typeof o[i] == 'object' && !o[i].length)
			s += ajax_parameters(o[i], p ? p+'['+i+']' : i);
		else if(o[i] instanceof Array)
			s += ajax_parameters(o[i], p ? p+'['+i+']' : i);
		else
			s += '&'+ (p ? p + '['+i+']' : i) +'=' + encodeURIComponent(o[i]);
	}
	return s;
}

var Element = {
	show: function(e){
		e = $(e);
		e.style.display = '';
	},
	
	hide: function(e){
		e = $(e);
		e.style.display = 'none';
	},
	
	toggle: function(e){
		e = $(e);
		if(e.style.display == 'none'){
			Element.show(e);
		}else{
			Element.hide(e);
		}
	},
	
	visible: function(e){
		e = $(e);
		e.style.visibility = 'visible';
	},
	
	invisible: function(e){
		e = $(e);
		e.style.visibility = 'hidden';
	},
	
	create: function(e){
		if(typeof e == 'object'){
			var o = document.createElement(e.tag);
			for(var k in e){
				if(k == 'tag' || typeof e[k] == 'function') continue;
				o.setAttribute(k, e[k]);
				if(!o.k)
					eval('o.'+k +'=e[k];');
			}
			return o;
		}
		return document.createElement(e);
	},
	
	getY: function(e){
		e = $(e);
		var offset = e.offsetTop;
		if(e.offsetParent!=null) offset += Element.getY(e.offsetParent);
		return offset;
	},
	
	getX: function(e){
		e = $(e);
		var offset = e.offsetLeft;
		if(e.offsetParent!=null) offset += Element.getX(e.offsetParent);
		return offset;
	},
	
	setX: function(e, x){
		e = $(e);
		if(x != null){
			e.style.left = x +'px';
			return true;
		}
		return false;
	},
	
	setY: function(e, y){
		e = $(e);
		if(y != null){
			e.style.top = y +'px';
			return true;
		}
		return false;
	},
	
	width: function(e, v){
		e = $(e);
		if(v != null)
			e.style.width = parseInt(v) + 'px';
		return e.clientWidth;
	},
	
	height: function(e, v){
		e = $(e);
		if(v != null)
			e.style.height = parseInt(v) + 'px';
		return e.clientHeight;
	},
	
	locate: function(e, x, y){
		e = $(e);
		if(x != null && y != null){
			e.style.left = x +'px';
			e.style.top = y +'px';
			return true;
		}
		return false;
	},
	
	clone: function(e, c){
		e = $(e);
		c = c ? true : false;
		return e.cloneNode(c);
	},
	//Element.insert(element_for_insert, source_element,  position)	position like this	{before: element}
	insert: function(e, s, ppa){
		s = $(s);
		e = $(e);
		s = s ? s : document.body;
		var p = 'default', e, pa;
		for(var i in ppa){
			p = i || 'before';		//position
			pa = $(ppa[i]); 		//parent
		}
		pa = pa ? pa : null;
		if(!p || !e) return;
		
		switch(p.toLowerCase()){
			case 'before':
				s.insertBefore(e, pa);
			break;
			case 'after':
				var pan = pa.nextSibling ? pa.nextSibling : null;
				s.insertBefore(e, pan);
			break;
			case 'top':
				var pan = s.firstChild ? s.firstChild : null;
				s.insertBefore(e, pan);
			break;
			default:
				s.appendChild(e);
		}
	},
	
	remove: function(e){		//complete remove
		e = $(e);
		try{
			e.removeNode(true);
		}catch(ex){
			e.parentNode.removeChild(e);
		}
	},
	
	clear: function(e){		//clear child node
		e = $(e);
		while(e.hasChildNodes()){
			e.removeChild(e.lastChild);
		}
	}
};

(function(){
	var initializing = false;
	this.Class = function(){};
	Class.extend = function(prop) {
		var _super = this.prototype;
		initializing = true;
		var prototype = new this();
		initializing = false;
		prototype.parent = _super.init;
		for (var name in prop) {
			if(typeof prop[name] == "function" && typeof _super[name] == "function"){
				prototype.parent[name] = _super[name];	// subclass call parent method like this	this.parent.methoe.call(this, parm1,parm2)
			}
			prototype[name] = prop[name];
		}
		function Class() {
			if ( !initializing && this.init )   
				this.init.apply(this, arguments);
		}
		Class.prototype = prototype;
		Class.constructor = Class;
		Class.extend = arguments.callee;
		return Class;
	};
})();

/*************************UTIL END**************************/

/***********************************************************
@	example	tab = new Tabs_handle({
					Tabs: ['title1', title2, ...],
					contents: [{content_container_id1: ''}, {content_container_id2: 'ajax_request_url.php?action=a'}, ...] ,
					init_tab: 'title2',
					event: 'click',
					normal_class: 'class',
					focus_class: 'class'
				});
***********************************************************/
var Tabs_handle = Class.extend({
	init: function(options){
		this.options = {
			elements: options.elements,					//required
			title_container: options.title_container || null,
			content_container: options.content_container || null,
			init_tab: options.init_tab || 0,
			event: options.event || 'mouseover',
			onMouseOut_hide: options.onMouseOut_hide || false, 
			normal_class: options.normal_class || '',
			focus_class: options.focus_class || '',
			onChange: options.onChange || null,
			onClose: options.onClose || null,
			change_interval: options.change_interval || null
		};
		
		this.elements = [];
		this.title_container = $(this.options.title_container);
		this.content_container = $(this.options.content_container);
		this.last_offset = -1;
		this.interval_id = null;
		this.hide_timeout = null;
		
		var _this = this;
		
		for(var i = 0; i < this.options.elements.length; i++){
		
			this.elements[i] = {
				title: $(this.options.elements[i].title),
				close_handle: $(this.options.elements[i].close_handle),
				content: $(this.options.elements[i].content),
				url: this.options.elements[i].url || null,
				reload: this.options.elements[i].reload || 'once'
			};
			
			//this.elements[i].title.offset = i;

			this.add(this.elements[i]);
		}
		
		if(this.options.init_tab != -1)
			this.change(this.options.init_tab);
		
		this.set_interval();
	},
	
	update: function(offset){
		offset = offset == this.last_offset ? this.last_offset -1 : this.last_offset;
		offset = Math.max(0, offset);
		
		for(var i = offset ; i < this.elements.length; i++){
			this.elements[i].title.offset = i;
			if(this.elements[i].close_handle)
				this.elements[i].close_handle.offset = i;
		}
		
		this.last_offset = -1;
		//this.change(offset);
	},
	
	add: function(item, pos){
		var _this = this;
		
		if(pos){
			pos = Math.min(this.elements.length, pos);
			pos = Math.max(0, pos);
		}else{
			pos = this.elements.length;
		}
		
		item.title.offset = pos;
		if(item.close_handle){
			item.close_handle.offset = pos;
			item.close_handle.onclick = function(){
				_this.onClose(this.offset);
			};
		}
		item.content.ajaxed = false;
		
		this.elements.splice(pos, 0, item);
		
		if(this.options.event == 'click'){	//点击方式
			item.title.onclick = function(){
				_this.onClick(this.offset);
			};
			
			item.title.onmouseover = function(){
				clearInterval(_this.interval_id);
			};
		}else{	//鼠标悬浮方式
			item.title.onmouseover = function(){
				_this.onMouseOver(this.offset);
			};
		}
		
		item.title.onmouseout = item.content.onmouseout = function(){
			_this.onMouseOut();
		};
		
		item.content.onmouseover = function(){
			clearInterval(_this.interval_id);
			clearTimeout(_this.hide_timeout);
		};
		
		//var ele_pos = pos == 0 ? {top: ''} : {after: this.elements[pos -1].title};
		/* if(this.options.event == 'click'){
			item.title.onclick = function(){
				_this.onClick(this.offset);
			};
			
			item.title.onmouseover = function(){
				clearInterval(this.interval_id);
			};
		}else{
			item.title.onmouseover = function(){
				_this.onMouseOver(this.offset);
			};
		} */
		
		//Element.insert(item.title, this.title_container, ele_pos);
		//Element.insert(item.content, this.content_container);
		//Element.hide(item.content);
		item.title.className = this.options.normal_class;
		Element.hide(item.content);
		
		this.update(pos);
		//this.change(pos);
		return pos;
	},
	
	set_interval: function(){
		var _this = this;
		if(this.options.change_interval)
			this.interval_id = setInterval(function(){_this.change(_this.last_offset+1);}, this.options.change_interval);
	},
	
	change: function(o){
		
		o = parseInt(o);
			
		this.last_offset = Math.min(this.last_offset, this.elements.length -1);
		o = o >= this.elements.length ? 0 : o;
		
		if(this.last_offset == -1){	//初始化工作
			
		}else{
			this.elements[this.last_offset].title.className = this.options.normal_class;
			Element.hide(this.elements[this.last_offset].content);
		}
		
		this.elements[o].title.className = this.options.focus_class;
		Element.show(this.elements[o].content);
		
		
		
		this.last_offset = o;
		if(this.options.onChange)
			this.options.onChange(o, this.elements[o].title, this.elements[o].content);
	},
	
	hide: function(){
		if(this.last_offset != -1){
			this.elements[this.last_offset].title.className = this.options.normal_class;
			Element.hide(this.elements[this.last_offset].content);
		}
	},
	
	onClick: function(offset){
		clearInterval(this.interval_id);
		this.change(offset);
	},
	
	onMouseOver: function(offset){
		clearInterval(this.interval_id);
		clearTimeout(this.hide_timeout);
		this.hide();
		this.change(offset);
	},
	
	onMouseOut: function(){
		clearInterval(this.interval_id);
		this.set_interval();
		
		if(this.options.onMouseOut_hide){	//移出后0.3秒消失
			var _this = this;
			this.hide_timeout = setTimeout(function(){_this.hide();}, 300);
		}
	},
	
	onClose: function(offset){
		Element.remove(this.elements[offset].title);
		Element.remove(this.elements[offset].content);
		this.elements.splice(offset, 1);
		
		this.update(offset);
		
		if(this.options.onClose)
			this.options.onClose(offset);
	}
});


