(function ($) {
    var orders = [];
$(document).ready(function(){
    var 
        date = new Date(),
        time = date.getHours() * 60 + date.getMinutes();
    
    renderOrders(time,1);
   
    setInterval(
        function(){
            var 
                d = new Date(),
                t = d.getHours() * 60 + d.getMinutes();
            renderOrders(t);
        },
        60*1000
    );
 });
 
  function renderOrders(time, first){
     $.ajax({
         url: "http://ungabunga.netau.net/lastorders.php",
         dataType: "jsonp",
         data: {"time":time},
         success: function(data){
             data = $.parseJSON(data);
             var itemsToAdd = subtr(orders, data);
             if (first !== undefined) {
                 itemsToAdd.push(itemsToAdd[itemsToAdd.length-1]);
                 itemsToAdd.reverse();
             }
             $.each(itemsToAdd,function(key, item){
                 animateItem(item);
             });
             orders = data;
         },
         error: function(e){
             console.log(e);
         }
     });     
 }

/**
 * Compares two order objects
 */
 function compare(o,n){
     var eq = true;
     $.each(o,function(key){
         if (o[key]!=n[key]){
             eq = false;
             return false;
         }
     });
     return eq;
 }
 
 /**
  * Founds and returns new items from new list of orders
  * compared with the old one
  */
 function subtr(o, n){
     var itemsToAdd = [];
     if (o.length){
         $.each(n, function(key, newItem){
             var exists = false;
             $.each(o, function(key, oldItem){
                if(compare(newItem, oldItem)){
                    exists = true;
                    return false;
                } 
             });
             if (!exists){
                 itemsToAdd.push(newItem);
             }
             else{
                 return false;
             }
         });
     }
     else{
         itemsToAdd = n;
     }
     
     return itemsToAdd;
 }

/**
 * Returns cars list filtered by country
 */
 function filterCarList(filter, list){
     var newList = [];
     
     $.each(
        list,
        function(index, obj){
            if (obj.country == filter)
                newList.push(obj);
        });
    
    return newList;
 }

/**
 * Returns time in minutes
 */
function timeToStamp(time){
    time = time.split(":");
    return parseInt(time[0])*60 + parseInt(time[1]);
}

function stampToTime(stamp){
    var h = Math.floor(stamp / 60);
    var m = stamp - h * 60;
    
    h = (h < 10)?"0"+h:h;
    m = (m < 10)?"0"+m:m;
    
    return h+":"+m;
}
/**
 * Formats the order and animates
 */
function animateItem(order){
    var container = $('.container');
    var output = '';
    
    output += '<div class="order">';
    output += '<img src="images/flags/' + order.country + '.png" />';
    output += '<h4>' + order.car + '</h4>';
    output += '<span>i ' + order.city + ' kl. ' + stampToTime(order.time) + '</span>';
    output += '</div>';
    
    var newItem = $(output);
    
    if (container.height()>112){
        newItem.hide().prependTo('.container').slideDown("slow");
        $('.container div:nth-child(6)').remove();
    }
    else{
        newItem.prependTo('.container');
    }
    
}

}(jQuery));
