/***************************************************************
* Description	: UI Application For Mely.com.cn.
* Copyright		: Mely.com.cn All Rights Reserved.
* Author		: Taskman QQ:5717297
***************************************************************/

var mely = {};
mely.$ = function(id, o){
	if(id == null || typeof id != "string" || id == "") return false;
	var element = o || document;
	return element.getElementById(id);
};
mely.$$ = function(tag){
	if(!(/^\w+$/).test(tag)) return false;
	return document.createElement(tag);
};
mely.evt = {
	add : function(el, e, listener) {
        if (el.addEventListener){
            el.addEventListener(e, listener, false);
        } else if (el.attachEvent){
            el.attachEvent('on' + e, listener);
        }
    },
    remove : function(el, e, listener) {
        if (el.removeEventListener){
            el.removeEventListener(e, listener, false);
        } else if (el.detachEvent){
            el.detachEvent('on' + e, listener);
        }
    }
};
mely.attrib = {
	set : function(o, a, v){
		if(o == null || o.tagName == undefined) return false;
		o.setAttribute(a, v);
		return true;
	},
	get : function(o, a){
		if(o == null || o.tagName == undefined) return false;
		return o.getAttribute(a);
	}
}
/*The pix player*/
mely.pixplayer = function(px, xp, pl, tl, il, fs, bs){
	var base = this;
	this.pix = mely.$(px || "pix");
	this.pxp = mely.$(xp || "pxp");
	this.pxpl = mely.$(pl || "pxpl");
	this.pxtl = mely.$(tl || "pxtl");
	this.pxil = mely.$(il || "pxtag");
	this.foc = fs || "ptif";
	this.blr = bs || "";
	this.data = [];//数据
	this.timer = null;//延时器
	this.interval = 3000;//间隔时间
	this.speed = 30;//切换速度，值越小速度越快
	this.playing = true;//是否播放
	this.current = 0;//当前位置
	this.converse = false;//是否逆向
	//添加
	this.add = function(bp, sp, lnk, txt){
		if(!bp || !sp || !lnk || !txt || base.data.length >= 6) return;
		var _photo = new Image();
		_photo.src = bp;
		_photo.width = 232;
		_photo.height = 310;
		_photo.onload = function(){
			this.width = 232;
			this.height = 310;
		}
		var _plink = mely.$$("a");
		_plink.style.cssText = "overflow:hidden;width:232px;height:310px;";
		_plink.href = lnk;
		mely.attrib.set(_plink, "title", txt);
		_plink.appendChild(_photo);
		var _icon = new Image();
		_icon.src = sp;
		_icon.width = 31;
		_icon.height = 41;
		_icon.onload = function(){
			this.width = 31;
			this.height = 41;
		}
		var _ilink = mely.$$("a");
		_ilink.href = lnk;
		mely.attrib.set(_ilink, "title", txt);
		_ilink.appendChild(_icon);
		_ilink.onmouseover = function(){base.dothis(this);}
		mely.attrib.set(_ilink, "idx", base.data.length.toString());
		base.data.push({plink : _plink, ilink : _ilink});
	}
	//显示
	this.show = function(){
		base.pxpl.style.width = (base.data.length * 232) + "px";
		var i = 0;
		while(i < base.data.length){
			var p = base.data[i];
			base.pxpl.appendChild(p.plink);
			base.pxil.appendChild(p.ilink);
			i++;
		}
	}
	//计算当前位置与目标位置的差值
	this.calcdis = function(){
		return ((base.current * 232) - base.pxp.scrollLeft);
	}
	//切换图片
	this.switchpix = function(){
		if(base.data.length == 0) return;
		clearTimeout(base.timer);
		var dis = base.calcdis();
		if(base.calcdis() == 0){
			base.timer = setTimeout(base.play, base.interval);
		}else{
			var u = dis / 4;
			if(Math.abs(u) < 1) u = u / Math.abs(u);
			base.pxp.scrollLeft += u;
			base.timer = setTimeout(base.switchpix, base.speed);
		}
	}
	//播放
	this.play = function(){
		if(base.data.length == 0 || !base.playing) return;
		base.data[base.current].ilink.className = base.blr;
		base.next();
		base.markcurrent();
		clearTimeout(base.timer);
		base.timer = setTimeout(base.switchpix, base.speed);
	}
	this.markcurrent = function(){
		var c = base.data[base.current].ilink;
		c.className = base.foc;
		base.pxtl.innerHTML = c.title;
		base.pxtl.href = c.href;
	}
	//移动到下一个位置
	this.next = function(){
		if(base.converse){
			if(base.current == 0){
				base.current += 1;
				base.converse = false;
			}else{
				base.current -= 1;
			}
		}else{
			if(base.current == (base.data.length - 1)){
				base.current -= 1;
				base.converse = true;
			}else{
				base.current += 1;
			}
		}
	}
	this.start = function(){
		base.timer = setTimeout(base.play, base.interval);
	}
	this.pix.onmouseover = function(){
		if(!base.playing) return;
		base.playing = false;
		clearTimeout(base.timer);
		base.timer = setTimeout(base.switchpix, 100);
	}
	this.pix.onmouseout = function(){
		if(base.playing) return;
		base.playing = true;
		clearTimeout(base.timer);
		base.start();
	}
	this.dothis = function(n){
		if(n == null) return;
		var idx = mely.attrib.get(n, "idx");
		if(!idx) return;
		base.data[base.current].ilink.className = base.blr;
		base.current = parseInt(idx);
		base.markcurrent();
		//base.switchpix();
	}
	//初始化播放器
	this.init = function(){
		base.show();
		if(base.data.length > 0){
			base.markcurrent();
		}
		base.start();
	}
	mely.evt.add(window, "load", base.init);
};
mely.menusw = function(ctrl, ctrlitem, ctrlto, start, delay, fstyle, bstyle)
{
	var base = this;
	this.Ctrl = mely.$(ctrl);
	if(this.Ctrl == null){return false;}
	this.itemtag = ctrlitem || "li";
	this.Items = null;
	this.CtrlTo = ctrlto || [];
	this.Start = start || 0;
	this.Delay = delay || 200;
	this.Focus = fstyle || null;
	this.Blur = bstyle || null;
	if(this.Items == null)
	{
		this.Items = [];
		for(i = 0; i < this.Ctrl.childNodes.length; i++)
		{
			var _item = this.Ctrl.childNodes[i];
			if(_item.tagName != null && _item.tagName != undefined && _item.tagName.toLowerCase() == this.itemtag)
			{
				_item.onmouseover = function()
				{
					base.strt = this;
					setTimeout(function(){base.Show();}, base.Delay);
				}
				this.Items.push(_item);
			}
		}
	}
	this.Mdls = null;
	if(this.Mdls == null)
	{
		this.Mdls = [];
		for(i = 0; i < this.Items.length; i++)
		{
			var _ctrlto = this.CtrlTo[i];
			if(_ctrlto != null && _ctrlto != undefined && typeof _ctrlto == "string")
			{
				var _ct = mely.$(_ctrlto);
				if(_ct != null)
				{
					this.Mdls.push(_ct);
				}
				else
				{
					this.Mdls.push(null);
				}
			}
			else
			{
				this.Mdls.push(null);
			}
		}
	}
	this.strt = this.Items[this.Start];
	if(this.strt == null || this.strt == undefined)
	{
		this.strt = this.Items[0];
	}
	this.Show = function()
	{
		if(this.strt == null || this.strt == undefined)
		{
			return;
		}
		for(i = 0; i < this.Items.length; i++)
		{
			var fitem = this.Items[i];
			var _mdl = this.Mdls[i];
			if(fitem == this.strt)
			{
				if(this.Focus != null)
				{
					if(typeof this.Focus == "object")
						fitem.className = this.Focus[i];
					else
						fitem.className = this.Focus;
				}
				if(_mdl != null)
				{
					_mdl.style.display = "";
				}
			}
			else
			{
				if(this.Blur != null)
				{
					if(typeof this.Blur == "object")
						fitem.className = this.Blur[i];
					else
						fitem.className = this.Blur;
				}
				if(_mdl != null)
				{
					_mdl.style.display = "none";
				}
			}
		}
	}
	this.Show();
};
mely.scroller = function(tgt, u){
	var base = this;
	this.target = mely.$(tgt);
	this.units = u;
	this.toLeft = function(){
		var sl = base.target.scrollLeft - base.units;
		if(sl < 0) sl = 0;
		base.target.scrollLeft = sl;
	}
	this.toRight = function(){
		var sl = base.target.scrollLeft + base.units;
		if(sl > base.target.scrollWidth)
			sl = base.target.scrollWidth;
		base.target.scrollLeft = sl;
	}
};

mely.floatmenu = function(c, m){
	if(c == null || m == null) return;
	this.ctrlr = mely.$(c);
	this.menu = mely.$(m);
	if(this.ctrlr == null || this.menu == null) return;
	var base = this;
	mely.floatmenu.cache.push(this.menu);
	this.ablehide = true;
	this.hide = function(){
		if(!base.ablehide) return;
		base.menu.style.left = "-65535px";
		base.menu.style.top = "-65535px";
	}
	this.init = function(){
		mely.evt.add(window, "load", base.hide);
		mely.evt.add(document, "click", base.hide);
	}
	this.ctrlr.onmouseover = function(evt){
		var e = window.event || evt;
		var x = e.x || e.pageX;
		var y = e.y || e.pageY;
		if(y < document.documentElement.scrollTop)
			y += document.documentElement.scrollTop;
		var ix = e.offsetX || e.layerX;
		var iy = e.offsetY || e.layerY;
		x -= ix;
		y += this.offsetHeight;
		y -= iy;
		base.menu.style.left = x+"px";
		base.menu.style.top = y+"px";
		for(i = 0;i < mely.floatmenu.cache.length;i++){
			var _fm = mely.floatmenu.cache[i];
			if(_fm != base.menu){
				_fm.style.left = "-65535px";
				_fm.style.top = "-65535px";
			}
		}
	}
	this.menu.onmouseover = function(ev){
		var e = window.event || evt;
		var nd = e.srcElement || e.target;
		if(nd == this || nd.parentNode == this)
			base.ablehide = false;
	}
	this.menu.onmouseout = function(){
		base.ablehide = true;
	}
}
mely.floatmenu.cache = [];