﻿/* UI scripts that appear on all or almost every page */

// Uses http://jQuery.com

$(window).load(function() {

});
$(function(){
	$('div.filter-search').fieldfocus().find('label.overlabel').overlabel();
	
	// requires jquery.overlabel.2.js
	//$.fn.overlabel();
	
});

var loadShareThisHandlers = function(ajaxRequestUrl)
{
    $(".share-this a.email").live("totalEmailCountChanged", function(){
        var elm = $(this);
        var storyId = elm.parents(".share-this:first").attr("id").replace("shareitem-", "");
        $.ajax({
          url: ajaxRequestUrl,
          dataType: "json",
          data: {action: "get-total-emails", id:storyId},
          success: function(data) {
                if(data.success)
                {
                    UpdateCounter(elm, data.total);
                }
            }});
    });
    
    $(".share-this a").live("click", function(){
        var elm = $(this);
        var actionStr = null;
        
        if(elm.hasClass("facebook"))
        {
            actionStr = 'increment-total-facebooked';
        }
        else if (elm.hasClass("twitter"))
        {
            actionStr = 'increment-total-twittered';
        }
        
        if(actionStr != null)
        {
            var storyId = elm.parents(".share-this:first").attr("id").replace("shareitem-", "");
            $.ajax({
              url: ajaxRequestUrl,
              dataType: 'json',
              data: {action: actionStr, id:storyId},
              success: function(data) {
                    if(data.success)
                    {
                        UpdateCounter(elm, data.total);
                    }
                }});
        }
    });
    
    function UpdateCounter(elm, total)
    {
        elm.attr("title", elm.attr("title").replace(/\d+ /gi, total+" "));
        var em = elm.find("em");
      
        if(em.length == 0)
        {
            elm.append("<em/>");
            em = elm.find("em");
            em.text(total);
        }
        else
        {
            em.text(total);
        }
    }
};

var loadStoryVideo = function(){
    if($("#story-media div.video").length != 0) 
    {
        setTimeout(function(){
            $("#story-media div.video").fadeIn(1000);
        }, 1000);
    }
};

