/*
 * jPresenter - jQuery Plugin
 * Emphasizes a multiple content block with a non-sliding effect
 *
 * Copyright (c) 2011 Caio Guimarães
 *
 * Version: 0.3 (16/03/2011)
 * Requires: jQuery v1.3+
 *
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 */

(function($){

	var methods = {
		init: function( options ) {
			var $this = $(this),
				data = $this.data('jPresenter'),
				domain = document.location.href.split('/')[2];

			if ( typeof data === 'undefined' ) {
				var options = $.extend({},$.fn.jPresenter.defaults, options);

				$(this).data('jPresenter', {
					target : $this,
					options: options
				});
				
				console.log($(this));
			}

			data = $this.data('jPresenter');

			data.location = 'http://' + domain + "/" + data.options.url;
			data.img = new Image();
			data.button = '.forward';

			data.target.attr('data-jpresenter-current', data.options.offset);

			$this.find(data.options.prevButton + ', ' + data.options.nextButton).bind('click', function(event){
				event.preventDefault();
				data.button = '.' + $(this).attr('class');
				// Sets the offset for the current banner
				if (data.button == data.options.prevButton) {
					data.target.attr('data-jpresenter-current', $('a').parent(data.button).attr('data-jpresenter-prev'));
				} else if (data.button == data.options.nextButton) {
					data.target.attr('data-jpresenter-current', $('a').parent(data.button).attr('data-jpresenter-next'));
				}

				data.options.loadingClass = 'loading-close';
				$this.jPresenter('close');

				$.doTimeout('jPresenter', data.options.delay*1000, function(){
					if (data.button == data.options.prevButton) {
						data.target.attr('data-jpresenter-current', $('a').parent(data.button).attr('data-jpresenter-prev'));
					} else if (data.button == data.options.nextButton) {
						data.target.attr('data-jpresenter-current', $('a').parent(data.button).attr('data-jpresenter-next'));
					}
					data.options.loadingClass = 'loading-open';
					$this.jPresenter('fadeOut');
					return true;
				});
			});

			$this.css({
				height: data.options.closedHeight + 'px'
			});
			$('#fimpontos').css({
				top: (data.options.closedHeight - data.options.shadowHeight) + 'px'
			});
			$('#pontinhos').css({
				height: data.options.closedHeight + 'px'
			});
			$('#arrows span').hide();

			$this.jPresenter('loadData');
			$.doTimeout('jPresenter', data.options.delay*1000, function(){
				data.target.attr('data-jpresenter-current', $(data.options.nextButton).attr('data-jpresenter-next'));
				data.options.loadingClass = 'loading-open';
				$this.jPresenter('fadeOut');
				return true;
			});
		},
		loadData: function(fx) {
			var $this = $(this),
				data = $this.data('jPresenter'),
				img = data.img,
				options = data.options,
				current = parseInt(data.target.attr('data-jpresenter-current'));

			if (fx === undefined) fx = 'open';
				
			$.ajax({
				url: data.location,
				type: 'POST',
				data: 'offset='+current,
				dataType: 'json',
				success: function(response, textStatus, jqXHR) {
					data.data = response.data;
					data.titulo = response.titulo;
					data.desc = response.desc;
					data.link = response.link;
					data.src = response.src;
					data.imgWidth = response.imgWidth;

					// Sets the total banner found
					data.target.attr('data-jpresenter-total', response.num_rows);

					// Sets the offset for getting the previous banner
					if ((current - 1) < 0 || current === 0) {
						$(data.options.prevButton).attr('data-jpresenter-prev', response.num_rows-1);
					} else {
						$(data.options.prevButton).attr('data-jpresenter-prev', current-1);
					}

					// Sets the offset for getting the next banner
					if ((current + 1) == response.num_rows) {
						$(data.options.nextButton).attr('data-jpresenter-next', 0);
					} else {
						$(data.options.nextButton).attr('data-jpresenter-next', current+1);
					}

					// Sets the offset for the current banner
					if (data.button == data.options.prevButton) {
						data.target.attr('data-jpresenter-current', $('a').parent(data.button).attr('data-jpresenter-prev'));
					} else if (data.button == data.options.nextButton) {
						data.target.attr('data-jpresenter-current', $('a').parent(data.button).attr('data-jpresenter-next'));
					}

					// Fills 'text' block with some data
					$(data.options.txtHolder + ' span.data').text(data.data + ' | ' + data.titulo);
					$(data.options.txtHolder + ' span.wrap-h2').html('<a href="' + data.link + '"><h2>' + data.desc + '</h2></a>');

					img.onload = function() {
						$(options.imgHolder)
						// remove the loading class (so no background spinner),
						.removeClass(options.loadingClass).append(img);

						$this.jPresenter(fx);
					};
					img.src = data.src;
				}
			});
		},
		fadeIn: function() {
			var $this = $(this),
				data = $this.data('jPresenter'),
				img = data.img,
				options = data.options;

			$this.removeClass(options.loadingClass);
			$(data.options.imgHolder + ', ' + data.options.txtHolder).fadeIn(options.duration);
			$(img).fadeIn('duration');
		},
		fadeOut: function() {
			var $this = $(this),
				data = $this.data('jPresenter'),
				img = data.img,
				options = data.options;

			$(data.options.imgHolder + ', ' + data.options.txtHolder).fadeOut(options.duration);
			$(img).fadeOut(options.duration, function(){
				$this.addClass(options.loadingClass);
				$this.jPresenter('loadData','fadeIn');
			});
		},
		open: function() {
			var $this = $(this),
				data = $this.data('jPresenter'),
				img = data.img,
				options = data.options;

			$this.removeClass(options.loadingClass);
			
			$('#arrows span').fadeIn(options.duration);
			$('#pontinhos').animate({
				height: options.openedHeight + 'px'
			});
			$('#fimpontos').animate({
				top: (options.openedHeight - options.shadowHeight) + 'px'
			});
			$this.animate({
				height: options.openedHeight + 'px'
			},options.duration);

			// fade our image in to create a nice effect
			$(img).fadeIn('slow');
		},
		close: function() {
			var $this = $(this),
				data = $this.data('jPresenter'),
				options = data.options;
				
			$(options.imgHolder + ' img').fadeOut('fast').remove();
			$('#fimpontos').animate({
				top: (options.closedHeight - options.shadowHeight) + 'px'
			});
			$('#pontinhos').animate({
				height: options.closedHeight + 'px'
			});
			$('#arrows span').fadeOut(options.duration);
			$this.animate({
				height: options.closedHeight + 'px'
			}, options.duration, function(){
				$this.addClass(options.loadingClass);
				$this.jPresenter('loadData','open');
			});
		}
	}

	$.fn.jPresenter = function( method ) {
		// Method calling logic
		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.jPresenter' );
		}
	};

	$.fn.jPresenter.defaults = {
		url:			'load_image.php',
		loadingClass:	'loading-close',
		openedHeight:	375,
		closedHeight:	130,
		arrowsHeight:	65,
		shadowHeight:	20,
		duration:		500,
		imgHolder:		'#banner',
		txtHolder:		'#legenda',
		prevButton:		'.back',
		nextButton:		'.forward',
		offset:			0,
		delay:			10
	}
})(jQuery);
