var Funbox = window.Funbox || {}; 

Funbox.Site = {
	init : function(){
		//article controller
		Funbox.Site.articleController();

		//ie
		if($('.ie6').length > 0 || $('.ie7').length > 0 || $('.ie8').length > 0  ){
			alert("Life moves pretty fast. If you don't stop and [get a new browser], you could miss it. -Ferris Bueller");
		}
		
	},
	articleController: function(){
		var $article = $('.index section.category article.entry'),
			$thumb = $article.find('img.thumb');
		
		$article.each(function(){
			if($(this).hasClass('link')) return;
			new Funbox.Site.article($(this));
			
		});
		$thumb.each(function(){
			new Funbox.Site.thumbPop($(this));
		});
	}
};
//here we go
$(Funbox.Site.init);


//article class
Funbox.Site.article = function( view ){
	this.view = view;
	this.topBox = this.view.children('.top');
	this.thumb = this.topBox.children('a');
	this.titleTxt = this.topBox.children('.txt').children('p.title').children('a');
	this.plus = this.topBox.children('.plus').children('a');
	this.contentBox = this.view.children('.content');
	
	
	
	this.init();
};
Funbox.Site.article.prototype.init = function(){
	var self = this;
	this.isOpen = false;
	this.contentBox.hide();
	
	this.thumb.click(function(){
		if(self.isOpen === false){
			self.open();
		}else{
			self.close();
		}
	});
	this.titleTxt.click(function(){
		if(self.isOpen === false){
			self.open();
		}else{
			self.close();
		}
	});
	this.plus.click(function(){
		if(self.isOpen === false){
			self.open();
		}else{
			self.close();
		}
	});
};
Funbox.Site.article.prototype.open = function(){
	this.plus.html('-');
	this.contentBox.slideDown('fast');
	this.isOpen = true;
};
Funbox.Site.article.prototype.close = function(){
	this.plus.html('+');
	this.contentBox.slideUp('fast');
	this.isOpen = false;
};


//thumb pop class
Funbox.Site.thumbPop = function( view ){
	var self = this;
	this.view = view;
	this.dimensions = 300;
	this.timer;
	this.notOn = false;
	this.buildPopUp();
	this.view.hover(function(e){
		self.show(e);
	},function(){
		self.notOn = true;
		self.hide();
	});
};
Funbox.Site.thumbPop.prototype.show = function(e){
	var self = this;
	var x, y;
		x = e.pageX;
		y = e.pageY;
	function pop(){

		$('body').append(self.markup);
		self.markup.css({
			position: 'absolute',
			top: y  - (self.dimensions / 6),
			left: x - (self.dimensions / 2),
		}).fadeIn();
		self.killTimer();
	}
	this.timer = setTimeout(pop, 1000);
};
Funbox.Site.thumbPop.prototype.buildPopUp = function(){
	var self = this;
	this.markup = $('<div class="thumbPop"><img src="'+this.view.attr('src')+'" width="'+this.dimensions+'px" height="'+this.dimensions+'px"></div>');
};
Funbox.Site.thumbPop.prototype.hide = function(){
	var self = this;
	if(this.notOn){
		this.markup.hide();
	}
	clearTimeout(self.timer);
};
Funbox.Site.thumbPop.prototype.killTimer = function(){
	var self = this;
	function kill(){
		self.markup.fadeOut('fast');
		clearTimeout(self.timer);
		clearTimeout(killTime);
	}
	var killTime = setTimeout(kill, 2500);
	
};






