//--------------------------------
//********************************
//******** Class widget***********
//********************************
//--------------------------------


//********************************
//********* Constructors *********
//********************************
function widget()
{
   
}

function owk_widget_resize_background_patterns()
{  
   
}

function owk_widget_on_every_frame()
{
  this.resize_background_patterns();
}


function owk_widget_create(layer_group, i_region, i_visible, i_resizable, i_dragable, i_closeable, i_title)
{   
   if(layer_group!=null)
   {
      this.set_parent(layer_group);
   }
   
   
     this.ancestor_create(i_region, i_visible, i_resizable, i_dragable);  
   
   if(layer_group!=null)
   {
      layer_group.push_back_layer(this);
   }
   this.m_showable_region = new rectangle();
   this.m_pick_point = new point();
   
   this.m_switch_button = new layer();
   this.m_background_decorator = new pattern_decorator(); 
   this.m_background_decorator.create(this);
   
   //this.m_resize_button = new layer();
   this.m_menu_button = new layer();

   this.m_repeat_middle_patern = false;
              
   //drop_shadow().on(false);
   //disable().on(false);

   this.set_draging(false);

   //rend_bitmap().create(region().width(), region().height());

   this.set_scale_factor(1.0);	
   this.set_rotation_angle(0.0);
   
   this.pivot_point().xy(this.region().center_x(), this.region().center_y()); // by default the center of region
      
   this.set_closeable(i_closeable);

   this.set_title(i_title);
   //this.set_menuable(i_menuable);

   //this.rend_bitmap().background().r = 0;  
  // this.rend_bitmap().background().g = 0; 
  // this.rend_bitmap().background().b = 0; 
   //this.rend_bitmap().background().a = 0; 

  // layer_group::create(i_region, i_visible, i_resizable, i_dragable); 

   this.opacity(75.0);

   //this.background().create(this.region());
   this.draw_background();

   this.m_mouse_on_layer_ptr = 0;
   
   var this_object = this;
   this.m_resize_button = new layer();
   this.m_resize_button.set_parent(this);
   this.m_resize_button.create(new rectangle(0,20,20,40),false,false,false);
 
   this.m_close_button = new layer();
   this.m_close_button.set_parent(this);
   this.m_close_button.create(new rectangle(0,0,20,20),false,false,false);
   this.m_close_button.m_div.onclick = function(){this_object.set_visible(false);};
}

function owk_widget_destroy() 
{ 
   this.m_close_button.destroy();   
   this.m_resize_button.destroy();
   //this.menu_button().destroy();

   //this.background().destroy();
   
   this.ancestor_destroy();
} 

function owk_widget_switch_button() 
{ 
   return this.m_switch_button; 
}

function owk_widget_closeable()
{
   return this.m_closeable;
}

function owk_widget_set_closeable(i_value)
{
   this.m_closeable = i_value;
}

function owk_widget_wid()
{
   return this.m_wid;
}

function owk_widget_set_wid(i_value)
{
   this.m_wid = i_value;
}

function owk_widget_menuable()
{
   return this.m_menuable;
}

function owk_widget_set_menuable(i_value)
{
   this.m_menuable = i_value;
}

function owk_widget_auto_showable()
{
   return this.m_showable_region;
}


function owk_widget_set_resize_button(i_value)
{
   this.m_resize_button= i_value;
}

function owk_widget_resize_button()
{
   return this.m_resize_button;
}

function owk_widget_set_close_button(i_value)
{
   this.m_close_button = i_value;
}

function owk_widget_close_button()
{
   return this.m_close_button;
}


function owk_widget_set_menu_button(i_value)
{
   this.m_menu_button = i_value;
}

function owk_widget_menu_button()
{
   return this.m_menu_button;
}

function owk_widget_set_menuable(i_value)
{
   this.m_menuable = i_value;
}

function owk_widget_auto_showable()
{
   return this.m_showable_region;
}

function owk_widget_set_menuable(i_value)
{
   this.m_menuable = i_value;
}

function owk_widget_auto_showable()
{
   return this.m_showable_region;
}

function owk_widget_set_auto_showable(i_value)
{
   this.m_showable_region = i_value;
}

function owk_widget_place_buttons()
{
  if(this.m_background_decorator.has_top_left() && this.m_background_decorator.has_top())
  {
      var resize_rectangle = new rectangle(this.m_region.width() - this.m_background_decorator.bottom_right_size().width, this.m_region.height() - this.m_background_decorator.bottom_right_size().height, this.m_region.width(), this.m_region.height());
      
      this.m_resize_button.set_region(resize_rectangle);
      this.m_resize_button.m_div.style.zIndex=999;
      
      //var close_rectangle = new rectangle(this.m_region.width() - this.m_background_decorator.top_right_size().width,0, this.m_region.width(), this.m_background_decorator.top_right_size().height);
    
      var resize_rectangle2 = new rectangle(this.m_region.width() - this.m_background_decorator.top_right_size().width, 0, this.m_region.width(), this.m_background_decorator.top_left_size().height*1);
      
      
      this.m_close_button.set_region(resize_rectangle2);
      this.m_close_button.m_div.style.zIndex=999;


  }
}   

function owk_widget_draw_background()
{
  
} 

function owk_widget_on_resizing()
{
   this.m_background_decorator.draw();
   this.place_buttons();   
}




function owk_widget_background_decorator()
{
   return this.m_background_decorator;
}

function owk_widget_on_drag()
{
   this.on_drag();
}

// var myobject = this;
//this.m_resize_button.on_mouse_move_enter = function(){myobject.m_resize_button.m_div.style.display='';};	  
//this.m_resize_button.on_mouse_move_leave = function(){myobject.m_resize_button.m_div.style.display='none';};
//this.m_resize_button.on_mouse_button_down = function(i_mouse){myobject.m_resizing = true;return myobject.on_mouse_button_down(i_mouse);};
//this.m_resize_button.on_mouse_move = function(i_mouse){myobject.m_resizing = true;return  myobject.on_mouse_move(i_mouse);};
//this.m_resize_button.on_mouse_button_up = function(i_mouse){return myobject.on_mouse_button_up(i_mouse);};

function owk_widget_on_mouse_move_enter(i_mouse)
{
   if(this.resizable())
   {
      this.m_resize_button.set_visible(true);
      this.m_close_button.set_visible(true);
   }
   
   return false;
}

function owk_widget_on_mouse_move_leave(i_mouse)
{
   if(this.resizable())
   {
      this.m_resize_button.set_visible(false);
      this.m_close_button.set_visible(false);
   }
   
   return false;
}

function owk_widget_is_inside_resizing_box(i_point)
{
   return this.m_resize_button.region().is_point_inside(i_point);
}

function owk_widget_on_loaded()
{
   this.place_buttons();
}

function owk_widget_load_patterns(i_url, i_repeat_middle, i_is_horizontal, i_is_vertical)
{
   if(i_is_horizontal && i_is_vertical)
   {
      this.m_background_decorator.load(i_url,i_repeat_middle);
   }
   else if(i_is_horizontal)
   {
      this.m_background_decorator.load_hori(i_url,i_repeat_middle);
   }
   else if(i_is_vertical)
   {
      this.m_background_decorator.load_vert(i_url,i_repeat_middle);
   }
    this.m_close_button.load_image(i_url + "close_button.png");
    this.m_resize_button.load_image(i_url + "resize_button.png");
   
   
}

function owk_widget_invalidate()
{
   this.m_background_decorator.draw();
   this.place_buttons();   
}

function owk_widget_set_title(i_title)
{
   this.m_title = i_title;
}

function owk_widget_title()
{
   return this.m_title;
}

widget.prototype = new layer_group;
widget.prototype.title = owk_widget_title;
widget.prototype.set_title = owk_widget_set_title;
widget.prototype.create = owk_widget_create;
widget.prototype.on_every_frame = owk_widget_on_every_frame;
widget.prototype.ancestor_create = owk_layer_create;
widget.prototype.destroy = owk_widget_destroy;
widget.prototype.ancestor_destroy = owk_layer_destroy;
widget.prototype.draw_background = owk_widget_draw_background;
widget.prototype.set_wid = owk_widget_set_wid;
widget.prototype.wid = owk_widget_wid;
widget.prototype.on_resizing = owk_widget_on_resizing;
widget.prototype.on_drag = owk_widget_on_drag;
widget.prototype.background_decorator = owk_widget_background_decorator;
widget.prototype.set_closeable = owk_widget_set_closeable;
widget.prototype.closeable = owk_widget_closeable;
widget.prototype.set_menuable = owk_widget_set_menuable;
widget.prototype.menuable = owk_widget_menuable;
widget.prototype.switch_button = owk_widget_switch_button;
widget.prototype.place_buttons = owk_widget_place_buttons;
widget.prototype.resize_button = owk_widget_resize_button;
widget.prototype.close_button = owk_widget_close_button;
widget.prototype.resize_background_patterns = owk_widget_resize_background_patterns;
widget.prototype.is_inside_resizing_box = owk_widget_is_inside_resizing_box;
widget.prototype.on_mouse_move_enter = owk_widget_on_mouse_move_enter;
widget.prototype.on_mouse_move_leave = owk_widget_on_mouse_move_leave;
widget.prototype.ancestor_widget_on_mouse_move = owk_layer_group_on_mouse_move;
widget.prototype.ancestor_widget_on_mouse_button_down = owk_layer_group_on_mouse_button_down;
widget.prototype.ancestor_widget_on_mouse_button_up = owk_layer_group_on_mouse_button_up;
widget.prototype.on_loaded =  owk_widget_on_loaded;
widget.prototype.load_patterns = owk_widget_load_patterns;
widget.prototype.ancestor_invalidate = owk_widget_invalidate;



function space_switcher_widget()
{

   this.create = owk_space_switcher_widget_create;
   this.surface_device = owk_space_switcher_widget_surface_device;
   this.set_surface_device = owk_space_switcher_widget_set_surface_device;
   this.on_draw = owk_space_switcher_widget_on_draw;
   this.switch_button = owk_space_switcher_widget_switch_button;
   this.choose_space = owk_space_switcher_widget_choose_space;
   this.space0 = owk_space_switcher_widget_space0;
   this.space1 = owk_space_switcher_widget_space1;
   this.space2 = owk_space_switcher_widget_space2;
   this.space3 = owk_space_switcher_widget_space3;
}

function owk_space_switcher_widget_create()
{
   this.m_surface_device_ptr = 0;
   this.original_region = new rectangle();
   this.m_choose_space = false;
  
   // space
   this.m_space_0 = new layer();
   this.m_space_1 = new layer();
   this.m_space_2 = new layer();
   this.m_space_3 = new layer();      
}

function owk_space_switcher_widget_switch_button()
{
   return this.m_switch_button;
}

function owk_space_switcher_widget_choose_space(i_value)
{
   this.m_choose_space = i_value;
}


function owk_space_switcher_widget_space0()
{
   return this.m_space_0;
}

function owk_space_switcher_widget_space1()
{
   return this.m_space_1;
}

function owk_space_switcher_widget_space2()
{
   return this.m_space_2;
}

function owk_space_switcher_widget_space3()
{
   return this.m_space_3;
}

function owk_space_switcher_widget_set_surface_device(i_surface)
{
   this.m_surface_device_ptr = i_surface;
}


	  

function owk_space_switcher_widget_surface_device()
{
   return this.m_surface_device_ptr;
}

function owk_space_switcher_widget_on_draw(i_surface)
{  
   
}



function owk_space_switcher_widget_on_draw2()
{
}



function owk_space_switcher_widget_on_draw3()
{

}



function owk_space_switcher_widget_surface_device()
{
   return this.m_surface_device_ptr;
}




space_switcher_widget.prototype = new widget;
space_switcher_widget.create = owk_space_switcher_widget_create;

space_switcher_widget.surface_device = owk_space_switcher_widget_surface_device;
space_switcher_widget.set_surface_device = owk_space_switcher_widget_set_surface_device;

space_switcher_widget.on_draw = owk_space_switcher_widget_on_draw;
space_switcher_widget.on_draw2 = owk_space_switcher_widget_on_draw2;
space_switcher_widget.on_draw3 = owk_space_switcher_widget_on_draw3;

space_switcher_widget.switch_button = owk_space_switcher_widget_switch_button;
space_switcher_widget.choose_space = owk_space_switcher_widget_choose_space;
space_switcher_widget.space0 = owk_space_switcher_widget_space0;
space_switcher_widget.space1 = owk_space_switcher_widget_space1;
space_switcher_widget.space2 = owk_space_switcher_widget_space2;
space_switcher_widget.space3 = owk_space_switcher_widget_space3;
   
//space_switcher_widget.on_mouse_button_up = owk_space_switcher_widget_on_mouse_button_up;

//private




/*#ifndef _owk_widget_h_                  
#define _owk_widget_h_ 

#include "owk_types.h"
#include "owk_mouse.h"
#include "owk_keyboard.h"
#include "owk_layer.h"

namespace owk {

   class surface;

   class widget : public layer_group
   {
   public:

    
      owk_bool auto_showable() { return  m_auto_showable; };
      void auto_showable(owk_bool i_value) { m_auto_showable = i_value; };

      // keyboard listener events are send only if the mouse is inside the widget
      virtual owk_bool on_key_down(keyboard& i_keyboard) { return true;};
      virtual owk_bool on_key_up(keyboard& i_keyboard) { return true;};

      // mouse listener events are send only if the mouse is inside the widget
      virtual owk_bool on_mouse_move_enter(mouse& i_mouse);
      virtual owk_bool on_mouse_move_leave(mouse& i_mouse);
      virtual owk_bool on_mouse_move(mouse& i_mouse);
      virtual owk_bool on_mouse_button_down(mouse& i_mouse);
      virtual owk_bool on_mouse_button_up(mouse& i_mouse);
 
      owk_int wid() { return  m_wid; };
      void wid(owk_int i_value) { m_wid = i_value; };

      // refresh & rendering interface
      layer& background() { return  m_background; };

      // refresh & rendering interface
      virtual owk_bool draw_background(); // refresh the entire surface
 
      // activity interface
      virtual void on_begin_resizing();
      virtual void on_resizing();
      virtual void on_end_resizing();
      virtual void on_begin_draging();
      virtual void on_end_draging();
      virtual owk_bool is_inside_resizing_box(point& i_point); // coordinates are in layer coordinates

   protected:
      virtual void flatern_layers();

   private:
      owk_bool m_closeable;
      owk_bool m_menuable;
      owk_bool m_auto_showable;

      rectangle m_showable_region;
      owk_int m_wid; 	
      point m_pick_point;
      rectangle m_pick_region;

      // background
      layer m_background;

      // resize button
      layer m_resize_button;
      layer& resize_button() { return  m_resize_button; };

      // close button
      layer m_close_button;
      layer& close_button() { return  m_close_button; };

      // menu button
      layer m_menu_button;
      layer& menu_button() { return  m_menu_button; };

      void place_buttons();
   };

   class space_switcher_widget : public widget
   {
   public:
      virtual void create(rectangle i_rect);
      virtual owk_bool on_mouse_button_up(mouse& i_mouse);

	  surface& surface_device() { return *m_surface_device_ptr; };
	  void surface_device(surface& i_surface) { m_surface_device_ptr = &i_surface; }
      // refresh & rendering interface
      virtual owk_bool on_draw(surface& i_surface, rectangle& i_region); // refresh a part of surface

   private:
	   surface* m_surface_device_ptr;

      // switch button
      layer m_switch_button;
      layer& switch_button() { return  m_switch_button; };

      rectangle original_region;
      owk_bool m_choose_space;
      owk_bool choose_space() { return  m_choose_space; };
      void choose_space(owk_bool i_value) { m_choose_space = i_value; };

      // space
      layer m_space_0;
      layer& space_0() { return  m_space_0; };

      layer m_space_1;
      layer& space_1() { return  m_space_1; };

      layer m_space_2;
      layer& space_2() { return  m_space_2; };

      layer m_space_3;
      layer& space_3() { return  m_space_3; };

   };


   typedef widget* widget_ptr;

} // end namespace

#endif // _owk_widget_h_*/