
var shareSection = null;
var shareUrl = null;
var sTitle = "MyCoke";
var sDescription = "Hey, look at what I found at mycoke.com. I thought you might want to check it out.";
var sImage = homeBase+"images/logo.png";

function getShareTitle($obj)
{
	var value = sTitle;
	if($obj != null)
	{
		value = getHiddenShareField($obj.children("#title"));
		if(value != null) return value;
	}
	value = $('meta[name=title]').attr("content");
	if(value != null) return value;
	else
	{
		value = $('title').text();
		if(value != null && value != "") return value;
	}
	return sTitle;
}
function getShareDescription($obj, type)
{
	var value = sDescription;
	if($obj != null)
	{
		if(type == "twitter")
		{
			value = getHiddenShareField($obj.children("#twitter"));
			if(value != null) return value;
		}
		
		value = getHiddenShareField($obj.children("#description"));
		if(value != null) return value;
		value = getHiddenShareField($obj.children("#description_id"));
		if(value != null) value = $("#"+value).attr("innerHTML");
		if(value != null) return value;
	}
	if(type == "twitter")
	{
		value = $('meta[name=twitter]').attr("content");
		if(value != null) return value;
	}
	value = $('meta[name=description]').attr("content");
	if(value != null) return value;

	return sDescription;
}
function completeBaseURL(url)
{
	return (url.indexOf("http") >= 0)?url:(homeBase+url);
}
function getShareImage($obj)
{
	var value = sImage
	if($obj != null)
	{
		value = getHiddenShareField($obj.children("#image"));
		if(value != null) return completeBaseURL(value);
	}
	value = $('link[rel=image_src]').attr("href");
	if(value != null) return completeBaseURL(value);
	return completeBaseURL(sImage);
}
function getShareTracking($obj)
{
	var value = null;
	if($obj != null)
	{
		value = getHiddenShareField($obj.children("#tag"));
	}
	return value;
}
function shortUrl(url)
{
	var short = "http://";
	if(url.indexOf(short) == 0) url = url.substring(short.length);
	return url;
}
function getShareUrl($obj)
{
	var value = location.href;
	if($obj != null)
	{
		value = getHiddenShareField($obj.children("#url"));
		if(value != null) return completeBaseURL(value);
	}
	value = shareUrl;
	if(value != null) return completeBaseURL(value);
	return location.href;
}
function getShareSection($obj)
{
	var value = shareSection;
	if($obj != null)
	{
		value = getHiddenShareField($obj.children("#section"));
		if(value != null) return value;
	}
	return shareSection;
}
function needsFacebookProxy(obj)
{
	if(obj != null && $(obj).parents('[class *= share_rollover]') != null)
	{
		var $jObj = $(obj).parents('[class ^= share]');
		var value = getHiddenShareField($jObj.children("#proxy"));
		if(value != null) return (value != "false");

		value = $('meta[name=proxy]').attr("href");
		return (value != "false");
	}
	else return false;
	//return (obj != null && $(obj).parents('[class *= share_rollover]') != null);
}

function shareThis(obj, type, title, summary, image, url)
{
	var $jObj = (obj == null)?null:$(obj).parents('[class ^= share]');
		
	if(title == null) title = getShareTitle($jObj);
	if(summary == null) summary = getShareDescription($jObj, type);
	if(image == null) image = getShareImage($jObj);
	if(url == null) url = getShareUrl($jObj);
	var trackingTag = getShareTracking($jObj);

    if (type != null)
    {
        var Bookmark = encodeURIComponent(url);
        var Title = escape(title);
        var Content = escape(summary);
        var URL = "";
		var bShared = true;
		var sSocial = null;
		var lenUrl = 0;
		var lenContent = 0;
        switch (type.toLowerCase())
        {
            case "linkedin":
                URL = "http://www.linkedin.com/shareArticle?mini=true&url="+Bookmark+"&title="+Title+"&summary="+Content+"&source="+BookmarkRoot;
                window.open(URL);
                break;

            case "digg":
                URL = "http://digg.com/remote-submit?phase=2&url="+Bookmark+"&title="+Title+"&bodytext="+Content; //"&topic=510"; <.food and drink???  509 autos
                window.open(URL);
				sSocial = "Digg";
                break;

            case "delicious":
                URL = "http://del.icio.us/post?url="+Bookmark+"&title="+Title+"&notes="+Content;
                window.open(URL);
				sSocial = "Delicious";
                break;

            case "myspace":
                URL = "http://www.myspace.com/index.cfm?fuseaction=postto&u="+Bookmark;
				URL += "&t="+Title;
				URL += "&c="+Content+encodeURIComponent("<br /><img src='"+image+"' />");
                window.open(URL);
				sSocial = "mySpace";
                break;

            case "twitter":
				url = shortUrl(url);
				lenUrl = url.length;
				lenContent = summary.length;
				Content = summary;
				if((lenUrl + lenContent + 1) > 140)
				{
					Content = summary.substr(0,(140-lenUrl-1));
					if(Content.length == (140-lenUrl-1))
						Content = Content.substr(0, 137-lenUrl-1)+"...";
				}
				Content = encodeURIComponent(url)+" "+escape(Content);
				//Content = url+" "+summary;
				//Content = Content.substr(0,140);
				//if(Content.length == 140)
					//Content = Content.substr(0, 137)+"...";
                URL = "http://twitter.com/home?status="+Content;
                window.open(URL);
				sSocial = "Twitter";
                break;

            case "facebook":
				if(needsFacebookProxy(obj))
				{
					if(Bookmark.indexOf("http") != 0) Bookmark = Bookmark;
					Bookmark = encodeURIComponent(homeBase+"jsp/common/facebookShare.jsp?url="+Bookmark+"&title="+Title+"&description="+Content+"&image="+encodeURIComponent(image));
				}
                URL = "http://www.facebook.com/sharer.php?u="+Bookmark+"&t="+Title;
                window.open(URL);
				sSocial = "Facebook";
                break;
				
            case "blogger":
				Content += "<br /><img src='"+image+"' />";
			    URL = 'http://www.blogger.com/blog_this.pyra?t='+Content+'&u='+Bookmark+'&n='+ Title;
                window.open(URL);
				sSocial = "Blogger";
                break;		
			case "email":
				shareEmail(Bookmark);
				sSocial = "Email";
				break;
			default:
				bShared = false;
				break;
        }
		if(bShared) shareTracking(trackingTag,type.toLowerCase(),sSocial);
    }
	return false;
}
function shareThisFlash(type, title, summary, image, url)
{
	return shareThis(null, type, title, summary, image, url);
}

function shareThisByID(sParams)
{
	var params = sParams.split("|");
	var obj = (params[0] != null && params[0] != "")?document.getElementById(params[0]):null;
	return shareThis(obj, params[1], params[2], params[3], params[4], params[5]);
}
function shareEmail(url)
{
	var oDiv = null;
	var params = "url="+url;
	var aPos = "top";
	var sUrl = homeSSL+"startShareByEmail.do?"+params;
	if(typeof isIE6 != "undefined" && isIE6 == true)
		setTimeout(function() { openDivPopup_external("email","email",sUrl,aPos); }, 300);
	else
		openDivPopup_external("email","email",sUrl,aPos);
	return false;
}

function shareTracking(tag,type,social)
{
	if(tag == null || tag == "") return;
	tag += "."+type;
	doMyCokeTracking(tag,null,"Share",social);
}
function openShareOver(id)
{
	$("#"+id).onShareRollover();
}

// =============================================================
// = Plugin function. Assigns tooltips to each matched element =
// = @example $("#mycontent").setShareLinks();                 =
// ============================================================= 
(function($) {
	$.fn.setShareLinks = function(options) {
		return this.each(function() {
			$this = $(this);
			
			$(this).find('a').each(function(){
				this.onclick = function(){
					shareThis(this,$(this).attr("class"));
				};
				this.href = "javascript:void(0);";
			});
		});
	};
})(jQuery);
function initLikeFrame(obj,sUrl)
{
	var urlFrame = "http://www.facebook.com/plugins/like.php?href="+sUrl+"&amp;layout=standard&amp;show_faces=false&amp;width=450&amp;action=like&amp;font=lucida+grande&amp;colorscheme=light&amp;height=35";
	$(obj).append('<IFRAME class="iframe" src="'+urlFrame+'" scrolling="no" frameborder="0" allowTransparency="true"></iframe>');
}
function initLike()
{
	var sUrl = encodeURIComponent(getShareUrl($(this)));
	var divLike = $(this).find(".fb_like");
	initLikeFrame(divLike,sUrl);
}
function initLikeMod()
{
	var sUrl = encodeURIComponent(getShareUrl($(this).parent()));
	var divLike = $(this);
	initLikeFrame(divLike,sUrl);
}
$(document).ready(function() {
	$('.share').each(initLike);
	$('.like_fb_mod').each(initLikeMod);
});