;(function($){
  $.site = {
    start: function() {
      monitorLinkableTags();
      monitorTabs();
      monitorLoginForm();
      defaultCommentCopy();
      monitorFlash();
      monitorFavorites();
    }
  };

  function monitorLinkableTags(){
    $('#tag_box').click($.delegate({
      '.linkable_tag': function(e){
        tag_list_field = $('.tag_list_field');
        var link = e.target;
        $(e.target).toggleClass('selected');
        var text = link.text;
        var currentVal = tag_list_field.val();
        var newVal = '';
        var currentValWords = currentVal.split(',')
        newVal = newTagList(currentVal, text);
        tag_list_field.val(newVal);
        return false;
      }
    }));
  }

  function newTagList(tagString, newTag){
    var separator = ',';
    var words = tagString.split(separator)
    var newWords = [];
    var positionOfText = -1;
    var cleanedWord = '';
    var hasNewTag = false;

    // For each word
    // clean it
    // see if it's in our new array already
    // if not, add it
    for(i=0; i <= words.length-1; i++){
      cleanedWord = words[i].replace(/^\s/, '').replace(/\s$/, '');
      positionOfText = newWords.indexOf(newTag);
      if(cleanedWord == newTag){
        hasNewTag = true;
      }else if (positionOfText == -1 && cleanedWord != ''){
        newWords.push(cleanedWord)
      }
    }
    if(hasNewTag == false){
      newWords.push(newTag);
    }
    return newWords.join(', ');

  }

  function monitorTabs(){
    $('.tabs').click($.delegate({
      '.tab_link': function(clicked){
        var htmlClass = $(clicked.target).attr('class');
        var tabClass = htmlClass.replace('tab_link', '').replace(/^\s/, '');
        
        $('.tabs').children('span.tab_link').each(function(tab_link){
          var text = $(this).text();
          var thisClass = $(this).attr('class');
          var link = $("<a>").html(text).attr('class', thisClass).attr('href', '#');
          $(this).replaceWith(link);
        });

        var text = $(clicked.target).text();
        var span = $("<span>").text(text).attr('class', htmlClass);
        $(clicked.target).replaceWith(span);

        $('.tabs').children('.tab').each(function(tab){
          if($(this).hasClass(tabClass)) {
            $(this).show();
          }else{
            $(this).hide();
          }
        });
        return false;
      }
    }));
    /*$('#close_tab').click(function(){
      $(this).parents('.tab:first').hide().after($("<div>").html('Image Saved'));
    });*/
  }

  function monitorLoginForm(){
    var form = $('#login_form');
    var username = form.children(':text:first');
    var password = form.children(':password:first');
    if(username.val() == undefined){
      username.val('username');
    }
    if(password.val() == undefined){
      username.val('password');
    }
  }

  function defaultCommentCopy(){
    var comment_body = $('#comment_body');
    if(comment_body.val() == ''){
      comment_body.val('type your comment here...');
      comment_body.css({'color':'#999'});
    }
    comment_body.focus(function(){
      if(comment_body.val() == 'type your comment here...'){
        comment_body.val('').css({'color':'#333'});
      }
    });
  }

  function monitorFlash(){
    $('.flash.notice')
    .animate({opacity: 1.0}, 3000) // this is just for a timeout effect
    .animate({opacity: 0.0}, 500)
    .slideUp()

  }

  function monitorFavorites(){
    $('a.delete_favorite').hide();
    $('div.favorite').hover(function(){
      $(this).children('a.delete_favorite').show();
    },
    function(){
      $(this).children('a.delete_favorite').hide();
    });
  }

})(jQuery);
