/* Author: Insaitov */
(function($){
  function createCookie(name,value,days) {
  	if (days) {
  		var date = new Date();
  		date.setTime(date.getTime()+(days*24*3600*1000));
  		var expires = "; expires="+date.toGMTString();
  	}
  	else var expires = "";
  	document.cookie = name+"="+value+expires+"; path=/";
  }
  
  function readCookie(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;
  }
  
  function eraseCookie(name) {
  	createCookie(name,"",-1);
  }

  Drupal.behaviors.admin_helper = {
    attach: function(context) {
      if (Drupal.behaviors.theme.navigation_hidden) {
        $("#block-system-navigation .content", context).hide();
      }
      
      $("#block-system-navigation h2").css('cursor', 'pointer').click( function (e) {
        e.preventDefault();
        if (Drupal.behaviors.theme.navigation_hidden) {
          $("#block-system-navigation .content").slideDown();
          Drupal.behaviors.theme.navigation_hidden = 0;
          eraseCookie('navigation_hidden');
        } else {
          $("#block-system-navigation .content").slideUp();
          Drupal.behaviors.theme.navigation_hidden = 1;
          createCookie('navigation_hidden', 1, 7);
        }
      });
      
      if ($('body', context).hasClass('admin') && (!$('#block-system-navigation', context).hasClass('main-expanded-processed'))) {
        $('#block-system-navigation', context).addClass('main-expanded-processed');
        if (Drupal.behaviors.theme.main_expanded) {
          $("#page", context).css({'width': 'auto', 'overflow' : 'visible'});
        }
        
        $('<div class="toggle-main-expand">\u2194</div>').prependTo('#block-system-navigation').click( function (e) {
          e.preventDefault();
          if (Drupal.behaviors.theme.main_expanded) {
            $("#page").removeAttr('style');
            Drupal.behaviors.theme.main_expanded = 0;
            eraseCookie('main_expanded');
          } else {
            $("#page").css({'width': 'auto', 'overflow' : 'visible'});
            Drupal.behaviors.theme.main_expanded = 1;
            createCookie('main_expanded', 1, 7);
          }
        });
      }

    },
    navigation_hidden: readCookie('navigation_hidden'),
    main_expanded: readCookie('main_expanded'),
  };
})(this.jQuery);

