/**
* 网页动态列表控制器 
8 版本：Ver 1.0
* 方法：DOM对象访问，根据ID控制已定义的列表对象，建立关联目标之间的事件控制关系，根据用户行为驱动各关联目标的显示或隐藏
* 特点：多浏览器支持，与界面显示设计无关
* 作者：Jackie Liu, DarwinMarketing.com 上海达闻文化传播有限公司
* 授权：本程序免费授权给Sephora.cn，可自由修改，如果对外传播，请保留作者声明
* 变更：
*/

//控制列表列表，当页面发生点击时，如果事件源ID与指定的cmpID相同（可指定多个，用空格分开），则否显示ctrId标签，否则忽略不做任何操作
function hsPageList(cmpId,ctrId,evt){
	if (!evt) var evt = window.event;
	var src = evt.target || evt.srcElement;
	var srcId = src.id;
	if(typeof srcId == 'undefined' || srcId==null || srcId=="" || srcId=="undefined"){
		srcId = "";
	}
	var cids = cmpId.split(" ");
	var find=false;
	for(var f=0;f<cids.length;f++){
		if(cids[f]==srcId) find=true;
		if(find) break;
	}
	var curObj = document.getElementById(ctrId);
	if(typeof curObj == 'undefined' || curObj==null){
	}else{
		var clsn = curObj.className + "";
		var re = new RegExp("hidden");
		clsn = clsn.replace(re,"");
		if(find){
			curObj.className = clsn;
		}else{
			curObj.className = clsn + " hidden";
		}
	}
}
//显示列表选项
function showOptions(relativedId,cmpId,ctrId,evt,movePox){
	 var posi = getElementPosition(relativedId);
	 posi.y = posi.y + movePox;
	 positionElement(ctrId,posi);
	 hsPageList(cmpId,ctrId,evt);
}
//定位列表位置
function positionElement(elementId,position){
	var curObj = document.getElementById(elementId);
	if(typeof curObj == 'undefined' || curObj==null){
	}else{
		curObj.style.position = "absolute";
		curObj.style.top = position.y+"px";
		curObj.style.left = position.x+"px";
	}
}
//获得当前元素正确位置
function getElementPosition(elementId) {
	var ua = navigator.userAgent.toLowerCase();
	var isOpera = (ua.indexOf('opera') != -1);
	var isIE = (ua.indexOf('msie') != -1 && !isOpera); // not opera spoof
	var el = document.getElementById(elementId);
	if(el.parentNode === null || el.style.display == 'none'){
		return false;
	}
	var parent = null;
	var pos = [];
	var box;
	if(el.getBoundingClientRect){//IE
		box = el.getBoundingClientRect();
		var scrollTop = Math.max(document.documentElement.scrollTop, document.body.scrollTop);
		var scrollLeft = Math.max(document.documentElement.scrollLeft, document.body.scrollLeft);
		return {x:box.left + scrollLeft, y:box.top + scrollTop};
	}else if(document.getBoxObjectFor){// gecko
		box = document.getBoxObjectFor(el);
			var borderLeft = (el.style.borderLeftWidth)?parseInt(el.style.borderLeftWidth):0;
			var borderTop = (el.style.borderTopWidth)?parseInt(el.style.borderTopWidth):0;
			pos = [box.x - borderLeft, box.y - borderTop];
		}else{// safari & opera
			pos = [el.offsetLeft, el.offsetTop];
			parent = el.offsetParent;
			if (parent != el) {
			while (parent){
				pos[0] += parent.offsetLeft;
				pos[1] += parent.offsetTop;
				parent = parent.offsetParent;
			}
		}
		if (ua.indexOf('opera') != -1 || ( ua.indexOf('safari') != -1 && el.style.position == 'absolute' )){
			pos[0] -= document.body.offsetLeft;
			pos[1] -= document.body.offsetTop;
		}
	}
	if (el.parentNode) { parent = el.parentNode; }else { parent = null; }
	while (parent && parent.tagName != 'BODY' && parent.tagName != 'HTML'){ // account for any scrolled ancestors
		pos[0] -= parent.scrollLeft;
		pos[1] -= parent.scrollTop;
		if (parent.parentNode) { parent = parent.parentNode; }else { parent = null; }
	}
	return {x:pos[0], y:pos[1]};
}
//事件监听，必要时关闭选项列表
var aeOL = [];
function addEvent(o, n, f, l)
{
 var a = 'addEventListener', h = 'on'+n, b = '', s = '';
 if (o[a] && !l) return o[a](n, f, false);
 o._c |= 0;
 if (o[h])
 {
  b = '_f' + o._c++;
  o[b] = o[h];
 }
 s = '_f' + o._c++;
 o[s] = f;
 o[h] = function(e)
 {
  e = e || window.event;
  var r = true;
  if (b) r = o[b](e) != false && r;
  r = o[s](e) != false && r;
  return r;
 };
 aeOL[aeOL.length] = { o: o, h: h };
};
addEvent(window, 'unload', function() {
 for (var i = 0; i < aeOL.length; i++) with (aeOL[i])
 {
  o[h] = null;
  for (var c = 0; o['_f' + c]; c++) o['_f' + c] = null;
 }
});

function cancelEvent(e, c)
{
 e.returnValue = false;
 if (e.preventDefault) e.preventDefault();
 if (c)
 {
  e.cancelBubble = true;
  if (e.stopPropagation) e.stopPropagation();
 }
};

addEvent(document, 'click', function(evt)
{
 evt = evt || window.event;
 if(typeof(vcmpId) == "undefined"){
 }else hsPageList(vcmpId,vctrId,evt);
}, 1);

function changeBgImg(img){
	var bg = document.getElementById("Selections");
	bg.style.height = "100px";
	bg.style.padding = "3px"; 
	bg.style.lineHeight = "1.5em";
	bg.style.background = "url(" + img + ") no-repeat left 30px";
}
