var deeb = {
    
    setURLs: function(v1, v2)
    {
        this.cartURL = v1;
        this.productURL = v2;
    },
    
    addtocart: function(id, attrs)
    {
        $('#cart-loader').show();
        $('#cart-wrap').hide();

        $.post(this.cartURL, 'id='+id+'&'+$('#product-form').serialize(), deeb.cartResponse, "json");

        $('html, body').animate({scrollTop: $('#cart').position().top}, 'slow');
    },

    cartResponse: function(data, status)
    {
        $('#cart-loader').hide();

        if (data.count == 0)
        {
            $('#cart-wrap').hide();
        } else {
            $('#cart-wrap').show();
            $('#cart-count').html(data.count);
        }

        if (data.error == 1)
        {
            alert('Nem létező termék!');
        } else if (data.error == 2) {
            alert('Több darab már nem helyezhető a kosárba a termékből!');
        } else {
            
            if (data.attrs != '') {
                $('#cart-recent-attrs').html('<br>'+data.attrs);
            } else {
                $('#cart-recent-attrs').html('');
            }
            
            $('#cart-recent').css('display', 'block');
            $('#cart-recent-title').html(data.title);
            $('#cart-content').html(data.content);
            $('#cart-recent-link').attr('href', deeb.productURL + data.slug);
            $('#cart-recent-price').html(data.price+ ' Ft');
            $('#cart-recent-item-count').html(data.count_item);
        }
    },

    getStore: function(form)
    {
        $.post("/?q=ajax", 'function=getStore&' + $(form).serialize(), function(data)
        {
            $('#stores').html(data);
        }, "json");
    },
    
    getStoreCity: function(form)
    {
        $.post("/?q=ajax", 'function=getStoreCity&' + $(form).serialize(), function(data)
        {
            $('#cities').html(data);
        }, "json");
    }

};
   
function mediaImages(el, numbers)
{
    var current = 0;

	var speed = 4000;
    var next = 1;
    var transitionSpeed = 1000;

    function cycleImages()
	{
        $(numbers[current]).removeClass("active");
        $(numbers[next]).addClass("active");

		$(el[current]).fadeOut(transitionSpeed);
        $(el[next]).fadeIn(transitionSpeed, function() {
            current = current + 1;
            if ( current > el.length - 1 ) current = 0;
            next = next + 1;
            if ( next > el.length - 1 ) next = 0;
        });
    }

    var interval = setInterval(cycleImages, speed);

    for (var i=0; i<el.length; i++)
    {
        if (i == 0) $(numbers[i]).addClass('active');
        if (i !== 0) $(el[i]).hide();

        var $el = $(numbers[i]);
        $el.attr('rel', i);

        $el.mouseover(function(){

			clearInterval(interval);

            $this = $(this);

            for (var j=0; j<numbers.length; j++)
            {
                $(numbers[j]).removeClass('active');
            }

            $this.addClass("active");
            var index = $this.attr("rel");

            if ( index != current ) {
                $(el[current]).stop().animate({ opacity: 0 }, function() {
                    $(this).hide();
                });
                $(el[index]).stop().css({ display: "block", opacity: 0 }).animate({ opacity: 1 });
            }
            current = index;
        });
    }

}

$(document).ready(function() {
	mediaImages($('#images ul.images li'), $('#images ul.infos li'))
});

