// JavaScript Document: common.js @ 2008 03 24 for MyShow 3.0

function m_over(e, c) {
	e = $(e);
	c = c || is_status('active', e) || is_status('disabled', e);
	if (e && !c)
		e.className += ' hover';
	e.setAttribute('mouseStatus', 'hover');
}

function m_out(e, c) {
	e = $(e);
	c = c || is_status('active', e) || is_status('disabled', e);
	if (e && !c)
		e.className = e.className.replace(' hover', '');
	e.setAttribute('mouseStatus', '');
}

function b_active(e, c) {
	e = $(e);
	c = c || is_status('disabled', e);
	if (e && !c) {
		if (is_status('hover', e))
			e.className = e.className.replace('hover', 'active');
		else
			e.className += ' active';
	}
}

function b_deactive(e, c) {
	e = $(e);
	c = c || is_status('disabled', e);
	if (e && !c) {
		if (e.getAttribute('mouseStatus') == 'hover')
			e.className = e.className.replace('active', 'hover');
		else
			e.className = e.className.replace(' active', '');
	}
}

function b_disable(e, c) {
	e = $(e);
	c = c || is_status('active', e) > 0;
	if (e && !c) {
		if (is_status('hover', e))
			e.className = e.className.replace('hover', 'disabled');
		else
			e.className += ' disabled';
	}
}

function b_enable(e, c) {
	e = $(e);
	c = c || is_status('active', e) > 0;
	if (e && !c) {
		if (e.getAttribute('mouseStatus') == 'hover')
			e.className = e.className.replace('disabled', 'hover');
		else
			e.className = e.className.replace(' disabled', '');
	}
}

function m_click(m, e) {
	if (e = $(e)) {
		if (is_status('active', e) || is_status('disabled', e))
			return;

		if (e.getAttribute('postion') == 'commend')
			redirect('?action=main&type=' + 
						e.getAttribute('type') + '&id=' + e.getAttribute('wid') + 
						'&pass=commend');
		else
			alert(e);
	}
}

function m_scroll(e, p, m) {
	if (e = is_status('!disabled', e)) {
		if (p = $(p)) {
			for (c = 0; c < Math.abs(m); c ++) {
				if (m > 0)
					p.appendChild(p.firstChild);
				else
					p.insertBefore(p.lastChild, p.firstChild);
			}
		}
	}
}

function m_brand(e, p) {
	if (e = $(e)) {
		var f = -1;
		for(var i = 0; i < e.parentNode.childNodes.length; i ++) {
			var o = e.parentNode.childNodes[i];
			if (o.nodeType != 3) {
				f = f < 0 ? i : f;
				if (o == e) 
					p += ',' + (i - f);
			}
		}
	}
	redirect(p);
}

e_content = 'content';

function m_update(e, url) {
	if (e = is_status('!active', e)) {
		if (c = $(e_content)) {
			for (i = 0; i < c.childNodes.length; i ++) {
				if (c.childNodes[i].className) c.childNodes[i].className += ' hidden';
			}
				
			l = $('loader');
			if (!l) {
				l = document.createElement('div');
				l.id = 'loader';
				c.appendChild(l);
			}
			l.innerHTML = '正在读取数据, 请稍等...';
			
			if (e.parentNode) {
				for (i = 0; i < e.parentNode.childNodes.length; i ++) {
					b_deactive(e.parentNode.childNodes[i]);
				}
				b_active(e);
			}

			var updater = new Ajax(e_update_error, e_update_state, e_update_end);
			updater.method = 'GET';
			updater.URL = url;
			updater.send();	

			if (typeof e_update_start == 'function')
				e_update_start(updater);
		} else {
			redirect(url, top);
		}
	}
}

function e_update_error(ajax) {
	l = $('loader');
	if (ajax && ajax.ErrorCode != 404) {
		l.innerHTML = '数据获取失败，5秒后自动重试';
		setTimeout(function () { m_update(ajax.URL, this) }, 5000);
	} else {
		l.innerHTML = '服务器处理错误, 请尝试访问其他频道';
	}
}

function e_update_state(status) {
	//alert(status);
}

function e_update_end(html) {
	var c = $(e_content);
	if (c) {
		l = $('loader');
		if (l && l.parentNode == c)
			c.removeChild(l);
		c.innerHTML = html;
	}

	if (typeof e_update_finish == 'function')
		e_update_finish();	
}

function m_message(t) {
	$('messagebox').style.top = parseInt(document.documentElement.scrollTop) + 250;
	if (document.all && !navigator.userAgent.match(/msie 7/gi))
		$('wait').style.height = parseInt(document.body.offsetHeight);
	if (t) {
		$('message').innerText = t;
		$('wait').style.display = 'block';
	} else {
		$('wait').style.display = 'none';
	}
}

function m_item(e) {
	if (e = $(e)) {
		var id = e.getAttribute('wid');
		var subject = e.getAttribute('subject') || -1;
		var postion = e.getAttribute('postion');
		var recommend = postion == 'commend' ? 2 : -1;
		
		if (config.param['id'] == id) {
			switch (config.param['pass']) {
				case 'client':
				recommend = 1;
				break;
				
				case 'commend':
				recommend = 2;
				break;
				
				case 'get':
				recommend = 3;
				break;
				
				case 'ad':
				recommend = 4;
				break;
			}
		}
		if(e.getAttribute('type')=='checkbox')
			return {id: id, type: config.param['typeid'], track: e.getAttribute('track'), subject: subject, recommend: recommend};
		else
			return {id: id, type: e.getAttribute('type'), track: e.getAttribute('track'), subject: subject, recommend: recommend};
	}
}

function m_log(u) {
	var logger = new Image();
	logger.src = u + 's=' + (new Date().getTime()) + '&';
}

function m_preview_log(i) {
	if (i = $(i)) {
		i = i.tagName ? m_item(i) : i
		if (!i.id || i.id == '') i.id = -1;
		if (!i.recommend || i.recommend == '') i.recommend = -1;
		var u = config.get(config.log["preview"], user.userid, i.type, i.id, i.subject, i.recommend);
		m_log(u);
		return i;
	}
}

function m_download_log(is) {
	if (is && is.push) {
		i = is[is.length - 1]; s = []; f = [];
		var pay = (i.vkey && i.evvalue) ? config.get(config.log['payinfo'], i.vkey, i.evvalue) : '';
		
		for (j = 0; j < is.length; j ++) {
			if (is[j].success) 
				s.push(is[j].id);
			else
				f.push(is[j].id);
		}
		if (s.length > 0) {
			m_log(config.get(config.log['download'], user.userid, i.type, s.join(','), i.subject, i.recommend, pay));
		}
		if (f.length > 0)
			m_log(config.get(config.log['fail'], user.userid, i.type, f.join(','), i.subject, i.recommend, pay));
	}	
}

function is_status(status, e) {
	if (e = $(e)) {
		if (!e.className) return e;
		if (status.substr(0, 1) == '!') {
			if (e.className.indexOf(status.substr(1)) < 0)
				return e;
		} else {
			if (e.className.indexOf(status) >= 0)
				return e;
		}
	}
}

function redirect(url, frame) {
	frame = frame ? frame : window;

		frame.location.assign(url);
}

function m_preload(a) {
	var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var a = a ? a : []; var i,j=d.MM_p.length; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=(a[i].indexOf('/')>0?a[i]:'images/'+a[i]);}}
}

var a_pimgs = ['menutab_l_hover.gif', 'menutab_r_hover.gif', 'menutab_bg_hover.gif',
		  'subbutton_r_hover.gif', 'subbutton_l_hover.gif', 'subbutton_bg_hover.gif',
		  'subbutton_bg_active.gif', 'subbutton_l_active.gif', 'subbutton_r_active.gif',
		  'subbutton_prev_hover.gif', 'subbutton_next_hover.gif',
		  'item_c_hover.gif', 'item_m_hover.gif', 'item_a_hover.gif',
		  'brand_a_hover.gif', 'button_installall_hover.gif', 'bluebutton_r_hover.gif',
		  'bluebutton_l_hover.gif', 'bluebutton_bg_hover.gif', 'brand_s_hover.gif',
		  'crystal_l_hover.gif', 'crystal_r_hover.gif', 'crystal_bg_hover.gif', 'item_hover.gif',
		  'tab_male.gif', 'tab_male_active.gif', 'tab_body_active.gif'];

/* include from prototype.js */
function $() {
	var elements = new Array();
	for (var i = 0; i < arguments.length; i++) {
		var element = arguments[i];
		if (typeof element == 'string') {
			if (typeof window.document[element] != 'undefined')
				element = window.document[element];
			else
				element = document.getElementById(element);
		}
		if (arguments.length == 1)
			return element;
		elements.push(element);
	}
	return elements;
}

Function.prototype.bind = function() {
	var __method = this, args = $A(arguments), object = args.shift();
		return function() {
		return __method.apply(object, args.concat($A(arguments)));
	}
}

function empty() {
}

/* include ajax class */
function Ajax(OnError, OnState, OnDownloadEnd) {
	this.ErrorStr			= null;
	this.ErrorCode			= null;
	this.OnError			= OnError;
	this.OnState			= OnState;
	this.OnDownloadEnd		= OnDownloadEnd;
	
	this.method				= "GET";
	this.URL                = null;
	this.sync				= true;
	this.PostData			= null
	this.RetData			= null;
	this.Browser			= null;
	
	this.HttpObj			= this.createXMLHttpRequest();
	if(this.HttpObj == null) return;
	
	var Obj = this;
	this.HttpObj.onreadystatechange = function () {
		Ajax.handleStateChange(Obj);
	}
}

Ajax.prototype.createXMLHttpRequest = function () {
	if (window.ActiveXObject) {
		this.Browser = 'ie';
		var msxmls = new Array('Microsoft.XMLHTTP');
		for (var i = 0; i < msxmls.length; i++) {
			try {
				return new ActiveXObject(msxmls[i]);
			}catch (e){}
		}
	} else if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	}
	this.ErrorCode = 1;
	this.ErrorStr = "你的浏览器不支持XMLHttpRequest对象．";
	if(this.OnError) {
		this.OnError(this);
	}
	return null;
}

Ajax.prototype.send = function () {
	if (this.HttpObj !== null) {
		this.URL = this.URL;
		this.HttpObj.open(this.method, this.URL, this.sync);
		
		if(this.method.toLocaleUpperCase() == "GET") {
			this.HttpObj.send(null);
		}
		else if(this.method.toLocaleUpperCase() == "POST") {
			this.HttpObj.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
			this.HttpObj.send(this.PostData);
		} else {
			this.ErrorCode = 2;
			this.ErrorStr = "错误的[method]命令．";
			if (this.OnError) {
				this.OnError(this);
			}
			return;
		}
		
		if (this.sync) return;
		if (this.HttpObj.readyState == 4) {
			// 判断对象状态
			if (this.HttpObj.status == 200) {
					if(this.OnDownloadEnd) {
						this.OnDownloadEnd(this.HttpObj.responseText, this.HttpObj.responseXML);
					}
					return;
			} else {
				this.ErrorCode = this.HttpObj.status;
				this.ErrorStr = "您所请求的页面有异常．";
				if(this.OnError) {
					this.OnError(this);
				}
				return;
			}
		}
	}
}

Ajax.handleStateChange = function (Obj) {
	if (Obj.OnState) {
		Obj.OnState(Obj.HttpObj.readyState);
	}
	
	if (Obj.HttpObj.readyState == 4)  {
		// 判断对象状态
		if (Obj.HttpObj.status == 200) {
			if(Obj.OnDownloadEnd)
					Obj.OnDownloadEnd(Obj.HttpObj.responseText, Obj.HttpObj.responseXML);
			return;
		} else {
			Obj.ErrorCode = Obj.HttpObj.status;
			Obj.ErrorStr = "您所请求的页面有异常．";
			if(Obj.OnError) {
					Obj.OnError(Obj);
			}
			return;
		}
	}
}
