
(function($) {

	// plugin definition
	$.fn.peakaboo = function(options) {
  	return this.each(function() {
        new $pkb(this, options);
    });
	};

	// defaults
	var defaults = {
      query_url : false,
			items_per_row : 3,
			loadedCallback : null
  };
	
	// peakaboo object
	$.peakaboo = function(e, o) {
		this.options    = $.extend({}, defaults, o || {});
		this.el = e;
		this.setup();
	};
	
	// create shortcut for internal use
  var $pkb = $.peakaboo;
  $pkb.fn = $pkb.prototype = { peakaboo: '0.2.3' };
  $pkb.fn.extend = $pkb.extend = $.extend;

  $pkb.fn.extend({
		
		setup: function() {
			
			if (this.options.query_url) {
				var self = this;
				
				this.curr_peak 		= null;
				this.curr_trigger = null;
				
				this.els = $(this.el).find('.peakaboo-item');
				
				if ($(this.el).attr('class').indexOf('peakaboo-static') == -1) {
					
					jQuery.each(this.els, function(i, item) {
						$(item).click(function () {
							self.peakControl(this);
				    });
					});
				
				}
				
				this.findDefault();
				
			}	
    },
		
		/*
			controls whether to load a new item or just close the current one
		*/
		peakControl: function(load_item) {
			
			var self = this;
						
			if (this.curr_peak) {
				$(this.curr_peak).slideUp(500, function () {
					if (self.curr_trigger)
						self.removeCurrentSelected(self.curr_trigger);
						
					$(self.curr_peak).remove();
					if (self.curr_trigger != load_item) {
						self.peakLoad(load_item);
					} else {
						self.curr_trigger = null;
					}
				});
				if (this.curr_trigger == load_item) {
					return;
				}
			} else {
				this.peakLoad(load_item);
			}
			
		},
		
		/*
			main function to ajax load information
		*/
		peakLoad: function(load_item) {
			
			var self = this;	
			
			// display ajax loader
			var htmlLoaderStr = "<div class=\"peakaboo-loader\"><img src=\"/images/common/ajax_loader.gif\" /></div>";
			$(load_item).append(htmlLoaderStr);
			var loader_el = $(load_item).find('.peakaboo-loader');
			
			// find next available row based on th load item passed
			var curr_row = 1;
			
			jQuery.each(this.els, function(i, item) {
				
				if ( i / self.options.items_per_row == curr_row ) {
					curr_row++; 
				}
				
				if (item.id == load_item.id) {
					// insert div element
					var htmlStr = "<div class=\"peakaboo-peak\"></div>";
					var row_els = $(self.el).find('.peakaboo-row');
					
					$(row_els[curr_row-1]).after(htmlStr);
					self.curr_peak = $(self.el).find('.peakaboo-peak')[0];
					
					$(self.curr_peak).load(self.options.query_url + load_item.id.split('-')[1], {}, function () {
						
						if (self.options.ajaxLoadedCallback != null) {
							self.options.ajaxLoadedCallback(self);
						}
						
						$(this).hide();
						$(this).slideDown(1000, function () {
							$.scrollTo($(load_item), 500, {offset:-13});
							if (self.options.loadedCallback != null) {
								self.options.loadedCallback(self);
							}
						});
						
						// show current selected tab
						self.showCurrentSelected(load_item);
						
						// fade and remove preloader
						var loader_el = $(load_item).find('.peakaboo-loader');
						loader_el.fadeOut(1000, function () {
			        $(this).remove();
			      });
					});
				}
			});
			this.curr_trigger = load_item;
		},
		
		/*
		
		*/
		showCurrentSelected : function(item) {
			var html = '<div class="active"><img src="/images/common/active_indicator.gif" /></div>';
			$(item).append(html);
		},
		
		/*
		
		*/
		removeCurrentSelected : function(item) {
			var el = $(item).find('.active')[0];
			$(el).remove();
		},
		
		/*
		
		*/
		findDefault : function () {
			
			var peak_el = $(this.el).find('.peakaboo-peak');
			
			if (peak_el.length == 1) {
				
				this.curr_peak = $(peak_el[0]);
				
				// initialise inner bits
				//$.scrollTo($(peak_el[0]), 500, {offset:-13});
				if (this.options.loadedCallback != null) {
					//console.log(this.options.loadedCallback);
					this.options.loadedCallback(self);
				}
			}
			
		}
		
	});
	
	$pkb.extend({
      defaults: function(d) {
          return $.extend(defaults, d || {});
      }
  });
	
})(jQuery);