$(document).ready(function() {

addDropcap();

var relatedArticleCount = 0;
var tagCount = 0;

//Check if there are any related articles
$("#similar_articles li").each(function(){
	relatedArticleCount++;
});

$("#article_tags li").each(function(){
	tagCount++;
});

if (relatedArticleCount == 0) {
	$("#similar_articles").hide();
}

if (tagCount == 0) {
	$("#article_tags").hide();
}

	/* This is basic fancybox scrip	 - uses default settings */
	
	$("a.fancybox").fancybox({
		'zoomSpeedIn':		300, 
		'zoomSpeedOut':	300, 
		'zoomOpacity' : true,
		'imageScale' : true,
		'overlayOpacity' : 0.7,
		'overlayShow': true
	});
	
	//Get the links in the body
	var returnedLinks = new Array();
	var existingLinks = new Array();
	var dedupedLinks = new Array();
	
	var count = 0;
	
	$("#body_section a").each(
	
		function(){
		
			theHTML = jQuery.trim($(this).html());
			theHref = jQuery.trim($(this).attr("href"));
			theTitle = jQuery.trim($(this).attr("href"));
		
			if (theHTML !="") {
				if ((theHref.substring(0,21) != "http://develop3d.com/") && (theHref.substring(0,7) != "mailto:")) {
					returnedLinks.push(checkLink(theHref,theTitle));
				}			
			}
			
	});
	
	
		for (a=0;a<returnedLinks.length;a++) {
		
		if (returnedLinks[a]) {
		
		var theMatch = returnedLinks[a].match(/href="([^"]+)"/);
		returnedLink = theMatch[1];
		
		
						
		if (existingLinks.length == 0) {
			existingLinks.push(returnedLink);
			dedupedLinks.push(returnedLinks[a]);
			
		} else {
			alreadyLinked = 0;
			for (b=0;b<existingLinks.length;b++) {

				if (existingLinks[b] == returnedLink) {
					alreadyLinked++;
				} 
			}
				
			if (alreadyLinked == 0) {
				existingLinks.push(returnedLink);
				dedupedLinks.push(returnedLinks[a]);
						
			}
		}
		
		}
		
	}
	

	
	
	if (dedupedLinks.length > 0) {
		
		$("#article_tags").before("<div id=\"inline_Links\" class=\"sidebar_item l_twocol\"><h3>Links</h3><ul id=\"links_list\"></ul></div>");
		
		for (i=0;i<dedupedLinks.length;i++) {
			if (dedupedLinks[i]) {
			$("#links_list").append("<li>" + dedupedLinks[i] + "</li>");
			}
			
		}
	}
	
	
	
     
	
});



//Link checker
	
	function checkLink(urlToCheck,urlTitle) {
	
		if (urlTitle == "") {
			urlTitle = urlToCheck;
		}
		
			
		//Check for http
		if (urlToCheck.substring(0,4) != "http") {
			urlToCheck = "http://" + urlToCheck;
		}
		
		if (urlTitle.substring(0,7) == "http://") {
			urlTitle = urlTitle.substring(7);
		}
		
		if (urlTitle.substring(0,4) == "www.") {
			urlTitle = urlTitle.substring(4);
		}
		
		//Split title urls at slashes
		var mySplitResult = urlTitle.split("/");
		urlTitle = mySplitResult[0];
		
		showAllParts = 1;
		
		//Check to see if any URL parts are longer than 20 characters. If they are, only show the first part of the URL
		for(i = 1; i < mySplitResult.length; i++){
			if (mySplitResult[i].length > 20) {
				showAllParts = 0;
			} 
		} 
		
		if (showAllParts == 1) {
			for(i = 1; i < mySplitResult.length; i++){
				urlTitle = urlTitle + "/<br/>" + mySplitResult[i]; 
			}
		}
			
		return "<a href=\"" + urlToCheck + "\" title=\"" + urlToCheck + "\">" + urlTitle + "</a>";
			
	}
	
//Add drop caps
function addDropcap() {

	var first_paragraph = $('#body_section p:first').html();
	if (!first_paragraph) return false;
 	
	var first_letter = first_paragraph.substr(0,1);

 	var match = /[a-zA-Z]/.test(first_letter);
 	if (match) {
  		first_paragraph = first_paragraph.slice(1);
 		 $('#body_section p:first').html(first_paragraph);
 		 $('#body_section p:first').prepend('<span class="dropcap">' + first_letter + '</span>');
 	}

}


