var bb = bb || {};

function BlockMove(event) {
	// Tell Safari not to move the window.
	event.preventDefault();
}

var isTouch = false;
if((typeof Touch == "object") && (typeof WebKitTransitionEvent == "object")) {
	isTouch = true;
}

bb.Scroller = Backbone.View.extend({
    initialize: function(options){
		if(isTouch) {
			var ins = this;
			window.addEventListener('load', function(){ins.loaded();}, false);
		}
    },
	loaded: function() {
		var ins = this;
		setTimeout(function () {
			ins.myScroll = new iScroll(ins.id);
		//console.log(ins);
		}, 100);
	},
	refresh: function() {
		if(isTouch) {
			var ins = this;
			setTimeout(function () {
				ins.myScroll.refresh();
			}, 100);
		}
	},
	toTop: function () {
	//console.log('go to top of tray');
		if(isTouch) {
		//console.log('myScroll = ',this.myScroll);
			this.myScroll.scrollTo(0, 0, 0, false);
		} else {
			$('#'+this.id).scrollTop(0);
		}
	}
});
//Setup main content scroll object
bb.mainScroll = new bb.Scroller({'id':'content-scroll'});

bb.TrayView = Backbone.View.extend({
    initialize: function(options){
		this.isOpen = false;
		this.setScroll();
		this.insertTray();
		if(options.browse) this.addBrowse();
		if(options.content) this.inject(options.content);
    },
	addEndEvent: function() {
		var transEndEventNames = {
		    'WebkitTransition' : 'webkitTransitionEnd',
		    'MozTransition'    : 'transitionend',
		    'OTransition'      : 'oTransitionEnd',
		    'transition'       : 'transitionEnd'
		};
		
		var transEndEventName = transEndEventNames[ Modernizr.prefixed('transition') ];
		var ins = this;
		this.$tray[0].addEventListener(transEndEventName, function() {ins.transitionEnd();}, true);	
	},
	transitionEnd: function() {
		if(this.isOpen) {
		//console.log(Modernizr.prefixed('transition'));
			this.$tray.removeClass('transition');
			this.$tray[0].style[Modernizr.prefixed('transform')] = "none";
			this.$tray[0].style.top = "0";
		} else {
			this.$tray.removeClass('transition');
			this.$tray[0].style[Modernizr.prefixed('transform')] = "none";
			this.$tray[0].style.top = "100%";
		}
	},
	addBrowse: function() {
		this.$tray.addClass('browse');
		$('#browse').append('<div class="nav"><a id="browse_prev" href="#">Previous</a><a id="browse_next" href="#">Next</a></div>');
	},
	inject: function(content) {
		this.$trayContent.append(content);
	},
	insertTray: function() {
		this.$tray = $('<div id="'+this.id+'" class="transition tray"><div id="browse"><div class="close"><a class="browse_close" href="#">Close</a></div></div><div id="'+this.id+'-scroll" class="tray-content-scroll"><div class="tray-content"></div></div></div>');
		$('#container').append(this.$tray);
		this.$trayContent = this.$tray.find('.tray-content');
		if(Modernizr.csstransitions) {
			this.addEndEvent();
		}
	},
	setScroll: function() {
		this.scroll = new bb.Scroller({'id':this.id+'-scroll'});
	},
	open: function() {
		this.isOpen = true;
		if(Modernizr.csstransitions) {
		//console.log(Modernizr.prefixed('transform'));
			this.$tray.addClass('transition');
			this.$tray[0].style[Modernizr.prefixed('transform')] = "translate(0,-100%)";
		} else {
			this.$tray.animate({'top':'0px'},1000); 
		}
	},
	close: function() {
		this.isOpen = false;
		if(Modernizr.csstransitions) {
			this.$tray.addClass('transition');
			this.$tray[0].style[Modernizr.prefixed('transform')] = "translate(0,100%)";
		} else {
			this.$tray.animate({'top':'100%'},1000);
		}
	}
});

bb.Looper = Backbone.View.extend({
    tagName: "div",
	events: {
		'click .next' : 'next',
		'click .prev' : 'prev'
	},
    initialize: function() {
		this.moving = false;
		this.el = $('#'+this.id);
		this.delegateEvents();
		// this.width = $('#'+this.id+' ul').width();
    },
	next: function() {
		if(this.moving) return false;
		var ins = this;
		this.width = $('#'+this.id+' ul').width();
		var total = $('#'+this.id+' ul li').length;
		var current = $('#'+this.id+' .current');
		var number = parseInt(current[0].id.split("-")[1]);
		
		if(number == total - 1) {number = 0;} else {number = number + 1;}
		var curEl = $('#'+this.id+' .current');
		var nextEl = $('#'+this.id+' #'+this.id+'-'+number);
		
		this.moving = true;
		nextEl.css({'left':this.width});
		curEl.animate({left:'-'+this.width+'px'},500);
		nextEl.animate({left:'0px'},500,function () {
			ins.finised(nextEl,curEl);
		});
	},
	prev: function() {
		if(this.moving) return false;
		var ins = this;
		this.width = $('#'+this.id+' ul').width();
		var total = $('#'+this.id+' ul li').length;
		var current = $('#'+this.id+' .current');
		var number = parseInt(current[0].id.split("-")[1]);
		if(number == 0) {number = total - 1;} else {number = number - 1;}
		
		var curEl = $('#'+this.id+' .current');
		var nextEl = $('#'+this.id+' #'+this.id+'-'+number);
		
		this.moving = true;
		nextEl.css({'left':'-'+this.width+'px'});
		curEl.animate({left:this.width},500);
		nextEl.animate({left:'0px'},500,function () {
			ins.finised(nextEl,curEl);
		});
	},
	finised: function(nextEl,curEl) {
		this.moving = false;
		curEl.removeClass('current');
		nextEl.addClass('current');
		curEl.attr('style','');
		nextEl.attr('style','');
	}
});

bb.SEOlinks = (function() {
	return {
		init: function() {
			this.addListeners();
			console.log('init');
		},
		addListeners: function() {
			$('.seo-link').live('click',function(e) {
				var url = e.currentTarget.href.replace(currentURI, "");
				window.location.href = '#'+url;
				return false;
			});
		}
	}
})();



