/* 
 * Author: Zach Wolf 
 * Owner: SoulPancake
 * Description: Create a 'slide open' effect for the larger images in the feed to save space
 * Version: 2.0
 */

(function($) {

	var slides = [];
	
	var	controls  = '<div class="controls">';
		controls += '<a href="#view" class="controls-btn view"><span>View</span></a>';
		controls += '</div>';
		
	var expndicn  = '<span class="click-to-expand">Click to Expand</span>';

	var methods = {
		init : function() {
			methods.add.apply();
		},
		update : function() { 
			methods.add.apply();
		}, 
		add: function () {
			$('.img.large').each(function () {
				if($(this).data('added-to-array') == undefined || $(this).data('added-to-array') != true) {
					slides.push($(this));
					$(this).data('added-to-array', true);
				}
			});
			methods.create.apply();
		}, 
		create: function () {
			$.each(slides, function (key, value) {
				// check if the image is loaded aka has height
				if($(this).find('img').height() == 0) {
					$(this).find('img').load(function(){
						methods.publish($(this).parents('.img.large').find('img'));
					});
				} else {
					methods.publish($(this).find('img'));
				}
			});
		},
		publish: function ($this) {
			imgtop = '-' + (($this.height()/2)-50) + 'px';
			$this.data('imgtop',imgtop);
			$this.css({
				top: $this.data('imgtop')
			});
			// if the image is on the dashboard, don't show the link eye link to the inline comment
			if($this.parents(".feed").length == 1) {
				$this.parents('.img.large').append(controls, expndicn);
				$this.parents('.img.large').find('.view').bind({
					mouseenter: function () {
						$(this).parents('.img.large').data("eye-hover", true);
					},
					mouseleave: function () {
						$(this).parents('.img.large').data("eye-hover", false);
					}
				});
			} else {
				$this.parents('.img.large').append(expndicn);
			}
			
			$this.parents('.img.large').bind({
				click: function () {
					if($(this).data("eye-hover") != true){
						if($(this).data("img-opened") == undefined || $(this).data("img-opened") != true) {
							$(this).animate({
								"height": $(this).find('img').height()
							}, 300);
							$(this).find('img').animate({
								"top": 0
							}, 300);
							$(this).find('.controls').animate({
								"top": 0
							}, 300);
							$(this).find('.click-to-expand').addClass("open");
							$(this).data("img-opened", true);
						} else {
							$(this).animate({
								"height": 100
							}, 300);
							$(this).find('img').animate({
								"top": $(this).find('img').data('imgtop')
							}, 300);
							$(this).find('.controls').animate({
								"top": "-35px"
							}, 300);
							$(this).find('.click-to-expand').removeClass("open");
							$(this).data("img-opened", false);
						}
						return false;
					} else {
						window.location = $(this).siblings().find('a').attr('href');
					}
				},
				mouseenter: function () {
					$(this).find('.click-to-expand').stop().animate({
						"opacity": .9
					}, 300);
					if($(this).data("img-opened") === true) {
						$(this).find('.controls').stop().animate({
							"top": 0
						}, 300);
					}
				},
				mouseleave: function () {
					$(this).find('.click-to-expand').stop().animate({
						"opacity": .15
					}, 300);
					if($(this).data("img-opened") === true) {
						$(this).find('.controls').stop().animate({
							"top": "-35px"
						}, 300);
					}
				}
			});
			$this.parents('.img.large').find('.click-to-expand').css({ "opacity": .15 });
		}
	}
	
$.fn.feedthumbnail = function(method) {
	
	if ( methods[method] ) {
		return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
	} else if ( typeof method === 'object' || ! method ) {
		return methods.init.apply( this, arguments );
	} else {
		$.error( 'Method ' +  method + ' does not exist on jQuery.tooltip' );
	}  

};
})(jQuery);
