function load(s_url, data, function1, function2, function3) {
 if (!s_url) return;
 if (!data) data = {};
 if (!function1) function1 = function() {};
 if (!function2) function2 = function() {};
 if (!function3) function3 = function() {};

 function1();

 preloaderShow(function() {
  $.ajax({
   type: 'POST',
   url: s_url,
   data: data,
   dataType: 'json',
   success: function(data) {
    cssDisable($.GLOBALS.CSS || {});
    jsUnload($.GLOBALS.JS || {});
    $.GLOBALS.CSS = (data.css && data.css != '') ? data.css.split(';') : '';
    $.GLOBALS.JS = (data.js && data.js != '') ? data.js.split(';') : '';
    cssEnable($.GLOBALS.CSS);
    function2(data, function() {
     jsLoad($.GLOBALS.JS, function() { 
      preloaderHide(function() { function3(); });
     });
    });
   }
  });
 });
}

function loadAll(s_url, data) {
 load(s_url, data, function() {
  imageHide();
  headHide();
  textHide();  
 }, function(data, callback) {
  if (!callback) callback = function() {};
  imageSet(data.image, function() {
   headSet(data.head);
   textSet(data.content);
   callback();
  });
 }, function() {
  imageShow();
  headShow();
  textShow();
 });
}

function submitForm(form, s_url, data, function1, function2, function3) {
 if (!form) return;
 if (!s_url) s_url = $(form).attr('action');
 if (!data) data = {};
 if (!function1) function1 = function() {};
 if (!function2) function2 = function() {};
 if (!function3) function3 = function() {};

 function1();

 preloaderShow(function() {
  $(form).ajaxSubmit({
   type: 'POST',
   url: s_url,
   data: data,
   dataType: 'json',
   success: function(data) {
    cssDisable($.GLOBALS.CSS || {});
    jsUnload($.GLOBALS.JS || {});
    $.GLOBALS.CSS = (data.css && data.css != '') ? data.css.split(';') : '';
    $.GLOBALS.JS = (data.js && data.js != '') ? data.js.split(';') : '';
    cssEnable($.GLOBALS.CSS);
    function2(data, function() {
     jsLoad($.GLOBALS.JS, function() { 
      preloaderHide(function() { function3(); });
     });
    });
   }
  });
 });
}

function submitFormUpdateAll(form, s_url, data) {
 submitForm(form, s_url, data, function() {
  imageHide();
  headHide();
  textHide();  
 }, function(data, callback) {
  if (!callback) callback = function() {};
  imageSet(data.image, function() {
   headSet(data.head);
   textSet(data.content);
   callback();
  });
 }, function() {
  imageShow();
  headShow();
  textShow();
 });
}

function cssEnable(css) {
 if (!css) css = [];

 $.each(css, function(index, value) {
  if (value && value != '') $('link[@rel*=style][title=' + value + ']').attr('disabled', false);
 });
}

function cssDisable(css) {
 if (!css) css = [];

 $.each(css, function(index, value) {
  if (value && value != '') $('link[@rel*=style][title=' + value + ']').attr('disabled', true);
 });
}

function jsLoad(js, callback) {
 if (!js) js = [];
 if (!callback) callback = function() {};

 if (js.length > 0) {

  var countOfLoaded = 0;
  var busy = false;

  var loadItem = function(value) {
   if (value && value != '') {

    $.ajax({
     url: $.GLOBALS.DIR_JS + value + '.js',
     dataType: 'script',
     success: function(data) {
//      $('<script type="text/javascript" title="' + value + '">' + data + '</script>').appendTo($('head'));
//      while (busy) {}
//      busy = true;
      if (++countOfLoaded >= js.length) callback();
//      busy = false;
     }
    });
   } else {
//     while (busy) {}
//     busy = true;
     if (++countOfLoaded >= js.length) callback();
//     busy = false;
   }
  };

  for (var i = 0; i < js.length; i++) {
   loadItem(js[i]);
  }

 } else callback();
}

function jsUnload(js) {
 if (!js) js = [];
/*
 $.each(js, function(index, value) {
  if (value && value != '') $('script[title=' + value + ']', $('head')).remove();
 });
*/
// eval('function jsLoadedExecution() {}');
}

function setContent(destenation, content) {
 $(destenation)
  .empty()
  .html(content ? content : '');
}

function imageSet(content, callback) {
 var $o_image = $('#image');

 setContent($o_image, content);

 var $o_firstChild = $o_image.children(':first-child');
 if ($o_firstChild.length > 0) {
  $o_firstChild.css({marginTop: 0});
  if (($o_firstChild.attr('tagName')).toLowerCase() == 'ul') $o_firstChild.children(':first-child').css({marginTop: 0});
 }

 if ($o_image.css('backgroundImage') != 'none') {
  $(new Image())
   .load(function() { callback(); })
   .appendTo($o_image)
   .attr('src', $o_image.css('backgroundImage').replace(/(url)|\)|\(|"/g, ''));
 } else callback();
 
}

function headSet(content) {
 setContent('#head > h3:first', content);
}

function textSet(content) {
 var $o_scrollContent = $('#contentMask > div.scrollContent');
 setContent($o_scrollContent, content);

 $o_scrollContent.css({top: 0})
 var $o_firstChild = $o_scrollContent.children(':first-child');
 if ($o_firstChild.length > 0) {
  $o_firstChild.css({marginTop: 0});
  if (($o_firstChild.attr('tagName')).toLowerCase() == 'ul') $o_firstChild.children(':first-child').css({marginTop: 0});
 }

 $('input.checkbox', $o_scrollContent)
  .checkbox({empty: $.GLOBALS.DIR_HTML+'images/input-checkboxTransparent.gif'});

 $('#scrollbar')
  .unScrollbar($('#contentMask'), $o_scrollContent);
}

function showContent(destenation) {
 $(destenation)
  .fadeIn(1000, function() {
//   this.style.removeAttribute('filter');
  });
}

function imageShow() {
 showContent('#image');
}

function headShow() {
 showContent('#head');
}

function textShow() {
 var $o_contentMask = $('#contentMask');
 showContent($o_contentMask);

 var $o_scrollContent = $('#contentMask > div.scrollContent');

 if ($o_scrollContent.height() > $o_contentMask.height()) {
  $('#scrollbar')
   .unScrollbar($o_contentMask, $o_scrollContent)
//   .css({height: ($o_scrollContent.height() > $o_contentMask.height() ? $o_contentMask.height() : $o_scrollContent.height()) + 6 + 'px'})
   .css({height: $o_contentMask.height() + 6 + 'px'})
   .scrollbar($o_contentMask, $o_scrollContent);
   showContent('#scrollbar');
 }
}

function hideContent(destenation) {
 $(destenation).fadeOut(1000);
}

function imageHide() {
 hideContent('#image');
}

function headHide() {
 hideContent('#head');
}

function textHide() {
 hideContent('#contentMask');
 hideContent('#scrollbar:visible');
}

function preloaderShow(callback) {
 $('<div id="preloader"></div>')
  .css({opacity: 0.0})
  .appendTo($('#body'))
  .animate({opacity: 0.5}, 1000, function() {
   var $o_div = $('<div id="preloaderContent"></div>');

   $o_div
    .appendTo($('#body'))
    .flash({
     src: $.GLOBALS.DIR_HTML + 'images/preloader.swf',
     width: $o_div.width() + 'px',
     height: $o_div.height() + 'px',
     bgcolor: '#000000',
     loop: false,
     menu: false,
//     wmode: 'opaque'
     wmode: 'transparent'
    })
    .hide()
    .fadeIn('fast', function() { setTimeout(function() { callback(); }, 3000); });
  });
}

function preloaderHide(callback) {
 if (!callback) callback = function() {};

 $('#preloaderContent')
  .fadeOut(1000, function() {
   $(this).remove();
   callback();
  });
 $('#preloader')
  .fadeOut(1000, function() { $(this).remove(); });
}