(function($) {

	$.fn.GallOp = function(options){
	  
		// default configuration properties
		var defaults = {			
            delta: 1.61,
            prevBid: "#prevB",
            nextBid: "#nextB",
            imgWidth: 480,
            imgHeight: 318,
		};
		var options = $.extend(defaults, options);  
		var main_obj = $(this);
		var m_w = main_obj.width();
		var m_h = main_obj.height();
		var items = $(".gallop_item", main_obj);
		var in_process = 0;
		function MoveObj(obj1, ind) {
		    in_process = 1;
		    var img_o = $('img',obj1);
		    index1 = obj1.css("z-index");
            var w = img_o.width();
            var h = img_o.height();
            var ww = w / (ind + 1) * options.delta;
            var hh = h / (ind + 1) * options.delta;
            if (ind == 0) {
                var sw = options.delta * 50;
                var sh = h - (h / options.delta);
                img_o.animate({width: w / 3, height: h / 3, opacity: 0.1, marginLeft: w / options.delta, marginTop: 0,}, 1 )
                .animate({width: w / 3, height: h / 3, opacity: 0.2, marginLeft: sw * 6.6, marginTop: h / 1.2,}, 500 )
                .animate({width: w, height: h, opacity: 1, marginLeft: sw, marginTop: sh, }, 1400 );
                $('.number_out').html($('.number_' + ind).html())
                $('.text_out').animate({opacity: 0.1,},500);
                setTimeout(function(){
                    $('.text_out').html($('.text_' + (ind + 1)).html()).animate({opacity: 1,},1000);
                },600);
            }
            else {
                var sw = (ww * ind) - (ww / options.delta / options.delta);
                var sh = (hh * (ind / options.delta));
                img_o.width(ww);
                img_o.height(hh);
                img_o.animate({width: ww / 3, height: hh / 3, opacity: 0.1, marginLeft: w + (w / (items.length / ind)), marginTop: 0,}, 1 )
                .animate({width: ww / 3, height: hh / 3, opacity: 0.2, 
                    marginLeft: w + (ww * ind / options.delta / options.delta) , 
                    marginTop: (hh / options.delta) * ind + (hh /2),}, 700 )
                .animate({width: ww, height: hh, opacity: 0.9, marginLeft: sw, marginTop: sh, }, 1600 );
            }
            img_o.css("margin-left",sw);
            img_o.css("margin-top",sh);
            setTimeout(function(){
		        in_process = 0;
		    },2000);
		};
		function Z_Ind(obj22, vect) {
		    var obj2_z = parseInt(obj22.css('z-index'));
            if (vect > 0) {
                setTimeout(function(){
                    if (obj2_z == 1) {
                        obj22.css('z-index', items.length);
                    };
                    if (obj2_z > 1 && obj2_z <= items.length) {
                        obj22.css('z-index',(obj2_z - vect));
                    };
                }, 500);
            }
            else {
                setTimeout(function(){
                    if (obj2_z >= 1 && obj2_z < items.length) {
                        obj22.css('z-index',(obj2_z - vect));
                    };
                    if (obj2_z == items.length) {
                        obj22.css('z-index', 1);
                    };
                }, 1000);
            };		        
		};
		$(items).each(function(index) {
		    var obj = $(this);
		    var obj_id = index + 1
		    obj.css("position","absolute");
			obj.css("overhead","hidden");
			obj.css("z-index",(items.length - index));
            obj.addClass('obj_' + obj_id);
			MoveObj(obj, index);
		});
		
		$(options.nextBid).css("cursor","pointer");
		$(options.prevBid).css("cursor","pointer");

		var item_i = 1;
		$(options.prevBid).click(function(){
            if (item_i > 1 && in_process == 0) {
                $(items).each(function(indx2){
                    in_process = 1;
                    var ii2 = indx2 + 1;
                    var idts = $('.obj_' + ii2);
                    var ppr = $('img', idts);
                    var h2 = ppr.height();
                    //alert(idts.attr('class'));
                    if (idts.hasClass('obj_' + items.length)){
                        ppr.animate({
                            width: options.imgWidth,
                            height: options.imgHeight,
                            marginLeft: options.delta * 50,
                            marginTop: h2 - (h2 / options.delta),
                            opacity: 1,
                        }, 1200);
                        idts.removeClass('obj_' + items.length).addClass('obj_' + (items.length + 1));
                        
                    }
                    else {
                        var aamw = $('.obj_' + (ii2 + 1) + ' img').css('margin-left');
                        var aamh = $('.obj_' + (ii2 + 1) + ' img').css('margin-top');
                        var aaw = $('.obj_' + (ii2 + 1) + ' img').width();
                        var aah = $('.obj_' + (ii2 + 1) + ' img').height();                            

                        if (idts.hasClass('obj_' + (items.length - 1))){
                            ppr.animate({
                            marginLeft: aamw,
                            marginTop: aamh,
                            width: aaw,
                            height: aah,
                            opacity: 0,
                            }, 1200);
                        }
                        else {
                            ppr.animate({
                            marginLeft: aamw,
                            marginTop: aamh,
                            width: aaw,
                            height: aah,
                            //opacity: 0,
                            }, 1200);
                        };
                        idts.removeClass('obj_' + ii2).addClass('t_obj_' + ii2);
                    };
                    Z_Ind(idts,1);
                    //alert(idts.attr('class'));
                });
                $('.obj_' + (items.length + 1)).removeClass('obj_' + (items.length + 1)).addClass('obj_' + 1);
                $(items).each(function(indx3){
                    var ii3 = indx3 + 1;
                    var t_obj1 = $('.t_obj_' + ii3);
                    if (t_obj1){
                        t_obj1.removeClass('t_obj_' + ii3).addClass('obj_' + (ii3 + 1));
                    };
                });
                setTimeout(function(){
                    $('.number_out').html($('.number_' + (item_i - 1)).html());
                }, 500);
    		    $('.text_out').animate({opacity: 0.1,},500);
    		    var p = item_i - 1;
                item_i = p;
    		    setTimeout(function(){
    		        $('.text_out').html($('.text_' + item_i).html()).animate({opacity: 1,},1000);
    		    },600);
    		    setTimeout(function(){
    		        in_process = 0;
    		    },1500);
            }
		});
		$(options.nextBid).click(function(){
		    var ppw = options.imgWidth;
 	        var pph = options.imgHeight;
 	        var ppml = 0;
 	        var ppmt = 0;
 	        if (item_i < items.length && in_process == 0) {
 	            in_process = 1;
		        $(items).each(function(indx){
		            var ii = indx + 1;
    		        idt = $('.obj_' + ii);
        		    var pp = $('.obj_' + ii + ' img');
        		    if (idt.hasClass('obj_' + 1)){
        		        pp.animate({marginLeft: "100",}, 500).animate({marginLeft: "-600", opacity: 0}, 600);
        		        ppml = pp.css("margin-left");
        		        ppmt = pp.css("margin-top");
        		        idt.removeClass('obj_' + 1).addClass('obj_' + (items.length + 1));
        		    }
        		    else {
        		        setTimeout(function(){
            		        pp.animate({
            		            marginLeft: ppml,
            		            marginTop: ppmt,
            		            width: ppw,
            		            height: pph,
            		            opacity: 0.99,
            		            }, 1200);
            		        ppw = pp.width();
                		    pph = pp.height();
                		    ppml = pp.css("margin-left");
                		    ppmt = pp.css("margin-top");
        		        }, 600);
        		        idt.removeClass('obj_' + ii);
            		    idt.addClass('obj_' + (ii - 1));
        		    };
            		Z_Ind(idt,-1);
		        });
		        $('.obj_' + (items.length + 1)).removeClass('obj_' + (items.length + 1)).addClass('obj_' + items.length);		        

    		    setTimeout(function(){
        		    $('.number_out').html($('.number_' + item_i).html());    		        
        		    item_i++;
    		    }, 500);
    		    $('.text_out').animate({opacity: 0.1,},500);
    		    setTimeout(function(){
    		        $('.text_out').html($('.text_' + item_i).html()).animate({opacity: 1,},1000);
    		    },600);
    		    setTimeout(function(){
    		        in_process = 0;
    		    },1500);
        	};
		});
    };
})(jQuery);
