/*
 * 	 imCheckBoxDB - a JQuery Plugin
 * 	 @author Les Green
 * 	 Copyright (C) 2008 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);
	if (opts.db_map != '') {
		var d = getDataString();
		doAjax('GET', opts.data_url, d, '', doCreate);
	}
	
	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
	 	}); //close $.ajax(
	};
	
	function doCreate(data) {
		//var tbl, tr;
		if (opts.return_type == 'list') {
			var daAR = data.split(',');
		} else {
			var daAR = new Array();
			$.each(data, function(i, itm) {
				daAR[i] = itm[opts.imageName];
			});
		}
		var pic = opts.base_path + daAR[Math.floor(Math.random()*daAR.length)];
		var img = new Image();
		$this.append($(img).attr({ src: pic, alt: pic}));
	};
};

$.bannerRotater.defaults = {
	//mode: 'display',
	//upload: {"container": "uploadContainer", "url": "<?php echo base_url(); ?>index.php/admin/gallery_doAdd", "showOptions": true},
	data_url: '',
	data: '',
	return_type: 'list', //list, json
	base_path: '',
	imageName: '' //
};
})(jQuery);		   