var pagination = {
  init: function() {
    jQuery(".pagination ol li").each(function() {
      if (jQuery(this).find('a').html() == 'Previous') jQuery(this).addClass('first');
      if (jQuery(this).find('a').html() == 'Next') jQuery(this).addClass('last');
    })
  }
}

var height_adjuster = {
  init: function() {
    height_adjuster.size();
    jQuery(window).resize(function() {
      height_adjuster.size();
    })
  },

  size: function() {
    if (jQuery(document).height() > jQuery('body').height()) {
      var difference = jQuery(document).height() - jQuery('body').height();
      jQuery('body > .content').css('height', jQuery('.content').height() + difference);
    } else {
      jQuery("body .content").css('height', 'auto');
    }
  }
}

var ads = {
  init: function() {
    var count = 1;
    jQuery(".ads li").each(function() {
      jQuery(this).find('img').addClass('ad'+count);
      count = count + 1;
    });
  }
}

var nav = {
  init: function() {
    jQuery("#menu-navigation .sub-menu").each(function() {
      jQuery(this).find('> li:first').addClass('real-top');
      jQuery(this).find('> li:last').addClass('real-bottom');
      jQuery(this).find("> li:first a").mouseenter(function() {
        jQuery(this).parent().parent().find("> li.top:first").addClass("active");
      }).mouseleave(function() {
        jQuery(this).parent().parent().find("> li.top:first").removeClass("active");
      });
      jQuery(this).find("> li:last a").mouseenter(function() {
        jQuery(this).parent().parent().find("> li.bottom:first").addClass("active");
      }).mouseleave(function() {
        jQuery(this).parent().parent().find("> li.bottom:first").removeClass("active");
      });
    });
    
    jQuery("#menu-navigation .sub-menu").prepend("<li class='top'></li>");
    jQuery("#menu-navigation .sub-menu").append("<li class='bottom'></li>");
    jQuery("#menu-navigation > li").mouseenter(function() {
      if (jQuery(this).find(".sub-menu:first").size() > 0) {
        var submenu = jQuery(this).find(".sub-menu:first");
        jQuery(submenu).css('left', ((jQuery(this).width() - 155) / 2));
        jQuery(submenu).stop().css('opacity', 1.0).fadeIn();
      }
    }).mouseleave(function() {
      if (jQuery(this).find(".sub-menu").size() > 0) {
        jQuery(this).find(".sub-menu").stop().fadeOut();
      }
    });
    jQuery(".sub-menu li").mouseenter(function() {
      if (jQuery(this).find('.sub-menu:first').size() > 0) {
        var submenu = jQuery(this).find('.sub-menu:first');
        jQuery(submenu).css('left', 141).css('top', -10);
        jQuery(submenu).stop().css('opacity', 1.0).fadeIn();
      }
    });
  }
};

var accordion = {
  should_play: true,
  current: 0,
  
  init: function() {
    var ul = jQuery("ul.accordion");
    jQuery(ul).find("a.handle").click(function() {
      if (jQuery(this).parent().hasClass("open")) {
        return false;
      }
      accordion.current = parseInt(jQuery(this).html()) - 1;
      jQuery(ul).find("li.open").animate({width: 19}).removeClass("open");
      jQuery(this).parent().animate({width: 481}).addClass("open");
      return false;
    });

    if (ul.size() > 0) {
      setInterval('accordion.auto_play()', 10000);
      jQuery(ul).find('li').mouseenter(function() {
        accordion.should_play = false;
      }).mouseleave(function() {
        accordion.should_play = true;
      });
    }
  },

  auto_play: function() {
    if ((accordion.should_play == true) && (jQuery("body").hasClass('focused'))) {
      var numbers = ['one', 'two', 'three', 'four'];
      var ul = jQuery("ul.accordion");
      var next = accordion.current + 1;
      if (next > 3) next = 0;
      jQuery(ul).find('li.'+numbers[accordion.current]).animate({width:19}).removeClass("open");
      jQuery(ul).find('li.'+numbers[next]).animate({width:481}).addClass("open");
      accordion.current = next;
    }
  }
};

var tabs = {
  init: function() {
    jQuery(".triggers a").click(function() {
      if (jQuery(this).hasClass('current')) {
        return false;
      }
      jQuery(".triggers a").removeClass('current');
      jQuery(this).addClass('current');
      jQuery(".tabs .tab").hide();
      jQuery("#"+jQuery(this).attr('data-id')).show();
      return false;
    });
  }
};


var ribbons = {
  init: function() {
    ribbons.size()
    jQuery(window).resize(function() {
      ribbons.size();
    })
  },
  
  size: function() {
    var width = jQuery(document).width();
    var ribbon_width = (width - 1000) / 2;
    jQuery(".top-ribbon-left").css('width', ribbon_width);
    jQuery(".top-ribbon-right").css('width', ribbon_width);
    jQuery(".nav-ribbon-left").css('width', ribbon_width);
    jQuery(".nav-ribbon-right").css('width', ribbon_width);
    if (jQuery(window).width() < 1000) {
      jQuery(".nav-ribbon-right, .top-ribbon-right").hide();
    } else {
      jQuery(".nav-ribbon-right, .top-ribbon-right").show();
    }
  }
};

var social_sidebar = {
  init: function() {
    var side = jQuery(".social-sidebar");
    var width = jQuery(document).width();
    var side_width = (width - 1000) / 2;
    jQuery(side).css('left', side_width - 108);
    jQuery(side).css('top', 330);
    jQuery(side).fadeIn();
    jQuery(side).find('li').hide();
    setInterval('social_sidebar.check_scroll()', 10);
    jQuery('.social-sidebar .tab').click(function() {
      if (jQuery(side).hasClass('open')) {
        jQuery(side).removeClass('open').addClass('closed');
        jQuery('.social-sidebar ul li').fadeOut(function() {
          jQuery('.social-sidebar ul').animate({right: -66});
          jQuery('.social-sidebar .tab').animate({right: 0})
        });
      } else {
        jQuery(side).removeClass('closed').addClass('open');
        jQuery('.social-sidebar ul').animate({right: 7}, function() {
          jQuery('.social-sidebar ul li').fadeIn();
        });
        jQuery('.social-sidebar .tab').animate({right: 73})
      }
    })
    jQuery(window).resize(function() {
      var width = jQuery(document).width();
      var side_width = (width - 1000) / 2;
      jQuery(side).css('left', side_width - 108);
    });
  },

  check_scroll: function() {
    var side = jQuery(".social-sidebar");
    if (jQuery(document).scrollTop() > 270) {
      jQuery(side).css('position', 'fixed');
      jQuery(side).css('top', 65);
    }
    if (jQuery(document).scrollTop() < 273) {
      jQuery(side).css('position', 'fixed');
      jQuery(side).css('top', 350 - jQuery(document).scrollTop());
    }
    if (jQuery(document).scrollTop() > (jQuery(document).height() - 670)) {
      jQuery(side).css('position', 'fixed');
      jQuery(side).css('top', (jQuery(document).height() - 605) - jQuery(document).scrollTop());
    }
  }
};

jQuery(function() {
  nav.init();
  accordion.init();
  tabs.init();
  ribbons.init();
  social_sidebar.init();
  ads.init();
  height_adjuster.init();
  pagination.init();

  jQuery('body').addClass('focused');

  jQuery("#s").focus(function() {
    if (jQuery(this).val() == "Type text to") {
      jQuery(this).val("");
    }
  }).blur(function() {
    if (jQuery(this).val() == "") {
      jQuery(this).val("Type text to");
    }
  });

  jQuery("span.collapsing.archives.expand").live('click', function() {
    jQuery("li span.collapsing.archives.collapse").each(function() {
      jQuery(this).removeClass("collapse").addClass("expand");
      jQuery(this).parent().find('ul').slideUp();
    });
    jQuery(this).addClass("collapse").removeClass("expand");
    jQuery(this).parent().find('ul').slideDown();
  });
});

function displayTweets(obj) {
  var twitters = obj;
  var statusHTML = "";
  var username = "";
  for (var i=0; i<3; i++) {
    username = twitters[i].user.screen_name
    statusHTML += ("<li class='tweet-"+ (i+1) +"'><img src='"+twitters[i].user.profile_image_url+"' /><span class='body'>"+htmlifyTweet(twitters[i].text)+"</span><span class='info'><a class='tweet-link' href='http://twitter.com/#!/"+username+"/status/"+twitters[i].id+"'>"+relative_time(twitters[i].created_at)+"</a> - <a href='http://twitter.com/intent/tweet?in_reply_to="+twitters[i].id_str+"'>reply</a></span></li>")
  }
  document.getElementById('tweets').innerHTML = statusHTML;
}

function htmlifyTweet(tweet) {
  var txt = tweet.replace(/(https?:\/\/[-a-z0-9._~:\/?#@!$&\'()*+,;=%]+)/ig,'<a href="$1">$1</a>').replace(/@+([_A-Za-z0-9-]+)/ig,'<a href="http://twitter.com/$1">@$1</a>').replace(/#+([_A-Za-z0-9-]+)/ig,'<a href="http://search.twitter.com/search?q=%23$1">'+'#$1</a>');
  return txt;
}

function relative_time(time_value) {
  var values = time_value.split(" ");
  time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
  var parsed_date = Date.parse(time_value);
  var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
  var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
  delta = delta + (relative_to.getTimezoneOffset() * 60);
  if (delta < 60) {
    return 'less than a minute ago';
  } else if(delta < 120) {
    return 'about a minute ago';
  } else if(delta < (60*60)) {
    return (parseInt(delta / 60)).toString() + ' minutes ago';
  } else if(delta < (120*60)) {
    return 'about an hour ago';
  } else if(delta < (24*60*60)) {
    return 'about ' + (parseInt(delta / 3600)).toString() + ' hours ago';
  } else if(delta < (48*60*60)) {
    return '1 day ago';
  } else {
    return (parseInt(delta / 86400)).toString() + ' days ago';
  }
} 

function onBlur() {
    document.body.className = 'blurred';
};
function onFocus(){
    document.body.className = 'focused';
};
if (/*@cc_on!@*/false) { // check for Internet Explorer
    document.onfocusin = onFocus;
    document.onfocusout = onBlur;
} else {
    window.onfocus = onFocus;
    window.onblur = onBlur;
}

