﻿var Saharan = {
  Version: '0.0.1',
  prototypeVersion: parseFloat(Prototype.Version.split(".")[0] + "." + Prototype.Version.split(".")[1])
}

Saharan.Controls ={
  Items:[]
}
Saharan.Controls.DataGrid =Class.create();
Saharan.Controls.DataGrid.prototype = {
  initialize:function(table,options){
    this.container = $(table);
    this.options ={
      containerClass: 'DataGrid_Container',
      headerClass:    'DataGrid_Header',
      itemClass:      'DataGrid_Item',
      alterItemClass: 'DataGrid_AlternatingItem',
      footerClass:    'DataGrid_Footer'
    }
    this.options = Object.extend(this.options, options || {});
    options = this.options;
    this.container.className =  this.options.containerClass;
    if(this.container.tHead){
      this.container.tHead.className =this.options.headerClass;
    }
    $A(this.container.tBodies).each(function(tBody){
      $A(tBody.rows).each(function(tr){
        if(tr.rowIndex%2==0){
          tr.className = options.itemClass;
        }else{
          tr.className = options.alterItemClass;
        }
      })
    });
  }
}

Saharan.Controls.Repeater =Class.create();
//define repeater item  as span td div with shrctl="repeateritem"
Saharan.Controls.Repeater.prototype = {
  initialize:function(container,options){
    this.container = $(container);
    if(!this.container){
       return void(0);
    }
    this.options ={
      repeatercss:  'repeater',
      itemcss:      'repeateritem',
      hovercss:     'repeaterhover',
      activecss:    'repeateractive',
      repeaterstyle:{},
      itemstyle:    {},
      hoverstyle:   {},
      activestyle:  {}
    }
    this.options = Object.extend(this.options, options || {});

    this.container.className =this.options.repeatercss;
    this.container.setStyle(this.options.repeaterstyle);
    this.selectedItem =null;
    this.repeaterItems =[];
    this.parseItems();
    //this.repeaterItems = this.parseItems();
    
    //Saharan.Controls.Items.push(this);
    //this.select(this.repeaterItems[0]);
    //alert(this.container.innerHTML);
  },
  addItem: function(item){
    if(item && item.key){
      this.repeaterItems[item.key]=item;
    }
    else{
      this.repeaterItems.push(item);
    }
  },
  parseItems: function(){
    var repeater = this;
    $A(this.container.rows || this.container.childNodes).each(function(node){
      if(node.shrctl && node.shrctl=="repeateritem"){
        repeater.addItem(new Saharan.Controls.RepeaterItem(repeater,node));
      }
      else if(node.cells){
          $A(node.cells).each(function(cellnode){
          if(cellnode.shrctl && cellnode.shrctl=="repeateritem"){
           repeater.addItem(new Saharan.Controls.RepeaterItem(repeater,cellnode));
          }
         });
      }  
    });
  }, 
  select: function(item){
    if(this.selectedItem != item){
        var repeater = this;
        var preSelectedItem = this.selectedItem;
        
        this.selectedItem = item;
        if(preSelectedItem){
            preSelectedItem.selected = false;
            preSelectedItem.container.className = repeater.options.itemcss;
            preSelectedItem.container.setStyle(repeater.options.itemstyle);
        }
        this.selectedItem.selected = true;
        this.selectedItem.container.className = repeater.options.activecss;
        this.selectedItem.container.setStyle(repeater.options.activestyle);
        if(this.onChange)
          this.onChange(preSelectedItem,this.selectedItem);
    }
    if(this.onSelect)
      this.onSelect(this.selectedItem);
  },
  show:function(){
    this.container.show();
  },
  hide:function(){
    this.container.hide();
  }
}

Saharan.Controls.RepeaterItem =Class.create();

Saharan.Controls.RepeaterItem.prototype = {
  initialize: function(repeater,container){
    this.repeater = repeater;
    this.container = $(container);
    this.key = this.container.repeateritemkey || null;
    this.value = this.container.repeateritemvalue || null;
    this.selected = false;
    Event.observe(this.container, 'mouseover', this.onMouseOver.bindAsEventListener(this));
    Event.observe(this.container, 'mouseout', this.onMouseOut.bindAsEventListener(this));
    Event.observe(this.container, 'mousedown', this.onMouseDown.bindAsEventListener(this));
    Event.observe(this.container, 'mouseup', this.onMouseUp.bindAsEventListener(this));
    Event.observe(this.container, 'focus', this.onFocus.bindAsEventListener(this));
    Event.observe(this.container, 'blur', this.onBlur.bindAsEventListener(this));
    Event.observe(this.container, 'click', this.onClick.bindAsEventListener(this));

    

    this.select = this.onClick.bind(this);
    this.container.className = this.repeater.options.itemcss;
    this.container.setStyle(this.repeater.options.itemstyle);

  },
  onMouseOver:function(event){
    this.container.className = this.repeater.options.hovercss;
    this.container.setStyle(this.repeater.options.hoverstyle);
  },
  onMouseOut:function(event){
    if(!this.selected)
    {
       this.container.className = this.repeater.options.itemcss;
       this.container.setStyle(this.repeater.options.itemstyle);
    }
    else
    {
       this.container.className = this.repeater.options.activecss;
       this.container.setStyle(this.repeater.options.activestyle);
    }
  },
  
  onMouseDown:function(event){
  },
  onMouseUp:function(event){
  },
  onFocus:function(event){
  },
  onBlur:function(event){
  },
  
  onClick:function(event){
    this.repeater.select(this);
    this.container.className = this.repeater.options.activecss;
    this.container.setStyle(this.repeater.options.activestyle);
    //alert(this.repeater.container.innerHTML);
  }
  
}

Saharan.Controls.SmartIframe =Class.create();

Saharan.Controls.SmartIframe.prototype={
  initialize:function(iframe,minWidth,minHeight){
    this.iframe = $(iframe);
    this.location = this.iframe ? this.iframe.src:'';
    this.minWidth = minWidth;
    this.minHeight = minHeight;
    this.loadListener = this.onload.bindAsEventListener(this);
    this.winUnloadListener = this.onWinUnload.bindAsEventListener(this);
    this.resizeListener = this.resizeIframe.bindAsEventListener(this);
    Event.observe(this.iframe, 'load', this.loadListener);
  },
  load: function(uri){
    this.iframe.src = uri;
    this.iframe.height ="100%";
    this.iframe.width = "100%";
  },
  onload:function(event){
    this.location = this.iframe.contentWindow.location.href;
    Event.observe(this.iframe.contentWindow.document.body,'click',this.resizeListener);
    Event.observe(this.iframe.contentWindow,'unload',this.winUnloadListener);
    this.resizeIframe();
    if(this.onComplete)
      this.onComplete(this.location);
  },
 onWinUnload:function(event){
    var iBody =this.iframe.contentWindow.document.body;
    var iWindow =this.iframe.contentWindow;
    Event.stopObserving(iBody,'click',this.resizeListener);
    Event.stopObserving(iWindow,'unload',this.winUnloadListener);
    Event.observers=Event.observers.reject(function(value){
       return value && (value[0]==iBody  || value[0]==iWindow) ;
    });
    this.iframe.height ="100%";
    this.iframe.width = "100%";
  },
  resizeIframe:function(event){
   if(this.iframe.contentWindow.document.body){
        var w,h;
        w = this.iframe.contentWindow.document.body.scrollWidth;
        h = this.iframe.contentWindow.document.body.scrollHeight;
        if(this.minWidth && this.minWidth>w){
          w = this.minWidth;
        }
        if(this.minHeight && this.minHeight>h){
          h = this.minHeight;
        }
        this.iframe.height = h ;
        this.iframe.width =  w ;
    }
  }
}
