$(document).ready(function() {

	characterMapSwap.characterSwitch();
	
	$('#otherbios').click( function(e) {
		e.preventDefault()
		$('#dropdown').slideToggle("fast");
	})
	$('#dropdown').mouseleave( function(e) {
		$(this).slideToggle("fast");
	})
	
	
	guidance();
	
	formNewWindow();

});		



var bcExp;
var modVP;
 
// called when template loads, this function stores a reference to the player and modules.
function onTemplateLoaded(experienceID) {
	bcExp = brightcove.getExperience(experienceID); 
	modVP = bcExp.getModule(APIModules.VIDEO_PLAYER);
}



function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}




function guidance() {
	
	var x = readCookie('underbelly')
	if (x == "guidanceOK") {
		return false;
	} else if (x == "guidanceNo") {
		$('#videocontainer').prepend('<div id="videooverlay"><div id="warningmsg"><h3>Watch Out</h3><p>This video contains content of an adult nature. You have chosen not to watch it.</div></div>')
	} else {
		$('#videocontainer').prepend("<div id='videooverlay'><div id='warningmsg'><h3>Watch-Out</h3><p>Look, this video is about a bunch of criminals, OK? So it's pretty violent, with guns and stuff, there's some shocking language and a load of sex. We're big on family values around here so we don't want anyone under 18 watching it. Are we clear?</p><ul id='guidelist'><li>So if you click on 'Yes', below, that means you're a grown up and have chosen to watch the video and you've read and agreed to our <a href='terms.php'>rules</a>. What's more, if you tick the little white box as well, it means anyone on this computer will be able to watch it. No more warnings.</li><li>If you don't tick the little white box, you'll get this warning every time you come to this page.</li><li>If you choose, 'No', and tick the box, that means no-one, including you, will be able to watch the video at all. Permanently.</li></ul><form id='formguidancecheck'><div class='clearing'><label for='guidecheck'><input id='guidecheck' type='checkbox'></label><span>This is the box we're talking about!</span></div></form><ul Id='yesno' class='clearing'><li><a href='#' id='yes'>Yes</a></li><li><a href='index.php' id='no'>No</a></li></ul></div></div>")
	}
	
	
	$('#yes').click( function(e) {
		e.preventDefault();
		$('#videooverlay').slideUp();
		modVP.play();
		if ($('#guidecheck:checked').length) {
			createCookie('underbelly','guidanceOK',365);
		}
	});
	
	$('#no').click( function(e) {
		e.preventDefault();
		$('#videocontainer object').fadeOut();
		

		//$('#videocontainer').prepend('')
		$('#no').addClass('pointer')
		if ($('#guidecheck:checked').length) {
			$('#videooverlay').replaceWith('<div id="videooverlay"><div id="warningmsg"><h3>Watch Out</h3><p>This video contains content of an adult nature. You have chosen not to watch it.</div></div>');
			createCookie('underbelly','guidanceNo',365)
		} else {
			$('#videooverlay').replaceWith('<div id="videooverlay"><div id="warningmsg"><h3>Watch Out</h3><p>This video contains content of an adult nature. You have chosen not to watch it.</div></div>');
		}
	});

}



function formNewWindow() {
	$('#mc-embedded-subscribe-form').attr('target', '_blank')
}



var characterMapSwap = {

	characterSwitch: function() {
	
		var charnames = document.getElementById('characternames');
		var charmap = document.getElementById('charactermap');
		var bodyid = document.getElementsByTagName('body')[0].id;
		var group0 = 'rollnumber-0';
		var group1 = 'rollnumber-1';
		var group2 = 'rollnumber-2';
		var group3 = 'rollnumber-3';
		var group4 = 'rollnumber-4';
		var group5 = 'rollnumber-5';
		var group6 = 'rollnumber-6';
		
		// ABV
			$('#abv').hover(function(e) {
				$(charnames).css('backgroundPosition', '0 -33px');
				if (bodyid == group0) {
					$(charmap).css('backgroundPosition', '0 -534px');
				}
				}, function() {
				$(charnames).css('backgroundPosition', '0 0');
				$(charmap).css('backgroundPosition', '0 0');
				
			})
		//Lewis Caine
			$('#lewiscaine').hover( function() {
				$(charnames).css('backgroundPosition', '0 -153px')
				if (bodyid == group0) {
					$(charmap).css('backgroundPosition', '0 -534px');
				}
				}, function() {
				$(charnames).css('backgroundPosition', '0 0');
				$(charmap).css('backgroundPosition', '0 0');
			})
		// Zara Garde Wilson
			$('#zgw').hover( function() {
				$(charnames).css('backgroundPosition', '0 -114px')
				if (bodyid == group0) {
					$(charmap).css('backgroundPosition', '0 -534px');
				}
				}, function() {
				$(charnames).css('backgroundPosition', '0 0');
				$(charmap).css('backgroundPosition', '0 0');
			})
		//Mr T
			$('#mrt').hover( function() {
				$(charnames).css('backgroundPosition', '0 -74px')
				if (bodyid == group0) {
					$(charmap).css('backgroundPosition', '0 -534px');
				}
				}, function() {
				$(charnames).css('backgroundPosition', '0 0');
				$(charmap).css('backgroundPosition', '0 0');
			})
		
			
	// Roll 1
		// Jason Moran
			$('#jasonmoran-a, #jasonmoran-b').hover( function(e) {
				if (bodyid == group1 || bodyid == group2 || bodyid == group3 || bodyid == group4 || bodyid == group5 || bodyid == group6) {	
					$(charnames).css('backgroundPosition', '0 -435px')
				}
				if (bodyid == group1) {
					$(charmap).css('backgroundPosition', '0 -534px');
				}
				}, function() {
				$(charnames).css('backgroundPosition', '0 0');
				$(charmap).css('backgroundPosition', '0 0');
			})
		// Mark Moran
			$('#markmoran').hover( function() {
				if (bodyid == group1 || bodyid == group2 || bodyid == group3 || bodyid == group4 || bodyid == group5 || bodyid == group6) {	
					$(charnames).css('backgroundPosition', '0 -474px')
				}
				if (bodyid == group1) {
					$(charmap).css('backgroundPosition', '0 -534px');
				}
				}, function() {
				$(charnames).css('backgroundPosition', '0 0');
				$(charmap).css('backgroundPosition', '0 0');
			})
			
	
	//Roll 2
		//Roberta Williams
			$('#robertawilliams-a, #robertawilliams-b').hover( function() {
				if (bodyid == group2 || bodyid == group3 || bodyid == group4 || bodyid == group5 || bodyid == group6) {	
					$(charnames).css('backgroundPosition', '0 -394px')
				}
				if (bodyid == group2) {
					$(charmap).css('backgroundPosition', '0 -534px');
				}
				}, function() {
				$(charnames).css('backgroundPosition', '0 0');
				$(charmap).css('backgroundPosition', '0 0');
			})
	
		// Danielle McGuire
			$('#daniellemcguire-a, #daniellemcguire-b').hover( function() {
				if (bodyid == group2 || bodyid == group3 || bodyid == group4 || bodyid == group5 || bodyid == group6) {	
					$(charnames).css('backgroundPosition', '0 -274px')
				}
				if (bodyid == group2) {
					$(charmap).css('backgroundPosition', '0 -534px');
				}
				}, function() {
				$(charnames).css('backgroundPosition', '0 0');
				$(charmap).css('backgroundPosition', '0 0');
			})
	// Roll 3	
		//Carl Williams
			$('#carlwilliams-a, #carlwilliams-b').hover( function() {
				if (bodyid == group3 || bodyid == group4 || bodyid == group5 || bodyid == group6) {	
					$(charnames).css('backgroundPosition', '0 -354px')
				}
				if (bodyid == group3) {
					$(charmap).css('backgroundPosition', '0 -534px');
				}
				}, function() {
				$(charnames).css('backgroundPosition', '0 0');
				$(charmap).css('backgroundPosition', '0 0');
			})
	// Roll 4
		// Garry Butterworth
			$('#garrybutterworth').hover( function() {
				if (bodyid == group4 || bodyid == group5 || bodyid == group6) {	
					$(charnames).css('backgroundPosition', '0 -513px')
				}
				if (bodyid == group4) {
					$(charmap).css('backgroundPosition', '0 -534px');
				}
				}, function() {
				$(charnames).css('backgroundPosition', '0 0');
				$(charmap).css('backgroundPosition', '0 0');
				}
			)
		//Steve Owen
			$('#steveowen').hover( function() {
				if (bodyid == group4 || bodyid == group5 || bodyid == group6) {	
					$(charnames).css('backgroundPosition', '0 -232px')
				}
				if (bodyid == group4) {
					$(charmap).css('backgroundPosition', '0 -534px');
				}
				}, function() {
				$(charnames).css('backgroundPosition', '0 0');
				$(charmap).css('backgroundPosition', '0 0');
			})
		// Jacqui James
			$('#jacquijames').hover( function() {
				if (bodyid == group4 || bodyid == group5 || bodyid == group6) {	
					$(charnames).css('backgroundPosition', '0 -192px')
				}
				if (bodyid == group4) {
					$(charmap).css('backgroundPosition', '0 -534px');
				}
				}, function() {
				$(charnames).css('backgroundPosition', '0 0');
				$(charmap).css('backgroundPosition', '0 0');
			})
	// Roll 5	
		// Alphonse Gangitano
			$('#alphonse').hover( function() {
				if (bodyid == group5 || bodyid == group6) {
					$(charnames).css('backgroundPosition', '0 -314px')
					$(charmap).css('backgroundPosition', '0 -534px');
				}
				}, function() {
				$(charnames).css('backgroundPosition', '0 0');
				$(charmap).css('backgroundPosition', '0 0');
			})
	
	// Roll 6
		// Alphonse Gangitano
			/*$('#alphonse').hover( function() {
				if (bodyid == group6) {
					$(charnames).css('backgroundPosition', '0 -314px')
					$(charmap).css('backgroundPosition', '0 -534px');
				}
				}, function() {
				$(charnames).css('backgroundPosition', '0 0');
				$(charmap).css('backgroundPosition', '0 0');
			})*/
			
			
// Default Gropup
/*
$('#abv, #lewiscaine, #zgw, #mrt').mouseover(function(e) {
if ($(charmap).hasClass('active')) {
return false
} else {
$(charmap).addClass('active');			 
}
})
if (bodyid == group0) {
$('#jasonmoran-a, #jasonmoran-b, #markmoran').mouseleave(function(e) {																 
$(charmap).removeClass('active');
})
}*/
		
			
	}
}

var socialBookmarking = {

	bookmark: function () {
		if (!document.getElementById("share")) return false;
		var bookmarkingSites = $("#share a");
		for (var i = 0; i < bookmarkingSites.length; i++) {
			/* Get page header to be used in URL string */
			var pageHeader = $('title').text();
			var titleText = encodeURIComponent(pageHeader);
			/* Build URL based on what link is selected */
			bookmarkingSites[i].onclick = function (e) {	
				e.preventDefault();
				var socialBookmarkSite = this.href;
				var thisPageURL = encodeURIComponent(location.href);			
				switch (socialBookmarkSite) {
				case "http://www.stumbleupon.com/":
					var socialLink = socialBookmarkSite+ 'submit?url=' + thisPageURL + '&title=' + titleText;
					break;   
				case "http://www.youtube.com/":
					var socialLink = 'http://www.youtube.com/';
					break;   

				case "http://www.facebook.com/sharer.php":
					var socialLink = socialBookmarkSite+ '?u=' + thisPageURL + '&t=' + titleText;
					break; 
				case "http://twitter.com/":
					var articleId = $('#articleid').text();
					//var shortUrl = 'http://stv.tv/a/' + articleId +'/';
					//var socialLink = 'http://twitter.com/home?status=' + titleText + ', ' + shortUrl;
					var socialLink = 'http://twitter.com/home?status=' + titleText + ', ' + thisPageURL;
					break;										
				default:
					var socialLink = socialBookmarkSite+ '?url=' + thisPageURL + '&title=' + titleText;
					break;
				}
				var thisLink = this.href;
				window.open(socialLink); 
				//g.stopDefault(thisLink);
				return false;
			}
		}
	}
};



$(window).load(function () {
	socialBookmarking.bookmark();
	
	//Check if cookie has been set, if its set to no remove the video
	var x = readCookie('underbelly')
	if (x == "guidanceNo") {
		$('#videocontainer object').fadeOut();
	}	
})