// Application
tr.app = function(){
  
  return {
    
    /**
    * initialize the app
    **/
    init : function(mode) {
      var query = this.getQueryFromUrl();
      if (query || mode) {
        if (query) {
          $('q').value = query; 
          tr.twitter.search(query);
        } else if (mode != 'single') {
          query = 'from:' + mode;
          $('q').value = query; 
          tr.twitter.search(query);
        } 
        $(document.body).addClassName('has_query');
        tr.player.playStatic();
      } else {
        $(document.body).addClassName('no_query');
      }
      if (mode === 'single') {
        this.singleTweetMode = true;
        this.setupSingleTweetUI();
      } else {
        this.setupMainUI();
      }
      tr.player.play();
    },
    
    /**
    * get the query from the url
    **/
    getQueryFromUrl : function() {
      this.query = document.location.href.toQueryParams()["q"];
      return this.query;
    },
    
    /**
    * setup the ui
    **/
    setupMainUI : function() {
      // this.animateClouds();
      this.animateTower();
      this.starrySky();
      tr.twitter.getTrends();
      var searchButton = $('search_button');
      searchButton.onclick = this._onSearchButtonClick.bind(this);
      searchButton.onmousedown = this._onSearchButtonMousedown.bind(this);
      searchButton.onmouseup = this._onSearchButtonMouseup.bind(this);
      $('q').focus();
      $('tabs').observe('click', this._onStationClick.bindAsEventListener(this));
    },
    
    /**
    * setup the single tweet ui
    **/
    setupSingleTweetUI : function() {
      // this.animateClouds();
      this.animateTower();
      this.starrySky();
      tr.twitter.getTweet(window.TWEET_ID);
      tr.twitter.getTrends();
      var searchButton = $('search_button');
      searchButton.onclick = this._onSearchButtonClick.bind(this);
      searchButton.onmousedown = this._onSearchButtonMousedown.bind(this);
      searchButton.onmouseup = this._onSearchButtonMouseup.bind(this);
      $('q').focus();
      $('tabs').observe('click', this._onStationClick.bindAsEventListener(this));
    },
    
    /**
    * ui for a page 
    **/
    setupPageUI : function() {
      this.animateClouds();
      this.animateTower();
      this.starrySky();
    },
    
    /**
    * generate a starry sky
    */
    starrySky : function(){
      var x=0;
      var stars = [];
      while(x < 85) {
        var star = new Element('img', {src : '/images/star.png', className : 'star'});
        star.style.position = 'absolute';
        star.opacity = Math.random();
        star.velocity = 1;
        star.style.left = Math.ceil(Math.random()*95).toString() + '%';
        star.style.top = Math.ceil(Math.random()*150).toString() + 'px';
        star.style.zIndex = 3;
        var dim = Math.ceil(Math.random()*15);
        star.width = star.height = dim;
        $(document.body).insert({top:star});
        stars.push(star);
        if (!Prototype.Browser.IE) {
          star.setStyle({opacity : star.opacity});
        }
        x++;
      }
      if (Prototype.Browser.IE) return;
      setInterval(function(){
        stars.each(function(star){
          setTimeout(function(){
            star.setStyle({opacity : star.opacity});
            if(star.opacity >= 1 || star.opacity <= 0) star.velocity = star.velocity *-1;
            star.opacity = star.opacity + star.velocity * 0.15 * Math.random();
          },10 * Math.random());
          // star.height = star.width = (star.width + star.velocity);
          // star.style.paddingTop = (15-star.height)+'px';
          // star.style.paddingLeft = (15-star.width)+'px';
        });
      }, 100);
    },
    
    /**
    * 
    **/
    animateClouds : function() {
      this.bgPos = 0;
      setInterval(function(){
        if (this.bgPos >= 10000) {
          this.bgPos = 0;
        }
        $('clouds').setStyle({backgroundPosition : this.bgPos.toString() + 'px 0px'});
        this.bgPos = this.bgPos + 1;
      }.bind(this), 75);
    },
    
    /**
    * animate the radio tower
    **/
    animateTower : function() {
      if (Prototype.Browser.IE) return;
      this.waves = this.waves || 0;
      if (this.waves < 10) {
        this.paper = this.paper || Raphael($('waves'), 200, 200);
        var wave = this.paper.path("M75.241,59.831c-3.095-6.598-9.794-11.169-17.564-11.169c-7.769,0-14.468,4.571-17.563,11.169");

        this.waves++;
        wave.attr("stroke", "#fff");  
        wave.attr("stroke-opacity", 0.5);
        wave.attr("stroke-width", 2);
        wave.animate({path : "M114.902,36.889C104.818,15.393,82.991,0.5,57.677,0.5c-25.313,0-47.14,14.893-57.224,36.389", "stroke-width" : 8, "stroke-opacity" : 0}, 12000, function(){this.waves--;}.bind(this));
      }
      setTimeout(this.animateTower.bind(this), 3500);
    },
    
    /**
    * open the feedback window from Uservoice
    **/
    openFeedback : function() {
      UserVoice.Popin.show(); 
      UserVoice.Metrics.track('tab_click_conversion', 2, 'Tab Click'); 
      return false;
    },
    
    /**
    * share tweet
    **/
    share : function(twid) {
      tr.twitter.shareTweet('http://tweetrad.io/tweet/' + twid);
    },
    
    /**
    * station clicked
    **/
    _onStationClick : function(e) {
      var elem = Event.element(e);
      q = this.stations[elem.innerHTML].join(' OR ');
      window.location.href = "http://tweetrad.io?q=" + escape(q);
    },
    
    /**
    * 
    **/
    _onSearchButtonMousedown : function() {
      var searchButton = $('search_button');
      searchButton.addClassName('active');
      return true;
    },
    
    /**
    * 
    **/
    _onSearchButtonMouseup : function() {
      var searchButton = $('search_button');
      searchButton.removeClassName('active');
      return true;
    },
    
    /**
    * 
    **/
    _onSearchButtonClick : function() {
      var searchForm = $('search_form');
      searchForm.submit();
      return true;
    },
    
    stations : {
      tech : [
        'from:mashable',
        'from:engadget',
        'from:arstechnica',
        'from:techcrunch',
        'from:cnetnews',
        'from:techmeme',
        'from:hackernews'
      ],
      funny : [
	    'from:comictwit',
        'from:shitmydadsays',
        'from:attell',
        'from:stephenathome',
      ],
      news : [
        'from:CBSNews',
        'from:bbcnews',
        'from:nprnews',
        'from:nytimes',
        'from:cnn'
      ],
      sports : [
        'from:NBA',
        'from:espn',
        'from:cnnsportsnews'
      ],
      featured : [
        'from:shitmydadsays'
      ]
    }
    
  };
}();