

var __BidWidgets = (__BidWidgets) ? __BidWidgets : [];
BidWidgets = {
  widgetTld: 'bidbii.com',
  widgetPath: 'http://bidbii.com/widget',
  refVar: 'iw',
  
  widget: function(opts) {
    var w = new BidWidget(opts);
    w.show();
    __BidWidgets.push(w);
//      alert('w,id: '+w.getId() );
    
//    for(var i in __BidWidgets) {
//      var w = __BidWidgets[i];
//      alert('__BidWidgets i: ' + i+ '; id: '+w.getId() );
// //      alert('w: ' + w);
//     }
    
  },
  
  getElem: function(id) {
    return document.getElementById(id);
  },
  
  rand: function(path) {
    return Math.floor(Math.random()*100000);
  },
  
  getWidgetPath: function() {
    return ("https:" == document.location.protocol) ? BidWidgets.widgetPath : BidWidgets.widgetPath;
  }
  
};


function BidWidget(opts) {
  
  this._opts = opts ? opts : {};
  this._wid = opts.wid ? opts.wid : 'w_' + BidWidgets.rand();
  this._parent = opts.parent ? opts.parent : '';
  this._ref = opts.referrer ? opts.referrer : 0;
  this._target = opts.target ? opts.target : 0;
  
  this._type =  opts.type ? opts.type : 'box';
  this._effect = opts.effect ? opts.effect : '';
  this._style = opts.style ? opts.style : '';
//   this._bgcolor = opts.bgcolor ? opts.bgcolor : '';
  this._width = (opts.width && opts.width == 160) ? opts.width : 180;
  this._height = opts.height ? opts.height : '';
  this._items = opts.items ? opts.items : '';
  
  this._title = opts.title ? opts.title : '';
  this._title_color = opts.title_color ? opts.title_color.replace('#', '') : '';
  
  this._show_share = opts.show_share ? opts.show_share : '';
  
  // func
  this.show = show;
  this.getId = getId;
  this.getQuery = getQuery;
  
  // sanity
  if( this._type == 'box' ){
   this._height = '300';
   }
  
  
  
  function getId() {
    return this._wid;
  }
  function getQuery() {
    return '' +
           ( (!this._ref)    ? '' : '&'+ BidWidgets.refVar +'='  + this._ref ) +
           ( (!this._target) ? '' : '&target='   + this._target ) +
           
           ( (!this._type)   ? '' : '&type='   + this._type ) +
           ( (!this._effect) ? '' : '&effect='  + this._effect ) +
           ( (!this._style)  ? '' : '&style='  + this._style ) +
           
           ( (!this._title)  ? '' : '&title='  + encodeURIComponent(this._title) ) +
           ( (!this._title_color)  ? '' : '&title_color='  + encodeURIComponent(this._title_color) ) +
           
           ( (!this._width)  ? '' : '&width='  + this._width ) +
           ( (!this._height) ? '' : '&height=' + this._height ) +
           ( (!this._items)  ? '' : '&items='  + this._items ) +
           
           ( (!this._show_share)  ? '' : '&show_share='  + this._show_share )
           ;
  }
  function show() {
    if( !this._frame_html ){
     // Old style - todo: DOM!!!
     var fsrc = BidWidgets.getWidgetPath() + '?' + this.getQuery();
     this._frame_html = '<div id="widget-wrapper" style="display:block;width:'+this._width+'px;height:'+this._height+'px;">' +
                           '<div id="widget-inside">' +
                             '<iframe src="' + fsrc + '" id="w-frm-'+this._wid+'" allowTransparency="true" scrolling="no" frameborder="0" height="'+this._height+'px" width="'+this._width+'px"></iframe>' +
                           '</div>' +
                         '</div>'
                         ;
     var pe;
     if( this._parent ) {
      pe = BidWidgets.getElem(this._parent);
      //alert('pe: '+pe );
      }
     if( pe ){
      pe.innerHTML = this._frame_html;
      }
     else{
      document.write(this._frame_html);
      }
     }
  }
  
};



