$(document).ready(function () {
    var gallerySkeleton = '<div class="slideshow" style="display:none"><div class="left" id="left-col">&nbsp;</div><div class="right" id="right-col">loading&hellip;</div><span class="clear"></span><a href="#" class="close-slideshow" title="chiudi slideshow">chiudi [x]</a></div>';
	
	function setCurrentItem() {
        $('.slideshow').find('#right-col').find('a').click(function () {
            if (!$(this).hasClass('current-pic')) {
                $(this).parent().parent().find('a').removeClass('current-pic');
                $(this).addClass('current-pic');
            }
        });
        $('.slideshow').find('#right-col').find('ul.products-menu').find('a').click(function () {
            $('.slideshow').find('#right-col').find('ul.products-submenu').find('a').removeClass('current-pic');
            $('.slideshow').find('#right-col').find('ul.products-submenu').find('a:first').addClass('current-pic');
        });
    }
	
	function loadItem(incomingPic){
        var $leftCol = $('.slideshow').find('#left-col');
		$leftCol.empty();
        $leftCol.addClass('loading');
        $leftCol.html('<img src="' + incomingPic + '" style="display:none" />');
        $leftCol.children('img').load(function() {
		//if ($leftCol.children('img').length) {
			$leftCol.removeClass('loading');
			$leftCol.children('img').fadeIn();
        //}
		});
	}
	
	function resetGallery(){
		$('.slideshow').fadeOut('fast',function(){
			$('#get-colors').show();
			$('.slideshow').remove();
			$('.article').fadeIn();
		});
	}
	
    function startGallery() {
        var $leftCol = $('.slideshow').find('#left-col');
        var $rightCol = $('.slideshow').find('#right-col');
        $rightCol.find('ul.products-submenu').find('a:first').addClass('current-pic');
        $rightCol.find('ul.products-submenu').fadeIn();
        $rightCol.find('ul.products-menu').find('a').click(function () {
            if (!$(this).hasClass('current-pic')) {
            	var incomingPic = $(this).attr('href');
				loadItem(incomingPic);                
            }
            return false
        });
        $rightCol.find('ul.products-submenu').find('a').click(function () {
            if (!$(this).hasClass('current-pic')) {
            	var incomingType = $(this).attr('rel');
            	var currentImg = $leftCol.children('img').attr('src');
            	var voidPath = currentImg.substr(0, currentImg.lastIndexOf("_") + 1);
            	var incomingPic = voidPath + incomingType + ".png";
				loadItem(incomingPic);                
            }
            return false
        });
		$('a.close-slideshow').click(function(){
			resetGallery();
		return false	
		});		
    }

    $('#get-colors').click(function () {						   
        $(this).hide();
        $(this).parent().parent().fadeOut('normal', function () {
            $('#section-pvd').append(gallerySkeleton);
            var $leftCol = $('.slideshow').find('#left-col');
            var $rightCol = $('.slideshow').find('#right-col');
            if ($('.slideshow').length) {
                $('.slideshow').fadeIn('normal', function () {
                    $leftCol.addClass('loading');
                    $rightCol.load("/include/pages/pvd-list.php", function () {
                        var firstImage = $(this).find('ul.products-menu').find('a:first').attr('href');
                        $leftCol.html('<img src="' + firstImage + '" style="display:none" />');
                        $leftCol.children('img').load(function() {
						//if ($leftCol.children('img').length) {
                            $leftCol.removeClass('loading');
                            $rightCol.find('ul.products-menu').fadeIn().find('a:first').addClass('current-pic');
                            $leftCol.children('img').fadeIn('normal', function () {
                                startGallery();
                                setCurrentItem();
                            });
                        //}
						});
                    });
                });
            }
        });
        return false
    });	
	
});