﻿/*
功能:频道详细页元素展示
*/

ChannelExtend = new Class({
	
		initialize:function(option){
			var self = this;
			this.options = option
			this.getTotal = this.views = this.currentTotal = this.ajaxPostPosition = 0;
			//合并完成调用函数
			var costomComplete = this.options.oncomplete;
			this.options.oncomplete = [];
			this.options.oncomplete.push(this.completeProcess);
			costomComplete ? this.options.oncomplete.push(costomComplete) : undefined;
			
			//合并点击调用函数
			var costomOnclick = this.options.onclick;
			this.options.onclick = [];
			//this.options.onclick.push(this.clickProcess);
			costomOnclick ? this.options.onclick.push(costomOnclick) : undefined;
			
			this.postAjaxReady(this.ajaxBackCall, this.options.oncomplete);
			this.bindEvent();
		},
		
		//初始化完成后需要执行的参数
		completeProcess :function(self){		
			if(self.elTotal > 0){
				self.options.frontel.addClass('end');
				self.options.frontel.setStyle('display','block');
				self.options.backel.setStyle('display','block');				
				//self.options.target.getFirst().getElement('img').fireEvent('click');
			}
		},
		
		//鼠标点击时调用
		clickProcess : function(data){
			var albumLink = new Element('a',{
				'href':data.detialUrl,
				'class':'E_album',
				'html':data.type + '描述'
			});
			
			var E_like = new Element('a',{
				'href':data.detialUrl,
				'class':'E_like',
				'html':data.linkViews + '人喜欢'
			});

			var E_comment = new Element('a',{
				'href':data.detialUrl,
				'class':'E_comment',
				'html':data.commentViews + '条评论'
			});
			$('extendLink').empty().adopt(albumLink, E_like, E_comment);		
		},
		
		//初始化设定包裹元素宽度
		setWidth : function(count){
			this.totalWidth = this.options.elSize * count;
			this.options.target.setStyle('width',this.totalWidth);
		},
		
		//插入返回元素
		injectElement:function(){
			var self = this;
			self.elArray.each(function(item){
				
				var newImg = new Element('img',{
					'alt':item.pname,
					'src': item.pSmaillImage
				});
				
				var newlink = new Element('a',{
					'title':item.pname,
					'href':'javascript:void(0);',
					'events':{
						'click':function(){
							self.clickEvent(newImg,item);
						}
					}
				});
				
				/*
				var newspan = new Element('span',{
				});
				
				var newstrong = new Element('strong',{
					'html':item.pname
				})
				*/
				self.options.target.adopt(newlink.adopt(newImg));
			});
		},
		
		//用户点击图片设定
		clickEvent: function(img, data){
			this.options.target.getElements('img').each(function(item){
				item.removeClass('current');
			});
			img.addClass('current');
			
			for(var i = 0; i < this.options.onclick.length; i++){
				this.options.onclick[i](data);
			};

		},
		
		//ajax调用准备		
		postAjaxReady : function(backCall, costomBackCall){
			var data =  {
				'channel.id':this.options.channelId,
				'currentPage':this.views
				};
			this.postAjax(this.options.url, data, backCall, costomBackCall);
		},
		
		//ajax调用
		postAjax : function(url, data, backCall, costomBackCall){
			var self = this;
			new Request({
				url:url,
				method:'post',
				data:data,
				link:'cancel',
				onSuccess:function(responseText){
					backCall(self, responseText, costomBackCall);
				}
			}).send();			
		},
		
		//ajax回调
		ajaxBackCall:function(self, responseText, costomBackCall){
			var jsonData = JSON.decode(responseText);
			//self.elTotal = jsonData.total.toInt();
			//$('extendTitle').set('html','频道资源<span>('+ self.elTotal +')</span>');		
			self.elArray = jsonData;
			self.setWidth(self.elArray.length);
			self.injectElement();
			//self.computerAjax(jsonData.el);
			//self.initPosition(self)
			var defaultData = self.elArray.length > 0 ? self.elArray[0] : undefined;
			if(costomBackCall){
				for(var i = 0; i < costomBackCall.length; i++){
					costomBackCall[i](self,defaultData);
				};
			};
		},
		
		//将移动距离转换成移动到哪里
		moveScoll : function(el,moveSize){
			var currentScollX = el.getScroll().x;
			var moveTo = 0;
			if(moveSize > 0){
				moveTo = currentScollX + moveSize < this.totalWidth - this.options.windowSize ? currentScollX + moveSize + moveSize / this.options.windowSize * this.options.offset : this.totalWidth - this.options.windowSize - this.options.offset;
				this.fx(el, moveTo, this.options.fps);	
			}else{
				moveTo = currentScollX + moveSize - this.options.offset > 0 ? currentScollX + moveSize - this.options.offset : 0;
				this.fx(el, moveTo, this.options.fps - this.options.fps * 2);	
			};
			//this.ajaxPostPosition != 0 ? moveTo == this.ajaxPostPosition ? this.postAjaxReady(this.ajaxBackCall) : '' : '';
			this.flipPageCss(moveTo);
			//this.replaceToImage(moveTo / this.options.elSize);
		},
		
		//动态加载图片
		replaceToImage : function(elIndex){
			var self = this;
			var endIndex = Math.ceil(elIndex) + Math.ceil(this.options.windowSize / this.options.elSize) > self.elArray.length ? self.elArray.length : Math.ceil(elIndex) + Math.ceil(this.options.windowSize / this.options.elSize);
			var tmpElArray = [];
			for(var i = Math.ceil(elIndex); i < endIndex; i++){
				tmpElArray.push(self.elArray[i]);
			};
			tmpElArray.each(function(item){
				if(item.newdiv.getFirst().get('tag') == 'div'){
					new Element('img',{
										'src':item.imgsrc,
										'events':{
											'click':function(){
												self.clickEvent(this, item);
											}
										}
					}).replaces(item.newdiv.getFirst());
				}			
			});
		},
		
		//初始化定位
		initPosition : function(self){
			var moveToIndex = 0
			var moveToEl;
			self.completeIndex = undefined;
			if(self.options.targetId){
				for(var i = 0; i < self.elArray.length; i++){
					if(self.elArray[i].pname == self.options.targetId){
						 moveToIndex = i
						 moveToEl = self.elArray[i].newdiv;
						 break;
					}
				}
			}
			
			var moveWidth = moveToIndex * self.options.elSize;
			
			moveWidth = moveWidth < self.options.windowSize ? 0 : self.options.windowSize * Math.floor(moveWidth / self.options.windowSize);
			moveToEl = moveToEl == undefined ? self.elArray.length > 0 ? self.elArray[0].newdiv : undefined : moveToEl;
			if(moveToEl != undefined){
				self.debindEvent();
				self.completeIndex = moveToIndex;
				self.moveScoll(self.options.target.getParent(),moveWidth);
				self.clickEvent(moveToEl.getFirst(), self.elArray[moveToIndex]);
			}
		},
		
		//翻页器效果
		flipPageCss : function(moveTo){
			this.options.frontel.removeClass('end');
			this.options.backel.removeClass('end');
			if(moveTo == 0){
				this.options.frontel.addClass('end');				
			}else if(moveTo == this.totalWidth - this.options.windowSize - this.options.offset){
				this.options.backel.addClass('end');
			};
		},
		
		//执行移动到哪里
		fx:function(el, endValue, fps){
			var self = this;		
			var currentScollX = el.getScroll().x;
			var fp = fps > 0 ? currentScollX + fps > endValue ? endValue : currentScollX + fps : currentScollX + fps < endValue ? endValue : currentScollX + fps;
			el.scrollTo(fp);
			if(fp != endValue){
				setTimeout(function(){
					self.fx(el, endValue, fps);
				},self.options.duration / (endValue == 0 ? 1 : endValue) / self.options.fps);
			}else{
				this.bindEvent();
			};
		},
		
		//计算ajax请求时机
		computerAjax : function(jsonData){
			//获得频道元素总个数
			this.getTotal = this.getTotal + jsonData.length;
			//累计获得页数
			this.views++;
			//计算获得累计元素的个数			
			this.currentTotal = this.currentTotal + jsonData.length;
			//console.log('this.elTotal: '+this.elTotal +'  currentTotal: ' + this.currentTotal);
			//下次请求Ajax的位置
			this.ajaxPostPosition = this.currentTotal < this.elTotal ?  this.ajaxPostPosition + this.options.elSize * this.options.ajaxLength - this.options.windowSize -  this.options.offset : 0;
			//console.log('nextAjax: ' + this.ajaxPostPosition);
		},
		
		//事件绑定
		bindEvent : function(){
			var self = this;
			this.options.frontel.addEvent('click',function(){
				self.debindEvent();
				self.moveScoll(self.options.target.getParent(), self.options.windowSize - self.options.windowSize * 2 );
			});
			
			this.options.backel.addEvent('click',function(){
				self.debindEvent();
				self.moveScoll(self.options.target.getParent(), self.options.windowSize);
			});
		},
		
		//解除事件
		debindEvent : function(){
			this.options.frontel.removeEvents();
			this.options.backel.removeEvents();
		}	
		
	});