/*
* jQuery place image plugin
* 
* @version  0.1
* @homepage ??
* @author   Boz Kay (http://www.boz.co.uk)
*
* Copyright (c) 2009 Boz Kay
* Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
* and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
*/

(function($){

  $.fn.placeImage = function(options){
    
    var options = $.extend({
    	formID: 'form',
    	inputID: 'input',
    	images: [],
    	imageChoose: '/images/shared/choose-image.gif',
		preSelect: '',
		preSelectDisable: false,
		imagePre: '',
		imagePost: '',
		callback: function() {}
	}, options);
    
    // selectNumber - todo
    var image = this;
    
    // style list
    image.addClass('placeImage');
    
    // make clickable
	image.css('cursor','pointer').hover(function() {
		$(this).css('border-color','#686805');
	}, function() {
		$(this).css('border-color','#000');
	});

	$('#'+options.formID).prepend($('<input type="hidden" />').attr('id',options.inputID).attr('name',options.inputID));
	
	image.click(function() {
		// launch imageGallery
		$().imgLib({
			imageClick: function(id) {
		   		image.attr('src',options.imagePre+id+options.imagePost);
		   		$('input#'+options.inputID).val(id);
		   		options.callback();
	   		},
	   		reLaunch: true,
	   		destroySwf: false
	   	});
	});
    
	// show words 'choose image' if there is no image in place
	if(options.preSelect == '') {
		image.attr('src',options.imageChoose);
	} else {
		image.attr('src',options.imagePre+options.preSelect+options.imagePost);
	   	$('input#'+options.inputID).val(options.preSelect);
    	if(options.preSelectDisable == true) {
    		image.unbind();
    	}
    }
    
    
  };

})(jQuery);