jQuery.extend({
  system: {
    redirect: function (href) {
      window.location.href = href;
    },
    
    submit: function (form){
      $("form#"+form).submit();
    },
    
    updateBlock: function (id, html) {
      $("#" + id).html(html);
    },
    
    appendBlock: function (id, html) {
      $("#" + id).append(html);
    },

    replaceBlock: function (id, html) {
      $("#" + id).empty();
      $("#" + id).replaceWith(html);
    },

      
    success: function (payload) {
      // redirect
      if (payload.redirect) {
        redirect(payload.redirect);
        return;
      }

      // update blocks
      if (payload.blocks) {
        for (var i in payload.blocks) {
          jQuery.system.updateBlock(i, payload.blocks[i]);
        }
      }
      
      // replace blocks
      if (payload.blocks_replace) {
        for (var i in payload.blocks_replace) {
          jQuery.system.replaceBlock(i, payload.blocks_replace[i]);
        }
      }
      
      // append blocks
      if (payload.blocks_append) {
        for (var i in payload.blocks_append) {
          appendBlock(i, payload.blocks_append[i]);
        }
      }
    },
    
    // nastavi swip predanemu elementu
    setSwip: function ($element, message) {
      $element.focus(function(){
        $.system.swip($element, message, "");
      });
      
      $element.blur(function(){
        $.system.swip($element, "", message);
      });
    },
    
    // Vymazani hodnoty value u inputu
    swip: function ($element, swipFrom, swipTo) {
      if ($element.val() == swipFrom) $element.val(swipTo);
    },
    
    /**
    * vytvori cookie
    */
    createCookie: function (name, value, days, path) {
      path = path || "/";
      
      if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
      }
      else var expires = "";
      document.cookie = name+"="+value+expires+"; path=" + path;
    },

    /**
    * precte cookie
    */
    readCookie: function (name) {
      var nameEQ = name + "=";
      var ca = document.cookie.split(';');
      for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
      }
      
      return null;
    },
    
    /**
    * vrati hodnotu z cookie
    */
    readCookieItem: function(cookie, param) {
      var values = readCookie(cookie);
      
      if (values) {
        var valuesArr = values.split(';');
        
        for(var i=0;i < ca.length;i++) {
          pair = valuesArr[i];
          
        }
      }
      
      return null;
    }
  }
});

jQuery.ajaxSetup({
    success: jQuery.system.success,
    dataType: "json"
});

/*
// trida pro praci s cookies
var Cookies = {
  init: function () {
    var allCookies = document.cookie.split('; ');
    for (var i=0;i<allCookies.length;i++) {
      alert(allCookies[i]);
      var cookiePair = allCookies[i].split('=');
      this[cookiePair[0]] = cookiePair[1];
      alert(cookiePair[1]);
    }
  },
  create: function (name,value,days) {
    if (days) {
      var date = new Date();
      date.setTime(date.getTime()+(days*24*60*60*1000));
      var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
    this[name] = value;
  },
  erase: function (name) {
    this.create(name,'',-1);
    this[name] = undefined;
  }
};
*/

