/**
* @version		3.3.0
* @package		JomLand TicketCenter
* @copyright	Copyright (C) 2014 JomLand.com. All rights reserved.
* @license		GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
*/

(function($) {
    $( document ).ready(function() {

        $('#cart #cart_back_btn').on('click',function(e){
            e.preventDefault();
            e.stopPropagation();
            window.location = document.referrer;
        });

        //Cart Expired
        console.log("target-time =", jltc_cart.target_time);
        var target_time  = new Date().getTime() + jltc_cart.target_time;
            var timer_stop = false;

            function calculateCountdown(){

                var today  = new Date();
                var todayTime  = today.getTime();
                var target = new Date(target_time);
                var targetTime = target.getTime();

                var diff = targetTime - todayTime;

                var sec = Math.floor(diff / 1000);
                var min = Math.floor(diff / 60000);
                var h = Math.floor(diff / 3600000);
                var days = Math.floor(diff / 86400000);

                var hoursLeft = h - days * 24;
                var minsLeft = min - h * 60;
                var secsLeft = sec - min * 60;

                if (minsLeft  <10){
                    minsLeft  = "0"+minsLeft ;
                }
                if (secsLeft <10){
                    secsLeft = "0"+secsLeft;
                }
                $("#counter #countdown_sec").html(secsLeft);
                $("#counter #countdown_min").html("" + minsLeft);

                if (!timer_stop){
                    //var myTimer = calculateCountdown.delay(1000); //wait 5 seconds and execute my function.
                    setTimeout(calculateCountdown, 1000);
                }else{
                    $("#counter #countdown_sec").html("00");
                    $("#counter #countdown_min").html("00");

                    $.ajax({
                        url:  jltc_cart.cleanUrl,
                        type: "POST"
                    }).done(function() {
                        $("#cart").html(jltc_cart.emptyCartLabel);
                        alert (jltc_cart.expiredCartLabel);
                    });
                }
            }

            setTimeout(calculateCountdown, 1000);

        // CART UPDATE


        function refresh_cart(line_id,qty_value){
            var line_sum ='sum_'+line_id;
            var commission = 0;
            $('#TicketTotal').html('<img src=\"'+jltc_cart.spinnerUrl+'". />');
            $('#Total').html('<img src=\"'+jltc_cart.spinnerUrl+'". />');

            var url = '".JURI::base()."index.php?option=com_jlticketcenter&view=cart&format=raw;';



            var data = {'id':line_id,'qty':qty_value};
            data[jltc_cart.jToken] = 1;

            $.ajax({
                url: jltc_cart.refreshCartUrl,
                type: "POST",
                data: data
            }).done(function(response) {
                response = JSON.parse(response);
                if (response.length == 0){
                    $('#cart').html(jltc_cart.emptyCartLabel)
                    return;
                }

                if (response.length == 0){
                    var cart_row = '#row_'+line_id;
                    $(cart_row).html(jltc_cart.emptyCartLabel);
                }else{
                    $('#TicketTotal').html(response.Total);
                    commission = parseFloat(response.commission);
                    if ($('#Delivery_Price') == null){
                        var delivery = 0;
                    }
                    else{
                        var delivery = parseFloat($('#Delivery_Price').html());
                    }

                    var sum = parseFloat(response.Total)+ delivery+commission;

                    if(jltc_cart.tax_rate > 0){
                        var tax_total = sum * jltc_cart.tax_rate /100;
                        tax_total.toFixed(2);

                        sum = sum+tax_total;
                        $('#Tax').html(tax_total.toFixed(2));
                    }

                    $('#Total').html(sum.toFixed(2));
                    if($('#comission')!==null){
                        $('#comission').html(commission.toFixed(2));
                    }
                }

            });
        }

            $('.jltc_qty_select').each(function(inx, el) {
                $(el).on('change', function(e) {
                    e.preventDefault();

                    var event_id = this.id.substr(4);
                    var qty_value = this.value;

                    refresh_cart(event_id,qty_value);
                });
            });

            $('.del_row').each(function(inx,  el) {
                $(el).on('click', function(e) {
                    e.preventDefault();

                    var line_id = this.id.substr(4);
                    refresh_cart(line_id,0);

                    var line_pointer = '#row_'+line_id;
                    $(line_pointer).remove();
                });
            });



    });
})(jQuery);
