/**
 * jQueryController plugin for productlist
 * 
 * @autor Administrator
 * @date 28 jan 2009
 */
( function($) {
    productlist = jQueryController.init('productlist');

    /**
	 * Example action
	 */
    productlist.sel = {};
  
    productlist.actions.addToSelection= function() {
        productlist.sel = $("#ThumbnailContainer").Sel();
        // disable the default links
        $("#ThumbnailContainer").find('a').click(function(e) {
            e.preventDefault();
            return false;
        });
        $().bind('selection.limit', function(e,o,f){
            if (f < 0)
            {
                //document.location = $('.nav-prev').find('a').attr('href');
                document.location = $('.nav-prev').find('a').attr('href');
            }
            if (f > 0)
            {
                document.location = $('.nav-next').find('a').attr('href');
            }


        });

        $().bind("selection.add",function (e,o) {
            if (typeof(o.attr('pid')) == "undefined") {
                return true;
            }
			
            $('#leftContent').load('/frontend.php/product/show/id/' + o.attr('pid'),{},function () {
                productlist.actions.enableToggleImages();
                productlist.actions.addToBasket();
                productlist.actions.enableNav();
            });
			
			
            // 1. ajax load the product into the dom.
            //$("#leftContent").html(html);
            // 2. attach event listeners for:
            // 1. adding things to the shopping basket
            // 2. viewing thumbnails
            // 3. an overlay?
            return true;
        });
                    console.log(ProductPage);
            // global defined in template;
            // ugly hack.
            if (ProductPage > 1)
            {
                $("#ThumbnailContainer").find('a:first').mousedown();
            }
    };

    productlist.actions.enableNav = function ()
    {
        $('#nav-next').click(function (e)
        {
            productlist.sel.key.handleKey(e,1);
        });
        $('#nav-prev').click(function (e)
        {
            productlist.sel.key.handleKey(e,-1);
        });


    }

    productlist.actions.enableToggleImages = function () {
        $('#productSmallThumbnailsContainer').find('a').click(function(e) {
            e.preventDefault();
            var parent = $(this).parent().get(0);
            var index = $(this).parent().parent().children().index(parent);
            productlist.methods.showImage(index);
            return false;
        });
        $('.productSmallThumbnail').click(
            function() {
                productlist.methods.showImage($(this).parent().index(this));
            }
            );

        $("a.thumbgroup").fancybox({
            'overlayShow': true
        });
        $('#productMediumThumbnail ul li').hide();
        $('#productMediumThumbnail ul li:first').addClass('current').show();
        productlist.methods.showImage(0);
    };
    productlist.methods = {};
    productlist.methods.showImage = function (id) {
        if ($('#productMediumThumbnail ul li').eq(id).hasClass('current'))
        {
            clearTimeout(productlist.timeout);
            productlist.timeout = setTimeout(productlist.methods.showNextImage, 3500);
        }
        if (!($('$#productMediumThumbnail ul li').eq(id).is(':visible')))
        {
            $('#productMediumThumbnail ul li.current').fadeOut(300, function ()
            {
                $('#productMediumThumbnail ul li.current').removeClass('current').hide();
                $('#productMediumThumbnail ul li').eq(id).addClass('current').fadeIn(500);
            });
        }
        $('#productSmallThumbnailsContainer').find('.thumb_square').removeClass('thumb_square_selected');
        $('#productSmallThumbnailsContainer').find('.thumb_square').eq(id).addClass('thumb_square_selected');
        clearTimeout(productlist.timeout);
        productlist.timeout = setTimeout(productlist.methods.showNextImage, 3500);
    };
    productlist.methods.showNextImage = function () {
        var current = $('#productMediumThumbnail ul li.current').get(0);
        var currentIdx = $('#productMediumThumbnail ul li').index(current);
        var next = $('#productMediumThumbnail ul li').eq(currentIdx+1);
        var nextIdx = currentIdx+1;
        if (next.size() == 0)
        {
            next = $('#productMediumThumbnail ul li').eq(0);
            nextIdx = 0;
        }
        productlist.methods.showImage(nextIdx);
    };

    productlist.actions.addToBasket = function () {
        $('#orderConfirm').jqm({
            trigger: $('#orderButton').parent(),
            ajax: $('#orderButton').parent().attr('href'),
            target: 'div.jqmAlertContent',
            overlay: 50,
            onLoad: function()
            {
                $('#jqAlertConfirm').click(function()
                {
                    var colorId = $('option:selected','#confirmOrder');
                    var append = '';
                    if (colorId.size() > 0)
                    {
                        append = '?color_id=' + colorId.val();
                    }
                    $.get($('#orderTarget').val() + append);
                    $('#orderConfirm').jqmHide()
                });
                $('#jqAlertConfirmBasket').click(function()
                {
                    var colorId = $('option:selected','#confirmOrder');
                    var append = '?forward=true';
                    if (colorId.size() > 0)
                    {
                        append = append + '&color_id=' + colorId.val();
                    }
                    document.location = $('#orderTarget').val() + append;
                    $('#orderConfirm').jqmHide();
                });

            }
        });

        $('#orderButton').parent().click(function(e) {
            e.preventDefault();
            // $('#product_list').animate({
            //     "opacity": "show"
            // }, "slow", "easein");
            return false;
        }
        );
    };
})(jQuery);




//$("#rightContent").update();