var masthead = function(d){
	this.totalWidth = 0;
	this.increment = 0;
	this.timer = null;
	this.delay = d;
	this.current = 0;
	this.numMastheads = 0;
}

masthead.prototype.initialize = function(){
	var w = this.getTotalItemsWidth();
	$('#rotator').width(w);
	this.totalWidth = w;
	this.increment = this.getIncrement();
	this.setupNavData();
	this.setupEvents(this);
	//this.timer = setInterval(this.rotate, this.delay, this);
	//this.startRotator();
}

masthead.prototype.getIncrement = function(){
	return $('#rotator .item').eq(0).width();
}
masthead.prototype.getTotalItemsWidth = function(){
	var w = 0;
	$('#rotator .item').each(function(){
		w += $(this).width();
	});
	return w;
}
masthead.prototype.rotate = function(self){
	self.current++;
	var r = self.current*self.increment;
	if (r == self.totalWidth){
		self.current = 0;
		$('#rotator').stop().animate({'left': [0, 'swing']}, 300);
		$('#navigation .nav a').removeClass('active');
		$('#navigation .nav a').eq(0).addClass('active');
	}
	else{
		$('#rotator').stop().animate({'left': [-r, 'swing']}, 500);
		$('#navigation .nav a').removeClass('active');
		$('#navigation .nav a').eq(self.current).addClass('active');
	}
}
masthead.prototype.setupEvents = function(self){
	$('#navigation .nav a').hover(function(){
		//over
		//clearInterval(self.timer); 
		self.moveTo($(this).data('position'));
		},
		function(){
		//out
		//self.startRotator(); 	
		}
	);
	
	/*$('#masthead').hover(
		function(){
			//clearInterval(self.timer);
		}, 
		function(){
			//self.startRotator();
		}
	);
	*/
	$('#leftSlider').click(function(){
		if (self.current - 1 >= 0){
			self.current--;
		}
		else{
			self.current = self.numMastheads;
		}
		self.moveTo(self.current);
		return false;
	});
	
	$('#rightSlider').click(function(){
		if (self.current + 1 <= self.numMastheads){
			self.current++;
		}
		else{
			self.current = 0;
		}
		self.moveTo(self.current);
		return false;
	});
	
	$('#boxes .box a.masted').click(function(){
		//clearInterval(self.timer);
		self.moveTo($('#'+ $(this).attr('rel')).data('position'));
		return false;
	});
	
	/*$('#boxes .box').each(function(){
		$element = $(this);
		var w = $element.width();
		var h = $element.height();
		var $img = $element.find('img').eq(0);
		var src = $img.attr('src');
		$element.find('a img').hover(
		function(){
			if (!$img.hasClass('animating')){
				$img.stop().flip(w,h, src);
			}	
		},
		function(){
			
		}
		);
    });
    */
}
masthead.prototype.initRotate = function(){
	$('#boxes .box').each(function(i){
		$element = $(this);
		var w = $element.width();
		var h = $element.height();
		var $img = $element.find('img').eq(0);
		var src = $img.attr('src');
		setTimeout(function(){$img.flip(w,h,src)}, i*200);
    });
	
}
masthead.prototype.moveTo = function(p){
	if (p*this.increment < this.totalWidth){
		this.current = p;
		$('#rotator').stop().animate({'left': [-p*this.increment, 'swing']}, 150);
		$('#navigation .nav a').removeClass('active');
		$('#navigation .nav a').eq(p).addClass('active');
	}
}
masthead.prototype.startRotator = function(){
	var self = this;
	self.timer = setInterval(function(){
		self.rotate(self)
	}, self.delay);
}
masthead.prototype.setupNavData = function(){
	
	$('#navigation .nav a').each(function(i){
		if(i == 0) $(this).addClass('active');
		$(this).data({'position': i});
	});
	var m = 0;
	$('#rotator .item').each(function(i){
		$(this).data({'position': i});
		m = i;
	});
	this.numMastheads = m;
}

var bubbles = function(max, bc){
	this.numBubbles = 0;
	this.winX = 0;
	this.winY = 0;
	this.bubbleClass = bc;
	this.maxBubbles = max;
	this.images = new Array('/images/bubble-fizz.png', '/images/bubble-round.png');
}
bubbles.prototype.initialize = function(){
	var self = this;
	this.getWindowSize();
	this.bindEvents();
	this.factory(250);
}
bubbles.prototype.factory = function(timeout){
	var self = this;
	setInterval(function(){
		if(self.numBubbles < self.maxBubbles)
			self.createBubble(self.getRandom(20, 100), self.getRandom(0,1));
	}, timeout);	
}

bubbles.prototype.bindEvents = function(){
	var self = this;
	$(window).resize(function(){self.getWindowSize();});
}

bubbles.prototype.getWindowSize = function(){
	this.winY = $(document).height();
	this.winX = $(document).width();
}

bubbles.prototype.getRandom = function(low, high){
	var adjustedHigh = (parseFloat(high) - parseFloat(low)) + 1;
	return Math.floor(Math.random()*adjustedHigh) + parseFloat(low);
}

bubbles.prototype.createBubble = function(size, image){
	var self = this;
	var top = self.winY - size - 10; 
	var newBubble = jQuery('<img />', {
		'class': this.bubbleClass,
		'src': this.images[image],
		'width': size,
		'height': size,
		'style': 'width: '+ size + 'px; height: ' + size + 'px; position: absolute; top: ' + top + 'px; left: ' +  self.getRandom(0, self.winX-size) + 'px; z-index: ' + self.numBubbles 
	});
	this.numBubbles++;
	$('body').append(newBubble);
	newBubble.stop().animate({'top': [0-size, 'linear']}, 5000, function(){newBubble.remove(); self.numBubbles--;});
}

jQuery.fn.flip = function(width, height, originalSrc){	 
	return this.each(function($element) {
		$element = $(this);
		$element.addClass('animating');
		$element.animate({'height': [height,'linear'], 'width': [1, 'linear'], 'marginLeft': [width/2, 'linear']}, 150, function(){$element.attr('src', 'images/touts/blank.jpg');})
		.animate({'height': [height, 'linear'], 'width': [width, 'linear'], 'marginLeft': [0, 'linear']},150)
		.animate({'height': [height, 'linear'], 'width': [1, 'linear'], 'marginLeft': [width/2, 'linear']},150, function(){$element.attr('src', originalSrc);})
		.animate({'height': [height,'linear'],'width': [width, 'linear'], 'marginLeft': [0, 'linear']},150, function(){$element.attr('src', originalSrc).removeClass('animating');})
	});
}

var scroller = function(){}
scroller.prototype.initialize = function(viewable, space){
	$('.scroller').each(function(i){
		var $scroller = $(this);
		var numItems = $scroller.find('.item').length;
		if (numItems > viewable){
		var $item = $scroller.find('.item').eq(0)
		var increment = $item.width() + space;
		var data = {'low': 0, 'high': viewable-1}
		$scroller.data('current', data);
		$scroller.data('position',0);
		$scroller.find('.leftArrow').click(function(){
			var curr = $scroller.data('current');	
			var $mover = $scroller.find('.scrollingSection');
			var pos = $scroller.data('position');
			if (curr.low - 1 >= 0){
				curr.low--;
				curr.high--;
				$scroller.data('current',curr);
				$scroller.data('position', pos+increment);
				$mover.animate({'left': [pos+increment, 'swing']}, 200);	
			}
			console.log(curr.low + ' ' + curr.high);
			return false;
		});
		$scroller.find('.rightArrow').click(function(){
			var curr = $scroller.data('current');
			var $mover = $scroller.find('.scrollingSection');
			var pos = $scroller.data('position');
			if (curr.high + 1 < numItems){
				curr.high++;
				curr.low++;
				$scroller.data('current',curr);
				$scroller.data('position', pos-increment);
				$mover.animate({'left': [pos-increment, 'swing']}, 200);		
			}
			console.log(curr.low + ' ' + curr.high);
			return false;
		});
		}
		else{
			$scroller.find('.leftArrow, .rightArrow').css('visibility','hidden');
		}
	});
}

