/**
专题脚本
*/
/*kEvent.addEvent(window, "load", 
				function(){
					if(Dom.$id('nav_box')){
						kStyle.fixPngBg("nav_box");
					}
				});*/
kEvent.addEvent(window, "load", function(){kPage.initFixColsHeight({fixElm:'md_bd'});});

function imagePageViewInPage(elmId, opt){
	addWinLoadFuns(
		function(){
			new imagePageView_build_in_page(elmId, opt);
		}
	);
}
function imagePageView_build_in_page(elmId, opt){
	var me=this;
	this.elmId=elmId;
	this.elmRoot=Dom.$id(elmId);
	if(!this.elmRoot){return false;}
	if(opt){//如果带了配置，则读取
	}
	var elmListW=Dom.$tagHasClassInNodeChild('div', 'image_list_box', this.elmRoot)[0];//动画显示的范围
	this.lists=Dom.$tagInNode('li', elmListW);//所有图片框的集合
	this.listsLen=this.lists.length;//图片总数
	var list_0=this.lists[0];//第一张图片
	var list_0_style=kStyle.getRealStyle(list_0);
	this.elmList=list_0.parentNode;//存放图片的ul, 滚动动画就是靠控制它的位置来实现的(滚动的整体)
	this.listSize=[
		this.lists[1].offsetLeft-this.lists[0].offsetLeft,
		list_0.offsetHeight+parseInt(list_0_style.marginTop)+parseInt(list_0_style.marginBottom)
	];//一个图片框的尺寸[width, height]
	if(this.elmList.offsetHeight<(2 * this.lists[0].offsetHeight)){//如果没有2 行，则返回
		//setTimeout(function(){me=null;},100); //此功能未测试,不知道是否能清空内存数据
		return false;
	}
	elmListW.style.position='relative';
	elmListW.style.width=elmListW.offsetWidth+'px';
	elmListW.style.overflow='hidden';//限制动画显示区域
	/*
	此处之前的代码与imageScroll_build 类似
	*/
	
	this.positionMax=this.listsLen*this.listSize[0];
	var numberPerPage=Math.floor((elmListW.offsetWidth + parseInt(list_0_style.marginRight))/this.listSize[0]);
	this.pages=Math.ceil(this.listsLen/numberPerPage);
	this.elmList.style.width=this.listsLen*this.listSize[0]+100+'px';
	var i;
	this.positions=[];
	for(i=0; i<this.pages; i++){
		this.positions[i]=0-this.lists[numberPerPage*i].offsetLeft;
		//console.log('position['+i+'] is: '+this.positions[i]);
	}
	this.elmList.style.position='relative';
	this.current=0;
	this.roundView=false;//可以循环浏览
	this.duration=1200;//每次翻页时间(ms)
	this.scrollTimer;
	this.nav={};//按钮，状态栏等
	/**
	更新按钮等
	*/
	var updateNav=function() {
		/*
		var i, link;
		for(i=0; (link=me.nav.link[i]); i++){
			link.className='';
		}
		me.nav.link[me.current].className='current';
		*/
		if(!me.roundView){
			if(me.current==0){
				Dom.addClass('p_off', me.nav.previous);
			}else{
				Dom.removeClass('p_off', me.nav.previous);
			}
			if(me.current==me.pages-1){
				Dom.addClass('n_off', me.nav.next);
			}else{
				Dom.removeClass('n_off', me.nav.next);
			}
		}
	};
	/**
	添加翻页按钮
	*/
	var addNavButtons=function() {
		var p=document.createElement('a'), n=p.cloneNode(true);
		n.href=p.href="javascript:;"//"#"+me.elmId;
		p.innerHTML="&lt;&lt;"
		n.innerHTML="&gt;&gt;"
		p.className="previous";
		n.className="next";
		// insert nodes after <div class="image_list_box">
		//console.log(me);
		me.elmRoot.appendChild(p);
		me.elmRoot.appendChild(n);
		p.style.top=n.style.top=(list_0.offsetHeight-p.offsetHeight)/2+6+'px';
		var nFixValue=Base.isIE()?-16:2;
		p.style.left=(0-p.offsetWidth/2+nFixValue)+'px';//这里的nFixValue 起修正作用，往右边移nFixValue px
		nFixValue=Base.isIE()?20:2;
		n.style.left=(me.elmRoot.offsetWidth-n.offsetWidth/2-nFixValue)+'px';//这里的nFixValue 起修正作用，往左边移nFixValue px
		n.style.right='auto';
		// add handlers
		p.onmousedown=me.scrollPrev_click;
		n.onmousedown=me.scrollNext_click;
		me.nav.previous=p
		me.nav.next=n;
		//n.relIPV=p.relIPV=this;
	};
	
	/**
	添加翻页状态条
	*/
	var addNavStatus=function(){
		function findElmInParent(elm, opt){
			elm=elm.parentNode;
			//console.log(elm);
			if((!elm) || (!opt) || ((!opt.tag) && (!opt.className) && (!opt.id))){return;}
			if(opt.tag && (elm.nodeName.toLowerCase()!=opt.tag.toLowerCase())){
				return findElmInParent(elm, opt);
			}
			if(opt.className && (!(Dom.hasClass(opt.className, elm)))){
				return findElmInParent(elm, opt);
			}
			if(opt.id && (!(elm.id && elm.id==opt.id))){
				return findElmInParent(elm, opt);
			}
			return elm;
		}
		
		var mm=me.elmRoot.parentNode;
		var na=document.createElement('div');
		na.className='image_page_view_nav';
		na.style.visibility='hidden';
		mm.insertBefore(na, me.elmRoot);
		me.nav.link=[];
		for(var i=0; i<me.pages; i++){
			var l=document.createElement('a');
			l.href="javascript:;"//+me.elmId;
			if(i==me.current){
				l.className='current';
			}
			l.setAttribute('i', i);
			l.onclick=function(ev){
				this.blur();
				var v1, v2;
				v1=v2=0;
				var ito=this.getAttribute('i');
				v1=me.positions[me.current];
				me.current=ito;
				v2=me.positions[ito];
				if(v1!=v2){
					clearInterval(me.scrollTimer);
					me.scrollTimer=null;
					movePic(me.scrollTimer, me.elmList, v1, v2, me.duration, updateNav);
				}
				return false;
			};
			na.appendChild(l);
			me.nav.link.push(l);
		}
		na.style.left=(me.elmRoot.offsetWidth-na.offsetWidth)/2+'px';
		na.style.visibility='visible';
	};
	
	this.init=function(){
		addNavButtons();
		//addNavStatus();
		updateNav();
	};
	this.scrollPrev_click=function(){
		this.blur();
		if(this.className.indexOf('off')!=-1){
			return false;
		}
		var v1, v2;
		v1=v2=0;
		if(me.current>0){
			v1=me.positions[me.current];
			me.current--;
			v2=me.positions[me.current];
		}else if(me.current==0){
			if(me.roundView){
				v1=me.positions[me.current];
				me.current=me.pages-1;
				v2=me.positions[me.current];
			}
		}
		if(v1!=v2){
			clearInterval(me.scrollTimer);
			me.scrollTimer=null;
			movePic(me.scrollTimer, me.elmList, v1, v2, me.duration, updateNav);
		}
		return false;
	};
	this.scrollNext_click=function(){
		this.blur();
		if(this.className.indexOf('off')!=-1){
			return false;
		}
		var v1, v2;
		v1=v2=0;
		if(me.current<me.pages-1){
			v1=me.positions[me.current];
			me.current++;
			v2=me.positions[me.current];
		}else if(me.current==(me.pages-1)){
			if(me.roundView){
				v1=me.positions[me.current];
				me.current=0;
				v2=me.positions[me.current];
			}
		}
		if(v1!=v2){
			clearInterval(me.scrollTimer);
			me.scrollTimer=null;
			movePic(me.scrollTimer, me.elmList, v1, v2, me.duration, updateNav);
		}
		return false;
	};
	
	this.init();
}
