(function($) {

    $.fn.gal = function(options) {

        var options = $.extend({
            iWidth:500,
            iHeight:400,
            ratio:1.4,
            scale:0.9,
            ofset:30
        }, options || {});

        var con = this;

        var tWidth = $(con).find('.rot').width();
        var hWidth = tWidth / 2;

        var nWidth = options.iWidth * options.scale;
        var nHeight = options.iHeight * options.scale;
        var nTop = (options.iHeight - options.iHeight * options.scale) / 2;

        $(con).find('.act').css('left', '' + hWidth + 'px');
        $(con).find('.act').next().addClass('rnext').css({'display' : 'block', 'left' : '' + tWidth + 'px','opacity':'0.7','width':'' + nWidth + '','height':'' + nHeight + '','top':'' + nTop + 'px'});
        $(con).find('.act').prev().addClass('rprev').css({'display' : 'block', 'left' : '0px','opacity':'0.7','width':'' + nWidth + '','height':'' + nHeight + '','top':'' + nTop + 'px'});
        $(con).find('.act').css('left', '' + hWidth + 'px').css('marginLeft', '-' + options.iWidth / 2 + 'px').css('display', 'block');

        $(con).append('<div class="lshad">');
        $(con).append('<div class="rshad">');
        $(con).append('<div class="arn carn">');
        $(con).append('<div class="arp carp">');
        $(con).append('<div class="alt">');

        if(navigator.platform == 'iPad' || navigator.platform == 'iPhone' || navigator.platform == 'iPod')
            {
                $(con).find('.rot').bind("swipeleft", function(){
                      nt(con, options.iWidth, options.iHeight, nWidth, nHeight, nTop);
                  })
                  
                $(con).find('.rot').bind("swiperight", function(){
                      pv(con, options.iWidth, options.iHeight, nWidth, nHeight, nTop);
                })
            }
        

        var alt = $(con).find('.act').attr('alt');
        $(con).find('.alt').text(alt);

        $(con).find('.rshad').live('click', function() {
            if ($(con).find('.bact').length) {
                zmo(con, options.iWidth, options.iHeight, options.ratio);
                function fun() {
                    $(con).find(".rnext").click();
                }

                setTimeout(fun, 350);
            }
            else {
                $(con).find('.rnext').click();
            }
        });

        $(con).find('.rnext').live('click', function() {
            nt(con, options.iWidth, options.iHeight, nWidth, nHeight, nTop);
        });

        $(con).find('.carn').live('click', function() {
     
               nt(con, options.iWidth, options.iHeight, nWidth, nHeight, nTop);


        });
        
        $(con).find('.lshad').live('click', function() {
            if ($(con).find('.bact').length) {
                zmo(con, options.iWidth, options.iHeight, options.ratio);
                function fun() {
                    $(con).find(".rprev").click();
                }
                setTimeout(fun, 350);
            }
            else {
                $(con).find('.rprev').click();
            }
        });

        $(con).find('.rprev').live('click', function() {
            pv(con, options.iWidth, options.iHeight, nWidth, nHeight, nTop);
        });


        $(con).find('.carp').live('click', function() {
            pv(con, options.iWidth, options.iHeight, nWidth, nHeight, nTop);
        });


        $(con).find('.zum').live('click', function() {
              zm(con, options.iWidth, options.iHeight, options.ratio);
        });

        $(con).find('.act').live('click', function() {
            zm(con, options.iWidth, options.iHeight, options.ratio);
        });


        $(con).find('.rnext1').live('click', function() {
            zmo(con, options.iWidth, options.iHeight, options.ratio);
            function fun() {
                $(con).find(".rnext").click();
            }
            setTimeout(fun, 400);
        });

        $(con).find('.arn').live('click', function() {
            zmo(con, options.iWidth, options.iHeight, options.ratio);
            function fun() {
                 $(con).find(".rnext").click();
            }
            setTimeout(fun, 400);
        });

        $(con).find('.rprev1').live('click', function() {
            zmo(con, options.iWidth, options.iHeight, options.ratio);
            function fun() {
                $(con).find(".rprev").click();
            }
            setTimeout(fun, 400);
        });

        $(con).find('.arp').live('click', function() {
            zmo(con, options.iWidth, options.iHeight, options.ratio);
            function fun() {
                $(con).find(".rprev").click();
            }
            setTimeout(fun, 400);
        });


        $(con).find('.rprev1').live('click', function() {
            zmo(con, options.iWidth, options.iHeight, options.ratio);

        });

        $(con).find('.bact').live('click', function() {
            zmo(con, options.iWidth, options.iHeight, options.ratio);
        });

        wid(con, options.iWidth, options.ratio, options.scale, options.ofset);

        $(window).resize(function() {
            wid(con, options.iWidth, options.ratio, options.scale, options.ofset);
        });
    }

    function wid(con, iWidth, ratio, scale, ofset) {

        var nWidth = iWidth * scale;
        var zoomX = iWidth * ratio;
        var offX = zoomX - iWidth;
        offX = offX / 2;
        var tWidth = $(con).find('.rot').width();
        var hWidth = tWidth / 2;
        var bactLeft = hWidth - offX;

        var nextML = (tWidth - iWidth - ofset * 2) / 2;
        var prevML = nextML - nWidth;

        $(con).find('.rprev').css('marginLeft', '' + prevML + 'px');
        $(con).find('.rprev1').css('marginLeft', '' + prevML + 'px');

        $(con).find('.rnext').css('marginLeft', '-' + nextML + 'px').css('left', '' + tWidth + 'px');
        $(con).find('.rnext1').css('marginLeft', '-' + nextML + 'px').css('left', '' + tWidth + 'px');

        $(con).find('.act').css('left', '' + hWidth + 'px');
        $(con).find('.bact').css('left', '' + bactLeft + 'px');
    }

    function nt(con, iWidth, iHeight, nWidth, nHeight, nTop) {

        $(con).find('.arp').removeClass('carp');
        $(con).find('.arn').removeClass('carn');

        var alt = $(con).find('.rnext').attr('alt');
        $(con).find('.alt').text(alt);

        var tWidth = $(con).find('.rot').width();
        var hWidt = tWidth / 2;

        var posright = $(con).find('.rnext').css('marginLeft');
        var posleft = $(con).find('.rprev').css('marginLeft');

        var fst = $(con).find('.rot img:first').clone();
        $(con).find('.rot img:first').remove();
        $(con).find('.rot').append(fst);

        $(con).find('.rnext').next().css({'left':'' + tWidth + 'px','marginLeft':'' + nWidth + 'px','top':'' + nTop + 'px','width':'' + nWidth + 'px','height':'' + nHeight + 'px'});
        $(con).find('.rnext').next().show().stop().animate({
            left: '' + tWidth + 'px',
            marginLeft:'' + posright + '',
            opacity:0.7

        }, 1000, "easeOutCirc", function() {
            $(this).addClass('rnext').siblings().removeClass('rnext');
            $('.arp').addClass('carp');
            $('.arn').addClass('carn');
        });

        $(con).find('.rnext').removeClass('rnext').stop().animate({
            height:'' + iHeight + 'px',
            width:'' + iWidth + 'px',
            top:0,
            left: '' + hWidt + 'px',
            marginLeft:'-' + iWidth / 2 + 'px',
            opacity:1
        }, 1000, function() {
            $(this).addClass('act').siblings().removeClass('act');

        });

        $(con).find('.rprev').removeClass('rprev').stop().animate({
            left: '0px',
            marginLeft:'-' + nWidth + 'px',
            opacity:0.7
        }, 1000, function() {

        });

        $(con).find('.act').removeClass('act').stop().animate({
            height:'' + nHeight + 'px',
            width:'' + nWidth + 'px',
            top:'' + nTop + 'px',
            left: '0px',
            marginLeft:'' + posleft + '',
            opacity:0.7
        }, 1000, function() {
            $(this).addClass('rprev').siblings().removeClass('rprev');
        });

    }


    function pv(con, iWidth, iHeight, nWidth, nHeight, nTop) {

        $(con).find('.arp').removeClass('carp');
        $(con).find('.arn').removeClass('carn');

        var alt = $(con).find('.rprev').attr('alt');
        $(con).find('.alt').text(alt);

        var tWidth = $(con).find('.rot').width();
        var hWidth = tWidth / 2;

        var posright = $(con).find('.rnext').css('marginLeft');
        var posleft = $(con).find('.rprev').css('marginLeft');

        var lst = $(con).find('.rot img:last').clone();
        $(con).find('.rot img:last').remove();
        $(con).find('.rot').prepend(lst);

        $(con).find('.rprev').prev().css({'left':'0','marginLeft':'-' + nWidth + 'px','top':'' + nTop + 'px','width':'' + nWidth + 'px','height':'' + nHeight + 'px'});

        $(con).find('.rprev').prev().show().stop().animate({
            left: '0px',
            marginLeft:'' + posleft + '',
            opacity:0.7
        }, 1090, "easeOutQuad", function() {
            $(this).addClass('rprev').siblings().removeClass('rprev');
        });

        $(con).find('.rprev').removeClass('rprev').stop().animate({
            height:'' + iHeight + 'px',
            width:'' + iWidth + 'px',
            top:0,
            left: '' + hWidth + 'px',
            marginLeft:'-' + iWidth / 2 + 'px',
            opacity:1
        }, 1090, "easeOutCirc", function() {
            $(this).addClass('act').siblings().removeClass('act');

        });

        $(con).find('.rnext').removeClass('rnext').stop().animate({
            left: '' + tWidth + 'px',
            marginLeft:'' + iWidth + 'px',
            opacity:0.7
        }, 1090, function() {
            $('.arp').addClass('carp');
            $('.arn').addClass('carn');
        });

        $(con).find('.act').removeClass('act').stop().animate({
            height:'' + nHeight + 'px',
            width:'' + nWidth + 'px',
            top:'' + nTop + 'px',
            left: '' + tWidth + 'px',
            marginLeft:'' + posright + '',
            opacity:0.7
        }, 1090, "easeOutCirc", function() {
            $(this).addClass('rnext').siblings().removeClass('rnext');
        });
    }

    function zm(con, iWidth, iHeight, ratio) {
        $(con).find('.arp').removeClass('carp');
        $(con).find('.arn').removeClass('carn');
        zoomX = iWidth * ratio;
        zoomY = iHeight * ratio;

        var offX = zoomX - iWidth;
        offX = offX / 2;

        var offY = zoomY - iHeight;
        offY = offY / 2;

        $(con).find('.act').css('zIndex', '4').addClass('zuma');
        $(con).find('.rprev').addClass('rprev1').removeClass('rprev');
        $(con).find('.rnext').addClass('rnext1').removeClass('rnext');

        $(con).find('.act').animate({
            width: '' + zoomX + 'px',
            height:'' + zoomY + 'px',
            left: "-=" + offX + "px",
            top: "-=" + offY + "px"
        }, 300, function() {
            $(this).addClass('bact').removeClass('zuma');

        });
        $(con).find('.act').removeClass('act');
    }

    function zmo(con, iWidth, iHeight, ratio) {
        zoomX = iWidth * ratio;
        zoomY = iHeight * ratio;

        var offX = zoomX - iWidth;
        offX = offX / 2;

        var offY = zoomY - iHeight;
        offY = offY / 2;

        $(con).find('.bact').animate({
            width: '' + iWidth + 'px',
            height:'' + iHeight + 'px',
            left: '+=' + offX + '',
            top: '+=' + offY + ''
        }, 300, function() {
            $(this).removeClass('bact').addClass('act').css('zIndex', '1');
            $(con).find('.rprev1').addClass('rprev').removeClass('rprev1');
            $(con).find('.rnext1').addClass('rnext').removeClass('rnext1');
                   $(con).find('.arp').addClass('carp');
        $(con).find('.arn').addClass('carn');

        });
        $(con).find('.bact').removeClass('bact');
    }


})(jQuery);
