/**
 * jGallery
 * Project page - http://oseriale.ru/
 * Copyright (c) 2009 Anton Taranovskiy, http://altname.ru
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 * Version 0.1 (12.12.2009)
 */

(function($) {

$.fn.jgallery = function(options) {
    var defaults = {
        pos: 'h',
        items: 3,
        slidei: 2,
        init: 0
      };
    var opts = $.extend(defaults, options);

return this.each(function() {
    $this = $(this);
    var o = $.meta ? $.extend({}, opts, $this.data()) : opts;
      
    if (o.init) {
        $this.wrapInner($('<div class="jgallery-items"></div>'));       
        $this.find('a').each(function () {
            $(this).wrap('<div class="jgallery-item"></div>');
        });      
    }
        
    j=$this.find('div.jgallery-item img');
    var obj = new Object();
    obj.c=j.length;
    j=j.eq(0);
    obj.w=j.width()+14;
    obj.h=j.height()+2;
        
        
        
        function wpx(x) { return obj.w*x+'px'; }
        function hpx(x) { return obj.h*x+'px'; }
        
        $this.addClass(o.pos=='h'?'horisontal':'vertical');
        
        if (o.pos=='h') {
            
            $('div.jgallery-belt').width(obj.w*obj.c);
            
            $('<a href="javascript:;" class="jgallery-prev"></a>').click(function () {
                $el=$this.find('div.jgallery-belt');
                $el.stop();
                pt=Math.abs(parseInt( $el.css('left') ));
                if (pt > 0 ) {
                    t=pt - obj.w*o.slidei;
                    if ( t < 0 ) t=0;
                    $el.animate({left: '-'+t + 'px' }, "fast");
                }
            }).prependTo($this);
            
            $('<a href="javascript:;" class="jgallery-next"></a>').click(function () {
                $el=$this.find('div.jgallery-belt');
                $el.stop();
                pt=Math.abs(parseInt( $el.css('left') ));
                w=$el.width();
                iw=$this.find('div.jgallery-items').width();
                if ( pt < w+iw ) {
                    t=pt + obj.w*o.slidei;
                    if ( t >= w-iw ) t=w-iw;
                    $el.animate({left: '-'+t + 'px' }, "fast");
                }
            }).appendTo($this);
        
        } else {
            $('div.jgallery-belt').height(obj.h*obj.c);
            $('<a href="javascript:;" class="jgallery-prev"></a>').click(function () {
                $el=$this.find('div.jgallery-belt');
                $el.stop();
                pt=Math.abs(parseInt( $el.css('top') ));
                if (pt > 0 ) {
                    t=pt - obj.h*o.slidei;
                    if ( t < 0 ) t=0;
                    $el.animate({top: '-'+t + 'px' }, "fast","swing");
                }
            }).prependTo($this);
            
            $('<a href="javascript:;" class="jgallery-next"></a>').click(function () {
                $el=$this.find('div.jgallery-belt');
                $el.stop();
                pt=Math.abs(parseInt( $el.css('top') ));
                w=$el.height();
                iw=$this.find('div.jgallery-items').height();
                if ( pt < w+iw ) {
                    t=pt + obj.h*o.slidei;
                    if ( t >= w-iw ) t=w-iw;
                    $el.animate({top: '-'+t + 'px' }, "fast","swing");
                }
            }).appendTo($this);
        }


      
    });

  };
  
})(jQuery);