var shadeListener = null;
var imgBaseUrlElem = document.getElementById('imgBaseUrl');
var imgBaseUrl = '';
if (imgBaseUrlElem != null) {
	imgBaseUrl = document.getElementById('imgBaseUrl').value;
} else {
	imgBaseUrl = 'http://image.kuwo.cn';	
}

/**
 * 收起或展开右侧豆腐块的列表项
 * @param {Object} preFix
 * @param {Object} id
 * @param {Object} length
 */
function showPl(preFix, id, length){
	for (var i = 0; i < length; i++) {
		var idName = preFix + i;
		//$(idName).style.display="none";
		$(idName).style.height = "23px";
		$(idName).style.backgroundColor = "#fff";
	}
	//$(preFix+id).style.display="block";
	$(preFix + id).style.height = "103px";
	$(preFix + id).style.backgroundColor = "#f2f6fb";
}


/**
 * 收起或展开右侧豆腐块的列表项(通用版！)
 * @param {Object} preFix
 * @param {Object} id
 * @param {Object} length
 */
function showPlUniversal(preFix, id, length,bigHeight,smallHeight){
	//alert(length);
	for (var i = 0; i < length; i++) {
		var idName = preFix + i;
		//$(idName).style.display="none";
		$(idName).style.height =smallHeight;
		$(idName).style.backgroundColor = "#fff";
	}
	//$(preFix+id).style.display="block";
	$(preFix + id).style.height =bigHeight;
	$(preFix + id).style.backgroundColor = "#f2f6fb";
}

/**
 * kzone首页,本周火爆歌手处用的代码
 */
function showWeekHotUser(preFix, id, length){
	for(var i = 0; i < length; i++){
		$(preFix + i).style.display = 'none';
		$(preFix + i).style.width = '280px';
		$(preFix + i).style.height = '0';
		$(preFix + i).style.overflow = 'hidden';
	}
	$(preFix + id).style.display = 'block';
	$(preFix + id).style.height = '80px';
}

/**
 * 更改table背景样式
 * @param {Object} preFix
 * @param {Object} id
 * @param {Object} length
 */
function changeTableBG(preFix, id, length){
	for (var i = 0; i < length; i++) {
		var idName = preFix + i;
		//alert(idName);
		$(idName).style.backgroundColor = "#fff";
	}
	$(preFix + id).style.backgroundColor = "#f2f6fb";
}


/**
 * 去搜索页
 */
function goSearchNew(){
	var k = document.getElementById('key').value;
	k = k.trim();
	if (k == null || k == '') {
		document.getElementById('key').value = '';
		return false;
	}
	if(k.length>100){
		alert("搜索关键字长度请限制在100个字符以内");		
		return false;
	}
	
	
	document.getElementById('go_search').disabled = true;
	$('navSearchForm').submit();		
	return true;
}

function SearchByType(typeid,keyid){
	var k = document.getElementById(keyid).value;
	k = k.trim();
	if (k == null || k == '') {
		document.getElementById(keyid).value = '';
		return false;
	}
	if(k.length>100){
		alert("搜索关键字长度请限制在100个字符以内");		
		return false;
	}
	var stype = document.getElementById(typeid).options[document.getElementById(typeid).selectedIndex].value;
	window.location="/ws/Search?key=" + encodeURI(k) + "&type=" + stype;
}

/**
 * 去搜索页
 */
function goSearch(){
	var k = document.getElementById('key').value;
	k = k.trim();
	if (k == null || k == '') {
		document.getElementById('key').value = '';
		return false;
	}
	if(k.length>100){
		alert("搜索关键字长度请限制在100个字符以内");		
		return false;
	}
	
	
	document.getElementById('go_search').disabled = true;
	var ot = $('navSearchForm').action;
	ot += '?key=' + k;
	if(document.getElementById('search_type')){
		var t = document.getElementById('search_type').getAttribute('type').trim();
		ot += '&type=' + t ;
	}
	
//	alert(t);
	$('navSearchForm').action = ot;
	$('type').value = t;
//	if(confirm($('navSearchForm').action)) {
		$('navSearchForm').submit();		
//	}
}

/**
 * 显示搜索条件
 */
function showSearchItems(){
	removeClass(document.getElementById('search_item'), 'disp_none');
	addClass(document.getElementById('search_item'), 'disp_block');
	document.getElementById('search_item').style.top = (parseInt(getTop(document.getElementById('global_search'))) + 23) + 'px';
	document.getElementById('search_item').style.left = (parseInt(getLeft(document.getElementById('global_search'))) - 1) + 'px';
}

/**
 * 添加鼠标搜索条件的样式
 */
function addMouseSearchStyle(){
	var lis = document.getElementById('search_item').getElementsByTagName('li');
	for (var i = 0; i < lis.length; i++) {
		lis[i].onmouseover = function(){
			addClass(this, 'item_over');
			removeClass(this, 'item_out');
		};
		lis[i].onmouseout = function(){
			addClass(this, 'item_out');
			removeClass(this, 'item_over');
		};
		lis[i].onclick = function(){
			document.getElementById('search_type').childNodes[0].nodeValue = this.childNodes[0].nodeValue;
			document.getElementById('search_type').setAttribute('type', this.getAttribute('type'));
			removeClass(document.getElementById('search_item'), 'disp_block');
			addClass(document.getElementById('search_item'), 'disp_none');
		};
	}
}

/**
 * 确定频道样式
 */
function initMainNav(){
	var curChannelVal = document.getElementById('channelType');
	if (curChannelVal != null) {
		var curChannelLi = document.getElementById(curChannelVal.value);
		if (curChannelLi != null) {
			addClass(curChannelLi, 't_blue_14_b nav_def');
			var channelText = curChannelLi.getElementsByTagName('a')[0].firstChild.nodeValue;
			var channelNode = document.createTextNode(channelText);
			addClass(curChannelLi, 'h36');
			//alert(channelText);
			curChannelLi.replaceChild(channelNode, curChannelLi.firstChild);
			//alert(curChannel.outerHTML);
		}
	}
	//为其它频道添加鼠标效果
	var mainNavUl = document.getElementById('main_nav');
	if (mainNavUl != null) {
		var mainNavList = mainNavUl.getElementsByTagName('a');
		//		alert(mainNavList.length);
		for (var i = 0; i < mainNavList.length; i++) {
			mainNavList[i].onmouseover = function(){
				addClass(this, 'mnhover');
			}
			mainNavList[i].onmouseout = function(){
				removeClass(this, 'mnhover');
			}
		}
	}
}

function initMainNavNew(){
	var curChannelVal = document.getElementById('channelType');
	if (curChannelVal != null) {
		var curChanneldd = document.getElementById(curChannelVal.value);
		if (curChanneldd != null) {
			addClass(curChanneldd.getElementsByTagName('a')[0], 'aDefault');
			//var channelText = curChanneldd.getElementsByTagName('a')[0].firstChild.nodeValue;
			//var channelNode = document.createTextNode(channelText);
			
			//alert(channelText);
			//curChanneldd.replaceChild(channelNode, curChanneldd.firstChild);
			//alert(curChannel.outerHTML);
		}
	}
	
}
/**
 * 确定子频道样式
 */
function initSubNav(){
	var curPageVal = document.getElementById('pageType');
	if (curPageVal != null) {
		var curPageA = document.getElementById(curPageVal.value);		
		if (curPageA != null) {
			addClass(curPageA, 'font_b');
			addClass(curPageA, 'l_blue_12');
			removeClass(curPageA, 'l_blue_12_l');
			curPageA.removeAttribute('href');
		}
	}
}

/**
 * 初始化首页焦点图样式
 */
function initIndexFocusList(){
//	alert(imgBaseUrl);
	var ttsParent = $('fi_title_list');
	if (ttsParent != null) {
		var tts = $('fi_title_list').getElementsByTagName('li');
		//初始化第一栏
		if (tts.length > 0) {
			tts[0].style.fontWeight = 'bold';
			tts[0].style.borderBottom = '#c6d8e6 1px solid';
			tts[0].style.zIndex = '2';
			tts[0].style.color = '#fb6600';
			tts[0].style.borderLeft = '#fff 1px solid';
			tts[0].style.background = '#fff url(' + imgBaseUrl + '/tabjt.gif) no-repeat left center';
			if(tts[4]!=null) {
				tts[4].style.borderBottom = '0';				
			}
		}		
		for (var i = 1; i <= tts.length; i++) {
			curBt = tts[i - 1];
			curBt.onmouseover = function(e){
				for (var tb = 0; tb < tts.length; tb++) {
					tts[tb].style.background = 'url(' + imgBaseUrl + '/lx_t_bg.gif)';
					tts[tb].style.color = '#1d78d3';
					tts[tb].style.borderLeft = '#c6d8e6 1px solid';
					tts[tb].style.cursor = 'pointer';
					if(tb==4) {
						tts[tb].style.borderBottom = '0';
					} else {
						tts[tb].style.borderBottom = '#c6d8e6 1px solid';
					}
					tts[tb].style.zIndex = '3';
				}
				hideFocus();
				showFocus(e);
			}
		}
	}
}

/**
 * 隐藏首页焦点图
 */
function hideFocus(){
	var ctts = $('fi_list').getElementsByTagName('div');
	for (var i = 0; i < ctts.length; i++) {
		removeClass(ctts[i], 'disp_block');
		addClass(ctts[i], 'disp_none');
	}
}

/**
 * 显示首页焦点图
 * @param {Object} e
 */
function showFocus(e){
	var et = getEvtTarget(e);//得到当前焦点项
	var sid = et.id.substring(3);
	removeClass($('fi_' + sid), 'disp_none');
	addClass($('fi_' + sid), 'disp_block');
	removeClass($('fiimg_' + sid), 'disp_none');
	addClass($('fiimg_' + sid), 'disp_block');
	
	et.style.fontWeight = 'bold';
	if (et.id == 'lx_5') {
		et.style.borderBottom = '0';
	}
	else {
		et.style.borderBottom = '#c6d8e6 1px solid';
	}
	et.style.zIndex = '2';
	et.style.color = '#fb6600';
	et.style.borderLeft = '#fff 1px solid';
	et.style.background = '#fff url(' + imgBaseUrl + '/tabjt.gif) no-repeat left center';
}


//显示弹出框，并在外层加一个半透明黑边
function checkActivate(boxName,shadeName) {
	clearTimeout(shadeListener);
    var elemModalWin = $(shadeName);
    var elemModalContent = $(boxName);
	elemModalWin.style.display = "block";
	elemModalContent.style.display = "block";

	elemModalContent.style.top=(100+document.documentElement.scrollTop)+'px';
	elemModalContent.style.left="50%";
	elemModalWin.style.left="50%";
	elemModalWin.style.width=elemModalContent.clientWidth+20+"px";
	elemModalWin.style.height=elemModalContent.clientHeight+20+"px";
	elemModalWin.style.top=document.documentElement.scrollTop+100-10+"px";
	var temp=elemModalContent.clientWidth/2+10;
	elemModalWin.style.marginLeft="-"+temp+"px";
	//alert(elemModalWin.style.left);
	//alert(elemModalContent.style.left);
	//alert(elemModalWin.style.marginLeft);
	//alert(elemModalContent.style.marginLeft);
}

function resizeShadeBox(shadeName, h) {
//	console.log($(''+h).clientHeight);
    var elemModalWin = $(shadeName);
	var counter = 0;
	var resizeShadeBoxListener = null;
	if(elemModalWin!=null) {
		resizeShadeBoxListener = setInterval(function(){
	//		console.log($(''+h).clientHeight);
			counter++;
			if(DEBUG_ENABLE && confirm('clear resizeShadeBoxListener?' + 'now=' + counter)) {
				clearInterval(resizeShadeBoxListener);
			}
			if(counter>=20) {
				clearInterval(resizeShadeBoxListener);
			}
			elemModalWin.style.height=$(''+h).clientHeight+20+"px"
			var divs = $(''+h).getElementsByTagName('div');
			for(var i=0; i<divs.length; i++) {
				if(divs[i].className=='alertBottomBG') {
					divs[i].style.top=$(''+h).clientHeight-33+"px";
					break;
				}
			}
		}, 20);		
	}
}

/**
 * 列表轮显效果
 * @param {Object} obj
 */
function mouseOver(obj) {
	obj.style.backgroundColor='#ecf7ff';
}

/**
 * 列表轮显效果
 * @param {Object} obj
 */
function mouseOut(obj) {
	obj.style.backgroundColor='#fff';
}

function addMusicListOverEffect(objid) {
	var starCTableList = document.getElementById(objid);
	if(starCTableList!=null) {
		starCTableList = starCTableList.getElementsByTagName('table');
		for(var i=0; i<starCTableList.length; i++) {
			starCTableList[i].onmouseover = function(){mouseOver(this)};
			starCTableList[i].onmouseout = function(){mouseOut(this)};
		}
	}
}

function addMusicListOverEffectBiao(objid) {
	var starCTableList = document.getElementById(objid);
	if(starCTableList!=null) {
		starCTableList = starCTableList.getElementsByTagName('tr');
		for(var i=0; i<starCTableList.length; i++) {
			starCTableList[i].onmouseover = function(){mouseOver(this)};
			starCTableList[i].onmouseout = function(){mouseOut(this)};
		}
	}
}

/**
 * 添加字幕分类选中样式
 */
function initAbList() {
	var cab = document.getElementById('cat').value;
	var abList = document.getElementById('alphabetList');
	if(abList!=null) {
		abList = abList.getElementsByTagName('a');
		for(var i=0; i<abList.length; i++) {
			if(abList[i].getAttribute('href').indexOf('cat_' + cab + '.htm')>0) {
				abList[i].setAttribute('href', 'javascript:void(0);');
				abList[i].childNodes[0].style.backgroundPosition = 'left bottom';
				abList[i].childNodes[0].style.cursor = 'default';
				abList[i].childNodes[0].onmouseover = null;
				abList[i].childNodes[0].onmouseout = null;
				break;
			}
		}
	}
}

//添加搜索input的over样式
//var searchType = document.getElementById('search_type');
//var goSearchButton = document.getElementById('go_search');
//if(searchType!=null && goSearchButton!=null) {
//	searchType.onmouseover = function() {searchType.style.background = 'url(http://image.kuwo.cn/search_li_over.gif)'};
//	searchType.onmouseout = function() {searchType.style.background = 'url(http://image.kuwo.cn/search_li.gif)'};
//	goSearchButton.onmouseover = function() {goSearchButton.src = 'http://image.kuwo.cn/nav_searh_over.gif'};
//	goSearchButton.onmouseout = function() {goSearchButton.src = 'http://image.kuwo.cn/nav_searh.gif'};
//}


/********************************************************************************************/
var StylUtil = {};

/**
 * 删除elem的cName类样式
 * @param {Object} elem
 * @param {Object} cName
 */
StylUtil.removeClassName = function(elem, cName){
	var cStr = elem.className;
	if (cStr != null) {
		var reg = new RegExp('('+cName+')', 'g');
		cStr = cStr.replace(reg, '');
		reg = /\s{2,}/g;
		cStr = cStr.replace(reg, ' ');
		elem.className = cStr;
	}
}

/**
 * 为elem添加cName类样式
 * 如果存在cName类样式，则会将原来的cName删除，并在className尾部将其重新添加
 * 如果不同class的样式间有冲突，则后添加的样式会覆盖前面的样式
 * @param {Object} elem
 * @param {Object} cName
 */
StylUtil.addClassName = function(elem, cName){
	StylUtil.removeClassName(elem, cName);
	elem.className += ' ' + cName;
}

/**
 * 将某项选中，并替换为文本节点的通用函数
 * @param {Object} obj				需要改变的元素对象（对象应为一个链接元素）
 * @param {Object} className		新的样式
 * @param {Object} onCompleteFunc	设置完成之后的其它处理
 * @param {Object} oldClassName		需要去掉的旧样式（此时认为obj不需转换为文本节点）
 */
StylUtil.setSelected = function(obj, className, onCompleteFunc, oldClassName) {
	if(obj==null) {
		return false;
	}
	if(oldClassName==null) {//转换为文本节点
		var pNode = obj.parentNode;
		var sNode = document.createElement('span');		
		var tNode = document.createTextNode(obj.firstChild.nodeValue);
		sNode.appendChild(tNode);
		pNode.replaceChild(sNode, obj);
		if(className!=null) {
			StylUtil.addClassName(sNode, className);
		}
	} else {//不转换为文本节点
		StylUtil.removeClassName(obj, oldClassName);
		StylUtil.addClassName(obj, className);
	}
	
	if(onCompleteFunc!=null) {
		onCompleteFunc();
	}
}



