//Function to determine if a link has been followed from the Prince's Mirco-site
//Uses a list of known pages and inserts a button
//requires jQuery
var linkFrom = function(){
	if (document.referrer != ''){
   	var from = document.referrer;
	var link;
	var text;
	var URI = "http://www.chouse.group4/";
	var page = function(URI,from){
		filename = from.substr(URI.length);
		var show;
		switch(filename){
			case 'personalprofiles/thedukeandduchessofcambridge/thedukeofcambridge/':
			case 'personalprofiles/thedukeandduchessofcambridge/thedukeofcambridge/index.html':
			case 'personalprofiles/thedukeandduchessofcambridge/thedukeofcambridge/morenews.html':
			if (location.href.indexOf("thedukeandduchessofcambridge") == -1){
				link = '/'+filename;
				text = '&lt; The Duke of Cambridge';
				show = 1;
				}
			break;
			
			case 'personalprofiles/thedukeandduchessofcambridge/theduchessofcambridge/':
			case 'personalprofiles/thedukeandduchessofcambridge/theduchessofcambridge/index.html':
			case 'personalprofiles/thedukeandduchessofcambridge/theduchessofcambridge/morenews.html':
			if (location.href.indexOf("thedukeandduchessofcambridge") == -1){
				link = '/'+filename;
				text = '&lt; The Duchess of Cambridge';
				show = 1;
				}
			break;
			
			case 'personalprofiles/princeharry/':
			case 'personalprofiles/princeharry/index.html':
			case 'personalprofiles/princeharry/morenews.html':
			if (location.href.indexOf("princeharry") == -1){
				link = '/'+filename;
				text = '&lt; Prince Harry';
				show = 1;
				}
			break;
			
			case 'personalprofiles/thedukeandduchessofcambridge/':
			case 'personalprofiles/thedukeandduchessofcambridge/index.html':
			link = '/'+filename;
			text = '&lt; The Duke and The Duchess of Cambridge';
			show = 1;
			break;
			default:
			show = 0;
		}
		return(show);
	};
	var button = page(URI,from);
	if(button==1){
		$('.relatedContent').before("<p class='backLink'><a href='"+ link +"'>" + text + "</a></p>");
	}
	};
};
var sort = function(){
	$('li a.news').click(function(){
		$('.listingItem').show();
		$('.listingItem.film , .listingItem.speach').slideUp();
		$(this).parent().parent().children().removeClass('on');
		$(this).parent().addClass('on');
		return false;
	});
	$('li a.film').click(function(){
		$('.listingItem').show();
		$('.listingItem.news , .listingItem.speach').slideUp();
		$(this).parent().parent().children().removeClass('on');
		$(this).parent().addClass('on');
		return false;
	});
	$('li a.speach').click(function(){
		$('.listingItem').show();
		$('.listingItem.film , .listingItem.news').slideUp();
		$(this).parent().parent().children().removeClass('on');
		$(this).parent().addClass('on');
		return false;
	});
	$('li a.all').click(function(){
		$('.listingItem').slideDown();
		$(this).parent().parent().children().removeClass('on');
		$(this).parent().addClass('on');
		return false;
	});
};

var insertSortLinks = function(){
	var html = '<ul class="filter"><li><a href="#" class="film">Videos</a></li><li><a href="#" class="news">News &amp; Press Releases</a></li><li><a href="#" class="speach">Speeches</a></li><li class="on"><a href="#" class="all">All</a></li></ul>';
	$('#newsFeed').parent().append(html);

};

//Run functions when document is ready
$(document).ready(function(){
	$(document).pngFix();
	insertSortLinks();
	sort();
	

	linkFrom();




	
});

function showMe(el, div){
	id = document.getElementById(div).style;
	id.display = 'block';
}
function hideMe(el, div){
	id = document.getElementById(div).style;
	id.display = 'none';
}


//Swfobject Youtube Embedded video
var youTubeEmbed = function(ytVideoID, size){


	switch(size){
		case 'l':
			var h = "350px";
			var w = "540px";
		break;
		case 'm':
			var h = "310px";
			var w = "533px";
		break;
		case 's':
			var h = "168px";
			var w = "259px";
		break;
		default:
			var h = "168px";
			var w = "259px";
		break;
	}

	var s1 = new SWFObject('http://www.youtube.com/v/'+ytVideoID+"&hl=en&fs=1&rel=0&color1=0x333367&color2=0x7A7A9B&enablejsapi=1&playerapiid=ytplayer",
			   "vid", w, h, "8", null, null);
	s1.addParam('allowfullscreen','true');
	s1.addParam('allowscriptaccess','samedomain')
	s1.write('ytVid');
};
