/*
	Album Viewer Plugin 
	by Johnny Green / @bemusedjohnny
	of NoiseFloorDesign.com
	for AliceWells.com
*/

var viewer = (function() {
	var albums = [0],
		i = null;
		
	var settings = {
		container: null,
		name: null,
		description: null,
		selector: null,
		prev: null,
		next: null,
		controls: null,
		reset: null,
		padding: 10,
		height: 600
	};
	
	var setup = function(options) {
		$.extend(settings, options);
		
		settings['container'].parent().width($(window).width()).height(settings['height']).end().height(settings['height']);
		settings['controls'].fadeTo(0, 0.6).bind("mouseover", function() { $(this).stop().fadeTo(0, 1.0); }).bind("mouseout", function() { $(this).stop().fadeTo(0, 0.6); });
		settings['prev'].bind("click", prev);
		settings['next'].bind("click", next);
		settings['reset'].bind("click", reset);
	};
	
	var addAlbum = function(info) {
		albums.push({
			"name": info.name,
			"description": info.description,
			"images": info.images,
			"x": [0],
			"scroll_key": 0,
			"load_key": 0,
			"pos_x": settings['padding'],
			"pos_y": 0,
			"max_w": 0
		});
		
		var link = $("<a>", {href: "javascript:void(0);", text: albums.length - 1});
		settings['selector'].append(link);
		link.bind("click", function() { 
			setAlbum($(this).index() + 1); 
		}).bind("mouseover", function() { 
		    settings['name'].text(albums[$(this).index() + 1].name);
		    settings['description'].text(albums[$(this).index() + 1].description);  
		}).bind("mouseout", function() {
			settings['name'].text(albums[$(".selected").index() + 1].name);
		    settings['description'].text(albums[$(".selected").index() + 1].description);  
		});
	};
	
	var setAlbum = function(index) {
		i = index;
		
		var c = current();
		
		settings['name'].text(c.name);
		settings['description'].text(c.description);
		settings['container'].html("").css({"left": "0px"});
		settings['selector'].find(".selected").removeClass("selected");
		settings['selector'].find("a:contains(" + i + ")").addClass("selected");
		
		c.scroll_key = 0;
		c.load_key = 0;
		c.pos_x = settings['padding'];
		c.pos_y = 0;
		c.max_w = 0;
	
		load();
	};
	
	var current = function() {
		return albums[i];
	};
	
	var load = function() {
		var c = current();
		var key = c['load_key'];
		var images = c.images;
		
		for(key; key < images.length; key++) {
			var height = settings['container'].outerHeight(true);
			var image = $("<img>", {"src": images[key]['src']})
							.width(images[key]['width'])
							.height(images[key]['height']);
			
			if(image.outerHeight(true) > height) {
				image.width(height*(image.outerWidth(true)/image.outerHeight(true))).height(height);
			}
			
			if(c['pos_y'] + image.outerHeight(true) > height) {
				c['pos_x'] = c['pos_x'] + c['max_w'] + settings['padding']/2;
				c['pos_y'] = 0;
				c['max_w'] = 0;	
				c['x'].push(c['pos_x']);
			}
			
			image.css({"position": "absolute", "left": c['pos_x'], "bottom": c['pos_y']});
			
			c['max_w'] = Math.max(c['max_w'], image.outerWidth(true));
			c['pos_y'] = c['pos_y'] + image.outerHeight(true) + settings['padding']/2;
			
			settings['container'].append(image);
			
			if(c['pos_x'] > -settings['container'].position().left + $(window).width()) {
				break;
			}
			
			c['load_key']++;
		}
		
		c['load_key']++;
		c['images'] = images;
	};
	
	var next = function() {
		var c = current();
		
		if(typeof(c['x'][c['scroll_key'] + 1]) != "undefined") {
			settings['container'].stop().animate({"left": -parseInt(c['x'][++c['scroll_key']]) + "px"}, "slow", "swing", function() {
				load();
			});
		}
	};
	
	var prev = function() {
		var c = current();
		
		if(typeof(c['x'][c['scroll_key'] - 1]) != "undefined") {
			settings['container'].stop().animate({"left": -parseInt(c['x'][--c['scroll_key']]) + "px"}, "slow", "swing", function() {
				/*r_load();*/
			});
		}
	};
	
	var reset = function() {
		settings['container'].stop().animate({"left": "0px"}, "slow", "swing");
	}
	
	$(window).resize(function() {
		settings['container'].parent().width($(window).width()).height(settings['height']).end().height(settings['height']);
		load();
	});
	
	return {
		"setup": setup,
		"addAlbum": addAlbum,
		"setAlbum": setAlbum,
		"albums": albums,
		"Created By": "Johnny Green / @bemusedjohnny / NoiseFloorDesign.com"
	}
})();
