/* ------------------------------------------------------------------------
	Class: prettyGallery
	Use: Gallery plugin for jQuery
	Author: Stephane Caron (http://www.no-margin-for-errors.com)
	Version: 1.1.1
------------------------------------------------------------------------- */

jQuery.fn.prettyGallery=function(settings)
{
    settings=jQuery.extend(
            {
            itemsPerPage:2,
            animationSpeed:'normal',
            navigation:'top',
            of_label:' of ',
            previous_title_label:'Previous page',
            next_title_label:'Next page',
            previous_label:'<img src="../images/aero_left.png" alt="Previous Page">',
            next_label:'<img src="../images/aero_right.png" alt="Next Page">',
            tagname:'first',
            genreName:''
            },settings);return this.each(function()
            {
          
            var currentPage=1,itemWidth=0,itemMargin=0,itemHeight=0,galleryWidth=0,pageCount=0,animated=false,
            $gallery=$(this);
            var prettyGalleryPrevious=function(caller)
                {
                if(animated||$(caller).hasClass('disabled'))return;
                animated=true;
                $gallery.find('li:lt('+(currentPage*settings.itemsPerPage)+')').each(function(i){$(this).animate({'left':parseFloat($(this).css('left'))+(galleryWidth+itemMargin)
                },settings.animationSpeed,
                function()
                {
                    animated=false;
                });
              });
              $gallery.find('li:gt('+((currentPage*settings.itemsPerPage)-1)+')').each(function(i)
                  {
                    $(this).animate({'left':parseFloat($(this).css('left'))+(galleryWidth+itemMargin)
                  },settings.animationSpeed);
              });
              currentPage--;_displayPaging();
              };
              
              var prettyGalleryNext=function(caller)
              {
                if(animated||$(caller).hasClass('disabled'))return;
                animated=true;
                $gallery.find('li:lt('+(currentPage*settings.itemsPerPage)+')').each(function(i)
                {
                    $(this).animate({'left':parseFloat($(this).css('left'))-(galleryWidth+itemMargin)},settings.animationSpeed,
                    function(){
                            animated=false;
                        });
                 });
                 $gallery.find('li:gt('+((currentPage*settings.itemsPerPage)-1)+')').each(function(i)
                 {
                    $(this).animate({'left':parseFloat($(this).css('left'))-(galleryWidth+itemMargin)},settings.animationSpeed);
                  });currentPage++;_displayPaging();};
                  var _formatGallery=function()
                  {
                  
                    itemWidth=$gallery.find('li:'+settings.tagname).css('position','absolute').width(); 
                    itemMargin+=($gallery.find('li:'+settings.tagname).css('margin-right')=='auto')?0:parseFloat($gallery.find('li:'+settings.tagname).css('margin-right'));
                    itemMargin+=($gallery.find('li:'+settings.tagname).css('margin-left')=='auto')?0:parseFloat($gallery.find('li:'+settings.tagname).css('margin-left'));
                    itemMargin+=($gallery.find('li:'+settings.tagname).css('padding-right')=='auto')?0:parseFloat($gallery.find('li:'+settings.tagname).css('padding-right'));
                    itemMargin+=($gallery.find('li:'+settings.tagname).css('padding-left')=='auto')?0:parseFloat($gallery.find('li:'+settings.tagname).css('padding-left'));
                    var itemSpacingTopBottom=0;
                    itemSpacingTopBottom+=($gallery.find('li:'+settings.tagname).css('margin-top')=='auto')?0:parseFloat($gallery.find('li:'+settings.tagname).css('margin-top'));
                    itemSpacingTopBottom+=($gallery.find('li:'+settings.tagname).css('margin-bottom')=='auto')?0:parseFloat($gallery.find('li:'+settings.tagname).css('margin-bottom'));
                    itemSpacingTopBottom+=($gallery.find('li:'+settings.tagname).css('padding-top')=='auto')?0:parseFloat($gallery.find('li:'+settings.tagname).css('padding-top'));
                    itemSpacingTopBottom+=($gallery.find('li:'+settings.tagname).css('padding-bottom')=='auto')?0:parseFloat($gallery.find('li:'+settings.tagname).css('padding-bottom'));
                    var itemHeight=$gallery.find('li:'+settings.tagname).height()+itemSpacingTopBottom;
                    var marginRight=($gallery.find('li:'+settings.tagname).css('margin-right')=='auto')?0:parseFloat($gallery.find('li:'+settings.tagname).css('margin-right'));
                    galleryWidth=(itemWidth+itemMargin)*settings.itemsPerPage-marginRight;
                    $gallery.css({'width':galleryWidth,'height':itemHeight,'overflow':'hidden','position':'relative','clear':'left'});
                    $gallery.find('li').each(function(i)
                        {
                            $(this).css({'position':'absolute','top':0,'left':i*(itemWidth+itemMargin)
                        });
                  });
                  $gallery.wrap('<div class="prettyGalleryContainer"></div>');
                 };
                 var _displayPaging=function()
                 {
                    $cg=$gallery.parents('div.prettyGalleryContainer:'+settings.tagname);
                    $cg.find('ul.pg_paging span.current').text(currentPage);
                    $cg.find('ul.pg_paging span.total').text(pageCount);
                    $cg.find('ul.pg_paging li a').removeClass('disabled');
                    if(currentPage==1)
                    {
                        $cg.find('ul.pg_paging li.pg_previous a').addClass('disabled');
                    }
                    else if(currentPage==pageCount)
                    {
                        $cg.find('ul.pg_paging li.pg_next a').addClass('disabled');
                    };
                 };
                 var _applyNav=function()
                 {
                    var template='';
                    template+='<ul  style="padding-bottom:25px;vertical-align:top;" class="pg_paging">';
                    template+=settings.genreName+'<li class="pg_next" style="padding-right:20px;padding-top:2px;"><a href="#" style="text-decoration:none;" title="'+settings.next_title_label+'">'+settings.next_label+'</a></li>';
                    template+='<li>&nbsp;<span class="current">1</span>'+'<span class="current1">&nbsp;of&nbsp;</span>'+'<span class="total current">1</span>&nbsp;</li>'; 
                    template+='<li class="pg_previous" style="padding-top:2px;"><a href="#" style="text-decoration:none;" title="'+settings.previous_title_label+'">'+settings.previous_label+'</a></li>';
                    template+='</ul>';
                    switch(settings.navigation)
                    {
                        case'top':$gallery.before(template);
                        break;
                        case'bottom':$gallery.after(template);
                        break;
                        case'both':$gallery.before(template);
                        $gallery.after(template);
                        break;
                    };
                    var $theNav=$gallery.parent('div.prettyGalleryContainer:'+settings.tagname).find('ul.pg_paging');
                    galleryBorderWidth=parseFloat($theNav.css('border-left-width'))+parseFloat($theNav.css('border-right-width'));
                    $theNav.width(galleryWidth-galleryBorderWidth);$theNav.each(function()
                    {
                        $(this).find('li:eq(1)').width(galleryWidth-galleryBorderWidth-parseFloat($(this).parent().find('ul.prettyNavigation li:'+settings.tagname).width())-parseFloat($(this).parent().find('ul.prettyNavigation li:'+settings.tagname).width()));
                    });
                    $theNav.find('li.pg_previous a').click(function()
                    {   
                        prettyGalleryPrevious(this);return false;
                    });
                    $theNav.find('li.pg_next a').click(function()
                    {
                        prettyGalleryNext(this);return false;
                    });
                   };
                   
                   if($(this).find('li').size()>settings.itemsPerPage)
                   { 
                    pageCount=Math.ceil($(this).find('li').size()/settings.itemsPerPage); 
                    _formatGallery();_applyNav();_displayPaging(this);currentPage=1;
                   };
               });
            };
       
