
// 目录标签控制

var HOSTNAME = location.href.toLowerCase().substr(7);
HOSTNAME = HOSTNAME.substr(0, HOSTNAME.indexOf('/'));
HOSTNAME = 'local'==HOSTNAME ? 'http://local' : 'http://www.inesun.com';
var firstTagLeft = $('#nav li.item:first').offset().left;
var naviTimer, tags = new Array();
for(i=0; i<$('#nav li.item').length; tags[i++]='');

$('#nav li.item').hover(
	function(){
		$(this).addClass('focus');
		var subNavi = $(this).children("div.subCon");
		subNavi.attr('id', 'nav_focus');
		var id = $(this).attr('tagid') - 0;
		if (tags[id] != '') { // 读取标签内容并格式化输出 (仅在第一次悬停到该标签时)
			naviTimer = setTimeout("showSubNav();", 400);
		} else {
			tags[id] = ' '; // 避免重复读取
			var offset = $(this).offset().left - firstTagLeft;
			$.get('./themes/customer/cache.tags.php', {tagid: id}, function(data){
				if (data == 'Gadgets') { // 手工设置 $.99 标签
					tags[id] += '<div class="cataBox">';
					tags[id] += '<a href="' + HOSTNAME + '/Gadgets.html" class="lever1">$.99 Gadgets Store</a>';
					tags[id] += '<ul class="subCataList">';
					tags[id] += '<li><a href="' + HOSTNAME + '/Gadgets-1.html">US$ 0.99 - 1.99</a></li>';
					tags[id] += '<li><a href="' + HOSTNAME + '/Gadgets-2.html">US$ 2.99 - 5.99</a></li>';
					tags[id] += '<li><a href="' + HOSTNAME + '/Gadgets-3.html">US$ 6.99 - 9.99</a></li>';
					tags[id] += '<li><a href="' + HOSTNAME + '/Gadgets-4.html">US$ 10.99 - 15.99</a></li>';
					tags[id] += '</ul>';
					tags[id] += '</div>';
					columns = 1;
				} else { // 其它标签
					var arr = data.split('__');
					if (arr.length<2) return;
					for (i=total=0; i<arr.length; ++i) {
						arr[i] = arr[i].split('|');
						total += arr[i][2]=='0' ? 1 : 0; // 大类数目
					}
					columns = total<4 ? total : 4; // 分栏数
					for (col=1; col<=columns; ++col) {
						tags[id] += '<div class="cataBox">';
						for (i=ctr=0; i<arr.length; ) { // 定位到当前栏的第一个大类
							if ( arr[i][2]>0 ) { ++i; continue; }
							if ( ++ctr<col ) ++i; else break;
						}
						while (i<arr.length) { // 开始输出当前栏
							url = HOSTNAME + '/' + cataNameToURL(arr[i][1]) + '--A'+arr[i][0] + '.html';
							tags[id] += '<a href="' + url + '" class="lever1">' + arr[i][1] + '</a>'; // 大类
							tags[id] += '<ul class="subCataList">';
							for (++i; i<arr.length && arr[i][2]>0; ++i) { // 小类
								url = HOSTNAME + '/' + cataNameToURL(arr[i][1]) + '--B'+arr[i][0] + '.html';
								tags[id] += '<li><a href="' + url + '">' + arr[i][1] + '</a></li>';
							}
							tags[id] += '</ul>';
							for (ctr=0; i<arr.length; ) { // 定位到当前栏的下一个大类
								if ( arr[i][2]>0 ) { ++i; continue; }
								if ( ++ctr<4 ) ++i; else break;
							}
						}
						tags[id] += '</div>';
					}
				}
				subNavi.html(tags[id]); // 填充标签内容
				// 若少于4栏, 需调整内容框的宽度与位置
				if (columns<4) {
					var width = columns * 240;
					subNavi.width(width);
					if ( id > 0 ) offset -= 6;
					if ( offset + width > 988 ) offset = 988 - width;
					subNavi.css('marginLeft', offset); 
				}
				naviTimer = setTimeout("showSubNav();", 300);
			});
		}
	},
	function(){
		$(this).removeClass('focus');
		clearTimeout(naviTimer);
		$('#nav_focus').attr('id', '');
		$(this).children("div.subCon").stop(true, true).slideUp('fast');
	}
);

function showSubNav(){
	$('#nav_focus').stop(true, true).slideDown("fast");
}

// 格式化目录名称, 为链接的一部分
function cataNameToURL(name) {
	name = name.replace(/^ +/, '').replace(/ +$/, '');
	name = name.replace(/[^A-Za-z0-9\.]+/g, '-').replace(/\.+$/, '');
	return name;
}

