Twitter = {
  initialize:function(user, location, count){
    if(!$("tweets_wrapper")) return;

    Twitter.user = user;
    Twitter.location = location;
    Twitter.count = count;

    Twitter.extendString();
    Twitter.start()
  },

  extendString:function(){
    String.prototype.parseURL = function() {
      return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/, function(url) {
        return url.link(url);
      });
    };

    String.prototype.parseUsername = function() {
      return this.replace(/[@]+[A-Za-z0-9-_]+/, function(u) {
        var username = u.replace("@","")
        return u.link("http://twitter.com/"+username);
      });
    };

    String.prototype.parseHashtag = function() {
      return this.replace(/[#]+[A-Za-z0-9-_]+/, function(t) {
        var tag = t.replace("#","%23")
        return t.link("http://search.twitter.com/search?q="+tag);
      });
    };
  },

  start:function(){
    var script = document.createElement("script");
    script.src = "http://twitter.com/statuses/user_timeline.json?screen_name=" + Twitter.user + "&count=" + Twitter.count + "&callback=Twitter.render"
    document.getElementsByTagName("HEAD")[0].appendChild(script);
  },

  render:function(resp){
    $A(resp).each(function(tweet){
      $(Twitter.location).insert({bottom:'<div class="tweet_row"><!--<img class="twitter_avatar" src="'+ tweet.user.profile_image_url +'"/>--><div class="tweet_text">'+tweet.text.parseURL().parseUsername().parseHashtag()+'</div><br class="clear_left"</div>'});
    });

    $(Twitter.location).insert({bottom:'<div id="tweets_more"><a href="http://twitter.com/clrlysimple">Follow Us</a></div>'});
    $("tweet_busy").remove();
  }
}

Wordpress = {
  initialize:function(location, count){
    if(!$("blog_wrapper")) return;

    Wordpress.location = location;
    Wordpress.count = count;

    Wordpress.start();
  },

  start:function(){
    var script = document.createElement("script");
    script.src = "http://blog.clearlysimplesolutions.com/?feed=json&callback=Wordpress.render"
    document.getElementsByTagName("HEAD")[0].appendChild(script);
  },

  render:function(resp){
    $A(resp.items).each(function(post, i){
      if(i === Wordpress.count) return;

      var html = $A(['<div class="blog_snippet"><div><span class="blog_poster">Posted by: ']);
      html.push(post.author);
      html.push('</span>&nbsp;<span class="blog_time">');
      html.push(post.when);
      html.push('</span></div><p>');
      html.push(post.description);
      html.push('</p><a href="');
      html.push(post.link);
      html.push('" class="blog_read_more">Read More &gt;</a></div>');
      $(Wordpress.location).insert({bottom:html.join('')});

    });

    //$(Wordpress.location).insert({bottom:'<div id="blog_all"><a href="http://blog.clearlysimplesolutions.com">Read All</a></div>'});
    $("blog_busy").remove();

  }
}


QuestionFader = {
  interval:null,
  lastIndex:-1,
  questions:[
    {image:"img/caroclips/new_software.jpg", text:"Do I really need to buy new software?"},
    {image:"img/caroclips/crm.jpg", text:"Should I get a CRM system?"},
    {image:"img/caroclips/saas.jpg", text:"Should I go with SaaS?"},
    {image:"img/caroclips/cloud.jpg", text:"What is a &quot;Cloud&quot;?"},
    {image:"img/caroclips/find_me_online.jpg", text:"Can my customers find me online?"},
    {image:"img/caroclips/social_media.jpg", text:"Could social media help my business?"},
    {image:"img/caroclips/seo.jpg", text:"What is SEO and why is it important?"},

    {image:"img/caroclips/access_files.jpg", text:"How can I access my files when I am at a customer site?"},
    {image:"img/caroclips/twitter.jpg", text:"What is Twitter?"},
    {image:"img/caroclips/facebook.jpg", text:"What is Facebook?"},
    {image:"img/caroclips/apps_online.jpg", text:"Can customers make appointments online?"},
    {image:"img/caroclips/pay_online.jpg", text:"Can my customers pay me online?"},
    {image:"img/caroclips/blog.jpg", text:"Should I blog?"},
    {image:"img/caroclips/newsletter.jpg", text:"How can I email my customers a weekly newsletter?"},
    {image:"img/caroclips/efficient_employees.jpg", text:"How can I help my employees be more efficient?"},
    {image:"img/caroclips/work_closely.jpg", text:"How can I work more closely with my customers?"},
    {image:"img/caroclips/technospeak.jpg", text:"Help me with all the techno-speak!"}
    /**
    ,{image:"img/caroclips/temp_6.jpg", text:"How can I keep track of my clients?"},
    {image:"img/caroclips/temp_7.jpg", text:"Can my remote employees work better?"},
    {image:"img/caroclips/temp_13.jpg", text:"Are we using our technology correctly?"}
    */
  ],

  run:function(){
    QuestionFader.timeout = window.setTimeout(QuestionFader.fadeOne, 3000);
  },

  fadeOne:function(){
    if(!$("questions_wrapper")) return;

    var parent = $("questions_carosaulomobile");
    var index = Math.floor(Math.random()*7);
    var q = QuestionFader.questions[Math.floor(Math.random()*QuestionFader.questions.length)];

    while(parent.innerHTML.indexOf(q.text) != -1){
      q = QuestionFader.questions[Math.floor(Math.random()*QuestionFader.questions.length)];
    }

    while(index == QuestionFader.lastIndex){
      index = Math.floor(Math.random()*7);
    }
    QuestionFader.lastIndex = index;

    var el = $("q"+index);
    new Effect.Fade(el, {
      to:0.01,
      duration:2.2,
      afterFinish:function(){

        el.down("img").writeAttribute("alt", q.text).src = q.image;
        el.down("div").update(q.text);

        new Effect.Appear(el, {
          from:0.01,
          duration:2.2,
          afterFinish:QuestionFader.run
        });
      }
    });
  }
}

document.observe("dom:loaded", function(){
  Twitter.initialize('clrlysimple', 'tweets_wrapper', 4);
  Wordpress.initialize('blog_wrapper', 2)
  QuestionFader.run();
});
