// CUFON FONT REPLACEMENT
//=================================================================================================================================
$(function() {
	Cufon.replace('h1, #post-index h2, #article-content h2, #article-content h3, #article-content h4, #article-content h5, #article-content h6, #sidebar h2, .post-author, #related h2, #comments h2');
	Cufon.replace('#content .panel-desc h3', {
		textShadow: '#000 1px 1px'
	});
	Cufon.replace('#article-title h1, #sidebar h2', {
		textShadow: '#fff 0 1px'
	});
});

// FEATURED POSTS SLIDER
//=================================================================================================================================
$(function() {
	$('#slides').slides({
		preload: true,
		preloadImage: '../images/slider-loading.jpg',
		play: 5000,
		hoverPause: false,
		fadeSpeed: 500,
		effect: 'fade',
		crossfade: true,
		generatePagination: false,
		generateNextPrev: true
	});
});

// HIDE POPOVER, GET POPOVER COOKIE AND SHOW IF IT DOESN'T EXIST
//=================================================================================================================================
jQuery(document).ready(function() {
	$("#popover").hide();
	if ( !$.cookie("email_alerts") == "1" ) {
		$("#popover").delay("10000").fadeIn("slow");
	}
	$('#popover-close').click(function() {
		$("#popover").fadeOut("slow");
	});
});

// SET POPOVER COOKIE
//=================================================================================================================================
jQuery(document).ready(function() {
	$('#subscribe-options form, #popover form').submit(function(e) {
		$.cookie('email_alerts', '1', { 
			path: '/', expires: 999999 });
	});
	$('#promo span').click(function() {
		$("#popover").fadeIn("slow");
	});
});

jQuery(document).ready(function() {
	$('#popover p span').click(function() {
		$.cookie('email_alerts', '1', { 
			path: '/', expires: 999999 });
		$("#popover").fadeOut("slow");
	});
});

// BUTTON STATES
//=================================================================================================================================
$(function() {
	$("#nav-posts-tips li").click(function(){
	    $("#nav-posts-tips li").removeClass("current").addClass("");
	    $(this).addClass("current");
	});
	$("#nav-posts-news li").click(function(){
	    $("#nav-posts-news li").removeClass("current").addClass("");
	    $(this).addClass("current");
	});
	$("#top-sellers-tabs li").click(function(){
	    $("#top-sellers-tabs li").removeClass("current").addClass("");
	    $(this).addClass("current");
	});
	$("#nav-posts-tips-sb li").click(function(){
	    $("#nav-posts-tips-sb li").removeClass("current").addClass("");
	    $(this).addClass("current");
	});
	$("#nav-posts-news-sb li").click(function(){
	    $("#nav-posts-news-sb li").removeClass("current").addClass("");
	    $(this).addClass("current");
	});
});

// TWITTER INTENTS WIDGET
//=================================================================================================================================

(function() {
  if (window.__twitterIntentHandler) return;
  var intentRegex = /twitter\.com(\:\d{2,4})?\/intent\/(\w+)/,
      windowOptions = 'scrollbars=yes,resizable=yes,toolbar=no,location=yes',
      width = 550,
      height = 500,
      winHeight = screen.height,
      winWidth = screen.width;

  function handleIntent(e) {
    e = e || window.event;
    var target = e.target || e.srcElement,
        m, left, top;

    while (target && target.nodeName.toLowerCase() !== 'a') {
      target = target.parentNode;
    }

    if (target && target.nodeName.toLowerCase() === 'a' && target.href) {
      m = target.href.match(intentRegex);
      if (m) {
        left = Math.round((winWidth / 2) - (width / 2));
        top = 0;

        if (winHeight > height) {
          top = Math.round((winHeight / 2) - (height / 2));
        }

        window.open(target.href, 'intent', windowOptions + ',width=' + width +
                                           ',height=' + height + ',left=' + left + ',top=' + top);
        e.returnValue = false;
        e.preventDefault && e.preventDefault();
      }
    }
  }

  if (document.addEventListener) {
    document.addEventListener('click', handleIntent, false);
  } else if (document.attachEvent) {
    document.attachEvent('onclick', handleIntent);
  }
  window.__twitterIntentHandler = true;
}());
