(function($) {
    var subMenuOver = function() {
        var obj = $(this);
        if (parseFloat(obj.attr('id').split("m")[1]) != MainMenuIndex)
            obj.attr('src', '../images/menu/' + obj.attr('id') + '_o.gif');
    };
    
    var subMenuOut = function() {
        var obj = $(this);
        if (parseFloat(obj.attr('id').split("m")[1]) != MainMenuIndex)
            obj.attr('src', '../images/menu/' + obj.attr('id') + '.gif');
    };
	
	var loadStaticPage = function() {
		var obj = $(this);
		obj.attr('href', 'javascript:;');
		var tmpPIndex = SideMenuPIndex;
		var tmpIndex = SideMenuIndex;
		SideMenuPIndex = parseFloat(obj.attr('ref').split(",")[0]);
		SideMenuIndex = parseFloat(obj.attr('ref').split(",")[1]);
		if (tmpPIndex != SideMenuPIndex) {
			loadStaticSideMenu();
		}
		if (tmpIndex != SideMenuIndex) {
			loadStaticContent();
			$('#curr_location').html(getSideMenuNameByIndex());
		}
	};
	
	var loadStaticSideMenu = function() {
		$('.level2 > ul').slideUp('slow');
		$('.level2').removeClass('level2').addClass('level1');
		$('.level1[ref='+SideMenuPIndex+']').removeClass('level1').addClass('level2');
		$('.level2 > ul').slideDown('slow');
	};
	
	var loadStaticContent = function() {
		$('#subcontent').html('正在加载中...');
		$('#subcontent').hide();
		$('#subcontent').load(getSideMenuUrlByIndex());
		$('#subcontent').slideDown('slow');
	};
	
	var dynamicSubPage = function() {
		SideMenuPIndex = getSideMenuPIndexbyIndex();
		$('#subnav_title').css('background', 'url('+getMainMenuPicByKey()+') no-repeat 10px 5px');
		if (SideMenuIndex <= 0) $('#curr_location').html('全部');
		else $('#curr_location').html(getSideMenuNameByID());
		var str = "";
		var str2 = "";
		var trace = "";
		var page_count = 0;
		var page_num = 0;
		$('#itemlist').html('正在加载中...');
		if (pageKey == "news") {
			str += '<li class="level1" ref="0"><a href="' + MainMenu[2].Url + '" title="'+MainMenu[2].Name+'">全部</a></li>';
			for (var i=0; i<SideMenu.length; i++) {
				str += '<li class="level1" ref="'+SideMenu[i].Index+'"><a href="' + MainMenu[2].Url + '?p=' + SideMenu[i].Index + '" title="'+SideMenu[i].Name+'">'+SideMenu[i].Name+'</a></li>';
			}
			$.getScript(NEWSES_JS_DATA + '&class=' + SideMenuIndex + '&size=' + InfoItemPageSize + '&page=' + InfoItemCurrPage, function() {
				for (var m=0; m<InfoItem.length; m++) {
					str2 += '<li><a href="' + MainMenu[2].Url + '?p=' + InfoItem[m].ClassID + '" class="cate" title="' + InfoItem[m].ClassName + '">[' + InfoItem[m].ClassName + ']</a>';
					if (InfoItem[m].Focused == "1") {
						str2 += '<a href="thread.html?id=' + InfoItem[m].ID + '" class="hot" title="' + InfoItem[m].Subject + '">' + InfoItem[m].Subject + '</a>';
					} else {
						str2 += '<a href="thread.html?id=' + InfoItem[m].ID + '" title="' + InfoItem[m].Subject + '">' + InfoItem[m].Subject + '</a>';
					}
					str2 += '<span>' + InfoItem[m].PubTime + '</span></li>';	
				}
				$('#itemlist').html('<ul>' + str2 + '</ul>').slideDown('slow');
				page_count = (InfoItemCount%InfoItemPageSize>0) ? Math.floor(InfoItemCount/InfoItemPageSize+1) : Math.floor(InfoItemCount/InfoItemPageSize);
				page_num = InfoItemCurrPage;
				trace = "<span>共" + page_count + "页</span><span>" + page_num + "/" + page_count + "</span>";
				if (page_num > 1) {
					trace += "<a href=\"#\" ref=\"1\">&lt;&lt;</a><a href=\"#\" ref=\"" + (parseInt(page_num)-1) + "\">&lt;</a>";
				} else {
					trace += "<span>&lt;&lt;</span><span>&lt;</span>";
				}
				if (page_num <= PAGER_SIDE_MAX) {
					PAGER_START_NUM = 1;
				} else if (page_count >= PAGER_COUNT && page_num > page_count - PAGER_SIDE_MAX) {
					PAGER_START_NUM = page_count - PAGER_COUNT + 1;
				} else {
					PAGER_START_NUM = page_num - PAGER_SIDE_MAX + 1;
				}
				PAGER_END_NUM = (page_count > (PAGER_COUNT+PAGER_START_NUM-1)) ? (PAGER_COUNT+PAGER_START_NUM-1) : page_count;
				for (var i=PAGER_START_NUM; i<=PAGER_END_NUM; i++) {
					if (i == page_num) {
						trace += "<span class=\"current\">" + i + "</span>";
					} else {
						trace += "<a href=\"#\" ref=\"" + i + "\">" + i + "</a>";
					}
				}
				if (page_num < page_count){
					trace += "<a href=\"#\" ref=\"" + (parseInt(page_num)+1) + "\">&gt;</a><a href=\"#\" ref=\"" + page_count + "\">&gt;&gt;</a>";
				} else {
					trace += "<span>&gt;</span><span>&gt;&gt;</span>";
				}
				$('#itempager').html(trace);
				$("#itempager a[@ref]").click(function() {
					window.location.href = 'index.html?p=' + SideMenuIndex + '&num=' +this.ref;
				});
			});
		} else if (pageKey == "guide") {
			str += '<li class="level1" ref="0"><a href="' + MainMenu[5].Url + '" title="'+MainMenu[5].Name+'">全部</a></li>';
			for (var i=0; i<SideMenu.length; i++) {
				str += '<li class="level1" ref="'+SideMenu[i].Index+'"><a href="' + MainMenu[5].Url + '?p=' + SideMenu[i].Index + '" title="'+SideMenu[i].Name+'">'+SideMenu[i].Name+'</a></li>';
			}
			$.getScript(GUIDES_JS_DATA + '&class=' + SideMenuIndex + '&size=' + InfoItemPageSize + '&page=' + InfoItemCurrPage, function() {
				for (var m=0; m<InfoItem.length; m++) {
					str2 += '<li><a href="' + MainMenu[5].Url + '?p=' + InfoItem[m].ClassID + '" class="cate" title="' + InfoItem[m].ClassName + '">[' + InfoItem[m].ClassName + ']</a>';
					if (InfoItem[m].Focused == "1") {
						str2 += '<a href="thread.html?id=' + InfoItem[m].ID + '" class="hot" title="' + InfoItem[m].Subject + '">' + InfoItem[m].Subject + '</a>';
					} else {
						str2 += '<a href="thread.html?id=' + InfoItem[m].ID + '" title="' + InfoItem[m].Subject + '">' + InfoItem[m].Subject + '</a>';
					}
					str2 += '<span>' + InfoItem[m].PubTime + '</span></li>';	
				}
				$('#itemlist').html('<ul>' + str2 + '</ul>').slideDown('slow');
				page_count = (InfoItemCount%InfoItemPageSize>0) ? Math.floor(InfoItemCount/InfoItemPageSize+1) : Math.floor(InfoItemCount/InfoItemPageSize);
				page_num = InfoItemCurrPage;
				trace = "<span>共" + page_count + "页</span><span>" + page_num + "/" + page_count + "</span>";
				if (page_num > 1) {
					trace += "<a href=\"#\" ref=\"1\">&lt;&lt;</a><a href=\"#\" ref=\"" + (parseInt(page_num)-1) + "\">&lt;</a>";
				} else {
					trace += "<span>&lt;&lt;</span><span>&lt;</span>";
				}
				if (page_num <= PAGER_SIDE_MAX) {
					PAGER_START_NUM = 1;
				} else if (page_count >= PAGER_COUNT && page_num > page_count - PAGER_SIDE_MAX) {
					PAGER_START_NUM = page_count - PAGER_COUNT + 1;
				} else {
					PAGER_START_NUM = page_num - PAGER_SIDE_MAX + 1;
				}
				PAGER_END_NUM = (page_count > (PAGER_COUNT+PAGER_START_NUM-1)) ? (PAGER_COUNT+PAGER_START_NUM-1) : page_count;
				for (var i=PAGER_START_NUM; i<=PAGER_END_NUM; i++) {
					if (i == page_num) {
						trace += "<span class=\"current\">" + i + "</span>";
					} else {
						trace += "<a href=\"#\" ref=\"" + i + "\">" + i + "</a>";
					}
				}
				if (page_num < page_count){
					trace += "<a href=\"#\" ref=\"" + (parseInt(page_num)+1) + "\">&gt;</a><a href=\"#\" ref=\"" + page_count + "\">&gt;&gt;</a>";
				} else {
					trace += "<span>&gt;</span><span>&gt;&gt;</span>";
				}
				$('#itempager').html(trace);
				$("#itempager a[@ref]").click(function() {
					window.location.href = 'index.html?p=' + SideMenuIndex + '&num=' +this.ref;
				});
			});
		}
		$('#subnav_title > ul').html(str);
		$('.level1[ref='+SideMenuIndex+']').removeClass('level1').addClass('level2');
	};
	
	var dynamicDetailPage = function() {
		$('#subnav_title').css('background', 'url('+getMainMenuPicByKey()+') no-repeat 10px 5px');
		var str = "";
		var str2 = "";
		$('#subcontent').html('正在加载中...');
		if (pageKey == "news_detail") {
			str += '<li class="level1" ref="0"><a href="' + MainMenu[2].Url + '" title="'+MainMenu[2].Name+'">全部</a></li>';
			for (var i=0; i<SideMenu.length; i++) {
				str += '<li class="level1" ref="'+SideMenu[i].Index+'"><a href="' + MainMenu[2].Url + '?p=' + SideMenu[i].Index + '" title="'+SideMenu[i].Name+'">'+SideMenu[i].Name+'</a></li>';
			}
			$.getScript(INFO_JS_DATA + '?id=' + InfoItemID, function() {
				str2 += '<p style="clear:both;"><span class="Yellow" style="float:left;">『 ' + InfoSubject + ' 』</span>';
				str2 += '<span class="gray" style="float:right;">发布日期：' + InfoPubTime + '</span></p>';
				str2 += '<div style="clear:both;">' + InfoContent + '</div>';
				$('#subcontent').html(str2).slideDown('slow');
				SideMenuIndex = InfoClassID;
				if (SideMenuIndex <= 0) $('#curr_location').html('全部');
				else $('#curr_location').html(getSideMenuNameByID());
				$('#subnav_title > ul').html(str);
				$('.level1[ref='+SideMenuIndex+']').removeClass('level1').addClass('level2');
			});
		} else if (pageKey == "guide_detail") {
			str += '<li class="level1" ref="0"><a href="' + MainMenu[5].Url + '" title="'+MainMenu[5].Name+'">全部</a></li>';
			for (var i=0; i<SideMenu.length; i++) {
				str += '<li class="level1" ref="'+SideMenu[i].Index+'"><a href="' + MainMenu[5].Url + '?p=' + SideMenu[i].Index + '" title="'+SideMenu[i].Name+'">'+SideMenu[i].Name+'</a></li>';
			}
			$.getScript(INFO_JS_DATA + '?id=' + InfoItemID, function() {
				str2 += '<p style="clear:both;"><span class="Yellow" style="float:left;">『 ' + InfoSubject + ' 』</span>';
				str2 += '<span class="gray" style="float:right;">发布日期：' + InfoPubTime + '</span></p>';
				str2 += '<div style="clear:both;">' + InfoContent + '</div>';
				$('#subcontent').html(str2).slideDown('slow');
				SideMenuIndex = InfoClassID;
				if (SideMenuIndex <= 0) $('#curr_location').html('全部');
				else $('#curr_location').html(getSideMenuNameByID());
				$('#subnav_title > ul').html(str);
				$('.level1[ref='+SideMenuIndex+']').removeClass('level1').addClass('level2');
			});
		}
	};
	
	var staticSubPage = function() {
		SideMenuPIndex = getSideMenuPIndexbyIndex();
		$('#subnav_title').css('background', 'url('+getMainMenuPicByKey()+') no-repeat 10px 5px');
		$('#curr_location').html(getSideMenuNameByIndex());
		$('.level1 > ul').hide();
		loadStaticSideMenu();
		loadStaticContent();
		$('#subnav_title a').bind('click', loadStaticPage);
	};
    
    $(document).ready(function() {
		$('#m'+MainMenuIndex).attr('src', '../images/menu/m'+MainMenuIndex+'_o.gif');
		$('#submenu img').bind('mouseover', subMenuOver);
		$('#submenu img').bind('mouseout', subMenuOut);
		if (pageKey == "news") {
			$.getScript(NEWS_CLASSES_JS_DATA, function() { dynamicSubPage(); });
		} else if (pageKey == "guide") {
			$.getScript(GUIDE_CLASSES_JS_DATA, function() { dynamicSubPage(); });
		} else if (pageKey == "news_detail") {
			$.getScript(NEWS_CLASSES_JS_DATA, function() { dynamicDetailPage(); });
		} else if (pageKey == "guide_detail") {
			$.getScript(GUIDE_CLASSES_JS_DATA, function() { dynamicDetailPage(); });
		} else {
			staticSubPage();
		}
    });

    $(window).bind('load', function() {
        correctPNG();
    });
})(jQuery);
