$.fn.archerImageGallery = function(settings) {
    
    settings = jQuery.extend({
        actual: 0,
		animation_speed: 0, 
		slideshow: 5000, 
		autoplay_slideshow: false, 
		opacity: 0.50,
		width: 500,
		height: 0,
		changepicturecallback: function(){            
        } 
	}, settings);

    var gallery = jQuery(this);
	var icons = new Array();
    var images = new Array();
    var mainHolder = jQuery('<div></div>')
        .attr('id','mainHolder')
        .attr('class','mainHolder');
    
    $(mainHolder).insertBefore($(this));
    
    var pictureHolder = jQuery('<div></div>')
        .attr('id','pictureHolder')
        .attr('class','pictureHolder');
    
    
    var nextHolder = jQuery('<div></div>')
        .attr('id','nextHolder')
        .attr('class','next-button');         
    var nextInner = jQuery('<div></div>').attr('id','nextInner').attr('class','next-button-inner');
    
    
	var prevHolder = jQuery('<div></div>')
        .attr('id','prevHolder')
        .attr('class','prev-button');
	var prevInner = jQuery('<div></div>').attr('id','prevInner').attr('class','prev-button-inner'); 
			
    function showImage(index) {
        
        $(pictureHolder).html($(images[index]));       
                
        var imageWidth = $(images[index]).width(); 
        var imageHeight = $(images[index]).height();
         
        // lying  
        if (imageWidth >= imageHeight) {
                     
            ratio = parseInt(settings.width) / parseInt(imageWidth);
            imageWidth = settings.width; 
            imageHeight = Math.round(imageHeight * ratio);   
        } else { // standing            
            
            ratio = settings.height / imageHeight;
            imageHeight = settings.height; 
            imageWidth = Math.round(imageWidth * ratio);  
        }
                
        $(images[index]).width(imageWidth);
        $(images[index]).height(imageHeight);  
        
               
        $('#nextHolder').css('left',($('#mainHolder').width() - 43) + 'px');
        
        $(pictureHolder).width(imageWidth);
        $(pictureHolder).height(imageHeight);    
        
        $(pictureHolder).css('left', ($('#mainHolder').width() / 2 - $(pictureHolder).width() / 2) + 'px');
        settings.actual = index;                 
    }
    
    function showNext() {
        // at last picture back to first
        if (settings.actual == images.length - 1) {
            showImage(0);
        } else {
            showImage(settings.actual + 1);
        }
    }
    
    function showPrev() {
        // at first picture jump to last
        if (settings.actual == 0) {
            showImage(images.length - 1);
        } else {
            showImage(settings.actual - 1);
        }
    }	
    
    
    
    if ($(this).attr('class')) {
    	      	
      	// remove the list and later put it into the mainholder
      	var list = $('.archerImageGallery');
      	$(list[0]).remove();  
                          
      	$(nextInner).bind('click', function(){
          	showNext();
        });
        
        $(prevInner).bind('click', function(){
          	showPrev();
        });
      	$(nextInner).appendTo($(nextHolder));
      	$(prevInner).appendTo($(prevHolder));
      	
    	$(pictureHolder).appendTo($(mainHolder));
        $(nextHolder).appendTo($(mainHolder));
        $(prevHolder).appendTo($(mainHolder));        
        $(list[0]).appendTo($(mainHolder));
        
        
        var listElements = jQuery(".archerImageGallery a[rel^='archerImageGallery']");
    	// load images from list's a tags	
    	$(listElements).each(function(i){
            icons[i] = jQuery($(listElements[i]).html()).attr('src');
            
            // preload images        	
            images[i] = jQuery('<img>').attr('id','image' + i).attr('src',$(listElements[i]).attr('href')).css('position','relative');
          	
            if (jQuery.browser.msie) {
                $(listElements[i]).get(0).removeAttribute('href');
            } else {
                $(listElements[i]).removeAttr('href'); 
            }  
                  	
      		$(listElements[i]).bind('click', function(){
          		showImage(i);
          	});
          	          	          	
      	});
        
        $(window).load(function () {
          showImage(settings.actual);     
        });
        
        if (listElements.length * 103 < settings.width) {         
            $('.archerImageGallery').css('width', (listElements.length * 103) + 'px');
            $('.archerImageGallery').css('left', ($(mainHolder).width() /2 - $('.archerImageGallery').width() / 2) + 'px');
        } else {
            $('.archerImageGallery').css('width', settings.width + 'px');
            $('.archerImageGallery').css('left', ($(mainHolder).width() /2 - $('.archerImageGallery').width() / 2) + 4 + 'px');
        }                               
            
        	
    }
    
 }
