function pngCheck(){
	if($.browser.msie() && ($.browser.version.number() == 6)){
		$("*").each(function(){
			var pngSrc = $(this).css("background-image");
			if(pngSrc.indexOf("png") > -1) {
				pngSrc = pngSrc.split('"');
				$(this).css({background: "none", filter: "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+pngSrc[1]+"',sizingMethod='crop')"});
			}
		});
	}
}

// vars for checking if html and swf are loaded
var pageIsReady = false;
var swfIsReady = false;

// var for giving swf the path to xml
var xmlpath = "/xml/ddr_races.xml";

// var for showing initial race
var initialRaceId = "featured4";
if (location.pathname.match(/\/ddr\//)) {
	initialRaceId = location.pathname.replace(/\/ddr\/([^\/]*)\/?/, "$1");
}

// called when html page is ready, called in $(document).ready() below
function pageReady() {
	pageIsReady = true;
	checkIfAllReady();
}

// called from swf when it has loaded it's ready
function swfReady() {
	swfIsReady = true;
	checkIfAllReady();
}

// check if html and swf are ready
function checkIfAllReady() {
	if ( pageIsReady && swfIsReady ) {
		document.getElementById("intel_ddr").continueSwfSetUp( xmlpath, initialRaceId );
	}
}

// use this to show race in swf
function setRaceInSwf( raceId ) {
	document.getElementById("intel_ddr").showRaceInSwf( raceId );
}

// use this to update carousel in html page
function setRaceFromSwf( raceId ) {
	location.href = "/ddr/"+raceId+"/";
}

function prepMenuVideo(){
	var x = 0;
	var y = 489;
	var first = $("#video_chooser_slideshow ul li:first-child").html();
	var last = $("#video_chooser_slideshow ul li:last-child").html();
	$("#video_chooser_slideshow ul li:first-child").before("<li class=\"bonus\">"+last+"</li>");
	$("#video_chooser_slideshow ul li:last-child").after("<li class=\"featured\">"+first+"</li>");
	$("#video_chooser_slideshow ul li").each(function(){
		x = x + y
	});
	$("#video_chooser_slideshow ul").width(x).css("left", "-489px");
}

function prepMenuBadges(){
	var a = 0;
	var b = $("#badge_wrap ul li").width();
	var first = $("#badge_wrap ul li:first-child").html();
	var last = $("#badge_wrap ul li:last-child").html();
	$("#badge_wrap ul li:first-child").before("<li>"+last+"</li>");
	$("#badge_wrap ul li:last-child").after("<li>"+first+"</li>");
	$("#badge_wrap ul li").each(function(){
		a = a + b
	});
	b = b - (2 * b);
	b = b + "px";
	$("#badge_wrap ul").width(a).css("left", b);
}

function prepMenuWallpapers(){
	var i = 0;
	var j = $("#wallpapers_wrap ul li").width();
	var first = $("#wallpapers_wrap ul li:first-child").html();
	var last = $("#wallpapers_wrap ul li:last-child").html();
	$("#wallpapers_wrap ul li:first-child").before("<li>"+last+"</li>");
	$("#wallpapers_wrap ul li:last-child").after("<li>"+first+"</li>");
	$("#wallpapers_wrap ul li").each(function(){
		i = i + j
	});
	j = j - (2 * j);
	j = j + "px";
	$("#wallpapers_wrap ul").width(i).css("left", j);
}


$(document).ready(function(){

	pngCheck();
	pageReady();
	prepMenuVideo();
	
	// LoL: rewrite of carousel to make page updates easier
	function slideTo($elem, direction) {
		if ($current.get(0) == $elem.get(0)) { return; }
		if (!direction) {
			direction = $current.prevAll().length > $elem.prevAll().length ? 'left' : 'right';
		}
		var pos = parseInt($current.css('left')) || 0;
		$('.carousel .slide').not($current).css('left', pos + (width * 2));
		$elem.css('left', pos + (direction == 'left' ? -width : width));
		$('.carousel .slideContainer').animate({
			left: (direction == 'left' ? '+=' : '-=') + width
		}, 500);
		$current = $elem;
		setSelectedState();
	}
	
	function setSelectedState() {
		var oldTab = $('.tab.selected').attr('id');
		$('.tabContainer .tab, .carousel .slide').removeClass('selected');
		$current.addClass('selected').data('tab').addClass('selected');
		var newTab = $('.tab.selected').attr('id');
		// track tab changes
//		if (oldTab != newTab) { waTrackAsPage(newTab.replace(/_/, " "),'','N'); }
		
		// set the sprite position based on current selection
		var bPos = 0; // default
		switch ($(".tab.selected").attr("id")) {
			case "invitational_races":
				bPos = -114; break;
			case "behind_the_scenes":
				bPos = -228; break;
		}
		$(".tabContainer").css("background-position", "0 " + bPos + "px");
	}
	
	var $tabs = $("<div class='tabContainer'>").appendTo('#video_chooser');
	$('.tab')
		.each(function() {
			var $first = $(this).find('.slide:first');
			$(this).click(function() { slideTo($first); });
		})
		.appendTo($tabs)
	
	var width =  $('.carousel').get(0).offsetWidth;
	var $cont = $("<div class='slideContainer'>").appendTo('.carousel');
	var $current = $('.tab .slide')
		.each(function() {
			$(this)
				.css('left', (width * 2))
				.show()
				.data('tab', $(this).parents('.tab:first'));
		})
		.appendTo($cont)
		.filter("#" + initialRaceId)
			.addClass('selected')
			.css('left', 0);
	
	setSelectedState(); // set initial tab (based on $current)
	
	$('#video_chooser .left_arrow').click(function() {
		var $prev = $current.prev();
		if ($prev.length == 0) { $prev = $('.carousel .slide:last'); }
		slideTo($prev, 'left');
	});
	
	$('#video_chooser .right_arrow').click(function() {
		var $next = $current.next();
		if ($next.length == 0) { $next = $('.carousel .slide:first'); }
		slideTo($next, 'right');
	});
	
	$('#watch_now').click(function() {
		this.href = "/ddr/" + $current.attr('id');
	});
	
	$(".label")
		.each(function() { $(this).text(""); }) // remove text label, background position has text
		.mouseout(setSelectedState)
		.mouseover(function() {
			// set the sprite position based on current and hover selection
			var active = $('.tab.selected').attr('id');
			var hover = $(this).parents('.tab:first').attr('id');
			var bPos = 0;
			
			if (active == hover) {
				return; // no hover over self
			} else if (active == "featured_races" && hover == "invitational_races") {
				bPos = -38;
			} else if (active == "featured_races" && hover == "behind_the_scenes") {
				bPos = -76;
			} else if (active == "invitational_races" && hover == "featured_races") {
				bPos = -152;
			} else if (active == "invitational_races" && hover == "behind_the_scenes") {
				bPos = -190;
			} else if (active == "behind_the_scenes" && hover == "featured_races") {
				bPos = -266;
			} else if (active == "behind_the_scenes" && hover == "invitational_races") {
				bPos = -304;
			}
			$(".tabContainer").css("background-position", "0 " + bPos + "px");
		});
	
	
	
	
	$(".right_arrow").bind("mousedown", function(){
		$(this).attr("class", "right_arrowactive");
	}).bind("mouseup", function(){
		$(this).attr("class", "right_arrow");
	}).bind("mouseout", function(){
		$(this).attr("class", "right_arrow");
	});

	$(".left_arrow").bind("mousedown", function(){
		$(this).attr("class", "left_arrowactive");
	}).bind("mouseup", function(){
		$(this).attr("class", "left_arrow");
	}).bind("mouseout", function(){
		$(this).attr("class", "left_arrow");
	});

	$("#dl_body span").click(function(e){
		e.preventDefault();
		var modalName = $(this).attr("rel");
		$(modalName).modal({persist: true, overlay: 80, onOpen: function(dialog){
			dialog.overlay.fadeIn(function(){
				dialog.container.fadeIn('slow');
				dialog.data.fadeIn('slow');
				pngCheck();
				if(modalName == "#badges"){ prepMenuBadges(); }
				if(modalName == "#wallpapers"){ prepMenuWallpapers(); }
				if($.browser.msie()) {
					$("#intel_ddr").hide();
					if($.browser.version.number() == 6){
						$("#modalContainer").css({top: $(window).scrollTop() + 30 + "px", position: "absolute"});
					}
				}
				if($.browser.firefox() && $.browser.mac() && ($.browser.version.number() == 2)){
					$("#intel_ddr").hide();
					$("#modalOverlay").css({opacity: "0"});
					$("#modalContainer").css({position: "absolute", zIndex: "99999"});
				}
			});
		},
		onClose: function(dialog){
			dialog.data.fadeOut('slow');
			dialog.container.fadeOut('slow', function(){
				dialog.overlay.fadeOut(function(){
					$.modal.close();
					if($.browser.msie() || ($.browser.firefox() && $.browser.mac() && ($.browser.version.number() == 2))){$("#intel_ddr").show();}
				});
			});
		}});
	}).mouseover(function(){
		var modalName = $(this).attr("rel");
		switch(modalName){
			case "#icons":
				$(this).css({backgroundPosition: "0 -70px"});
				break;
			case "#wallpapers":
				$(this).css({backgroundPosition: "0 -210px"});
				break;
			case "#badges":
				$(this).css({backgroundPosition: "0 -350px"});
				break;
		}
	}).mouseout(function(){
		var modalName = $(this).attr("rel");
		switch(modalName){
			case "#icons":
				$(this).css({backgroundPosition: "0 0"});
				break;
			case "#wallpapers":
				$(this).css({backgroundPosition: "0 -140px"});
				break;
			case "#badges":
				$(this).css({backgroundPosition: "0 -280px"});
				break;
		}
	});


	$("#icons li").hover(function(){
		$(this).css("background-image", "url(/images/icons-active.png)");
	}, function(){
		$(this).css("background-image", "none");
	});
	
	// LoL load various Flash files using SWFObject
	$.each([
		{	element: "first_meter", name: "bars_98faster",
			swf: "/flash/ddr_bars_encode.swf",
			height: 91, width: 262
		},
		{	element: "second_meter", name: "bars_49faster",
			swf: "/flash/ddr_bars_render.swf",
			height: 91, width: 262
		},
		{	element: "third_meter", name: "bars_25faster",
			swf: "/flash/ddr_bars_multitasking.swf",
			height: 91, width: 262
		},
		{	element: "badge7", name: "W4963d2816bf82ba449667f4da35eb204",
			swf: "http://widgets.clearspring.com/o/4963d2816bf82ba4/49667f4da35eb204/4963d2816bf82ba4/89f251d9",
			height: 324, width: 244, menu: true, network: true, script: true
		},
		{	element: "badge8", name: "W4963d2f06ad5addc49667f8a6dab3a99",
			swf: "http://widgets.clearspring.com/o/4963d2f06ad5addc/49667f8a6dab3a99/4963d2f06ad5addc/7631b3ad",
			height: 324, width: 244, menu: true, network: true, script: true
		},
		{	element: "badge5", name: "W494c09c0c932733449515b91fe811d65",
			swf: "http://widgets.clearspring.com/o/494c09c0c9327334/49515b91fe811d65/494c09c0c9327334/d9b08bcc",
			height: 324, width: 244, menu: true, network: true, script: true
		},
		{	element: "badge6", name: "W494c0a0eefd288b449515bb914680b12",
			swf: "http://widgets.clearspring.com/o/494c0a0eefd288b4/49515bb914680b12/494c0a0eefd288b4/82e03cb7",
			height: 324, width: 244, menu: true, network: true, script: true
		},
		{	element: "badge3", name: "W4918bf2479d982ce4918c0d706335222",
			swf: "http://widgets.clearspring.com/o/4918bf2479d982ce/4918c0d706335222/4918bf2479d982ce/f0e608c6",
			height: 324, width: 244, menu: true, network: true, script: true
		},
		{	element: "badge4", name: "W4918bef7c44a9a2f4918bfe38b7ea82c",
			swf: "http://widgets.clearspring.com/o/4918bef7c44a9a2f/4918bfe38b7ea82c/4918bef7c44a9a2f/a5eaa79",
			height: 324, width: 244, menu: true, network: true, script: true
		},
		{	element: "badge1", name: "W4918bfc3b757108a4918c02914dca2aa",
			swf: "http://widgets.clearspring.com/o/4918bfc3b757108a/4918c02914dca2aa/4918bfc3b757108a/c2cfc047",
			height: 324, width: 244, menu: true, network: true, script: true
		},
		{	element: "badge2", name: "W4918bf9950bc4f8f4918c011cfc08cb4",
			swf: "http://widgets.clearspring.com/o/4918bf9950bc4f8f/4918c011cfc08cb4/4918bf9950bc4f8f/80ef99b6",
			height: 324, width: 244, menu: true, network: true, script: true
		}
	], function(i, n) {
		if ($("#"+n.element).length) { // make sure target element exists
			var so = new SWFObject(n.swf, n.name, n.width, n.height, "9", "");
			so.addParam("wmode","transparent");
			if (!n.menu) { so.addParam("menu","false"); }
			if (!n.network) { so.addParam("allowNetworking","all"); }
			if (!n.script) { so.addParam("allowScriptAccess","always"); }
			so.write(n.element);
		}
	});


	$("p.buttons span").click(function(){
		var x = $(this).attr("rel");
		var headline = "#"+x+"_headline";
		var meter = "#"+x+"_meter";
		$("#chart h3").hide();
		$(headline).fadeIn();
		$("#chart .meter").hide();
		$(meter).fadeIn();
	}).mouseover(function(){
		$(this).css({backgroundImage: "url(/images/info-button-light.gif)"});
	}).mouseout(function(){
		$(this).css({backgroundImage: "url(/images/info-button-dark.gif)"});
	});

	$("#video_chooser .right_arrow").click(function(e){
		e.preventDefault();
		if(!$("#video_chooser_slideshow ul").is(":animated")){
			var x = parseInt($("#video_chooser_slideshow ul").css("left"));
			x = x - 489;
			x = x + "px";
			$("#video_chooser_slideshow ul").animate({left: x}, function(){
				x = parseInt($("#video_chooser_slideshow ul").css("left"));
				y = parseInt($("#video_chooser_slideshow ul").width());
				y = y - 489;
				y = y - (2 * y);
				if(x == y){
					$("#video_chooser_slideshow ul").css("left", "-489px");
				}
				tabPicker('right');
			});
		}
	});

	$("#video_chooser .left_arrow").click(function(e){
		e.preventDefault();
		if(!$("#video_chooser_slideshow ul").is(":animated")){
			var x = parseInt($("#video_chooser_slideshow ul").css("left"));
			x = x + 489;
			x = x + "px";
			$("#video_chooser_slideshow ul").animate({left: x}, function(){
				x = parseInt($("#video_chooser_slideshow ul").css("left"));
				y = parseInt($("#video_chooser_slideshow ul").width());
				y = y - 978;
				y = y - (2 * y);
				if(x == 0){
					$("#video_chooser_slideshow ul").css("left", y);
				}
				tabPicker('left');
			});
		}
	});

	$("#badges .right_arrow").click(function(e){
		e.preventDefault();
		if(!$("#badge_wrap ul").is(":animated")){
			var x = parseInt($("#badge_wrap ul").css("left"));
			x = x - 244;
			x = x + "px";
			$("#badge_wrap ul").animate({left: x}, function(){
				x = parseInt($("#badge_wrap ul").css("left"));
				y = parseInt($("#badge_wrap ul").width());
				y = y - 244;
				y = y - (2 * y);
				if(x == y){
					$("#badge_wrap ul").css("left", "-244px");
				}
			});
		}
	});

	$("#badges .left_arrow").click(function(e){
		e.preventDefault();
		if(!$("#badge_wrap ul").is(":animated")){
			var x = parseInt($("#badge_wrap ul").css("left"));
			x = x + 244;
			x = x + "px";
			$("#badge_wrap ul").animate({left: x}, function(){
				x = parseInt($("#badge_wrap ul").css("left"));
				y = parseInt($("#badge_wrap ul").width());
				y = y - 488;
				y = y - (2 * y);
				if(x == 0){
					$("#badge_wrap ul").css("left", y);
				}
			});
		}
	});

	$("#wallpapers .right_arrow").click(function(e){
		e.preventDefault();
		if(!$("#wallpapers_wrap ul").is(":animated")){
			var x = parseInt($("#wallpapers_wrap ul").css("left"));
			x = x - 405;
			x = x + "px";
			$("#wallpapers_wrap ul").animate({left: x}, function(){
				x = parseInt($("#wallpapers_wrap ul").css("left"));
				y = parseInt($("#wallpapers_wrap ul").width());
				y = y - 405;
				y = y - (2 * y);
				if(x == y){
					$("#wallpapers_wrap ul").css("left", "-405px");
				}
			});
		}
	});

	$("#wallpapers .left_arrow").click(function(e){
		e.preventDefault();
		if(!$("#wallpapers_wrap ul").is(":animated")){
			var x = parseInt($("#wallpapers_wrap ul").css("left"));
			x = x + 405;
			x = x + "px";
			$("#wallpapers_wrap ul").animate({left: x}, function(){
				x = parseInt($("#wallpapers_wrap ul").css("left"));
				y = parseInt($("#wallpapers_wrap ul").width());
				y = y - 810;
				y = y - (2 * y);
				if(x == 0){
					$("#wallpapers_wrap ul").css("left", y);
				}
			});
		}
	});

	$("a.popup").unbind("click").click(function(e){
		e.preventDefault;
		var dest = $(this).attr("href");
		window.open(dest);
		return false;
	});

});


// preload images
$(document).ready(function() {
	$.each([
		"/images/arrow-right-active.png",
		"/images/arrow-left-active.png",
		"/images/blog-button-hover.jpg",
		"/images/be-a-part-of-it-hover.jpg",
		"/images/watch-now-hover.jpg",
		"/images/learn-more-active.jpg",
		"/images/arrow-right.png",
		"/images/arrow-left.png"
	] , function(i, n) {
		(new Image()).src = n;
	});
});
