

/* H E R O R O T A T I O N
================================================*/

var tabs = [];
var ind = 0;
var inter;

function change(stringref){
$('.hero:not(#' + stringref + ')').hide();

//show proper tab, catch IE6 bug
if ($.browser.msie && $.browser.version.substr(0,3) == "6.0")
$('.hero#' + stringref).show();
else
$('.hero#' + stringref).fadeIn('fast');
//clear highlight from previous tab title
slideNumber = stringref.substring(5,6);

$('#.navSlideDot:not(#navDotSlide'+slideNumber+')').removeClass('slideSelected');
//highlight currenttab title
$('#.navSlideDot#navDotSlide'+slideNumber+'').addClass('slideSelected');
$('#bd-navigation a[href=#' + stringref + ']').addClass('slideSelected');
}

function next(){
//call change to display next tab
change(tabs[ind++]);
//if it's the last tab, clear the index
if(ind >= tabs.length)
ind = 0;
}


/* Hero rotation for button box */

/* H E R O R O T A T I O N
================================================*/

var tabsButton = [];
var indButton = 0;
var interButton;

function buttonChange(stringrefButton){
$('.heroButton:not(#' + stringrefButton + ')').hide();

//show proper tab, catch IE6 bug
if ($.browser.msie && $.browser.version.substr(0,3) == "6.0")
$('.heroButton#' + stringrefButton).show();
else
$('.heroButton#' + stringrefButton).fadeIn('fast');
//clear highlight from previous tab title
slideNumber = stringrefButton.substring(5,6);

$('#.navSlideDotButton:not(#navDotSlide'+slideNumber+'Button)').removeClass('slideSelectedButton');
//highlight currenttab title

/*for(var i=1; i<=4; i++){
$('#.navSlideDotButton#navDotSlide'+i+'Button').removeClass('navSlideDotButtonTop0');
$('#bd-navigationButton a[href=#' + stringrefButton + ']').removeClass('navSlideDotButtonTop0');
document.getElementById('fbfooterButton').className = 'fragmentBoxFooter';
}
if(stringrefButton == "hero-4Button")
{
for(var i=1; i<=4; i++){
$('#.navSlideDotButton#navDotSlide'+i+'Button').addClass('navSlideDotButtonTop0');
$('#bd-navigationButton a[href=#' + stringrefButton + ']').addClass('navSlideDotButtonTop0');
}
document.getElementById('fbfooterButton').className = 'fragmentBoxFooterButton';
}*/
$('#.navSlideDotButton#navDotSlide'+slideNumber+'Button').addClass('slideSelectedButton');
$('#bd-navigationButton a[href=#' + stringrefButton + ']').addClass('slideSelectedButton');
}

function buttonNext(){
//call change to display next tab
buttonChange(tabsButton[indButton++]);
//if it's the last tab, clear the index
if(indButton >= tabsButton.length)
indButton = 0;
}

/*
// Roatate BLOG entries code
var blogIndex = 1;
function nextBlog()
{
//alert(blogIndex);
for(var i=1; i<=4; i++)
{
var blogId = "blog"+i;
//alert(document.getElementById(''+blogId+'').style.display);
document.getElementById(''+blogId+'').style.display = 'none';
//alert(blogId);
//alert(document.getElementById(blogId).style.display);
if(blogIndex == i)
{
document.getElementById(''+blogId+'').style.display = 'block';
//alert("id: "+blogId+"--"+document.getElementById(''+blogId+'').style.display)
}
}
blogIndex = blogIndex + 1 ;
if(blogIndex > 4)
{
blogIndex = 1;
}

}*/
/* END OF BLOG ENTRIES ROTATION */


// Roatate TRENDING entries code
var trendingIndex = 0;
function nextTrending(urlCount)
{
	for(var i=0; i<urlCount; i++) 
{
var trendingId = "trending"+i;
var trendingDomainId = "trendingDomain"+i;

//alert(document.getElementById(''+blogId+'').style.display);
if(document.getElementById(''+trendingId+''))
document.getElementById(''+trendingId+'').style.display = 'none';
if(document.getElementById(''+trendingDomainId+''))
document.getElementById(''+trendingDomainId+'').style.display = 'none';
//alert(blogId);
//alert(document.getElementById(blogId).style.display);
if(trendingIndex == i)
{
if(document.getElementById(''+trendingId+''))
document.getElementById(''+trendingId+'').style.display = 'block';
if(document.getElementById(''+trendingDomainId+''))
document.getElementById(''+trendingDomainId+'').style.display = 'block';
//alert("id: "+blogId+"--"+document.getElementById(''+blogId+'').style.display)

}

}

trendingIndex = trendingIndex + 1 ;
	if(trendingIndex > urlCount-1) 
{
		trendingIndex = 0;
}
}


function getTrendingContent()
{
	$.ajax({
  		type: "GET",
   		url: "/ajax",
   		data: "method=getLiveStream&topic=YouTube&domain=&page=1&sort=0&range=7200&url=&url_limit=5&trending_topics=1&sort=0&comments=false&social_reach=false",

   		success: function(msg){
			var response = jQuery.parseJSON(msg);
			if(response.status=='SUCCESS')
			{
				if (response.urls && response.url_count > 0) 
				{
					populateTrendingContent(response.urls);
					// Trending entries rotation
					$('#trendingNowLoading').css('display','none');
					$('#trendingSeeMore').css('display', 'block');
					nextTrending(response.urls.length);
					interTrending = setInterval("nextTrending("+response.urls.length+")", 5000);
				}
}

}
	});
}


function populateTrendingContent(feedList)
{
	var feedHtml='';
	var feedListElement = document.getElementById('trendingNow');
	if(typeof(feedListElement)!='undefined')
	{
		for(var i=0;i<feedList.length;i++)
		{
			var title = feedList[i].title;
			title = htmlspecialchars_decode(title, "ENT_QUOTES");
			trimmedTitle = title.substring(0,50);
			if(trimmedTitle == title) {
				$('<a href="/stream" id="trending'+i+'" style="color:white;display:none;">'+title+'</a>').appendTo($(feedListElement));
			} else {
				$('<a href="/stream" id="trending'+i+'" style="color:white;display:none;">'+trimmedTitle+'...</a>').appendTo($(feedListElement));
}
			$('<a href="/stream" id="trendingDomain'+i+'" class="fragmentBoxContentLink" style="display:none;">'+feedList[i].domain+'</a>').appendTo($(feedListElement));
		}
	}
}
getTrendingContent();
/* END OF TRENDING ENTRIES ROTATION */

/* Fetch blog feed and display recent entries on homepage */
function getBlogFeed()
{
	$.ajax({
  		type: "GET",
   		url: "/ajax",
   		data: "method=getRSSStream&return=json&url_limit=2&source=http%3a%2f%2fblog.sharethis.com%2ffeed",
 		
   		success: function(msg){
			var response = jQuery.parseJSON(msg);
			if(response.status=='SUCCESS')
			{
				if (response.urls && response.url_count > 0) 
				{
					populateBlogFeed(response.urls);
					$('#blogFeedLoading').css('display','none');
				}
			}

		}
	});
}

function populateBlogFeed(urls)
{
	var feedHtml='';
	var feedListElement = document.getElementById('blogFeed');
	if(typeof(feedListElement)!='undefined')
	{
		for(var i=0;i<urls.length;i++)
		{
			var title = urls[i].title;
			title = htmlspecialchars_decode(title, "ENT_QUOTES");
			trimmedTitle = title.substring(0,25);
			if(trimmedTitle == title) {
				$('<a href="http://blog.sharethis.com" id="blog'+i+'" style="color:white;display:block;">'+title+' &#187;</a>').appendTo($(feedListElement));
			} else {
				$('<a href="http://blog.sharethis.com" id="blog'+i+'" style="color:white;display:block;">'+trimmedTitle+'... &#187;</a>').appendTo($(feedListElement));
			}
		}
		$('#newWidgetLink').css('display','block');
	}
}


getBlogFeed();

/* End of fetching blog feed */


/* IE^6 P N G F I X
================================================*/

// IE5.5+ PNG Alpha Fix v2.0 Alpha: Background Tiling Support
// (c) 2008-2009 Angus Turnbull http://www.twinhelix.com

// This is licensed under the GNU LGPL, version 2.1 or later.
// For details, see: http://creativecommons.org/licenses/LGPL/2.1/

var IEPNGFix = window.IEPNGFix || {};

IEPNGFix.tileBG = function(elm, pngSrc, ready) {
// Params: A reference to a DOM element, the PNG src file pathname, and a
// hidden "ready-to-run" passed when called back after image preloading.
var data = this.data[elm.uniqueID],
elmW = Math.max(elm.clientWidth, elm.scrollWidth),
elmH = Math.max(elm.clientHeight, elm.scrollHeight),
bgX = elm.currentStyle.backgroundPositionX,
bgY = elm.currentStyle.backgroundPositionY,
bgR = elm.currentStyle.backgroundRepeat;

// Cache of DIVs created per element, and image preloader/data.
if (!data.tiles) {
data.tiles = {
elm: elm,
src: '',
cache: [],
img: new Image(),
old: {}
};
}
var tiles = data.tiles,
pngW = tiles.img.width,
pngH = tiles.img.height;

if (pngSrc) {
if (!ready && pngSrc != tiles.src) {
// New image? Preload it with a callback to detect dimensions.
tiles.img.onload = function() {
this.onload = null;
IEPNGFix.tileBG(elm, pngSrc, 1);
};
return tiles.img.src = pngSrc;
}
} else {
// No image?
if (tiles.src) ready = 1;
pngW = pngH = 0;
}
tiles.src = pngSrc;

if (!ready && elmW == tiles.old.w && elmH == tiles.old.h &&
bgX == tiles.old.x && bgY == tiles.old.y && bgR == tiles.old.r) {
return;
}

// Convert English and percentage positions to pixels.
var pos = {
top: '0%',
left: '0%',
center: '50%',
bottom: '100%',
right: '100%'
},
x,
y,
pc;
x = pos[bgX] || bgX;
y = pos[bgY] || bgY;
if (pc = x.match(/(\d+)%/)) {
x = Math.round((elmW - pngW) * (parseInt(pc[1]) / 100));
}
if (pc = y.match(/(\d+)%/)) {
y = Math.round((elmH - pngH) * (parseInt(pc[1]) / 100));
}
x = parseInt(x);
y = parseInt(y);

// Handle backgroundRepeat.
var repeatX = { 'repeat': 1, 'repeat-x': 1 }[bgR],
repeatY = { 'repeat': 1, 'repeat-y': 1 }[bgR];
if (repeatX) {
x %= pngW;
if (x > 0) x -= pngW;
}
if (repeatY) {
y %= pngH;
if (y > 0) y -= pngH;
}

// Go!
this.hook.enabled = 0;
if (!({ relative: 1, absolute: 1 }[elm.currentStyle.position])) {
elm.style.position = 'relative';
}
var count = 0,
xPos,
maxX = repeatX ? elmW : x + 0.1,
yPos,
maxY = repeatY ? elmH : y + 0.1,
d,
s,
isNew;
if (pngW && pngH) {
for (xPos = x; xPos < maxX; xPos += pngW) {
for (yPos = y; yPos < maxY; yPos += pngH) {
isNew = 0;
if (!tiles.cache[count]) {
tiles.cache[count] = document.createElement('div');
isNew = 1;
}
var clipR = Math.max(0, xPos + pngW > elmW ? elmW - xPos : pngW),
clipB = Math.max(0, yPos + pngH > elmH ? elmH - yPos : pngH);
d = tiles.cache[count];
s = d.style;
s.behavior = 'none';
s.left = (xPos - parseInt(elm.currentStyle.paddingLeft)) + 'px';
s.top = yPos + 'px';
s.width = clipR + 'px';
s.height = clipB + 'px';
s.clip = 'rect(' +
(yPos < 0 ? 0 - yPos : 0) + 'px,' +
clipR + 'px,' +
clipB + 'px,' +
(xPos < 0 ? 0 - xPos : 0) + 'px)';
s.display = 'block';
if (isNew) {
s.position = 'absolute';
s.zIndex = -999;
if (elm.firstChild) {
elm.insertBefore(d, elm.firstChild);
} else {
elm.appendChild(d);
}
}
this.fix(d, pngSrc, 0);
count++;
}
}
}
while (count < tiles.cache.length) {
this.fix(tiles.cache[count], '', 0);
tiles.cache[count++].style.display = 'none';
}

this.hook.enabled = 1;

// Cache so updates are infrequent.
tiles.old = {
w: elmW,
h: elmH,
x: bgX,
y: bgY,
r: bgR
};
};


IEPNGFix.update = function() {
// Update all PNG backgrounds.
for (var i in IEPNGFix.data) {
var t = IEPNGFix.data[i].tiles;
if (t && t.elm && t.src) {
IEPNGFix.tileBG(t.elm, t.src);
}
}
};
IEPNGFix.update.timer = 0;

if (window.attachEvent && !window.opera) {
window.attachEvent('onresize', function() {
clearTimeout(IEPNGFix.update.timer);
IEPNGFix.update.timer = setTimeout(IEPNGFix.update, 100);
});
}


function getLiveStreamFeed()
{
$.ajax({
    type: "GET",
    url: "/ajax",
    data: "method=getLiveStream&url_limit=10&sort=0&range=3600&default_stream=all",
    success: function(msg){
var response = jQuery.parseJSON(msg);
if(response.status=='SUCCESS')
{
if (response.urls && response.url_count > 0)
{
populateFeed(response.urls);
}
}

}
});
}


function populateFeed(feedList)
{
var feedHtml='';
var feedListElement = document.getElementById('feed-carousel-body');
if(typeof(feedListElement)!='undefined')
{
for(var i=0;i<feedList.length;i++)
{
$('<li class="feed-excerpt"><a class="articleLink" href="' + feedList[i].url + '" title="' + feedList[i].snippet + '">' + feedList[i].title + '</a><span class="dots">&hellip;</span><a class="domainLink" href="http://' + feedList[i].domain + '">' + feedList[i].domain + '</a></li>').appendTo($(feedListElement));
}
}

$('<a href="/stream" title="See the Latest Trending Stories" class="moreLink sts-fr sts-oh">See More&hellip;</a>').appendTo($('#trendingFeed-wrapper'));

$('#trendingFeed-wrapper').removeClass('sts-dn');

/*
$("#feed-carousel").jCarouselLite({
vertical: true,
hoverPause: true,
visible: 1,
auto:3500,
speed:1000
});*/

if($.browser.safari || $.browser.version=='6.0' || $.browser.version=='7.0') {
$('#feed-carousel-body').css('padding-top', '12px');
}
}

$(document).ready(function() {

	$(".hero").map(function(){
		tabs[ind++] = $(this).attr("id");
	});
	//set index to next element to fade
	ind = 1;
	//initialize tabs, display the current tab
	$(".hero:not(:first)").hide();
	$(".hero:first").show();
	//highlight the current tab title
	$('#' + tabs[0] + 't').addClass('select');
//handler for clicking on tabs
	$("#navDots div").click(function(){
		//if tab is clicked, stop rotating
		clearInterval(inter);
		//store reference to clicked tab

		stringref = $(this).attr("data").split('#')[1];
		//display referenced tab
		change(stringref);
		return false;
	});
	//start rotating tabs
	inter = setInterval("next()", 8000);


/* hero button function */

	$(".heroButton").map(function(){
		tabsButton[indButton++] = $(this).attr("id");
	});
	//set index to next element to fade
	indButton = 1;
	//initialize tabs, display the current tab
	$(".heroButton:not(:first)").hide();
	$(".heroButton:first").show();
	//highlight the current tab title
	$('#' + tabsButton[0] + 't').addClass('select');
//handler for clicking on tabs
	$("#navDotsButton div").click(function(){
		//if tab is clicked, stop rotating
		clearInterval(interButton);
		//store reference to clicked tab

		stringrefButton = $(this).attr("data").split('#')[1];
		//display referenced tab
		buttonChange(stringrefButton);
		return false;
	});
	//start rotating tabs
	interButton = setInterval("buttonNext()", 7000);
	// -------------

	// Blog entries rotation
	// interBlog = setInterval("nextBlog()", 7000);

    $('#carousel-wrapper').jcarousel({
		wrap: 'circular',
		visible:4,
		scroll:4
	});

// getLiveStreamFeed();

/*$('#blogFeed').rssfeed('http://blog.sharethis.com/feed/rss/', {
limit: 4,
snippet:false,
date:false,
header:false
});*/
	stLight.options({
		publisher:'6beba854-ee6d-4ae1-a4f3-b69815c8ef63',
		type:'website',
		embeds:true,
		onhover:false,
		tracking:'google'
	});

});
function htmlspecialchars_decode(input, quote_style) {
	var c = {
		'&amp;': '&',
		'&lt;': '<',
		'&gt;': '>',
		'&quot;': '"',
		'&#039;': '\''
	};
	return ('' + input).replace(
		quote_style === 'ENT_QUOTES' ? /&amp;|&lt;|&gt;|&quot;|&#039;/g :
		quote_style === 'ENT_NOQUOTES' ? /&amp;|&lt;|&gt;/g :
		/&amp;|&lt;|&gt;|&quot;/,
		function (a) {
			return c[a];
		}
	);
}

