jQuery(function(){
    var cat_items = jQuery('#photogallery');
    var pos, prev, next, next_item, items_hidden, i_n, n, n1, curr, curr1, next, time;
	var zoom_in_div_fade_time = 250;



    if (jQuery(cat_items).length != 0) {
       // jQuery('.i0, .i1, .i3', cat_items).css('opacity', '0.5');

	    /*
        prev = jQuery('#prev', "#pgallery");
        next = jQuery('#next', "#pgallery");
		*/
		prev = jQuery('#prev', cat_items);
        next = jQuery('#next', cat_items);
		
        /* left, z-index, top, width, height */
       /*pos = new Array(
            new Array(0, 0, '100px', '137px', '245px'),
            new Array(0, 10, '100px', '137px', '245px'),
            new Array('50px', 50, '66px','174px','310px'),
            new Array('167px', 100, '0px', '250px','445px'),
            new Array('360px', 50, '66px','174px','310px'),
			new Array('440px', 10, '100px', '137px', '245px'),
            new Array('440px', 0, '100px', '137px', '245px')
        );*/
	   /* left, z-index, top, width, height, display */
		pos = new Array(
            new Array(0, 0, '100px', '137px', '245px'),
            new Array(0, 10, '100px', '137px', '245px'),
            new Array('15%', 50, '66px','174px','310px'),
            new Array('35%', 100, '0px', '250px','445px'),
            new Array('66%', 50, '66px','174px','310px'),
			new Array('82%', 10, '100px', '137px', '245px'),
            new Array('82%', 0, '100px', '137px', '245px')
        );
        i_n = jQuery('.im', cat_items);
		d_n = jQuery('.descr', cat_items);
		
/*        jQuery(i_n).css('opacity', 0.5).hide();

        for (var i = 0; i < 4; i++) {
            jQuery(i_n).eq(i).css({
                left: pos[i+1][0],
                marginLeft: pos[i+1][1],
                display: 'block'
            });
        }

        jQuery(i_n).eq(4).css({
            left: pos[5][0],
            marginLeft: pos[5][1]
        });

        jQuery(i_n).eq(i_n.length - 1).css({
            left: pos[0][0],
            marginLeft: pos[0][1]          
        });

        jQuery('.cat_images').show();*/

        curr = 0;
        next_item = 3;
        time = 0;
        
		jQuery(i_n).click(function(){
			var num = jQuery(i_n).index(jQuery(this));
			//alert('num='+num+' curr='+curr);
			if (num == (curr+1)%i_n.length) {prevPic();}
			else if (num == (curr)%i_n.length) {prevPic();}
			else if (num == (curr+2)%i_n.length) {  PopUp(this); }
			else if (num == (curr+3)%i_n.length) {nextPic();}
			else if (num == (curr+4)%i_n.length) {nextPic2();}
			//nextPic();
		});
		
		
		$("#zoom_in_div").mouseenter(function(){
			$(this).addClass("zoom_in_div_no_opacity");
		});
		$("#zoom_in_div").mouseleave(function(){
			$(this).removeClass("zoom_in_div_no_opacity");
		});
		$("#zoom_in_div").click(function(){
			var object = $("#photogallery .im").eq((curr+2)%i_n.length);
			PopUp(object);
		});

        /* next */
        jQuery(next).click(function() {
			nextPic();
        });
		
        /* prev */
        jQuery(prev).click(function() {
			prevPic();
        });
		
		/* next function */
        function nextPic() {
			//alert('curr='+curr);
            if (!time) {
                time = 1;
                //jQuery(i_n).eq((curr + 5) % i_n.length).css('display', 'block');
				$("#zoom_in_div").hide();
                for (var i = 0; i < 7; i++) {
					//alert(i);
                    jQuery(i_n).eq((curr + i) % i_n.length).animate({
                        left: pos[i][0],                       
                        top: pos[i][2],
						width:pos[i][3],
						height:pos[i][4]
                    }, 500, 'linear', function(){
                        if (time) {
                            jQuery(i_n).eq(curr).css('display', 'none');
                          //  jQuery('#cat_items_photos .item_images').hide();
                            curr = (curr + 1) % i_n.length;
							$("#zoom_in_div").show();
                        }
                        time = 0;
                    });
					jQuery(i_n).eq((curr + i) % i_n.length).css('z-index',pos[i][1]);
					jQuery(d_n).hide();
					jQuery(d_n).eq((curr + 3) % i_n.length).show();
                }
            }
        }
		
		function nextPic2() {
			//alert('curr='+curr);
            if (!time) {
                time = 1;
                //jQuery(i_n).eq((curr + 5) % i_n.length).css('display', 'block');
				$("#zoom_in_div").hide();
                for (var i = 0; i < 7; i++) {
					//alert(i);
                    jQuery(i_n).eq((curr + i) % i_n.length).animate({
                        left: pos[i][0],                       
                        top: pos[i][2],
						width:pos[i][3],
						height:pos[i][4]
                    }, 500, 'linear', function(){
                        if (time) {
                            jQuery(i_n).eq(curr).css('display', 'none');
                          //  jQuery('#cat_items_photos .item_images').hide();
                            curr = (curr + 1) % i_n.length;
                            $("#zoom_in_div").show();
                        }
                        time = 0;
                    });
					jQuery(i_n).eq((curr + i) % i_n.length).css('z-index',pos[i][1]);
					jQuery(d_n).hide();
					jQuery(d_n).eq((curr + 3) % i_n.length).show();
                }
				
            }
        }
		
		/* prev function */
		function prevPic() {
            if (!time) {
                time = 1;
                //jQuery(i_n).eq((curr + i_n.length - 1) % i_n.length).css('display', 'block');
				$("#zoom_in_div").hide();
                for (var i = 0; i < 7; i++) {
					
                    jQuery(i_n).eq((curr + i_n.length - 1 + i) % i_n.length).animate({
                        left: pos[(i + 1) % 7][0],                      
                        top: pos[(i + 1) % 7][2],
						width:pos[(i + 1) % 7][3],
						height:pos[(i + 1) % 7][4]
                    }, 500, 'linear', function(){
                        if (time) {
                            jQuery(i_n).eq((curr + 4) % i_n.length).css('display', 'none');
                            curr = (curr + i_n.length - 1) % i_n.length;
							$("#zoom_in_div").show();
                        }
                        time = 0;
                    });
					jQuery(i_n).eq((curr + i_n.length - 1 + i) % i_n.length).css('z-index',pos[(i + 1) % 7][1]);
					jQuery(d_n).hide();
					//alert(curr + i_n.length - 1 + i);
					jQuery(d_n).eq((curr + i_n.length + 1) % i_n.length).show();
                }
				
            }
        }
		
		var win;
		function PopUp(object) 
		{
			var popH=655;
			$.browser.chrome = /chrome/.test(navigator.userAgent.toLowerCase());
			if ($.browser.chrome || $.browser.safari) popH += 25;
			var popW=370;
			var href= jQuery(object).attr('big');
			var picdetail = jQuery(object).attr('picdetail');

			var wTop = (screen.height - popH) / 2;
			var wLeft = (screen.width - popW) / 2;

			var parStr = "height=" + popH + ",width=" + popW + ",left=" + wLeft + ",top=" + wTop + ",resizable=yes,toolbar=no,location=no,directories=no,scrollbars=no,status=no";
			win = window.open(picdetail,'imageupload',parStr);
		}
		
    }
	
	$('.fabrics_catalog_img, .palantines_catalog_img').click(function(){
		
		var popH = parseInt($(this).attr('big_height'));
		var popW = parseInt($(this).attr('big_width'));
		
		$.browser.chrome = /chrome/.test(navigator.userAgent.toLowerCase());
		if ($.browser.chrome || $.browser.safari) popH += 25;
		var href= $(this).attr('href');

		var wTop = (screen.height - popH) / 2;
		var wLeft = (screen.width - popW) / 2;

		var parStr = "height=" + popH + ",width=" + popW + ",left=" + wLeft + ",top=" + wTop + ",resizable=yes,toolbar=no,location=no,directories=no,scrollbars=no,status=no";
		win = window.open(href,'imageupload',parStr);
	});
});
