/*
 * 	 imBannerRotater - a JQuery Plugin
 * 	 @author Les Green
 * 	 Copyright (C) 2009 Intriguing Minds, Inc.
 *   Version 0.5
 * 
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.

 *   Demo and Documentation can be found at:   
 *   http://www.grasshopperpebbles.com
 *   
 */
 
;(function($) {
	$.fn.extend({
        imBannerRotater: function(options) { 
        	opts = $.extend({}, $.bannerRotater.defaults, options);
			return this.each(function() {
				new $.bannerRotater(this, opts);
			});
        }
    });	

$.bannerRotater = function(obj, opts) {
	var $this = $(obj);
	var imgCnt = 0;
	var ttlImg = 0;
	if (opts.image_url) {
		var d = getDataString();
		doAjax('GET', opts.image_url, d, '', doCreate);
	} else {
		doCreate(opts.images);
	}
	
	function getDataString() {
		var str = '';
		$.each(opts.data, function(i, itm) {
			str += itm.name + "=" + itm.value + "&";							
		});
		//remove last "&"
		str = str.substr(0, (str.length-1));
		return str;
	};
		
	function doAjax(t, u, d, fnBefore, fnSuccess) {
		var dt = (opts.return_type == 'json') ? 'json' : 'text';
		$.ajax({
			type: t,
			url: u,
			data: d,
			dataType: dt,
			beforeSend: fnBefore, //function(){$("#loading").show("fast");}, //show loading just when link is clicked
			//complete: function(){ $("#loading").hide("fast");}, //stop showing loading when the process is complete
			success: fnSuccess,
			error: showError
	 	}); //close $.ajax(
	};
	
	function showError(XMLHttpRequest, textStatus, errorThrown) {
		console.log(textStatus);
	};
	
	function doCreate(data) {
		//var tbl, tr;
		var img, pic;
		if (opts.return_type == 'list') {
			var daAR = data.split(',');
		} else {
			var daAR = new Array();
			$.each(data, function(i, itm) {
				if (opts.mode == 'random') {
					if (opts.data_map.url_name) {
						daAR[i] = new Array(itm[opts.data_map.image_name], itm[opts.data_map.url_name]);
					} else {
						daAR[i] = itm[opts.data_map.image_name];
					}	
				} else {
					daAR[i] = itm[opts.data_map.image_name];
				}	
			});
		}
		if (opts.mode == 'random') {
			img = new Image();
			if (opts.data_map.url_name) {
				var tgt = (opts.data_map.url_target) ? opts.data_map.url_target : '_blank';
				var sel = daAR[Math.floor(Math.random()*daAR.length)];
				pic = opts.base_path + sel[0];
				var url = sel[1];
				$this.append($('<a></a>').attr({'href': url, 'target': tgt}).append($(img).attr({ src: pic, alt: pic})));
			} else {
				pic = opts.base_path + daAR[Math.floor(Math.random()*daAR.length)];
				$this.append($(img).attr({ src: pic, alt: pic}));
			}	
		} else {
			ttlImg = daAR.length;
			for (var i = 0; i < ttlImg; i++) {
				pic = opts.base_path + daAR[i];
				img = new Image();
				$this.append($(img).attr({ src: pic, alt: pic}).css('display', 'none'));
			}
			imgFadeIn();
		}
	};
	
	function imgFadeIn() {
		$("img:hidden:eq("+imgCnt+")", $this).fadeIn(opts.speed, function(){
			imgFadeOut();
		});
	};
	
	function imgFadeOut() {
		$("img:eq("+imgCnt+")", $this).fadeOut(opts.speed, function(){
			imgCnt = (imgCnt == ttlImg-1) ? 0 : imgCnt + 1;
			imgFadeIn();
		});
	};
};

$.bannerRotater.defaults = {
	mode: 'random',//rotate
	image_url: '',
	data: '',
	images: '',//can be used instead of image_url. contains comma delimited list of images
	return_type: 'list', //list, json
	base_path: '',
	data_map: '', //{image_name: '', url_name: '', url_target: '_blank'}
	speed: 2000
};
})(jQuery);		   