/**
 * @version 0.0.2
 * @name jesmeralda
 * @author Nikitin Sergey <nikitinsm@gmail.com>
 */

jQuery.jesmeralda = {

  init: function(){
    
    $('html').ajaxSend(function(event, xhr, settings) {
      function getCookie(name) {
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
          var cookies = document.cookie.split(';');
          for (var i = 0; i < cookies.length; i++) {
            var cookie = jQuery.trim(cookies[i]);
            // Does this cookie string begin with the name we want?
            if (cookie.substring(0, name.length + 1) == (name + '=')) {
              cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
              break;
            }
          }
        }
        return cookieValue;
      }
      if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) {
        // Only send the token to relative URLs i.e. locally.
        xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
      }
    });
    
    $.ctrl = function(key, callback, args){
      $(document).keydown(function(e){
        if (!args) 
          args = []; // IE barks when args is null
        if (e.keyCode == key.charCodeAt(0) && e.ctrlKey) {
          callback.apply(this, args);
          return false;
        }
      });
    };
    
    //Common message
    $("#esmeralda-common-dialog").dialog({
      "autoOpen": false,
      "modal": true,
      "draggable": false,
      "resizable": false,
      "width": 420,
      "height": 160,
      "show": 'fade',
      "hide": 'fade',
      "buttons": {
        "Закрыть": function(){
          $(this).dialog("close");
        }
      }
    });

    //Core messages
    $(".esmeralda-core-messages-dialog").dialog({
      "autoOpen": true,
      "modal": true,
      "draggable": true,
      "resizable": false,
      "width": 420,
      "show": 'fade',
      "hide": 'fade',
      "buttons": {
        "Закрыть": function(){
          $(this).dialog("close");
        }
      }
    });
    
    
    //Login box init
    $("#esmeralda-login-dialog").dialog({
      "autoOpen": false,
      "modal": true,
      "draggable": false,
      "resizable": false,
      "width": 240,
      "height": 200,
      "show": 'fade',
      "hide": 'fade',
      "buttons": {
        "Войти": function(){
          $("#esmeralda-login-dialog form").submit();
        },
        "Закрыть": function(){
          $(this).dialog("close");
        }
      }
    });
    
    $.ctrl('1', function(s){
      if($("#esmeralda-login-dialog").dialog("isOpen")){
        $("#esmeralda-login-dialog").dialog("close");
      }else{
        $("#esmeralda-login-dialog").dialog("open");  
      }
    });


    //cart input init
    $("#esmeralda-cart-dialog").dialog({
      "autoOpen": false,
      "modal": true,
      "draggable": false,
      "resizable": false,
      "width": 420,
      "show": 'fade',
      "hide": 'fade',
      "buttons": {
        "Продолжить": function(){
          $(this).dialog("close");
        },
        "Корзина": function(){
          $(this).dialog("close");
          window.location.href = '/shop/cart/';
        },
        "Оформить": function(){
          window.location.href = '/shop/order/';
        },
        "Закрыть": function(){
          $(this).dialog("close")
        }
      }
    });

    this.init_cart_urls();
  },

  init_cart_urls: function(){
    $('.shop_cart_set_button').unbind('click');

    $('.shop_cart_set_button').click(function(event){
      event.preventDefault();

      //confirm
      if($(this).hasClass('confirm_delete')){
        if(!confirm('Удалить ?')){
          return false;
        }
      }

      var obj = this

      $.ajax({
        url: $(this).attr('href'),
        type: 'GET',
        success: function(){
          if($(obj).hasClass('notify')){
            $("#esmeralda-cart-dialog").dialog('open');
          }
          $(document).trigger('shop.cart.set');
        }
      });
    });
  }
}

