$(function() {  
  $.blog.blog101tags = new Array('Products','Computers', 'EcoDesign');
  $.blog.init();
  
  
});

$.blog = {
  init: function() {
    var $blogNav = $('li.page-item-536 a.toplevel');
    $blogNav.addClass('selected');
    
    //removes duplicates and mutliple categories from recent category list
    var recentCats = $('ul#recentCategories');
    var lis = recentCats.find('li');
    var innerHTML = '';
    var listArray = new Array;
    lis.each(function() {
      innerHTML = $(this).html();
      if(innerHTML.search(',') != -1) {
        var splitMe = innerHTML.split(',');
        listArray.push(splitMe[0]);
      }
      else
        listArray.push(innerHTML);
    });
    //listArray.sort(); uncomment if recent cats need to be alpha-sorted in the future
    listArray = _RemoveDuplicates(listArray);
    recentCats.html('');
    for(i = 0; i < 5; i++) {
      innerHTML = '<li>' + listArray[i] + '</li>';
      recentCats.append(innerHTML);
    }
    
    var recentTags = $('ul#recentTags');
    lis = recentTags.find('li');
    innerHTML = '';
    var tagArray = new Array;
    lis.each(function() {
      innerHTML = $(this).html();
      if(innerHTML.search(',') != -1) {
        var splitMe = innerHTML.split(',');
        tagArray.push(splitMe[0]);
      }
      else
        tagArray.push(innerHTML);
    });
    //tagArray.sort(); uncomment if recent tags need to be alpha-sorted in the future
    tagArray = _RemoveDuplicates(tagArray);
    recentTags.html('');
    for(i = 0; i < 5; i++) {
      innerHTML = '<li>' + tagArray[i] + '</li>';
      recentTags.append(innerHTML);
    }
    
    
    //finds tag of most recent posts and uses that to fire off Blog101 module
    var $firstPost = $('div.post:eq(0)');
    var tag = 0;
    var tags = $firstPost.find('div.tags a');
    tags.each(function() {
      for(i = 0; i < $.blog.blog101tags.length; i++){
        if($(this).html() == $.blog.blog101tags[i]){
          tag = $(this).html();
        }
      }
      if(tag)
        return false;         
    });
    $.blog.blog101(tag);

     
  /*  Sharing  */
    var $sharing = $('div#sharing div.content div.relatedPosts');
    var ajaxURL = $.templatePath + '/related-posts-tags.php?tag=sharing';
    
    $sharing.load(ajaxURL);
  
    this.blogEditors();
    
    var $tagTrigger = $('a#seeTags');
    var $tagCloud = $('div#tag-cloud');
    var tagCloudOptions = {
          'trigger'  : $tagTrigger,
          'cloud' : $tagCloud
    }
    
    this.blogTagCloud(tagCloudOptions);
    
    var $catTrigger = $('a#seeCategories');
    var categoryExpandOptions = {
      'targetList' : recentCats,
      'origHTML': recentCats.html(),
      'expandID': 'category',
      'trigger': $catTrigger
    }
    
    this.blogSideList(categoryExpandOptions);
    
    
    var $authorTrigger = $('a#seeContributors');
    var $recentAuthors = $('ul#recentContributors');
    innerHTML = '';
    var authorArray = new Array;
    var authors = $recentAuthors.find('li');
    authors.each(function() {
      innerHTML = $(this).html();
      if(innerHTML.search(',') != -1) {
        var splitMe = innerHTML.split(',');
        authorArray.push(splitMe[0]);
      }
      else
        authorArray.push(innerHTML);
    });
    //listArray.sort(); uncomment if recent cats need to be alpha-sorted in the future
    authorArray = _RemoveDuplicates(authorArray);
    $recentAuthors.html('');
    for(i = 0; i < 5; i++) {
      innerHTML = '<li>' + authorArray[i] + '</li>';
      $recentAuthors.append(innerHTML);
    }
    
    var authorExpandOptions = {
      'targetList' : $recentAuthors,
      'origHTML': $recentAuthors.html(),
      'expandID': 'contributors',
      'trigger': $authorTrigger
    }
    
    this.blogSideList(authorExpandOptions);
    
    this.blogRelatedPosts();
    this.postsFlash();
  },
  
  blog101: function(tag) {
    var $blog101 = $('div#blog101 div.content');
    if(tag) {
      var $title = $('div#blog101 h2');
      $title.html(tag + ' 101');
      
      var ajaxURL = $.templatePath + '/blog-101-info.php?tag=' + tag;  
      
      $blog101.load(ajaxURL, function() {
        $blog101.origHTML = $blog101.html();
        var innerHTML = $blog101.html();
        innerHTML = innerHTML.substring(0, 100);
        innerHTML += '...';
        $blog101.html(innerHTML);
        var $showMore = $('a#blog101-more');
        var $expand101 = $('#blog101-expand');
        $expand101.html($blog101.origHTML);
        
        var blog101ExpandOptions = {
          'targetList' : $blog101,
          'origHTML': $blog101.html(),
          'expandID': 'blog101',
          'trigger': $showMore
        }
        
        $.blog.blogSideList(blog101ExpandOptions);              
      });      
      
      ajaxURL = $.templatePath + '/related-posts-tags.php?tag=' + tag;
      var $relatedPosts = $('div#blog101 div.relatedPosts');
      $relatedPosts.load(ajaxURL);
    } else {
      $blog101.parents('.blog-rightcontent').remove();  
    }
  },
  
  blogEditors: function() {
    var $editorsList = $('ul#editors-list');
    var $links = $editorsList.find('a');
    var $featuredEditor = $('#featured-editor');
    $links.each(function() {
      $(this).click(function() {
        var $hiddenInfo = $(this).parent().find('div.info');
        $featuredEditor.css('display', 'none');
        $featuredEditor.html($hiddenInfo.html());
        $featuredEditor.fadeIn('slow');
        $(this).parent().parent().find('a').removeClass('selected');
        $(this).addClass('selected');
      });
    });
  },
  
  blogTagCloud: function(options) {
    var $trigger = options.trigger;
    var $cloud = options.cloud;
    var $close = $cloud.find('a#close-cloud');
    var $postFlash = $('div.postFlashContainer');
    $cloud.selfOpen = 0;
    
    var breaks = $cloud.find('br');
    breaks.each(function() {
      $(this).remove();           
    });
    
    var As = $cloud.find('a');
    As.each(function() {
      var fontSize = $(this).css('font-size');
      fontSize = fontSize.split('px');
      fontSize = fontSize[0];
      if(fontSize > 13)
        $(this).css('color', '#76B900');
    });
    
    $trigger.click(function(e) {
      var triggerPos = findPos($trigger.get(0));
      var left = triggerPos[0] + 85;
      $cloud.css('left', left);
      $cloud.css('top', triggerPos[1]);
      $cloud.selfOpen = 1;
      $postFlash.find('object').css('display', 'none');
    });
    
    $cloud.hide = function() {
      $(this).css('left', '-9999px');  
      $postFlash.find('object').css('display', 'block');
    }
    
    $close.click(function() {
      $cloud.hide();          
    });
    
    $(document).click(function(e){
      var triggerID = $trigger.attr('id');
      var targetID = $(e.target).attr('id');
      if(triggerID != targetID) {
        if($cloud.selfOpen) {           
          var parent = '#' + $cloud.attr('id');
          if(!$(e.target).parents(parent).get(0)) {
            $cloud.hide();
            $cloud.selfOpen = 0;
          }
        }
      }
    });
  },
  
  blogSideList: function(options) {
    var $targetList = options.targetList
    var $trigger = options.trigger;
    var origHTML = options.origHTML;
    var expandID = '#' + options.expandID + '-expand';
    var expandContent = $(expandID);
    $trigger.origHTML = $trigger.html();
    
    $trigger.click(function() {
      $targetList.hide('slow', function() {
        var innerHTML = $targetList.html();
        if(innerHTML == origHTML) {
          $targetList.html(expandContent.html());
          $trigger.removeClass('downArrow').addClass('upArrow').html('collapse');
        }
        else {
          $targetList.html(origHTML);
          $trigger.removeClass('upArrow').addClass('downArrow').html($trigger.origHTML);
        }
        $targetList.show('slow');
      });         
    });
    
  },
  
  blogRelatedPosts : function() {
    var $triggers = $('a.related-posts');
    $triggers.each(function() {
      $(this).click(function() {
        var $relatedPosts = $(this).parent().parent().parent().find('div.relatedPosts');
        if($relatedPosts.css('display') == 'none') {
          $relatedPosts.show('slow');
          $(this).removeClass('downArrow').addClass('upArrow');
        }
        else {
          $relatedPosts.hide('slow');
          $(this).removeClass('upArrow').addClass('downArrow');
        }
      });
    });
    
  },
 

  postsFlash: function() {
    var self = this;
    this.posts = $('div.post');
    this.posts.each(function() {
      var id = $(this).attr('id');
      var embedURL = $(this).find('input.postFlash').val();
      var flashHome = 'postFlash-' + id;
      var params = {background: "none"};
      var flashvars = "";
      swfobject.embedSWF(embedURL, flashHome, "480", "385", "9.0.0","expressInstall.swf", flashvars, params);
    });
  }
}