/*
* jQuery image gallery plugin
* 
* @version  0.1
* @homepage ??
* @author   Boz Kay (http://www.boz.co.uk)
*
* Copyright (c) 2009 Nowhere
*/

(function($){

	$.fn.imgLib = function(options){
    
		var opts = $.extend({}, $.fn.imgLib.defaults, options);
		
		$lib = $(opts.imgLibElement);


		if($('#imgLibWrap').length == 0) {

			// bind events to the container
			$lib.bind('next.imgLib',next);
			$lib.bind('previous.imgLib',previous);
			$lib.bind('loadImgs.imgLib',loadImgs);
			$lib.bind('setImageClick.imgLib',setImgClick);

			// setup initial html elements
		    $lib.append('<h2 id="imgLibTitle">Your image library</h2><div id="imgLibLoading"><img src="/images/shared/ajax-loader.gif" /></div><div id="imgLibWrap"><div id="imgLibUpload"></div><div id="imgLibImages"></div><div id="imgLibNav"><a class="previous"><img src="/images/shared/buttons/left.png" /></a><a class="next"><img src="/images/shared/buttons/right.png" /></a></div></div>');
	
			$(opts.imgLibElement+' #imgLibLoading').hide();

			// start with inital images from lib
			$lib.trigger('loadImgs.imgLib',[0]);
				
			addUpload();
			
		}
		
		if(opts.reLaunch) {
			// what to do when an image is clicked
			setImgClick('',opts.imageClick);
			// make it popup
			$().showPage({
				show: opts.imgLibElement,
				showNow: true,
				noClick: true,
				destroySwf: opts.destroySwf
			});
		}

		// the event functions ...
		function next() {
			$lib.trigger('loadImgs.imgLib',[$lib.imgLib.currentSet+opts.limit]);
		}
		function previous() {
			$lib.trigger('loadImgs.imgLib',[$lib.imgLib.currentSet-opts.limit]);
		}

		
		function loadImgs(e,fromNo) {
			
			$lib.imgLib.currentSet = fromNo;
			
		    // add images
			$(opts.imgLibElement+' #imgLibLoading').show();
		    $(opts.imgLibElement+' #imgLibImages').hide().load(opts.getImagePHP, {limit: 6, from: fromNo}, function(){
				
				// make clickable
				$(opts.imgLibElement+' #imgLibImages img').filter('img').css('cursor','pointer').hover(function() {
					$(this).css('border-color','#686805');
				}, function() {
					$(this).css('border-color','#000');
				});
				setImgClick('',opts.imageClick);

				$(opts.imgLibElement+' #imgLibLoading').hide();
		    	$(this).fadeIn();
		    	setArrows();
		    	
			});
		};
		
		function setImgClick(e,imageClick) {
			$('#imgLibImages img').unbind().click(function(){
				imageID = $(this).attr('id');
				imageClick(imageID);
				$(opts.imgLibElement+' .close').trigger('click');
			});
		}

		function setArrows() {

			if($(opts.imgLibElement+' #imgLibTotal').length == 0) {
				var total = 0;
			} else {
				var total = $(opts.imgLibElement+' #imgLibTotal').html();			
			}
			
			if(total == 0) {
				
				$(opts.imgLibElement+' #imgLibImages').html('<p class="noImages">Your image library is empty. Please add an image using the button below.<br />Once you have added your first image, you will then be able to use this library to place images into areas of the nowhere website.</p>');
				$(opts.imgLibElement+' #imgLibNav').hide();
						
			} else {

				$(opts.imgLibElement+' #imgLibNav').show();
				if($lib.imgLib.currentSet == 0) {
					$(opts.imgLibElement+' .previous').unbind().css({opacity:'0.5',cursor:'default'});
				} else {
					$(opts.imgLibElement+' .previous').unbind().click(function(){
						$lib.trigger('previous.imgLib');
					}).css({opacity:'1',cursor:'pointer'});
				}
				if(($lib.imgLib.currentSet+opts.limit) < total) {
					$(opts.imgLibElement+' .next').unbind().click(function(){
						$lib.trigger('next.imgLib');
					}).css({opacity:'1',cursor:'pointer'});
				} else {
					$(opts.imgLibElement+' .next').unbind().css({opacity:'0.5',cursor:'default'});
				}
			
			}
			
		}

		function addUpload() {
			$('#imgLibUpload').html('<form id="PBUploadForm"><input type="file" id="PBFile" name="PBFile" /></form>');
			// use a flash uploader
			$("#PBFile").makeAsyncUploader({
				upload_url: "/imgLib/AsyncUpload.php", 
				flash_url: '/imgLib/swfupload.swf',
				button_image_url: '/imgLib/addimage.png',
				file_size_limit: '3 MB',
				width: 100,
				height: 41,
				post_params: {"PHPSESSID" : window.PHPSESSID},
				file_types: '*.jpg; *.gif; *.png; *.tif;',
				button_text: '<span></span>',
				upload_complete_callback: function() {
					$lib.trigger('loadImgs.imgLib',[0]);
				}
			});
		}


	}
	
	

	$.fn.imgLib.defaults = {
		imgLibElement: '#imgLib',
		getImagePHP: '/imgLib/getImage.php',
		imageClick: function(){alert('done');},
		limit: 6,
		show: 0,
		destroySwf: true
	};        

})(jQuery);