/*
 * Doostang javascript library
 */
var DoostangJS = {};
DoostangJS.Event = {
  ResizeWindow : function(){
    this.ResizeOverlay();
  },
  ScrollWindow: function(){
    this.ResizeOverlay();
  },
  ResizeOverlay: function(){
    var page_overlay = $(DoostangJS.Window.page_overlay_id);
    if ( page_overlay && page_overlay.visible ){
      var view_port = document.viewport.getDimensions();
      var scroll_offset = document.viewport.getScrollOffsets();
      var height = view_port.height + scroll_offset.top;
      var width = view_port.width + scroll_offset.left;
//      console.log("scroll height:",scroll_offset.top)
//      console.log("scroll width:",scroll_offset.left)

      page_overlay.style.height = height + "px";
      page_overlay.style.width = width + "px";
    }

  }
};


//-------------------------------------------------------------
//------ window base ----------------------------------------------
//-------------------------------------------------------------

DoostangJS.Window = {
  page_overlay_id : "doo_overlay"
};

DoostangJS.Window.Base = Class.create({
  initialize: function(container, options){
    this.container = container;
    this.options = Object.extend({
      elm_error_id :  this.options.elm_error_id || this.container + "_error",
      elm_ok_id :  this.options.elm_ok_id || this.container + "_ok",
      elm_cancel_id :  this.options.elm_cancel_id || this.container + "_cancel",
      elm_spinner_id :  this.options.elm_spinner_id || this.container + "_spinner"

      }, options || {} );
  },

  show : function(){
    if ($(this.options.elm_error_id))
      $(this.options.elm_error_id).hide();
    if ($(this.options.elm_spinner_id))
      $(this.options.elm_spinner_id).hide();

    if ($(this.options.elm_ok_id))
      $(this.options.elm_ok_id).show();
    if ($(this.options.elm_cancel_id))
      $(this.options.elm_cancel_id).show();

    this.toggle_hide_ids("hide");
  },
  show_error : function(){
    if ($(this.options.elm_error_id))
      $(this.options.elm_error_id).show();
    if ($(this.options.elm_spinner_id))
      $(this.options.elm_spinner_id).hide();

    if ($(this.options.elm_ok_id))
      $(this.options.elm_ok_id).show();
    if ($(this.options.elm_cancel_id))
      $(this.options.elm_cancel_id).show();


  },
  show_spinner : function(){
    if ($(this.options.elm_spinner_id))
      $(this.options.elm_spinner_id).show();

    if ($(this.options.elm_ok_id))
      $(this.options.elm_ok_id).hide();
    if ($(this.options.elm_cancel_id))
      $(this.options.elm_cancel_id).hide();

  },

  close : function(){

    this.toggle_hide_ids("show");
  },

  toggle_hide_ids: function(type){
    this.options.hide_ids.each(function(id){
      if($(id)){
          if(type == "show")
            $(id).show();
          else
            $(id).hide();

      }
    })
  }

})

//-------------------------------------------------------------
//------Modal box----------------------------------------------
//-------------------------------------------------------------

DoostangJS.Window.Modal = Class.create(DoostangJS.Window.Base,{
  initialize:function($super, container, options){
    this.container = container;
    this.options = Object.extend({
      hide_ids: ["viewSelector"],
      top: "10px",
      left: "10px",
      width: "100px",
      height: "259px"
    }, options || {} );
    $super(this.container, this.options);

  },

  open: function(){
    if( this.options.ajax_url){
      var cthis = this;
      var rand=Math.floor(Math.random()*99999)
      new Ajax.Request(this.options.ajax_url + "&rnd=" + rand , {
        method		: 'get',
        onComplete	: function(transport){
          if (cthis.options.ajax_callback)
             cthis.options.ajax_callback(transport.responseText);
        }
      });
    }
    //alert("open");
    $(this.container).style.top = this.options.top;
    $(this.container).style.left = this.options.left;
    $(this.container).style.width = this.options.width;

    $(this.container).style.height = this.options.height;
    //$(this.container).innerHTML = "<p><br/><br/>Loading...</p>";
    $(this.container).show();
    fbwin_event.obj = this;

    Event.observe($(this.container), "mousedown", function(e){
      Event.stop(e);
      return false;
    });


    Event.observe(document, "mousedown", fbwin_event.b_mousedown);
    this.toggle_hide_ids("hide");
  },

  close: function() {
    if($(this.container)){
      $(this.container).hide();
    }
    Event.stopObserving(document, "mousedown", fbwin_event.b_mousedown);
    this.toggle_hide_ids("show");
  }

})



var fbwin_event = {
  obj: null,
  mousedown_handler: function(event){
    if (this.obj){
      this.obj.close();
    }
  }
}
fbwin_event.b_mousedown = fbwin_event.mousedown_handler.bindAsEventListener(fbwin_event);


//-------------------------------------------------------------
//------light box----------------------------------------------
//-------------------------------------------------------------

DoostangJS.Window.Lightbox = Class.create(DoostangJS.Window.Base,{
//var Doo_lightbox = Class.create({
  initialize:function($super, box_id, options){
    this.box_id = box_id;
    this.options = Object.extend({
      container_classname: "doo_modal",
      hide_ids: ["search_radius","expr_month_","expr_year_","feedback_tab"]
      }, options || {} );

    if ($(this.box_id).className == ""){
      $(this.box_id).addClassName(this.options.container_classname);
    }
    this.page_container_id = "page_wrap";
    this.overlay_id = DoostangJS.Window.page_overlay_id;
    //this.hide_ids =  ["search_radius","expr_month_","expr_year_","feedback_tab"];

    this.settings = {
      _wrap_html: '<div id="'+this.overlay_id+'" style="display:none"></div>'
    };
    if(!$(this.overlay_id)){
      $$('body').first().insert({
        bottom: this.settings._wrap_html
      });
    }

  //
    $super(this.box_id, this.options);
  },// end of initialize;


  show:function($super,html){
    DoostangJS.Event.ResizeOverlay();
    //resize_overlay();
    var box_dom = $(this.box_id)
    if (html)
      box_dom.innerHTML = html;

    box_dom.show();
    this.showOverlay();

    // to center the modal box in a page
    var box_width = box_dom.getWidth();
    var box_height = box_dom.getHeight();
    var view_port = document.viewport.getDimensions();

    var pageScroll = document.viewport.getScrollOffsets();
    var top = pageScroll.top + parseInt( (view_port.height - box_height)/2 );
    var left = pageScroll.left + parseInt( (view_port.width - box_width)/2 );

    box_dom.setStyle({
      'top': top + 'px',
      'left': left + 'px'
    });
    box_dom.style.width = this.options.width;
    $super();
  },
  
  close: function($super){
    this.hide_modal();
    this.hide_overlay();
    $super();
  },

  showOverlay :function(){
    var objOverlay = $(this.overlay_id);
    objOverlay.show();

  },
  hide_overlay:function(){
    var objOverlay = $(this.overlay_id);
    objOverlay.hide();

  },

  hide_modal:function(){
    if ($(this.box_id))
      $(this.box_id).hide();

  }


})


/**************************************************************************/
/*    Auto fill */
/**************************************************************************/

DoostangJS.Action = {};
DoostangJS.Action.Auto_fill = Class.create({
  initialize: function(options){
    this.element_id = options.element_id;
    this.default_value = options.default_value;
    this.blur_class = options.blur_class;
    if (options.value){
      $(this.element_id).value =  options.value;
    }else{
      $(this.element_id).value = this.default_value;
      $(this.element_id).addClassName(this.blur_class);
    }

    //Event.observe(this.element_id, 'click',this.focus_handler.bindAsEventListener(this));
    Event.observe(this.element_id, 'focus',this.focus_handler.bindAsEventListener(this));
    Event.observe(this.element_id, 'blur' ,this.blur_handler.bindAsEventListener(this));
  },
  focus_handler: function(){
    $(this.element_id).removeClassName(this.blur_class);
    if( this.default_value == $(this.element_id).value.strip()  ){
      $(this.element_id).value = '';
    }else{

    }

      return;
  },
  blur_handler: function(){
    var value = $(this.element_id).value.strip();
    if (value == this.default_value || value == ""){
      $(this.element_id).addClassName(this.blur_class)
      $(this.element_id).value = this.default_value;
    }else{
      $(this.element_id).removeClassName(this.blur_class);
    }
    return;
  },
  clear_default: function(){
    if( this.default_value == $(this.element_id).value.strip()  )
      $(this.element_id).value = "";
  }

})

/**************************************************************************/
/*    Form */
/**************************************************************************/

DoostangJS.Form = Class.create({
  initialize: function(options){
    this.form_id = options.form_id;
    this.submit_btn_id = options.submit_btn_id;
    this.spinner_id = options.spinner_id || "form_spinner";
    if ( !$(this.spinner_id) )
      this._insert_spinner_element();
    this.hide_spinner();
    Event.observe(this.form_id, 'submit',this.submit_handler.bindAsEventListener(this));
  },

  submit_handler: function(){
    this.show_spinner();
  },
  
  hide_spinner: function(){
    $(this.submit_btn_id).show();
    $(this.submit_btn_id).enable();
    if ( $(this.spinner_id))
        $(this.spinner_id).hide();
  },
  
  show_spinner: function(){
    $(this.submit_btn_id).hide();
    if ( $(this.spinner_id))
      $(this.spinner_id).show();
  },

  _insert_spinner_element: function(){
    var str_spinner = '<img id="form_spinner" src="/images/spinner.gif" alt="Processing" />';
    new Insertion.After(this.submit_btn_id, str_spinner);
    this.spinner_id = "form_spinner";
    $(this.spinner_id).hide();
  }
})

