
/*
var _categories 	= [];
var _subcategories 	= [];
var _issues 		= [];
var _category     = null;
var _subcategory  = null;
var _issue        = null;
*/

$(document).ready( function() {

    $.selector = {};
    $.selector.item = null;
    $.selector.hints = null;

    $('#close_button').click(function(){removeModal()});

    function setDescription(source,to){
        var price = source.price==0?'N':source.price;
        $(to).html(source.description);
        $('.services').html(source.tree.replace(/>/g, ', '));
        if (price === 'N') {
            $("input[@name='flexibility']").val('T');
            $('#budget').html('<div id="msg_bugdet">'+budget[price]+'</div>');
        } else {
            $('#budget').html(budget[price]);
        }
        if(price == 'N'){
            $("input[@name='flexibility']").val('T');
            $('.configure div').hide(); $('#msg_bugdet').show();
        }else{
            //$("input[@name='flexibility']").val('T');
            $('.configure div').show();
        }
    }

    /*
    function actualizar(data) {
            if (data.issue != null) {$('#hintIssue').html(data.hints.issue)} else {$('#hintIssue').html('')}
            if (data.hints.subcategory != null) {$('#hintSubcategory').html(data.hints.subcategory)} else {$('#hintSubcategory').html('')}
            if (data.hints.category != null) {$('#hintCategory').html(data.hints.category)} else {$('#hintCategory').html('')}
            if ($('#hintCategory').text() !='') { $('hintCategory').show()} else {$('hintCategory').hide();}
            if ($('#hintSubcategory').text() !='') { $('hintSubcategory').show()} else {$('hintSubcategory').hide();}
            if ($('#hintIssue').text() !='') { $('hintIssue').show()} else {$('hintIssue').hide();}
            $( '.hints').show();
    }
    */
   
    function removeModal(){
        $.nyroModalRemove();
        if ( $.selector.item != null) {
            $('.requestB2C').stopTime('controlled');  // paramos los mensajes
            $('textarea[name="text"]').focus();       // establecemos el foco
            $('#id_category').val($.selector.item.pk) // asignamos la categoría elegida
            $('.textarea').unblock();                 // actualizar
            $('.configure').unblock();
            $('.errores').hide();
        } else {
            // si no se ha establecido categoria
            jQuery('.textarea').block({
                message: ''
            });
            jQuery('.configure').block({
                message: ''
            });
            $('.hints').hide();
            var default_msg = 'Read the examples below then click here to choose your service...';
            $('.services').html(default_msg);
        }
        if(jQuery.browser.version == '6.0'){
            setTimeout(function(){
                $('.requestB2C').focus();
            },900);
        }
    }

    $('a.services').nyroModal({
        //endShowContent: function (elts, settings) {alert('end show content');},
        endFillContent: function(elts, settings) {
            jQuery('.textarea').unblock();
            jQuery('.configure').unblock();
        },
        width:300,
        minWidth:null,
        minHeight:null,
        modal:true,
        resizable:true,
        autoSizable:false
    });

    $('#category_tree').fileTree({
      root: '',
      script: '/backoffice/categories/',
      expandSpeed: 1000,
      collapseSpeed: 1000,
      multiFolder: false
        }, function(file) {
            var id = -1;
            var pattern =/\/backoffice\/categories\/(\d+)\//;
            var result = file.match(pattern);
            if (result != null) {
                id = result[1];

               $.getJSON("/json/hints/"+id+"/", function(data){
                    $.selector.hints = data;
                });

                $.getJSON("/json/info/"+id+"/", function(data){
                    $.selector.item = data;
                    setDescription(data, '#hintCategory')
                    removeModal();
                    $('.hints').show();
                    $('#hintCategory').show();
                });
                if (id == 737)  {
                    $('#spamly_logo').attr('src', "/media/images/logos/media.gif");
                } else if (id == 736)  {
                    $('#spamly_logo').attr('src', "/media/images/logos/weddings.gif");
                    $('#slogan_upper').attr("src", "/media/images/banners/home_weddings.gif");
                } else if ((id == 727) || (id == 726)) {
                    $('#spamly_logo').attr('src', "/media/images/logos/travel_business.gif");
                } else if ( (id <= 372) && (id >= 361) && (id!=370)|| (id==757)) {
                    $('#spamly_logo').attr('src', "/media/images/logos/travel_personal.gif");
                } else if ((id == 370) || (id == 826)) {
                    $('#spamly_logo').attr('src', "/media/images/logos/yachts.gif");
                } else if ((id == 735) || (id == 734)) {
                    $('#spamly_logo').attr('src', "/media/images/logos/translation.gif");
                } else if ((id == 740) || (id == 741)) {
                    $('#spamly_logo').attr('src', "/media/images/logos/movers.gif");
                } else if ((id == 738) || (id == 739)) {
                    $('#spamly_logo').attr('src', "/media/images/logos/shipping.gif");
                } else if ((id == 655) || (id == 654)) {
                    $('#spamly_logo').attr('src', "/media/images/logos/architecture.gif");
                } else if ((id == 732) || (id == 733)) {
                    $('#spamly_logo').attr('src', "/media/images/logos/printing.gif");
                } else if ((id == 689) || (id == 696) || (id == 704) || (id == 712)) {
                    $('#spamly_logo').attr('src', "/media/images/logos/state_agents.gif");
                } else if ((id == 731) || (id == 730)) {
                    $('#spamly_logo').attr('src', "/media/images/logos/conference.gif");
                } else if ((id == 743) || (id == 729)) {
                    $('#spamly_logo').attr('src', "/media/images/logos/personel.gif");
                } else if ((id == 657) || (id == 658)) {
                    $('#spamly_logo').attr('src', "/media/images/logos/fulfillment.gif"); /* fulfillment*/
                } else if (((id >= 648) && (id <= 652)) || (id == 724) || (id==341)) {
                    $('#spamly_logo').attr('src', "/media/images/logos/finances.gif"); /* financial business*/
                } else if ((id == 645) || (id == 646)) {
                    $('#spamly_logo').attr('src', "/media/images/logos/packaging.gif"); /* packaging */
                } else if (((id >= 662) && (id <= 668)) || (id == 660) || (id == 721) || (id == 746) || (id == 722) || (id == 747)) {
                    $('#spamly_logo').attr('src', "/media/images/logos/media.gif"); /* media */
                } else if (((id >= 353) && (id <= 355)) || ((id >= 344) && (id <= 346)) || ((id >= 344) && (id <= 346)) ||
                        (id == 357) || (id == 358) || (id == 348) || (id == 351) || (id == 350) || (id == 748) || (id == 749)) {
                    $('#spamly_logo').attr('src', "/media/images/logos/finances.gif"); /* financial personal */
                } else if (((id >= 514) && (id <= 642)) || (id == 742) || (id == 758) || (id == 759)) {
                    $('#spamly_logo').attr('src', "/media/images/logos/legal_services.gif"); /* legal services */
                } else if ((id >= 373) && (id <= 511)) {
                    $('#spamly_logo').attr('src', "/media/images/logos/legal_services.gif"); /* legal services */
                } else {
                    $('#spamly_logo').attr('src', "/media/images/logos/yachts.gif"); /* Ahora, el default es el BARCO */
                }
            }
        });

    function is_clean_request(){
          var id_category = $('#id_category').val();
          return id_category == '-1';
    }

    function is_new_text() {
        return !$(this).attr('clean')
    }

    $('textarea[name="text"]').focus(function(e){
        if (is_clean_request() && is_new_text()) {
            // si no había categoría limpia
            if(!$(this).attr('clean')){
                $(this).val('');
                $(this).html(''); $(this).attr('clean',true);
            }
        }
    })



    $('#move_hints_message').bind('drag',function( event ){
        $('#hints_message').css({
            top: event.offsetY,
            left: event.offsetX
            });
        });





});



