//copyright by jason [AT] dayoo.com

//属性 兼容性检测
//下标为IE属性名, 值为FireFox的属性名
var compatibility_ff = {'className':'class'}

var compatibility_ie = {'class':'className'}

var isIE = window.navigator.userAgent.toLowerCase().indexOf('msie') != -1;
var isFF = window.navigator.userAgent.toLowerCase().indexOf('firefox') != -1;

//取内容(innerText)
function getContent(object) {
	if(isIE){
		return object.innerText;
	} else{
		return object.textContent;
	}
}

//设内容
function setContent(object, content) {
	if(isIE){
		object.innerText = content;
	} else{
		object.textContent = content;
	}
}


//设定对象的属性值
function setObjectAttribute(object, attribute, value){
	
	if (!isIE) {
		if (compatibility_ff[attribute]) {
			attribute = compatibility_ff[attribute];
		}
	}else {
		if (compatibility_ie[attribute]) {
			attribute = compatibility_ie[attribute];
		}
	}
	
	return object.setAttribute(attribute, value);
}

//取对象的属性值
function getObjectAttribute(object, attribute){
	
	if (!isIE) {
		if (compatibility_ff[attribute]) {
			attribute = compatibility_ff[attribute];
		}
	}else {
		if (compatibility_ie[attribute]) {
			attribute = compatibility_ie[attribute];
		}
	}
	
	return object.getAttribute(attribute);
}

/**
 * _AddFuncToEvent
 *
 * @param string event  事件(onclick, onchange, onxxx等, DOM标准大小写)
 * @param Object element    要被增加func的对象
 * @param Function newFunc  增加的函数
 */
function _AddFuncToEvent(eventName, element, newFunc){
    /**
    //NOTE id011150027522: 取消 FF不兼容(not compatible)!!! ;( 类里没有定义的属性或没值(但定义了),FF总会返回undefined, 按ECMA应该是有定义的是null, 没定义才是undefined
    if(element.eventName === undefined){
        throw Error(element.tagName+"不支持"+eventName+"事件");
        return false;
    }
    */
    /* NOTE id011150023522: 不能用element.eventName 只能用element[eventName] */
	/* NOTE id211150027571: 改成这样用才不会死循环 */
    if(element["_add"+eventName+"orginalHadInit"] != true){
		element["orginal_event"] = element[eventName];
		element["_add"+eventName+"orginalHadInit"] = true;
	}
    
    /** 放入对象中 **/
    //初始化函数队列
    if(element["_add"+eventName+"FuncList"] == null){
        element["_add"+eventName+"FuncList"] = new Array();
    }
    
    element["_add"+eventName+"FuncList"].push(newFunc);
    
    element[eventName] = function() {
        //执行函数队列中的函数
        var result = void(0);
        for(var i=0; i<element["_add"+eventName+"FuncList"].length;i++){
            result = element["_add"+eventName+"FuncList"][i].call(element);
        }
        
		
        if(typeof element["orginal_event"] == "function"){
            return element["orginal_event"]();
        }
		
        
        return result;
    }
    
}

//条数更改
function numFormatter(object, tag, num) {
	var tags = object.getElementsByTagName(tag);
	
	var tagsLength = tags.length;
	
	//删去过多对象
	if (num < tagsLength) {
	    for (var i=0; i<tagsLength-num; i++) {
	        object.removeChild(tags[tags.length-1]);
	    }
	}
}

//文字字数更改
function textFormatter(object, tag, textLength, postfix){
	var tags = object.getElementsByTagName(tag);
	
	for(var i=0; i<tags.length; i++){

		var text = getContent(tags[i]);

		if (text.length > textLength){
			if (!getObjectAttribute(tags[i], 'title')) setObjectAttribute(tags[i], 'title', text);
			text = text.substr(0, textLength)+postfix;
			setContent(tags[i], text);
		}
	}
}

//取GET参数
function getUrlParam(url, param){
	var re = new RegExp("(\\\?|&)" + param + "=([^&]+)(&|$)", "i");
	var m = url.match(re);
	if (m)
		return m[2];
	else
		return '';
}



function ShowHideBoard(button, bu_show, bu_hide, board_array, board_show, board_hide, event, autoRun) {
	this.button = button;	//Array String
	this.bu_show = bu_show;	//string
	this.bu_hide = bu_hide;	//string
	this.board_array = board_array;//Array String
	this.board_show = board_show;//string
	this.board_hide = board_hide;//string
	this.event = (event?event:'onclick');

	//onClick不一定就是onclick的事件
	this.onClick = function (bu_object, b_show_id){
		for (var i=0; i<this.button.length; i++) {
		    setObjectAttribute(document.getElementById(this.button[i]), 'className', this.bu_hide)
		}
		setObjectAttribute(bu_object, 'className', this.bu_show)

		for (var i=0; i<this.board_array.length; i++) {
		    setObjectAttribute(document.getElementById(this.board_array[i]), 'className', this.board_hide);
		}
		setObjectAttribute(document.getElementById(b_show_id), 'className', this.board_show)

	};
	
	//bind事件处理
	//要用 board_array MUST 对应 button的位置
	this.attachElement = function (){
		
		var thisObject = this;
		//为了能被正确调用, 设一个对象, 以button的id为属性
		var bo_id_array =  new Object();

		for (var i=0; i<this.button.length; i++) {
			bo_id_array[this.button[i]] = this.board_array[i];
			_AddFuncToEvent(this.event, document.getElementById(this.button[i]), function(){thisObject.onClick(this, bo_id_array[this.id]);});
		}
	}
}

function ObjectsEventCommand(/*Array*/objects, event) {
	this.index = 0;
	this.objects = objects;
	this.event = event;
}

ObjectsEventCommand.prototype.play = function (){
	
	document.getElementById(this.objects[this.index])[this.event]();
	
	this.index++;
	if (this.index >= this.objects.length)
	{
		this.index = 0;
	}
}



function SetIntervalEvent(/*Array*/objects, event, timeout) {
	this.index = 0;
	this.objects = objects;

	this.play = function (){
		var thisObject = this;
		
		document.getElementById(thisObject.objects[thisObject.index])[event]();
		thisObject.index++;
		if (thisObject.index >= thisObject.objects.length)
		{
			thisObject.index = 0;
		}
	}

	return setInterval(this.play, timeout);
}

//加EVENT去对象组
function addEventToObjects (objects, event, func){
	for(var i=0; i<objects.length; i++){
		_AddFuncToEvent(event, document.getElementById(objects[i]), func); 
	}
}