var bookPicURL = "http://www.bookan.com.cn/bookcover/"    //图片预览地址前缀
var wzImgUrl="http://www.bookan.com.cn/lib-templates1/lib-comm-new/wzImg/";
var jpBookNum = 8;   //精品报刊显示数目
var zxBookNum = 18;  //最新报刊显示数目
var tjBookNum = 7;   //读者推荐显示数目
var jdBookNum = 15;  //焦点文章显示数目
var rankBookNum = 10;   //报刊排行显示数目
var sortnum = 4;   //分类中小类的显示数目


jQuery(document).ready(function () {
  loadzxsj();
  loadjqqk();
  loaddztj();
  loaddzb();
		
	});


function loaddzb()
{
  var s=new Array();
  $.ajax({url:"xml/dzb_index.xml",success:
    function(d){
      var temp=new Array();
      $('dzb1 list',d).each(function(i,o){
        if(checkName(temp,$(o).attr('name'))&&s.length<6)
          s.push('<div class="section"><a href="book_intro2.aspx?listid=' + $(o).attr('listid') + '"><IMG height="100" alt="'+$(o).attr('name')+$(o).attr('qs')+'期" src="'+bookPicURL+ $(o).attr('bkdz')+'fs.jpg" width="74" title="'+$(o).attr('name')+$(o).attr('qs')+'期"></a><h6><a href="book_intro.aspx?listid=' + $(o).attr('listid') + '" title="'+$(o).attr('title')+'">'+$(o).attr('title')+'</a></h6><p><a href="javascript:void(0)" onclick="openMBook(\'E'+$(o).attr('bkwzid')+'\',\''+$(o).attr('page')+'\')" title="'+$(o).text()+'">'+$(o).text()+'</a></p></div>');
        temp.push($(o).attr('name'));
      });
      $("#jpbk").html(s.join(''));
      function checkName(arr,name)
      {
        for(var i=0;i<arr.length;i++)
        {
          if(arr[i]==name)
            return false;
        }
        return true;
      }
    },
    error:function(e1,e2,e3){}
	});
}

function loadzxsj()
{
  $.ajax({url:"xml/index1.xml",success:
	function(d){
	    var newbooks=$("statistics",d).attr('newbooks');
	    $('#today').html('今日更新<em>'+newbooks+'</em>');
	    var h=new Array();
	    h.push('<div class="smallBox"><div class="secondBox"><ul>');
		$("zxsj list",d).each(function(idx,o){
			var tbstr='';
			if($(o).attr('id').length>1)
			  tbstr+='<span class="tb"><a href="javascript:void(0)" onclick="openMBook(\''+$(o).attr('id')+'\')">原貌版</a></span>';
			if($(o).attr('bkwzid').length>1)
			  tbstr+='<span class="tb"><a href="javascript:void(0)" onclick="openMBook(\''+$(o).attr('bkwzid')+'\')">文本版</a></span>';
			if(idx<15)
			{
			  h.push('<li><div class="sectionImg"><a href="book_intro.aspx?listid=' + $(o).attr('listid') + '"><IMG height="150" alt="" src="'+ bookPicURL +$(o).attr('imgurl')+'ff.jpg" width="110"></a></div><span>'+$(o).attr('bkmc')+'</span><span>'+$(o).attr('qishu')+'</span>'+tbstr+'</li>');
			}
			if(idx==14)
			{
			  h.push('</ul></div></div><div class="smallBox"><div class="secondBox"><ul>');
			}
			if(idx>=15)
			{
			  h.push('<li><div class="sectionImg"><a href="book_intro.aspx?listid=' + $(o).attr('listid') + '"><IMG height="150" alt="" src="'+ bookPicURL +$(o).attr('imgurl')+'ff.jpg" width="110"></a></div><span>'+$(o).attr('bkmc')+'</span><span>'+$(o).attr('qishu')+'</span>'+tbstr+'</li>');
			}
		});
		h.push('</ul></div></div>');
		//alert(h.join(''));
		$('#zxsj').html(h.join(''));
		jQuery.fn.box = function () {

		function repeat(str, num) {
			return new Array( num + 1 ).join( str );
		}
	  
		return this.each(function () {
			var jQuerysecondBox = jQuery('> div', this).css('overflow', 'hidden'),
				jQueryslider = jQuerysecondBox.find('> ul'),
				jQueryitems = jQueryslider.find('> li'),
				jQuerysingle = jQueryitems.filter(':first'),
				
				singleWidth = jQuerysingle.outerWidth(), 
				visible = Math.ceil(jQuerysecondBox.innerWidth() / singleWidth), // note: doesn't include padding or border
				currentPage = 1,
				pages = Math.ceil(jQueryitems.length / visible);            


			// 1. Pad so that 'visible' number will always be seen, otherwise create empty items
			if ((jQueryitems.length % visible) != 0) {
				jQueryslider.append(repeat('li.empty', visible - (jQueryitems.length % visible)));
				jQueryitems = jQueryslider.find('> li');
			}

			// 2. Top and tail the list with 'visible' number of items, top has the last section, and tail has the first
			jQueryitems.filter(':first').before(jQueryitems.slice(- visible).clone().addClass('cloned'));
			jQueryitems.filter(':last').after(jQueryitems.slice(0, visible).clone().addClass('cloned'));
			jQueryitems = jQueryslider.find('> li'); // reselect
			
			// 3. Set the left position to the first 'real' item
			jQuerysecondBox.scrollLeft(singleWidth * visible);
			
			// 4. paging function
			function gotoPage(page) {
				var dir = page < currentPage ? -1 : 1,
					n = Math.abs(currentPage - page),
					left = singleWidth * dir * visible * n;
				
				jQuerysecondBox.filter(':not(:animated)').animate({
					scrollLeft : '+=' + left
				}, 500, function () {
					if (page == 0) {
						jQuerysecondBox.scrollLeft(singleWidth * visible * pages);
						page = pages;
					} else if (page > pages) {
						jQuerysecondBox.scrollLeft(singleWidth * visible);
						// reset back to start position
						page = 1;
					} 

					currentPage = page;
				});                
				
				return false;
			}
			
			jQuerysecondBox.after('<a class="arrow back"></a><a class="arrow forward"></a>');
			
			// 5. Bind to the forward and back buttons
			jQuery('a.back', this).click(function () {
				return gotoPage(currentPage - 1);                
			});
			
			jQuery('a.forward', this).click(function () {
				return gotoPage(currentPage + 1);
			});
			
			// create a public interface to move to a specific page
			jQuery(this).bind('goto', function (event, page) {
				gotoPage(page);
			});
		});  
	};
	jQuery('.smallBox').box();
	
	var sp=new Array();
	$("speech list",d).each(function(i,o)
	{
	  if(i<jdBookNum)
	    sp.push('<li><img src="/lib-comm-new/image/tb-3.gif" border="0"><span class="jdwzbt"><a href="Reading.aspx?bkid=' + $(o).attr('bkid') + '">'+ $(o).attr('name') +'</a></span><span class="jdwzmc"><a href="Reading.aspx?bkid=' + $(o).attr('bkid') + '">' + $(o).attr('qishu') + '期</a></span></li>');
	});
	$('#jdwz').html(sp.join(''));
	},
	error:function(e1,e2,e3){}
	});
}

function loadjqqk()
{
  $.ajax({url:'xml/jpqk.xml',success:function(d)
  {
    var h=new Array();
    //h.push('<div id="first"><a href="book_intro.aspx?listid='89FF5BBF-75CF-4082-8FA1-F4C6BCB7EFCC'"><IMG height="150" hspace="1" src="'+ bookPicURL +'2-861/2-861-1002ff.jpg" width="110" border="0"></a></div>');

    $("jpqk list",d).each(function(i,o)
    {
      if(i==0)
        h.push('<div id="first"><a href="book_intro.aspx?listid=' + $(o).attr('listid') + '"><IMG height="150" hspace="1" src="'+ bookPicURL +$(o).attr('bkdz')+'ff.jpg" width="110" border="0"></a></div>');
      else
        h.push('<div><a href="book_intro.aspx?listid=' + $(o).attr('listid') + '"><IMG height="150" hspace="1" src="'+ bookPicURL +$(o).attr('bkdz')+'ff.jpg" width="110" border="0"></a></div>');
    });
 $('#marqueedivcontrol').html(h.join(''));
    var marquee2=new Marquee("marqueedivcontrol");
							marquee2.Direction="left";
							marquee2.Step=2;
							marquee2.Width=740;
							marquee2.Height=170;
							marquee2.Timer=20;
						/*	marquee2.ScrollStep=-1;//此句禁止鼠标控制 */
							marquee2.Start();
  },
  error:function(e1,e2,e3){
   
}
	});
}
function loaddztj()
{
  $.ajax({url:'xml/dztj.xml',success:function(d)
  {
    var h1=new Array();
    var h2=new Array();
    var h3=new Array();

    $("dztj row",d).each(function(i,o)
    {
      if(i<5)
      {
        if(i==0)
        {
          h1.push('<li><a href="book_intro.aspx?listid=' + $(o).attr('listid') + '"><IMG height="220" alt="magazine" src="'+ bookPicURL +$(o).attr('bkdz')+'fm.jpg" width="160"></a></li>');
          h2.push('<dl class="current"><dt>' + $(o).attr('bkmc') + '<dd>' + $(o).attr('qishu') + '</dd><dd>人气：' + $(o).attr('hits') + '</dd><dd class="read"><a href="book_intro.aspx?listid=' + $(o).attr('listid') + '">在线阅读</a></dd></dl>');
          h3.push('<li class="current" id="p0"><a href="book_intro.aspx?listid=' + $(o).attr('listid') + '">' + $(o).attr('bkmc') + '</a></li>');
        }
        else
        {
          h1.push('<li><a href="book_intro.aspx?listid=' + $(o).attr('listid') + '"><IMG height="220" alt="magazine" src="'+ bookPicURL +$(o).attr('bkdz')+'fm.jpg" width="160"></a></li>');
          h2.push('<dl class="normal"><dt>' + $(o).attr('bkmc') + '<dd>' + $(o).attr('qishu') + '</dd><dd>人气：' + $(o).attr('hits') + '</dd><dd class="read"><a href="book_intro.aspx?listid=' + $(o).attr('listid') + '">在线阅读</a></dd></dl>');
          h3.push('<li id="p'+i+'"><a href="book_intro.aspx?listid=' + $(o).attr('listid') + '">' + $(o).attr('bkmc') + '</a></li>');
        }
      }
    });
    $('#ifocus_piclist').html(h1.join(''));
    $('#intro').html(h2.join(''));
    $('#ifocus_btn').html(h3.join(''));
     var currentIndex = 0;
     var DEMO; //函数对象
     var currentID = 0; //取得鼠标下方的对象ID
     var pictureID = 0; //索引ID
     jQuery("#ifocus_piclist li").eq(0).show(); //默认
     autoScroll();
     jQuery("#ifocus_btn li").hover(function() {
     StopScrolll();
     jQuery("#ifocus_btn li").removeClass("current")//所有的li去掉当前的样式加上正常的样式
     jQuery(this).addClass("current"); //而本身则加上当前的样式去掉正常的样式
     currentID = jQuery(this).attr("id"); //取当前元素的ID
     pictureID = currentID.substring(currentID.length - 1); //取最后一个字符
     jQuery("#ifocus_piclist li").eq(pictureID).fadeIn("slow"); //本身显示
     jQuery("#ifocus_piclist li").not(jQuery("#ifocus_piclist li")[pictureID]).hide(); //除了自身别的全部隐藏
     jQuery("#intro dl").hide();
     jQuery("#intro dl").eq(pictureID).show();
     }, function() {
          //当鼠标离开对象的时候获得当前的对象的ID以便能在启动自动时与其同步
          currentID = jQuery(this).attr("id"); //取当前元素的ID
          pictureID = currentID.substring(currentID.length - 1); //取最后一个字符
          currentIndex = pictureID;
          autoScroll();
        });
            //自动滚动
     function autoScroll() {
       jQuery("#ifocus_btn li:last").removeClass("current");
       jQuery("#intro dl:last").hide();
       jQuery("#ifocus_btn li").eq(currentIndex).addClass("current");
       jQuery("#ifocus_btn li").eq(currentIndex - 1).removeClass("current");
       jQuery("#intro dl").eq(currentIndex).show();
       jQuery("#intro dl").eq(currentIndex - 1).hide();
       jQuery("#ifocus_piclist li").eq(currentIndex).fadeIn("slow");
       jQuery("#ifocus_piclist li").eq(currentIndex - 1).hide();
       currentIndex++; currentIndex = currentIndex >= 5 ? 0 : currentIndex;
       DEMO = setTimeout(autoScroll, 2000);
     }
     function StopScrolll()//当鼠标移动到对象上面的时候停止自动滚动
     {
       clearTimeout(DEMO);
     }

     var hh=new Array();
     hh.push('<h5><span class="head">阅读排行榜</span></h5><ul class="dztjlist-1">');
     $("ydph row",d).each(function(i,o){
       var imgnum=i+1;
       if(i<9){imgnum="0"+(i+1).toString();}
       hh.push('<li><img src="/lib-comm-new/image/'+ imgnum +'.gif" border="0"><a href="book_intro.aspx?listid=' + $(o).attr('listid') + '">'+  $(o).attr('bkmc') +'</a></li>');
     });
     hh.push('</ul>');
     $("#rankShow").html(hh.join(''));
  },
  error:function(e1,e2,e3){}
	});
}




