//Inicjalizacja elementów
$(document).ready(function() {
    //Zamieszczanie plikow
    
    $('#clipboard').html( '<br /><p class="c"><img src="fileadmin/templates/main/images/ajax-loader.gif" /></p><br />' );
    getClipboard();
});


var trips = Array();

function evalOferta( c ){
    
    var mod = c % 10;
    
    if( mod == 0 || (c >= 10 && c <= 19) )
        return "ofert";
    
    if( c == 1 )
        return "oferta";
    
    if( mod >= 2 && mod <= 4  )
        return "oferty";
    
    return "ofert";
    
}

function getClipboard(){
    $.ajax({
        type		: "GET",
        cache       : false,
        url		: 'index.php?type=31267&action=get',
        dataType    : 'json',
        success: function(data) {
            if(data.status != null && data.status == "ok"){
                if( data.count == "0" )
                    $('#noclipboard').removeClass('hidden');
                    
                $('#oferta').html(evalOferta( data.count ));
                $('#clipboardnum').html( data.count );
                $('#clipboard').html(data.html);
            }else{
                //replace brak lub juz jest
                $('#noclipboard').removeClass('hidden');
                $('#clipboard').html("");
            }
        }
    });
}

function delClipboard(uid){
    $(".trip-" + uid).fadeOut(150,function(){
        $(this).remove();
    });
    
    if( trips[uid] != 1 ){
        trips[uid] = 1;
        $.ajax({
            type		: "GET",
            cache       : false,
            url		: 'index.php?type=31267&action=del&uid=' + uid,
            dataType    : 'json',
            success: function(data) {
                if(data.status != null && data.status == "ok"){
                    var c = parseInt($('#clipboardnum').html()) - 1;
                    if( c <= 0){
                        $('#noclipboard').removeClass('hidden');
                        c = 0;
                    }
                    $('#oferta').html(evalOferta( c ));
                    if($("#rcount").length>0){
                        $('#rcount').html(c);
                    }
                    $('#clipboardnum').html(c);

                }else{
            //replace brak lub juz jest
            }
            }
        });
    }
}

function addToClipboardSingleView(uid, obj){

    var details = $('.trip_id');
    var tempString = $(details).attr('id');
    var dataArray = tempString.split('_', 3);
    
    var componentsArray = new Array();
    $('#optionalComponentsBox dl:visible input:checked').each(function(index){
        var componentArray = new Array();
        componentArray = $(this).val();
        componentsArray[index] = componentArray;
    });
    
    var childrenArray = new Array();
    var indexMain = 0;
    $('#childAgeOffer input').each(function(index){
        var childArray = new Array();
        childArray = $(this).val();
        childrenArray[index] = childArray;
    });
    
    var adult = $('#adultSelectCounter').val();
    var children = $('#childrenSelectCounter').val();

    
    $.ajax({
        type		: "POST",
        cache       : false,
        url		: 'index.php?type=31267&action=add&sv=1&uid=' + uid,
        dataType    : 'json',
        data: ({
            'transport' : $('#departureSelect').val(),
            'selected_components' : componentsArray,
            'personBirthday' : childrenArray,
            'adults' : adult[0],
            'children'  : children[0],
            'hoteluid' : $('#hoteluid').val()
        }),
        success: function(data) {
            if(data.status != null && data.status == "ok"){
                var c = parseInt($('#clipboardnum').html()) + 1;
                $('#noclipboard').addClass('hidden');
                $('#clipboardnum').html(c);
                $('#clipboard').html($('#clipboard').html() + data.html);
                $('#oferta').html(evalOferta( c ));
                //tutaj dodano do schowka
                showAddingMsg(obj, "Dodano do schowka", 1, uid);
            }else if( data.status != null && data.status == "info" ){
                showAddingMsg(obj, "Wycieczka już<br />jest w schowku", 0, uid);
            }
        }
    });
    
}

function addToClipboardResultList(uid, obj, flight){
    trips[uid] = 0;
    var $_GET = getQueryParams(document.location.search);
    
    if( typeof(flight) != 'undefined' ){
        $_GET['search[flight_uid]'] = flight;
    }
    
    $.ajax({
        type		: "POST",
        cache       : false,
        url		: 'index.php?type=31267&action=add&uid=' + uid,
        dataType    : 'json',
        data    : $_GET,
        success: function(data) {
            if(data.status != null && data.status == "ok"){
                var c = parseInt($('#clipboardnum').html()) + 1;
                $('#noclipboard').addClass('hidden');
                $('#clipboardnum').html(c);
                $('#clipboard').html($('#clipboard').html() + data.html);
                $('#oferta').html(evalOferta( c ));
                //tutaj dodano do schowka
                showAddingMsg(obj, "Dodano do schowka", 1, uid);
            }else if( data.status != null && data.status == "info" ){
                showAddingMsg(obj, "Wycieczka już<br />jest w schowku", 0, uid);
            }
        }
    });
}


/**
 * Pobieranie parametrow GET
 */
function getQueryParams(qs) {
    qs = qs.split("+").join(" ");
    var params = {},
        tokens,
        re = /[?&]?([^=]+)=([^&]*)/g;

    while (tokens = re.exec(qs)) {
        params[decodeURIComponent(tokens[1])]
            = decodeURIComponent(tokens[2]);
    }

    return params;
}
