// JavaScript Document 
$(document).ready(function(){
	// homepage stuff 
	var number_of_images = 5;
	var loaded = 0;

	$("body").addClass("has_js");
	$(".tabs h3").each(function(){
		var t = $(this).text();
		$(this).empty().append('<a href="#"> ' + t + '</a>');
	});
	$(".tabs h3 a").click(function(e){
		e.preventDefault();
		var th = $(this);
		th.blur();
		if(th.parent().parent().hasClass("active")){
			return;
		}
		th.parent().parent().parent().find(".active").removeClass("active").css("z-index", 10);
		th.parent().parent().addClass("active").css("z-index", 2000);
		

		
	});
	$(".navwatch h3 a, .on_track_tab h3 a").click();


	jc_offset = 1;
	$(".riderlist li").each(function(){
		//
		hr = $(this).find("a").attr("href")
		if(window.location.href.indexOf(hr) > -1){
			$(this).addClass("currentvid");
			jc_offset = $(".riderlist li").index(this);
			jc_offset++;
		}
	});
	$(".highlights_scroller li").each(function(){
		//
		hr = $(this).find("a").attr("href")
		if(window.location.href.indexOf(hr) > -1){
			$(this).addClass("currentvid");
			jc_offset = $(".highlights_scroller li").index(this);
			jc_offset++;
		}
	});
	$(".videos li").each(function(){
		//
		hr = $(this).find("a").attr("href")
		if(window.location.href.indexOf(hr) > -1){
			$(this).addClass("currentvid");
			jc_offset = $(".videos").index(this);
			jc_offset++;
		}
	});
	
	$(".videos li").each(function(){
		//
		hr = $(this).find("a").attr("href")
		if(window.location.href.indexOf(hr) > -1){
			$(this).addClass("currentvid");
			jc_offset = $(".videos").index(this);
			jc_offset++;
			$(".tabs li:has(.currentvid) h3 a").click();
		}
	});
	$(".gallery_list li").each(function(){
		//
		hr = $(this).find("a").attr("href")
		if(window.location.href.indexOf(hr) > -1){
			$(this).addClass("currentvid");
			jc_offset = $(".gallery_list").index(this);
			jc_offset++;
		}
	});
	

	$(".tab_content ul, .riderlist, .highlights_scroller, .videos, .gallery_list").jcarousel({ //, .gallery_list
		animation: 950,
		initCallback: function(c,s){
			$(".jcarousel-next, .jcarousel-prev").click(function(e){e.preventDefault();$(this).blur();});
		},
		start: jc_offset,
		buttonNextHTML: '<a href="#"><span></span>Next</a>',
		buttonPrevHTML: '<a href="#"><span></span>Prev</a>'
	});
	
	$(".navwatch h3, .on_track_tab h3").css("left", "10px");

	$(".expandable_content h3").append('<a class="show_hide"><span class="image open"></span><span class="text">Hide Extra Content</span></a>').each(function(){
		var div = $('<div class="content"></div>');
		var next = $(this).next();
		while(next.is("p, h4, h5, img, ul, ol, div, br")){
			next_new = next.next();
			div.append(next);
			next = next_new;
		}
		$(this).after(div);
	});
	
	
	$(".hide").before('<a class="show_hide"><span class="image"></span><span class="text">Hide Extra Content</span></a>');
	
	$(".race_caption .show_hide, .comments .show_hide").click(function(e){
		e.preventDefault();
		slider($(this).next(), $(this));
	});
	$(".race_caption .show_hide, .comments .show_hide").click();
	
	$(".expandable_content .show_hide").click(function(e){
		e.preventDefault();
		slider($(this).parent().next(), $(this));
	});
	$(".show_hide").hover(function(){$(this).addClass("hover");},function(){$(this).removeClass("hover");});
	$(".show_hide:gt(0)").click();
	
	function slider(el, th){
		th.blur();
		if(th.text() == "Hide Extra Content"){
			el.slideUp(1500);
			th.find(".image").removeClass("open");
			th.find(".text").text("Show More Content");
		} else {
			el.slideDown(1500);
			th.find(".image").addClass("open");
			th.find(".text").text("Hide Extra Content");
		}
		
	}
	
	
	/* IMAGE GALLERIES */
	if($(".imagegalleries").length > 0){
		thumbs_height = $(".thumbs").height();
		$(".thumbs").css({"opacity": "0.6", "position": "absolute"}).hide();
		$(".browser").empty().append('<a href="#">Show Thumbnails</a>');
		
		$("#image_holder").append('<p id="g_next"><span></span><a href="#">Next &raquo;</a></p>');
		$("#image_holder").append('<p id="g_prev"><span></span><a href="#">&laquo; Prev</a></p>');

		$(".browser a").click(function(e){
			e.preventDefault();
			$(this).blur();
			t = $(this).text();
			if(t == "Show Thumbnails"){
				$(this).text("Hide Thumbnails");
				$(".thumbs").slideDown(500);
			} else {
				$(this).text("Show Thumbnails");
				$(".thumbs").slideUp(500);
			}
		});
			
		$("#image_holder").hover(function(){
			//$(".browser").animate({bottom: thumbs_height}, 500);
			//$(".thumbs").slideDown(500);
			$("#g_next, #g_prev").show();
		}, function(){
			//$(".browser").animate({bottom: 0},500);
			//$(".thumbs").slideUp(500);
			$("#g_next, #g_prev").hide();
		});
		$(".thumbs").hover(function(){
			$(".thumbs").css("opacity", "1");
		}, function(){
			$(".thumbs").css("opacity", "0.6");
		});
		
		$("#g_next, #g_prev").hide();
		$("#g_next a").click(function(e){
			e.preventDefault();
			$(this).blur();
			
			if($(".thumbs .currentimage").next().length == 0){
				$(".thumbs .currentimage").removeClass("currentimage");
				$(".thumbs li:first").addClass("currentimage");
				images.current = 0;
			} else {
				$(".thumbs .currentimage").removeClass("currentimage").next().addClass("currentimage");
			}
			new_img = ++images.current;
			swapImage(images[new_img]);
			
			
		});
		$("#g_prev a").click(function(e){
			e.preventDefault();
			$(this).blur();
			
			if($(".thumbs .currentimage").prev().length == 0){
				$(".thumbs .currentimage").removeClass("currentimage");
				$(".thumbs li:last").addClass("currentimage");
				images.current = 11;
			} else {
				$(".thumbs .currentimage").removeClass("currentimage").prev().addClass("currentimage");
			}
			new_img = --images.current;
			swapImage(images[new_img]);
		});
		$(".thumbs a").click(function(e){
			//
			e.preventDefault();
			var num = $(this).attr("href");
			num_arr = num.match(/i=([0-9]{2})/);
			num = num_arr[1];
			num = parseInt(num,10);
			images.current = num;
			swapImage(images[num]);
			
			$(".currentimage").removeClass("currentimage");
			$(this).parent().addClass("currentimage");
		});
	}
	if($(".galleries").length > 0){
		$(".more_info").prev().append('<a id="js_more" href="#">More Info</a>');
		$(".more_info").hide();
		temp_var = $("#caption").height();
		$("#caption span").height(temp_var);
		$("#js_more").click(function(e){
			e.preventDefault();
			$(this).blur();
			if($(this).text() == "More Info"){
				$(this).parent().next().show(function(){
					temp_var = $("#caption").height();
					$("#caption span").height(temp_var);
				});
				$(this).text("Less Info");
				
			} else {
				$(this).text("More Info");
				$(this).parent().next().hide(function(){
					temp_var = $("#caption").height();
					$("#caption span").height(temp_var);
				});
			}
		});
	}
	if($("#home").length > 0){
		//if(document.cookie.indexOf("tease=seenit") != -1){
			addTeaser2();
		/*} else {
			console_original_content = $("#console").html();
			addTeaser1();
		}
		*/
	}
	if($("#tease").length > 0){
		addTeaser1();
	}
	if($("#circuit_holder").length > 0){
		var so = new SWFObject("/northernireland/nw200/swf/guide.swf", "circuit_guide", "784", "368", "8", "#000000");
		so.addParam("swliveconnect", true);
		so.addParam("allowScriptAccess", "always");
		so.write("circuit_holder");
	}
});
function finished(){
	$("#console").fadeOut(1000, function(){
		$("#console").empty().fadeIn(1000, function(){
			//$("#console").append('<div id="player"><span class="player_bg">&nbsp;</span><p>You need to have the Flash player installed and Javascript enabled in order to view the video content</p></div>');
			$("#console").html(console_original_content);
			addTeaser2();
	});});
	document.cookie = "tease=seenit;path=/northernireland/nw200";
}
function addTeaser1(){
	var so = new SWFObject("/northernireland/nw200/swf/tease.swf", "tease", "784", "368", "8", "#000000");
	so.addParam("swliveconnect", true);
	so.addParam("allowScriptAccess", "always");
	so.write("console");
}
function addTeaser2(){
	var emp = new bbc.Emp();  
	emp.setDomId("player");   
	emp.setWidth("512");  
	emp.setHeight("323");
	emp.setConfig("http://www.bbc.co.uk/northernireland/nw200/media/config.xml");  
	//	emp.setPlaylist("http://www.bbc.co.uk/northernireland/nw200/media/bestonboard512.xml");  
	var random_vid = randomTease();
	emp.setPlaylist(random_vid.playlist);
	$("#caption p").empty().append(random_vid.caption);  
	emp.write();
}
function swapImage(new_image_details){
	var img = new Image();
	$(".caption_text").fadeTo(1000, 0);
	$(".the_image_holder").fadeTo(1000, 0, function(){
		img.onload = function(){
			$(".the_image").attr("src", new_image_details.src);
			$(".the_image").attr("alt", new_image_details.alt);
			$(".caption_text").text(new_image_details.caption);
			$(".the_image_holder, .caption_text").fadeTo(1000, 1);
		}
		img.src=new_image_details.src;
		
	});	
}
function randomTease(){
	return teaser_videos[Math.floor(Math.random()*teaser_videos.length)];
}
var console_original_content = '<div id="player"><span class="player_bg">&nbsp;</span><p>You need to have the Flash player installed and Javascript enabled in order to view the video content</p></div><div id="caption"><span></span><p>&nbsp;</p>';