var productRating = 0;
var productId = 0;
var productVoted = false;
var recommendedPages = 0;

$(function() {
    $('.Detail_Form_Txt, .Search input[type="text"]').dsDefaultValue();
    $('a[rel="product_images"]').fancybox();

    $('.banners').cycle({
        fx: 'fade',
        timeout: '5000'
    });


    /************* detail produktu ****************/
    
    // hodnotenie produktov
    $('.Rating a').hover(function() {
       if (productVoted == false) {
           var cur = $(this).attr('id').substring(5);
           Shop.setStarRating(cur);
       }
    }, function() {
       Shop.setStarRating(productRating);
    }).click(function() {
        if (productVoted == false) {
            var cur = $(this).attr('id').substring(5);
            $.get(
               Shop.baseUrl+'detail/rating',
               {id: productId, rating: cur},
               function(data) {
                   if (data && data.status == 'ok') {
                       productVoted = true;
                       productRating = data.rating;
                       Shop.setStarRating(productRating);
                       $('#rating_users').html(data.ratingUsers);
                   }
               },
               'json'
           );
       }
       return false;
    });

    Shop.setStarRating(productRating);

    // otazky o produkte
    $('#frm_detail_contact .Detail_Formt_Submit').click(function() {
        var $frm = $('#frm_detail_contact');
        $frm.find('.Detail_Formt_Submit').hide();
        $frm.find('.Loader').show();

        $.ajax({
            type: 'POST',
            url: Shop.baseUrl+'detail/contact',
            data: $frm.serialize(),
            success: function (data) {
                if (data != null && data.status != null) {
                    if (data.status == true) {
                        alert("Vaša otázka bola úspešne odoslaná.");
                        $frm.get(0).reset();
                    } else {
                        alert("Pri odosielaní otázky došlo k nasledujúcim chybám:\n\n" + data.errors.join("\n"));
                    }
                } else {
                    alert("Chyba pri odosielaní otázky.");
                }
                $('#frm_detail_contact .Detail_Formt_Submit').show();
                $('#frm_detail_contact .Loader').hide();
            },
            dataType: 'json'
        });

        return false;
    });

    // komentare
    $('#frm_detail_comment .Detail_Formt_Submit2').click(function() {
        var $frm = $('#frm_detail_comment');
        $frm.find('.Detail_Formt_Submit2').hide();
        $frm.find('.Loader').show();

        $.ajax({
            type: 'POST',
            url: Shop.baseUrl+'detail/addcomment',
            data: $frm.serialize(),
            success: function(data) {
              if (data != null && data.status != null) {
                  if (data.status == true) {
                      Shop.showComments(productId, 1);
                      $frm.get(0).reset();
                  } else {
                      alert("Pri odosielaní príspevku došlo k nasledujúcim chybám:\n\n" + data.errors.join("\n"));
                  }
              } else {
                  alert('Došlo k chybe pri odosielaní príspevku.');
              }
              $frm.find('.Detail_Formt_Submit2').show();
              $frm.find('.Loader').hide();
            },
            dataType: 'json'
        });        
        return false;
    });

    $('#comments_container .Pages a').click(Shop.commentPage);

});

// nacitavanie komentarov
Shop.showComments = function(pid, p) {
    $.get(
        Shop.baseUrl+'detail/comments',
        {'pid': pid, 'p': p},
        function (data) {
            if (data) {
                $('#comments_container').html(data.html);
                $('#comments_container .Pages a').click(Shop.commentPage);
            }
        },
        'json'
    );
}

Shop.commentPage = function() {
    var page = $(this).html();
    Shop.showComments(productId, page);
    return false;
}

// hviezdicky hodnotenia
Shop.setStarRating = function(stars) {
    $('.Rating a').removeClass('Active');
    for (var i = 1; i <= stars; i++) {
        $('.Rating a#star_'+i).addClass('Active');
    }
}
