Rashlop = function(config) {
  this.shlop_timer = null;
  
  $.extend(this, {
    bannerId: 0,
    selector: '',
    url: '/bnrs/rashlop/',
    autoShlopDelay: 5000,
    maxShow: 1,
    showsStoreDays: 1
  }, config);
  
  this.init();
}

Rashlop.prototype = {
  init: function() {
    var show_counter = getCookie('rashlop');
    if(!show_counter)
      show_counter = 0;
    
    if(show_counter < this.maxShow) {
      setCookie('rashlop', ++ show_counter, this.showsStoreDays, '/');
      this.rashlop();
      this.startShlopTimer();
    } else{
      this.shlop();
    }
  },
  
  rashlop: function() {
    this.getEl().height(300);
    this._loadBanner('100%', 300, 'big');
  },
  
  shlop: function() {
    this.getEl().height(60);
    this._loadBanner('100%', 60, 'small');
  },
  
  startShlopTimer: function() {
    this.stopShlopTimer();
    
    this.shlop_timer = window.setTimeout($.proxy(this.onAutoShlop, this), this.autoShlopDelay);
  },
  
  stopShlopTimer: function() {
    if(this.shlop_timer) {
      window.clearTimeout(this.shlop_timer);
      this.shlop_timer = null;
    }
  },
  
  _loadBanner: function(width, height, type) {
    $.get('/bnrs/rashlop/' + this.bannerId + '/', {
      width: width,
      height: height,
      type: type
    }, $.proxy(this.onLoad, this));
  },
  
  getEl: function() {
    return $(this.selector);
  },
  
  onLoad: function(data) {
    var $flash = $(data);
    if($flash.is('.flash')) {
      var src = $('.src', $flash).html();
      new EmbedFlash($('.src', $flash).html(), 
      {
          allowScriptAccess: 'sameDomain',
          quality: 'high',
          //bgcolor: '#ffffff',
          wmode: 'transparent'
      },{
          width: $('.width', $flash).html(),
          height: $('.height', $flash).html()
      }).embed(this.selector);
    } else {
      this.getEl().html(data);
    }
  },
  
  onAutoShlop: function() {
    this.shlop();
  }
}
