var numberOfArticles=4;		//The number of each articles per column + 1;
var textArticleId=6;
var mediaArticleId=7;
var temp=0;
var id=0;
var newID=0;
var tempText='';
var fadeSpeed=500;
var scrollSpeed=100;

function nextSocial(divId)
{
	id=$(divId).parent().attr("id").substring(6);
	if(id<7)
	{
		newID=7;
		numberOfArticles=7;
	}
	else if(id>7 && id<11)
	{
		newID=11;
		numberOfArticles=4;
	}
	else
	{
		newID=15;
		numberOfArticles=4;
	}
	//newID=((Math.floor(id/numberOfArticles)+1)*numberOfArticles)-1;
	
	if(id<numberOfArticles)
	{
		textArticleId++;
    	if(textArticleId>apiTextArticles['posts'].length-1)
    	{
    		textArticleId=0;
    	}
    	if(apiTextArticles['posts'][textArticleId]["link-description"].length>=224)
    	{
			tempText=apiTextArticles['posts'][textArticleId]["link-description"].substring(0,224)+"...";
		}
		else
		{
			tempText=apiTextArticles['posts'][textArticleId]["link-description"].substring(0,224);
		}
        var now = new Date();
        var elapsedTime = (now.getTime()/1000)-apiTextArticles['posts'][textArticleId]["unix-timestamp"];
        if(elapsedTime>86400)
        {
        	var postTime = Math.round(elapsedTime/86400) + ' days ago';
        }
        else if(elapsedTime>3600)
        {
        	var postTime = Math.round(elapsedTime/3600) + ' hours ago';
        }
        else if(elapsedTime>60)
        {
        	var postTime = Math.round(elapsedTime/60) + ' minutes ago';
        }
        else
        {
        	var postTime ='Just posted';
        }
		$(divId).parent().parent().append('<div class="socialArticleContainer" id="social'+newID+'" style="visibility: hidden;"><a class="closeLink" onclick="nextSocial(this);">x</a><span class="timeStamp" id="postedTime'+newID+'">'+postTime+'</span><br/><a href="'+apiTextArticles['posts'][textArticleId]["url-with-slug"]+'" id="textArticleLink'+newID+'" title="'+apiTextArticles['posts'][textArticleId]["link-text"]+'">'+apiTextArticles['posts'][textArticleId]["link-text"]+'</a><p id="textArticleParagraph'+newID+'">'+tempText+'</p></div><hr id="hr'+newID+'"/>');
		socialDeli();
	}
	else
	{
		mediaArticleId++;
		if(mediaArticleId>apiVideoArticles.length-1)
		{
			mediaArticleId=0;
		}
		var now = new Date();
        var elapsedTime = (now.getTime()/1000)-apiVideoArticles[mediaArticleId]["unix-timestamp"];
        if(elapsedTime>86400)
        {
        	var postTime = Math.round(elapsedTime/86400) + ' days ago';
        }
        else if(elapsedTime>3600)
        {
        	var postTime = Math.round(elapsedTime/3600) + ' hours ago';
        }
        else if(elapsedTime>60)
        {
        	var postTime = Math.round(elapsedTime/60) + ' minutes ago';
        }
        else
        {
        	var postTime ='Just posted';
        }
		if(apiVideoArticles[mediaArticleId]["type"]=="photo")
		{
			if(apiVideoArticles[mediaArticleId]["photo-caption"].length>=40)
			{
				tempText=apiVideoArticles[mediaArticleId]["photo-caption"].substring(0,48)+"...";
			}
			else
			{
				tempText=apiVideoArticles[mediaArticleId]["photo-caption"].substring(0,48);
			}
			$(divId).parent().parent().append('<div class="socialArticleContainer" id="social'+newID+'" style="visibility: hidden;"><a class="closeLink" onclick="nextSocial(this);">x</a><span class="timeStamp" id="postedTime'+newID+'">'+postTime+'</span><br/><a href="'+apiVideoArticles[mediaArticleId]["url-with-slug"]+'" class="mediaArticleMediaClass" id="mediaArticleMedia'+newID+'" title="'+apiVideoArticles[mediaArticleId]["photo-caption"]+'"><img src="'+apiVideoArticles[mediaArticleId]["photo-url-400"]+'"/></a><p id="mediaArticleParagraph'+newID+'">'+tempText+'</p></div><hr id="hr'+newID+'"/>');
			socialDeli();
		}
  		else
  		{
			if(apiVideoArticles[mediaArticleId]["video-caption"].length>=40)
			{
				tempText=apiVideoArticles[mediaArticleId]["video-caption"].substring(0,48)+"...";
			}
			else
			{
				tempText=apiVideoArticles[mediaArticleId]["video-caption"].substring(0,48);
			}
			$(divId).parent().parent().append('<div class="socialArticleContainer" id="social'+newID+'" style="visibility: hidden;"><a class="closeLink" onclick="nextSocial(this);">x</a><span class="timeStamp" id="postedTime'+newID+'">'+postTime+'</span><br/><a href="'+apiVideoArticles[mediaArticleId]["url-with-slug"]+'" id="mediaArticleMedia'+newID+'" title="'+apiVideoArticles[mediaArticleId]["photo-caption"]+'">'+apiVideoArticles[mediaArticleId]["video-player"]+'</a><p id="mediaArticleParagraph'+newID+'">'+tempText+'</p></div><hr id="hr'+newID+'"/>');
			socialDeli();
  		}
	}
}

function socialDeli()
{
	$("#hr"+id).fadeOut();
	$("#hr"+id).remove();
	/*if(id<numberOfArticles)
	{
		for(var i=id; i<=newID; i++){
			temp=(i*1);
			$("#hr"+temp).fadeTo(100,0.4);
			$("#social"+temp).fadeTo(100,0.4);
			$("#mediaArticleMedia"+temp).fadeTo(100,0.4);
			$("#mediaArticleParagraph"+temp).fadeTo(100,0.4);
		}
	}*/
	$("#social"+id).fadeTo(fadeSpeed, 0, function() {
		var oldHeight = $("#social"+newID).height();
		$("#social"+newID).height(0);
		$("#social"+newID).css('visibility','visible');
		$("#social"+id).animate({height: 'toggle'}, scrollSpeed, function() {$("#social"+id).remove();});
		$("#social"+newID).animate({height: oldHeight}, scrollSpeed);
		if(id<numberOfArticles)
		{
			for(var i=id; i<newID; i++){
				temp=(i*1)+1;
				/*$("#hr"+temp).fadeTo("slow",1);*/
				$("#hr"+temp).attr("id","hr"+i);
				/*$("#social"+temp).fadeTo("slow",1);*/
				$("#social"+temp).attr("id","social"+i);
				/*$("#mediaArticleMedia"+temp).fadeTo("slow",1);*/
				$("#mediaArticleMedia"+temp).attr("id","mediaArticleMedia"+i);
				/*$("#mediaArticleParagraph"+temp).fadeTo("slow",1);*/
				$("#mediaArticleParagraph"+temp).attr("id","mediaArticleParagraph"+i);
			}
		}
		else
		{
			for(var i=id; i<newID; i++){
				temp=(i*1)+1;
				$("#hr"+temp).attr("id","hr"+i);
				$("#social"+temp).attr("id","social"+i);
				$("#mediaArticleMedia"+temp).attr("id","mediaArticleMedia"+i);
				$("#mediaArticleParagraph"+temp).attr("id","mediaArticleParagraph"+i);
			}
		}
	});
	$("div[@id ^= 'mediaArticleMedia'] iFrame").each(function(index) {
		if($(this).attr('src').substring(0,30) == 'http://player.vimeo.com/video/')
		{
			$(this).width(260).height(195).attr({width:260,height:195});
		}
	});
}

$(document).ready(function() {
	var tmpImage = new Image();
	// adding a check for apiVideoArticles for pages without a social deli (100to1)
	if(typeof apiVideoArticles != 'undefined')
	{
		for(var d=mediaArticleId; d<apiVideoArticles.length-1; d++)
		{
			if(apiVideoArticles[d]["type"]=="video")
			{
				if(apiVideoArticles[d]["feed-item"].substring(0,17)=="http://vimeo.com/")
				{
					$.getJSON("http://vimeo.com/api/oembed.json?url=http%3A//vimeo.com/"+apiVideoArticles[d]["feed-item"].substring(17)+".json&callback=?",function(data){
	          			apiVideoArticles[d]["type"]="photo";
	          			apiVideoArticles[d]["photo-url-400"]=data["thumbnail_url"];
	          			apiVideoArticles[d]["photo-caption"]=apiVideoArticles[d]["video-caption"];
	        		});    				
				}
				else
				{
					var search = new RegExp("\\bhttp://www.youtube.com/v/\\b");
					var results = search.exec(apiVideoArticles[d]["video-player"]);
					if(results !== null)
					{
						var linkPosition = results.index+25;
	   					var youtubeVideoId = apiVideoArticles[d]["video-player"].substr(linkPosition,11);
	   					apiVideoArticles[d]["type"]="photo";
	      				apiVideoArticles[d]["photo-url-400"]="http://img.youtube.com/vi/"+youtubeVideoId+"/0.jpg";
	      				apiVideoArticles[d]["photo-caption"]=apiVideoArticles[d]["video-caption"];
	  				}
				}
			}
			tmpImage = $('<img />').attr('src', apiVideoArticles[d]["photo-url-400"]);
		}
	}

	$(".topLevel ul li").hover(
	  function () {
		$(".topLevel ul li").stop(false,true);		//Stop any other animations on the menu to remove the trailing effect.
		$(".topLevel ul li").css('backgroundColor','#FFFFFF');
	    $(this).animate({backgroundColor:"#E4DC75"},400);
	  },
	  function () {
	    $(this).animate({backgroundColor:"#FFFFFF"},400);
	  }
	);
	
	$("div[@id ^= 'mediaArticleMedia'] iFrame").each(function(index) {
		if($(this).attr('src').substring(0,30) == 'http://player.vimeo.com/video/')
		{
			$(this).width(260).height(195).attr({width:260,height:195});
		}
	});
});
