jQuery.fn.cwQtip = function(stile){
  $(this).qtip( $.extend({style: { name: stile, tip: true }}, baseQtip));
};

$(function() {
  function qTipInit() {
    baseQtip = {
      content: { 
        text: 'aiuto',
        title: { 
        text: '', 
        button: 'Chiudi'
        }
      },
      position: { adjust: { screen: true } },
      show: { delay:0, when: { event: 'click' } },
      hide: { when: { event: 'unfocus' } },
      api: {
        onRender: function() {
          //var qtip = this;
          var a = this.elements.target;
          /*if ($(a).hasClass('jsScheda') && !$(a).data('loaded')) {
            $.get(scheda_url+'personali/scheda/'+$(a).attr('rel'), function(data) {
              $(a).next().html(data);
              qtip.updateContent($(a).next().html());
              $(a).data('loaded', 1);
            });
          } else {*/
            this.updateContent($(a).next().html());
          /*}*/
        }
      }
    };

    $.fn.qtip.styles.cwTip1 = { 
       width:250,
       background: '#ffec73',
       color: '#42210b',
       title: { 'font-size': '13px', 'color': '#42210b', 'background-color':'#ffd421' },
       border: {
        width: 3,
        radius: 3,
        color: '#ffce00'
       }
    };
    
    $.fn.qtip.styles.cwTip2 = {
       width:500,
       background: '#bcddf5',
       color: '#000000',
       title: { 'font-size': '15px', 'color': '#007fd9', 'background-color':'#aad5f5' },
       border: {
        width: 5,
        radius: 5,
        color: '#0090cb'
       }
    };
    
    $.fn.qtip.styles.cwTip3 = { 
        width:400,
        background: '#ffec73',
        color: '#42210b',
        title: { 'font-size': '13px', 'color': '#42210b', 'background-color':'#ffd421' },
        border: {
         width: 3,
         radius: 3,
         color: '#ffce00'
        }
     };
    
    $("a.qTipCall").cwQtip('cwTip1');
    $("a.qTip3Call").cwQtip('cwTip3');
    //$("a.qTip2Call").cwQtip('cwTip2');
  }
  qTipInit();
});

/*
 * @homepage
 * 
 */
function page_home() {
  
  function importo_richiesto_listener() {
    $(this).removeClass('InfoCampo').val('').unbind();
  };

  $(function() {
    /*$("#preventivo_personale_destination").change(function() { 
      switch($(this).val())
      {
        case 'Prestito personale':
          var url = home.destinazioni.prestito_personale;
          break;
        case 'Cessione del quinto':
          var url = '#_';
          break;
        case 'Prestito auto':
          var url = home.destinazioni.prestito_auto;
          break;
        default: return false; break;
      }
      $(this).parents("form").attr("action",url);
    });*/
    
    //var finalita = $('#preventivo_personale_finalita_id');
    //finalita.width(finalita.width()-20).jqTransSelect();

    /*$('#preventivo_personale_importo_richiesto').focus().bind('click', importo_richiesto_listener).bind('keypress', importo_richiesto_listener);*/
  });

};

/*
 * @prestito_personale_result
 * 
 */
function page_result() {
  
  $(function() {  
    var search_history = {};
    var choice_format = new sfChoiceFormat();
    var form = $('#jsFormSearch');
    var fields = form.serialize();
    var list = $('#jsFormSearchList');
    var loading = $('#jsFormSearchLoading');
    var text_slider = [];
    
    for(k in fields) {
      fields[k] = '';
    }
    
    ie_fix_height(list);
    list.parent().css({'clear': 'both'});
    
    search_history[document.location.href.match(/\/([^\/]*)$/)[1]] = form.serialize();
    
    /*$(".jsSlideOptions").click(function() {
      $(this).toggleClass("ExpBt").toggleClass("CollapsBt").parents("fieldset").next("fieldset").slideToggle();
    });*/
    
    $('.Filter1 ul>li>ul a').click(function() {
      
    if ($(this).is('.Disabled')) {
      return false;
    }
      loading.show();
      
      var a = $(this);
      var li = a.parent();
      var ul = a.parents('ul:first');
      
      li.siblings().removeClass('Actv');
      li.toggleClass('Actv');
      if (li.is('.Actv')) {
        ul.find(':hidden').val(a.attr('rel'));
      } else {
        ul.find(':hidden').val('');
      }
      
      if (ul.is('.jsRadio') && ul.find('.Actv').length == 0) {
        var other = li.siblings().eq(0);
        other.siblings().addClass('Actv');
        ul.find(':hidden').val(other.find('a').attr('rel'));
      }

      $.ajax({
        url: form.attr('action'),
        type: 'POST',
        data: form.serialize(),
        success: function(searchid) {
          search_history[searchid] = form.serialize();
          document.location.hash = searchid;
        },
        error: ajax_error
      });
      
      return false;
    });

    list.children('li').hover(toggle_ov, toggle_ov);
    
    $('#IfcM .jsScheda').live('click', scheda_load).live('scheda.loaded', scheda_show);

    $('#WinDettagli').dialog({
      autoOpen: false,
      modal: true,
      title: 'Dettagli Offerta', 
      width: 750, 
      height: 600,
      bgiframe: true,
      //position: 'top',
      open: function(e,ui) {
        $('#WinDettagli').get(0).scrollTop = 0;
      }
    });
    
    $('#WinCosaSig').dialog({
      autoOpen: false,
      modal: true,
      title: 'Cosa Significano?', 
      width: 750, 
      height: 400,
      bgiframe: true,
      //position: 'top',
      open: function(e,ui) {
        $('#WinCosaSig').get(0).scrollTop = 0;
      }
    });
    
    $('#jsCosaSig').click(function() {
      $('#WinCosaSig').dialog('open');

      return false;
    });
    
    $.unserialise = function(Data){
      var Data = Data.split("&");
      var Serialised = new Array();
      $.each(Data, function(){
        var Properties = this.split("=");
        Serialised[Properties[0]] = Properties[1];
      });
      return Serialised;
    };

    $.History.bind(function(state) {
      if (state == '_') {
        return false;
      }
      list.addClass('LoadingStatus');
      loading.show();
      if (!state) {
        state = document.location.href.replace('#','').match(/\/([^\/]*)$/)[1];
      }
      var new_url = document.location.href.replace(/\/([^\/]*)$/, '/'+state);
      $.ajax({
        url: new_url,
        type: 'GET',
        data: {is_xhr: 1},
        complete: function(a,b,c) {
          if (!search_history[state]) {
            $.ajax({
              url: preventivo_url.replace('hash', state),
              type: 'GET',
              success: function(data) {
                search_history[state] = data;
                load_into_form();
              }
            });
          } else {
            load_into_form();
          }
          
          function load_into_form() {
            var fstate = $.unserialise(search_history[state]);
            //console.log(state, fstate);
            $.extend(fields, fstate);
            for(k in fstate) {
              var v = fstate[k];
              var inp = $(':input[name="'+unescape(k)+'"]');
              inp.parents('ul:first').find('li.Actv').removeClass('Actv');
              inp.parents('ul:first').find('a[rel="'+v+'"]').parent().addClass('Actv').siblings().removeClass('Actv');
              inp.val(v);
            }
          }
        },
        success: function(data) {
          list.hide().html(data);
          ie_fix_height(list);
          list.show();
          var count = $('li.Itm', list).length;
          $('#IfcSubH span:first').html(choice_format.format(title.first, {'%count%': count}, count));
          $('#IfcSubH span:last').html(choice_format.format(title.second, {'%tot%': title.tot}, count));
          loading.hide();
          list.removeClass('LoadingStatus');
        },
        error: ajax_error
      });
    });
    
    var order_input = $('#preventivo_personale_ordinamento').appendTo(form);
    $('#IfcM .OrderBar ul a').click(function(e) {
      order_input.val($(e.currentTarget).attr('rel'));
      form.submit();

      return false;
    });

    /*var text_slider = $('#jsTextSlider').data('current', 0);
    text_slider.find('span:first').html(sliders[0]);
    text_slider.find('a').click(function() {
      clearInterval(text_i);
      $(this).parent().unbind().fadeOut('fast', function() {
        $(this).remove();
      });
      return false;
    });
    var text_i = setInterval(function() {
      var next = text_slider.data('current') + 1;
      next = next >= sliders.length ? 0 : next;
      text_slider.data('current', next);
      text_slider.find('span:first').html(sliders[next]);
    }, 15000);*/

  });
  

  function toggle_ov() {
    $(this).toggleClass('Itm-ov');
  };
  
  function scheda_load() {
    var a = this;
    if (!$(a).data('loaded')) {
      $.get($(a).attr('href'), function(data) {
        $(a).data('loaded', 1).next().html(data);
        $(a).trigger('scheda.loaded');
      });
    } else {
      $(a).trigger('scheda.loaded');
    }
    
    return false;
  };
  
  function scheda_show() {
    $('#WinDettagli').html($(this).next().html()).dialog('open');
    
    return false;
  };
  
  function ajax_error(a,b,c) {
    
  };
  
  function ie_fix_height(list) {
    //if ($.browser.msie) {
      var c = 30;
      var h = list.height() + c;
      var ch = 520;
      h = h < ch ? ch + c : h;
      //$('#IfcM').height(h);
      list.parent().height(h);
      //list.height(h);
      //list.parent().parent().height(h);
    //}
  };

};

/*
 * @prestito_personale_preventivo
 * 
 */
function page_preventivo() {
  //$('#Form3')
}
