var News = new Class({
	scroll: null,
	curNewsIdx: 0,
	elemItems: {},
	elemGallery: {},
	elemGalleryOuter: {},
	speed: 500,
	treshold: 50,
	moveConstant: 3.2,
	smoothScroll: null,
	timeSinceSwipe: 1,
	secsBetweenAutoswipe: 7,
	
	initialize: function() {
		this.elemGalleryOuter = $('newsGalleryOuter');
		this.elemGallery = $('newsGallery');
		this.elemItems = this.elemGallery.getElements('.newsItem');
		
		// only add scroller when more than one item
		if (this.elemItems.length < 2) return;
		
		// create current news indicator
		this.createIndicator();
		
		// calculate width of gallery
		this.elemGallery.setStyle('width', (this.elemItems.length)*100 + '%');

		this.elemItems.setStyle('width', (100/this.elemItems.length)+'%');
		
		this.setupSwipe();
		
		this.smoothScroll = new Fx.Scroll(window);
		
		// update "time since swipe"
		this.autoswipe.periodical(1000, this);
	},
	
	newsClick: function(ev,elem) {
		window.location.hash = elem.getElement('a').get('href');
	},
	
	setupSwipe: function()
	{
		// swipe
		var swipeOptions=
		{
			triggerOnTouchEnd : true,
			swipeStatus: this.swipeStatus.bind(this),
			allowPageScroll: "vertical",
			threshold: 150,
			click: this.newsClick.bind(this)
		}
		var imgs = jQuery("#newsGallery");
		imgs.swipe( swipeOptions );
		
		var f = (function() {
			this.moveConstant = $('newsGalleryOuter').getWidth()/100.0;
		}.bind(this));
		
		window.addEvent('orientationchange', function(e) {
			for(var i=0; i<10; i++) { f.delay(i*100, this); }
		}.bind(this));
		
		f();
	},
	
	autoswipe: function() {
		this.timeSinceSwipe++;
		
		if (this.timeSinceSwipe%this.secsBetweenAutoswipe == 0) {	
			if (this.elemItems.length-2 >= this.curNewsIdx) this.nextImage();
			else {
				this.curNewsIdx = 0;
				this.updateIndicator();
				this.scrollImages(0, this.speed);
			}
		}
	},
	
	swipeStatus: function (event, phase, direction, distance)
	{
		this.timeSinceSwipe = 0;
		
		//I f we are moving before swipe, and we are going L or R in X mode, or U or D in Y mode then drag.
		if( phase=="move" )
		{
			var duration=0;

			if (direction == "left")
				this.scrollImages((this.treshold*2 * this.curNewsIdx) + distance/this.moveConstant, duration);

			else if (direction == "right")
				this.scrollImages((this.treshold*2 * this.curNewsIdx) - distance/this.moveConstant, duration);
			else if (direction == "up" && !APPLE) {
				phase='cancel';
				this.smoothScroll.toElement($$('#newsOuter div.article')[0]);
			}
			else if (direction == "down" && !APPLE) {
				phase='cancel';
				this.smoothScroll.start(0,1);
			}
		}

		else if ( phase == "cancel")
		{
			this.scrollImages(this.treshold*2 * this.curNewsIdx, this.speed);
		}

		else if ( phase =="end" )
		{
			if (direction == "right")
				this.previousImage()
			else if (direction == "left")			
				this.nextImage()
		}
	},
	
	previousImage: function()
	{
		this.curNewsIdx = Math.max(this.curNewsIdx-1, 0);
		this.scrollImages( this.treshold*2 * this.curNewsIdx, this.speed);
		this.updateIndicator();
	},

	nextImage: function()
	{
		this.curNewsIdx = Math.min(this.curNewsIdx+1, this.elemItems.length-1);
		this.scrollImages( this.treshold*2 * this.curNewsIdx, this.speed);
		this.updateIndicator();
	},

	scrollImages: function (distance, duration)
	{
		jQuery('#newsGallery').css("-webkit-transition-duration", (duration/1000).toFixed(1) + "s");

		// inverse the number we set in the css
		var value = (distance<0 ? "" : "-") + Math.abs(distance).toString();

		jQuery('#newsGallery').css("-webkit-transform", "translate3d("+value/this.elemItems.length+"%,0px,0px)");
	},
	
	createIndicator: function() {
		// elements to create
		var max = this.elemItems.length;
		for(var i=0; i<max; i++) {
			$('currentNews').grab(new Element('div', {'class': 'indicator'+(i==0?' active':'')}));
		}
	},
	
	updateIndicator: function() {
		$$('#currentNews div.indicator').each(function(elem,idx) {
			if(idx==this.curNewsIdx) elem.addClass('active');
			else elem.removeClass('active');
		}.bind(this));
	}
});

var news = null;
window.addEvent('load', function() {
	news = new News();
});

