nfl.namespace('photos');

/*
 * @author arianna.winters
 * @class nfl.photos.carousel
 * @param {String} | {Object} parent container element of ul element
 * 
 * ported from video gallery carousel.
 */
nfl.photos.carousel = Class.create({
	initialize: function(container,options){
		this.container	= $(container);
		console.log('initializing '+container);
		this.list		= this.container.select('.list-items').first();
		this.options	= options || {};
		this.rows		= (typeof this.options.rows !== 'undefined')? this.options.rows:1;
		
		/* set list width to children total width */
		var tChild		= this.list.select('li').first();
		var itemMargin = 0;
		if(tChild.getStyle('margin') !== null && tChild.getStyle('margin') !== ''){
			//console.log('tChild.getStyle(\'padding\') = '+ tChild.getStyle('padding')+'\n type of tChild.getStyle(\'padding\') = '+(typeof tChild.getStyle('padding')));
			var itemMarginStr = tChild.getStyle('margin').replace('px','');
			console.log('itemMarginStr = '+ itemMarginStr);
			if(itemMarginStr.split(' ').length >= 2){
				itemMargin	= parseInt(itemMarginStr.split(' ')[1]);
			}
			if(itemMarginStr.split(' ').length >= 4){
				itemMargin	= itemMargin + parseInt(itemMarginStr.split(' ')[3]);
			}
		}else{
			if(tChild.getStyle('margin-left') !== null && tChild.getStyle('margin-left') !== ''){
				console.log('adding margin-left');
				itemMargin = itemMargin + parseInt(tChild.getStyle('margin-left').replace('px',''));
			}
			if(tChild.getStyle('margin-right') !== null && tChild.getStyle('margin-right') !== ''){
				console.log('adding margin-right');
				itemMargin = itemMargin + parseInt(tChild.getStyle('margin-right').replace('px',''));
			}
		}
		console.log('tChild.getWidth() = '+ tChild.getWidth() +'\nitemMargin = '+ itemMargin);
		this.itemWidth	= tChild.getWidth()+itemMargin;
		//this.totalWidth	= (this.itemWidth * (this.list.select('li').length));
		console.info('this.rows = '+this.rows)
		this.totalWidth	= (this.itemWidth * (Math.ceil(this.list.select('li').length / this.rows)));
		//this.totalWidth	= ((this.itemWidth * (this.list.select('li').length)) / this.rows);
		console.log('setting carousel list width('+this.itemWidth+' * ('+this.list.select('li').length+')) to '+ this.totalWidth);
		this.list.setStyle({width:(this.totalWidth)+'px'});
		console.log('set list style to '+this.list.getStyle('width'));
		this.scrollMultiplyer = 1;
		this.originalLeftMargin	= (parseInt(this.list.getStyle('margin-left').replace('px','')));
		console.log('initializing '+container+' ending.');
		document.observe('photos:carousel:scroll',this.onScroll.bind(this));
		document.observe('photos:carousel:pagechange',this.onPageChange.bind(this));
		document.fire('photos:carousel:pagechange',{id:this.container.identify()});
	},
	onPageChange: function(event){
		if(this.container.identify() == event.memo.id){
			console.info('nfl.photos.carousel.onPageChange');
			currentPageEle	= this.container.select('.carousel-controls-page').first();
			totalPagesEle	= this.container.select('.carousel-controls-totalpages').first();
			portwidth 		= this.container.getWidth()+ Math.abs(this.originalLeftMargin);
			//console.info('nfl.photos.carousel.onPageChange: '+ this.totalWidth +' / '+ portwidth +' = '+ (Math.floor(this.totalWidth / portwidth)));
			//console.info('nfl.photos.carousel.onPageChange: '+ this.container.identify() +' total pages = '+ (Math.floor(this.totalWidth / portwidth)) +', current page = '+ this.getCurrentPage());
			if(Math.floor(this.totalWidth / portwidth) == 0){
				totalPagesEle.update(Math.ceil(this.totalWidth / portwidth));
			}else{
				totalPagesEle.update(Math.floor(this.totalWidth / portwidth));
			}
			currentPageEle.update(this.getCurrentPage()+'');
		}
	},
	getCurrentPage: function(){
		itemcontainer	= this.list;
		portwidth 		= this.container.getWidth()+ Math.abs(this.originalLeftMargin);
		return Math.ceil(Math.abs(parseInt(itemcontainer.getStyle('margin-left'))) / portwidth);
	},
	onScroll: function(event){
		console.info("nfl.photos.carousel.onScroll: "+this.container.identify()+","+ event.memo.id);
		if(this.container.identify() == event.memo.id){
			var amountToScroll				= 0;
			var amountToScrollMultiplyer	= this.scrollMultiplyer;
			direction 		= event.memo.direction;
			itemcontainer	= this.list;
			portwidth 		= this.container.getWidth()+ Math.abs(this.originalLeftMargin);
			if(event.memo.multiplyer){ amountToScrollMultiplyer	= event.memo.multiplyer;}
			try{ 
				var itemLeft	= parseInt(itemcontainer.getStyle('margin-left'));
			}catch(e){
				itemcontainer.setStyle({'margin-left':'0px'});
				var itemLeft	= parseInt(itemcontainer.getStyle('margin-left'));
			}
			switch(direction){
				case 'left':
					amountToScroll	= (itemLeft == (0+this.originalLeftMargin))?(0 - (portwidth * Math.floor((this.totalWidth - Math.abs(this.originalLeftMargin)) / portwidth))):(amountToScroll + portwidth);
					break;
				case 'right':
					amountToScroll	= ((itemLeft + (0 - portwidth)) <= (0 - parseInt(itemcontainer.getStyle('width'))))?(this.originalLeftMargin - itemLeft):(amountToScroll - portwidth);
					//nfl.log('('+ (itemLeft + (0 - portwidth)) +' <= (0 - '+ parseInt(itemcontainer.getStyle('width')) +') ? '+ ((itemLeft + (0 - portwidth)) <= (0 - parseInt(itemcontainer.getStyle('width')))));
					break;
				default:
					break;
			}
			amountToScroll	= (amountToScroll * amountToScrollMultiplyer);
			console.log("nfl.photos.carousel.onScroll: amountToScroll = "+ amountToScroll +"");
			//now perform scroll operation based on value outputs from switch statement above
			if(itemLeft % portwidth == (0+this.originalLeftMargin)){
				newLeft	= (itemLeft + amountToScroll);
				console.log("{nfl.photos.carousel.onScroll: newLeft = "+ newLeft +"}");
				itemcontainer.morph('margin-left: ' + newLeft + 'px;',{afterFinish: function(){document.fire('photos:carousel:pagechange',{id:this.container.identify()});}.bind(this), duration: .8, fps: 30});
				console.info("nfl.photos.carousel.onScroll: morph started");
			}else{
				console.warn('nfl.photos.carousel.onScroll: item is in play.');
				//nfl.log("{nfl.videos.carousel.onScroll: item is in play: "+ itemLeft +" % "+ portwidth +" == "+ (itemLeft % portwidth == (0+this.originalLeftMargin)) +"}");
			}
		}
	}
});