	
	var slid = 0;
	var exp = null;
	var sharing = false;
	var next = null;
	var video = null;
	var sequence = true;
    var searchterms = "Search All Videos";
	
	//|	Kick off the slide functionality.
	//|-----------------------------------------------------
	function slide (e, t, p, d) {
		var m = get_num(e.style[d]);
		var a = p - m;
		var b = new Date().getTime ();
		var x = b + t;
		ice(e, d, a, m, t, x);
	}
	
	//|	Slide levels.
	//|-----------------------------------------------------
	function ice (e, d, a, s, t, x) {
		var c = new Date().getTime();
		var r = Math.max(0, x - c);
		var n = parseInt(a - (Math.pow(r, 3) / Math.pow(t, 3)) * a);
		e.style[d] = (s + n) + "px";
		if (r > 0) {
			setTimeout(function () { ice(e, d, a, s, t, x); }, 10);
		}
	}
	
	//|	Simplify life by making sure numbers are numbers.
	//|-----------------------------------------------------
	function get_num (n) {
		if (n == null) { return 0; }
		if (n.indexOf && (n.indexOf('px') > -1)) {
			n = n.substring(0, n.indexOf('px'));
		}
		if (!n && (n!==0)) { return 0; }
		if ((n < 0) || (n.substring && (n.substring(0, 1) == "-"))) { return (0 - parseInt(n.substring(1))); }
		return parseInt(n);
	}
	
	function valid(s) {
		return ((s.indexOf(".") > 2) && (s.indexOf("@") > 0));
	}
	
	//|	Figures out when to stop sliding :O!
	//|-----------------------------------------------------
	function factor (s) {
		var w = get_num(document.getElementById("slider").style.width);
		var n = ($("#slider div.video-thumb").length - 1);
		if (s > 0) {
			s =  0;
		} else if (s < (0 - (n * 136))) {
			s = (0 - (n * 136));
		}
		return slid = s;
	}
	
	function onTemplateLoaded(e) {
		exp = brightcove.getExperience(e);
		exp.getModule(APIModules.EXPERIENCE).addEventListener("templateReady", onTemplateReady);
	}
	
	//|	This nonsense sets the active video thumb
	//|-----------------------------------------------------
	function onTemplateReady(e) {	
		exp.getModule(APIModules.VIDEO_PLAYER).addEventListener("mediaBegin", onContentLoad);
		// exp.getModule(APIModules.VIDEO_PLAYER).addEventListener("mediaComplete", playNext);
        // uncomment above line to turn on auto-advance when the current video is done 
	}
	
	//|	Do some voodoo to remove scrollbars when not needed
	//|-----------------------------------------------------
	function overflow () {
		var e = $("#channels ul:first");
//		var z = $(e).clone().appendTo(document.body);
//		$(z).css("float", "left");
//		$(z).css("height", "auto");
		var sux = 0;
		$(e).children().each(function(i){
			sux += $(this).height();
		});
//		if ($(z).height() < $(e).height()) {
		if (sux < $(e).height()) {
			$("#channels ul:first").css("overflow", "auto");
		} else {
			$("#channels ul:first").css("overflow-y", "scroll");
		}
//		$(z).remove();
	}
	
	//|	Open the share overlay
	//|-----------------------------------------------------
	function share () {
		var s = $("#pop").clone().appendTo(document.body);
		var t = $("#share-share").offset();
		s.css("display", "block");
		s.css("position", "absolute");
		s.css("top", (t.top + 13)+"px");
		s.css("left", (t.left - 119)+"px");
		s.find("div.pop-middle");
		s.attr("id", "social");
		
		$(document.body).mousemove(function(e){
			var x = e.pageX;
			var y = e.pageY;
			var h = $("#social").height();
			var w = $("#social").width();
			var o = $("#social").offset();
			if (((o.top - 20) > y) || ((o.top + h) < y) || (o.left > x) || ((o.left + w) < x)) {
				$("#social").remove();
				sharing = false;
			}
		});
		
		var e = document.getElementById("social").getElementsByTagName("div")[1];
		e.innerHTML += "<div onclick='share_digg();'><img src='/cro/video-hub/static/images/social-digg.png' /> <span class='bcl'>Digg</span></div>";
		e.innerHTML += "<div onclick='share_facebook();'><img src='/cro/video-hub/static/images/social-facebook.png' /> <span class='bcl'>Facebook</span></div>";
		e.innerHTML += "<div onclick='share_stumble();'><img src='/cro/video-hub/static/images/social-stumble.png' /> <span class='bcl'>StumbleUpon</span></div>";
//		e.innerHTML += "<div onclick='share_delicious();'><img src='/images/social-delicious.png' /> <span class='bcl'>Delicious</span></div>";
//		e.innerHTML += "<div onclick='share_technorati();'><img src='/images/social-technorati.png' /> <span class='bcl'>Technorati</span></div>";
//		e.innerHTML += "<div onclick='share_furl();'><img src='/images/social-furl.png' /> <span class='bcl'>Furl</span></div>";
//		e.innerHTML += "<div onclick='share_myspace();'><img src='/images/social-myspace.png' /> <span class='bcl'>MySpace</span></div>";
//		e.innerHTML += "<div onclick='share_google();'><img src='/images/social-google.png' /> <span class='bcl'>Google</span></div>";

	}
	
	//|	Open the email overlay
	//|-----------------------------------------------------
	function email () {	
		if ($("#email").length > 0) { return; }
		var s = $("#pop").clone().appendTo(document.body);
		var t = $("#share-email").offset();
		s.css("display", "block");
		s.css("position", "absolute");
		s.css("top", (t.top + 13)+"px");
		s.css("left", (t.left - 119)+"px");
		s.find("div.pop-middle");
		s.attr("id", "email");
		
		/*
		$(document.body).mousedown(function(e){
			var x = e.pageX;
			var y = e.pageY;
			var h = $("#email").height();
			var w = $("#email").width();
			var o = $("#email").offset();
			if (((o.top - 20) > y) || ((o.top + h) < y) || (o.left > x) || ((o.left + w) < x)) {
				$("#email").remove();
			}
		});
		*/
		
		var e = document.getElementById("email").getElementsByTagName("div")[1];
		e.innerHTML += "<div id='email-head'><div id='email-close' onclick='$(\"#email\").remove();'></div>Send to a friend</div>";
		e.innerHTML += "<input type='text' id='email-email' value='Your email' />";
		e.innerHTML += "<input type='text' id='email-friend' value=\"Friend's email\" />";
		e.innerHTML += "<textarea id='email-message'>Message (optional)</textarea>";
		e.innerHTML += "<div id='email-send'><span class='bcl'>Send</span></div>";
		
		$("#email-send").click(function(){
			if (valid($("#email-friend").val())) {
				var m = ($("#email-message").val() == "Message (optional)") ? "None provided." : $("#email-message").val();
				exp.getModule(APIModules.SOCIAL).shareVideoViaEmail($("#email-email").val(), $("#email-friend").val(), m);
				$("#email div.pop-middle").html("<div id='email-head'>Email sent!</div>");
				setTimeout(function(){$("#email").remove();}, 2000);
			} else {
				alert("Please enter a valid email address");
			}
		});
		
		$("#email-email").focus(function(){
	    	if ($("#email-email").val() == "Your email") {
	    		$("#email-email").val("");
	    	}
	    });
		
	    $("#email-email").blur(function(){
	    	if ($("#email-email").val() == "") {
	    		$("#email-email").val("Your email");
	    	}
	    });
	    
	    $("#email-friend").focus(function(){
	    	if ($("#email-friend").val() == "Friend's email") {
	    		$("#email-friend").val("");
	    	}
	    });
	    
	    $("#email-friend").blur(function(){
	    	if ($("#email-friend").val() == "") {
	    		$("#email-friend").val("Friend's email");
	    	}
	    });
	    
	    $("#email-message").focus(function(){
	    	if ($("#email-message").val() == "Message (optional)") {
	    		$("#email-message").val("");
	    	}
	    });
	    
	    $("#email-message").blur(function(){
	    	if ($("#email-message").val() == "") {
	    		$("#email-message").val("Message (optional)");
	    	}
	    });
		
	}
	
	//|	Borrowed with permission from Skyspine UI
	//|-----------------------------------------------------
	function environment () {
		var t;
		var b = new Array(
			{string: navigator.userAgent, subString: "Chrome", identity: "Chrome"},
			{string: navigator.userAgent, subString: "OmniWeb", versionSearch: "OmniWeb/", identity: "OmniWeb"},
			{string: navigator.vendor, subString: "Apple", identity: "Safari", versionSearch: "version"},
			{prop: window.opera, identity: "Opera"},
			{string: navigator.vendor, subString: "iCab", identity: "iCab"},
			{string: navigator.vendor, subString: "KDE", identity: "Konqueror"},
			{string: navigator.userAgent, subString: "Firefox", identity: "Firefox"},
			{string: navigator.vendor, subString: "Camino", identity: "Camino"},
			{string: navigator.userAgent, subString: "Netscape", identity: "Netscape"},
			{string: navigator.userAgent, subString: "MSIE", identity: "Explorer", versionSearch: "MSIE"},
			{string: navigator.userAgent, subString: "Gecko", identity: "Mozilla", versionSearch: "rv"},
			{string: navigator.userAgent, subString: "Mozilla", identity: "Netscape", versionSearch: "Mozilla"});
		var o = new Array(
			{string: navigator.platform, subString: "Win", identity: "windows"},
			{string: navigator.platform, subString: "Mac", identity: "mac"},
			{string: navigator.platform, subString: "Linux", identity: "linux"});
		var g = function (d) { 
			for (var i = 0; i < d.length; i++)	{
				var s = d[i].string;
				var p = d[i].prop;
				t = d[i].versionSearch || d[i].identity;
				if (s) {
					if (s.indexOf(d[i].subString) != -1) { return d[i].identity; }
				} else if (p) { return d[i].identity; }
			}
		};
		var h = function (s, t) {
			var i = s.indexOf(t);
			if (i == -1) { return; }
				return parseFloat(s.substring(i + t.length + 1));
		};
		browser = g(b) || "unknown";
		version = h(navigator.userAgent, t) || h(navigator.appVersion, t) || "unknown";
		browser = browser.toLowerCase();
		os = g(o) || "unknown";
		return new Array(os, browser, version);
	}

	function playNext () {
		if (sequence) {
			exp.getModule(APIModules.VIDEO_PLAYER).loadVideo(next);
			highlight();
		}
	}
	
	//|	Set the currently playing video style
	//|-----------------------------------------------------
	function onContentLoad(e) {
		highlight();
	}
	
	function highlight() {
		var found = false;
	  	if(exp){
            video = exp.getModule(APIModules.VIDEO_PLAYER).getCurrentVideo();
            $("#viewing-overlay").remove();
            $("#browse div.video-thumb").each(function(i){
                if (found) {
                    next = $(this).attr("vid");
                    found = false;
                }
                if ($(this).attr("vid") == video.id) {
                    var c = $(this).clone(true);
                    c.attr("id", "viewing-overlay");
                    c.appendTo(document.body);
                    c.css("top", ($(this).offset().top - 6)+"px");
                    c.css("left", ($(this).offset().left - 6)+"px");
                    found = true;
                }
            });
            $("#desc-truncated").html(video.shortDescription);
            $("#videoinfo h4").html(video.displayName);
        }
	}
	
	//|	Scrape the DOM for some details to share
	//|-----------------------------------------------------
	function details () {
		return new Array(escape(window.location), escape($("#videoinfo h4").html()), escape($("#videoinfo p").html()));
	}
	
	//|	Share locations for a variety of services
	//|-----------------------------------------------------
	function share_digg () {
		var s = details();
		window.location = "http://digg.com/submit?url="+s[0]+"&title="+s[1]+"&bodytext="+s[2]+"&media=video";
	}
	
	function share_delicious() {
		var s = details();
		window.location = "http://del.icio.us/post?url="+s[0]+"&title="+s[1]+"&notes="+s[2];
	}
	
	function share_facebook() {
		var s = details();
		window.location = "http://www.facebook.com/sharer.php?u="+s[0]+"&t="+s[1];
	}
	
	function share_myspace() {
		var s = details();
		window.location = "http://www.myspace.com/index.cfm?fuseaction=postto&t="+s[1]+"&c="+s[1]+"&u="+s[0]+"&l=2";
	}
	
	function share_stumble() {
		var s = details();
		window.location = "http://www.stumbleupon.com/submit?url="+s[0]+"&title="+s[1];
	}
	
	function share_google() {
		var s = details();
		window.location = "http://www.google.com/bookmarks/mark?op=add&bkmk="+s[0]+"&title="+s[1];
	}
	
	function share_furl() {
		var s = details();
		window.location = "http://furl.net/storeIt.jsp?t="+s[1]+"&u="+s[0];
	}
	
	function share_technorati() {
		var s = details();
		window.location = "http://technorati.com/faves?add="+s[0];
	}
	
	function fix_ff2 () {
	
		$("#relatedvideos div.video-thumb").each(function(i){
			$(this).hover(function(){
				$(this).children("a").css("margin-bottom", "4px");
			}, function(){
				$(this).children("a").css("margin-bottom", "5px");
			});
		});
	
	}

$(document).ready(function() {

	//|	This nonsense is here to fix a really odd browser bug with FF2 on Windows
	var envi = environment();
	if ((envi[0] == "windows") && (envi[1] == "firefox") && (envi[2] < 3)) {
		fix_ff2();
		$("#search-box").css("top", "-6px");
	}

    $("form.search-form").submit(function(e) {
    	$("#viewing-overlay").remove();
        var form = $(e.target);
        var terms = form.children("input[name=searchterms]").val();

        if (jQuery.trim(terms) == "")
            return false;

        var url = form.attr("action") + encodeURIComponent(terms) + "/";

        $("#videolist").load(url, null, function (response, status, req) {
            if (status != "success") {
                $("<p class='error'>There was a problem with the search request</p>").appendTo(this);
            }
            
        });
        
        sequence = false;
		searchterms = terms;
        return false;
    });
	
	$("form#makeModelForm").submit(function(e) {
    	$("#viewing-overlay").remove();
        var form = $(e.target);
        var terms = form.children("input[name=searchterms]").val();

        if (jQuery.trim(terms) == "")
            return false;

        var url = form.attr("action") + encodeURIComponent(terms) + "/";

        $("#videolist").load(url, null, function (response, status, req) {
            if (status != "success") {
                $("<p class='error'>There was a problem with the search request</p>").appendTo(this);
            } else {
				$("#search-box2").focus(function(){
					if ($("#search-box2").val() == "Search All Videos") {
						$("#search-box2").val("");
					}
				});
				
				$("#search-box2").blur(function(){
					if ($("#search-box2").val() == "") {
						$("#search-box2").val("Search All Videos");
					}
				});
			}
            
        });
        
        sequence = false;
		searchterms = terms;
        return false;
    });

    $("a.search-prev, a.search-next").click(function(e) {
        var url = $(e.target).parent().attr("href");

        if (jQuery.trim(url) == "")
            return false;
        $("#viewing-overlay").remove();
        $("#videolist").load(url, null, function (response, status, req) {
            if (status != "success") {
                $("<p class='error'>There was a problem with the search request</p>").appendTo(this);
            }

        });

        return false;
    });
    
    $("#search-box").focus(function(){
    	if ($("#search-box").val() == "Search All Videos") {
    		$("#search-box").val("");
    	}
    });
    
    $("#search-box").blur(function(){
    	if ($("#search-box").val() == "") {
    		$("#search-box").val("Search All Videos");
    	}
    });
	
	$("#search-box2").focus(function(){
		if ($("#search-box2").val() == "Search All Videos") {
			$("#search-box2").val("");
		}
	});
	
	$("#search-box2").blur(function(){
		if ($("#search-box2").val() == "") {
			$("#search-box2").val("Search All Videos");
		}
	});
    
    $("#slide-left").click(function(){
    	slide(document.getElementById('slider'), 500, factor(slid+136), "left");
    });
    
    $("#slide-right").click(function(){
    	slide(document.getElementById('slider'), 500, factor(slid-136), "left");
    });
    
    $("#share-share").mouseover(function () {
    	if (!sharing) {
	    	share();
	    	sharing = true;
	    }
    });
    
    $("#share-email").click(function () {
	   	email();
    });

    $("#desc-truncated a").click(function() {
        return false;
    });

    $("#desc-truncated a").mouseover(function() {
        $("#desc-truncated").hide();
        $("#relatedlink").hide();
        $("#desc-full").show();
    });

    $("#desc-full").mouseout(function() {
        $("#desc-full").hide();
        $("#desc-truncated").show();
        $("#relatedlink").show();

    });
    
    overflow();
    
    searchterms = searchterms || "Search All Videos";
    
    $("#search-box").val(searchterms);

    window.onresize = highlight;
});

function selectMakeModel(m) {
	$("#search-box2").val("Search All Videos");
	$("#viewing-overlay").remove();
	ma = $("#makeList").val();
	mo = $("#modelList").val();
	url = "";
	
	if (m == "0") {//Just the make
		url = "/cro/video-hub/iihs-crash-tests/"+ma+"/";
	}
	else {//Make and model
		url = "/cro/video-hub/iihs-crash-tests/"+ma+"/"+mo+"/";
	}			
	
	if(ma != "na")
	{
		$("#videolist").load(url, null, function (response, status, req) {
			if (status != "success") {
				$("<p class='error'>There was a problem with the search request</p>").appendTo(this);
			} else {
				$("#search-box2").focus(function(){
					if ($("#search-box2").val() == "Search All Videos") {
						$("#search-box2").val("");
					}
				});
				
				$("#search-box2").blur(function(){
					if ($("#search-box2").val() == "") {
						$("#search-box2").val("Search All Videos");
					}
				});
			}
			
		});
	}
	else
		location.href = "/cro/video-hub/cars/iihs-crash-tests/";
	sequence = false;
	return false;
}

function search(){
	$("#viewing-overlay").remove();
	term = $("#search-box2").val();
	url = "/cro/video-hub/search/"+term.replace(/ /gi,"+")+"/";

	$("#videolist").load(url, null, function (response, status, req) {
		if (status != "success") {
			$("<p class='error'>There was a problem with the search request</p>").appendTo(this);
		}
		
	});
	return false;
}

setInterval('if ($("#makeList").val() != "na"){ $("#modelList").attr("disabled", ""); } else { $("#modelList").attr("disabled", "disabled"); }', 500);
