/**
*   Плагин jquery.kiwi_shadow.js
*   
*   @author Isaev Roman
*   @modufy_date 29.04.10
*   @version 1.1 
*   
*/

(function($) { 

	// constructor
	function Kiwi_shadow(self, ops) 
  {          
      var core_img = self.find('img:first');
      core_img.wrap('<div class="kv_shadow"></div>');
      var core = self.find('div.kv_shadow');

      var onPreload = function()
      {
          var core_w = $(core_img).width();
          var core_h = $(core_img).height();
          var core_w8 = core_w + 8;
          var core_h8 = core_h + 8;
          
          var st = '<div class="kvsh_lt"></div>' +
                   '<div class="kvsh_rt" style="left: ' + core_w8 + 'px; top: 0;"></div>' +
                   '<div class="kvsh_lb" style="left: 0; top: ' + core_h8 + 'px;"></div>' +
                   '<div class="kvsh_rb" style="left: ' + core_w8 + 'px; top: ' + core_h8 + 'px;"></div>' +
                   '<div class="kvsh_l" style="top: 8px; height:' + core_h + 'px;"></div>' +
                   '<div class="kvsh_t" style="left: 8px; width:' + core_w + 'px;"></div>' +
                   '<div class="kvsh_r" style="left: ' + core_w8 + 'px; top: 8px; height:' + core_h + 'px; right: 0;"></div>' +
                   '<div class="kvsh_b" style="left: 8px; top: ' + core_h8 + 'px; width:' + core_w + 'px;"></div>';
      
          core
            .width( core_w )
            .height( core_h )
            .append(st);
          
          // fix для картинов с подписью  
          self.width( core_w + 16 );
          self.find('table').width( core_w + 16 );
          self.find('td').width( core_w + 16 );
                                    
      }

      /* если картинки подгрузились из кеша, то событие load не сработает
         поэтому проверяем заранее */
                      
      if(core_img[0].complete){
          onPreload();              
      } else {                     
          $(core_img).load(function()
          {
              onPreload();                                                             
          });             
      }
   		
	}

  // jQuery plugin implementation
  $.fn.kiwi_shadow = function(ops)
  {  
  		var el = this.eq(0).data('kiwi_shadow');
  		if (el) { return el; }
      
      // default settings
      var ops = $.extend({
          /*default: 'temp'*/   
      },ops);
          
  		this.each(function() {			
  			el = new Kiwi_shadow($(this), ops);
  			$(this).data('kiwi_shadow', el);	
  		});
  		
  		return this;    
  };

})(jQuery);



