	$(document).ready(function(){
	placeLinks();
//	initSportsSelector();
});

var fadeDelay=400;
function initSportsSelector( preLink ){
	
	$(".splico").fadeTo(1, '0.3');
	
	$("#sportSelector").fadeIn();
	$("#sport_buttons_b2c").fadeIn();
	
	$("#sportSelectorGoBtn").parent().fadeTo(1, '0.3');
	
	preSelectCurrent(preLink);
	
	// only init when the sportSelector is there, and only on IE (other browsers support CCS3)
	if( $("#sportSelector").length!=0 && $.browser.msie){
		fadeDelay=0;
		$.getScript('/extension/amb/design/site/javascript/curvycorners.js', function(){
			initCorners("#sportSelectorWindow", 10);
		});
		
		// Attache event handles on the images in the label... (for IE)
		$("#b2c").parent().find("img").click(function(){
			$("#sport_buttons_b2b").hide();
			$("#sport_buttons_b2c").show();
			// Switch sport
			switchSport("b2c");
			switchRadio("b2c");
		});
		$("#b2b").parent().find("img").click(function(){
			$("#sport_buttons_b2b").show();
			$("#sport_buttons_b2c").hide();
			switchSport("b2b");
			switchRadio("b2b");
		
		});
		
	}
	
	$(".splico").mouseenter(function() {
		$(".splico_txt").each(function() {
			if( !$(this).hasClass('active') ){
				$(this).attr("src", $(this).attr("src").replace("/hover/", "/"));
			}
		});
		if( !$(this).parent().hasClass('active') ){
			$(this).parent().find('.splico_txt').attr('src', $(this).parent().find('.splico_txt').attr("src").replace("/textimage/", "/textimage/hover/"));
			$(this).stop(true, true).fadeTo(fadeDelay, '1');
		}
	});
	$(".splico").mouseleave(function() {
		// When the icon is active, do not fade out!
		if( !$(this).parent().hasClass('active') ){
			$(this).parent().find('.splico_txt').attr("src", $(this).parent().find('.splico_txt').attr("src").replace("/hover/", "/"));
			$(this).stop(true, true).fadeTo(fadeDelay, '0.3');
		}
	});
	
	// Show b2b buttons when selecting b2c
	$("#b2c").click(function(){
		switchIcons( "b2c" );
		
		// Switch sport
		switchSport( "b2c" );
		
		// Change text color of the radio buttons
		switchRadio( "b2c" );
		
	});
	
	// Show b2b buttons when selecting b2b
	$("#b2b").click(function(){
		switchIcons( "b2b" );
		
		// Switch sport
		switchSport( "b2b" );
		
		// Change text color of the radio buttons
		switchRadio( "b2b" );
		
	});
	
	// Close button
	$(".close").click(function(){
		$("#sportSelector").fadeTo(fadeDelay, '0.01', function(){
			// When the animation is complete, hide the selector
			$("#sportSelector").hide();
		});
		$("#pageBackground").fadeTo(fadeDelay, '0.01', function(){
			$("#pageBackground").hide();
		});
		
	});
	
	// Make a button from the sport icons
	$(".icons a").each(function() {
		$(this).click(function(){
			var deactivate=false;
			
			if( $(this).hasClass('active') )
				deactivate=true;
			
			// Remove the active state from the other buttons
			$(".splico").each(function(){
				if( $(this).parent().hasClass('active') ){
					$(this).parent().removeClass('active');
					$(this).parent().children(".splico_txt").removeClass('active');
					// fade out
					$(this).stop(true, true).fadeTo(fadeDelay, '0.3');
					
					// Change text color
					$(this).parent().children('.splico_txt').attr("src", $(this).parent().children('.splico_txt').attr("src").replace("/hover/", "/"));
					$(this).stop(true, true).fadeTo(fadeDelay, '0.3');
			
				}
			});
			
			if( deactivate ){
				fillLink();
				return false;
			}
			
			// Set the button to state active
			$(this).children(".splico").fadeTo(fadeDelay, '1');
			$(this).addClass('active');
			$(this).children(".splico_txt").addClass('active');	//.attr("src", $(this).children(".splico_txt").attr("src").replace("/textimage/", "/textimage/hover/"));
			
			// Place the link in the button
			var link = $(this).attr("href");
		//	$("#sportSelectorGoBtn").attr("href",link);
		//	fillLink(link)
		
			showTimer(this);
			
			return true;
		 });
	});
	
}

/*
*	Function to switch B2X, and but keep the same sport active
*/
function switchSport( switchto ){
	
	// Clear the link first
	fillLink();
	
	// set the correct from and to sports
	var from= 'sport_buttons_'+(switchto=='b2b' ? 'b2c':'b2b');
	var to  = 'sport_buttons_'+switchto;
	
	// first find the current active sport
	$("#"+from+" .splico_txt").each(function(){
		if( $(this).hasClass('active') ){
			
			var activeSport = $(this).attr("alt");
			$("#"+to+" .splico_txt").each(function(){
				
				if( $(this).attr("alt") == activeSport ){
					
					// Change icon style
					$(this).parent().addClass('active').children(".splico").fadeTo(1, '1');
					$(this).addClass('active').fadeTo(1, '1');
					
					// make text hover style
					$(this).attr('src', ($(this).attr("src").replace("/textimage/", "/textimage/hover/")) );
					
					// set the link
					fillLink( $(this).parent().attr("href") );
				}
				
			});
			
			// Set previous to orginal style
			$(this).removeClass('active');
			$(this).attr('src', ($(this).attr("src").replace("/hover/", "/")) ).removeClass('active');
			
			// return and and the funtion
			return;
		}
	});
	
}

/*
*	Function to switch the radio buttons, and change the text styles
*/
function switchRadio( newActive ){
	
	// set the old variable
	if( "b2b"==newActive ) var oldActive = "b2c";
	else
	if( "b2c"==newActive ) var oldActive = "b2b";
	else
		return;
		
	// Check radio button
	$("#"+newActive).attr('checked', true);
	
	// Change text color of the radio buttons back
	if( $("#"+oldActive).parent().hasClass('active')){
		$("#"+oldActive).parent().removeClass('active');
		$("#"+oldActive).parent().find("img.sportsselectorSmall").attr("src", $("#"+oldActive).parent().find("img.sportsselectorSmall").attr("src").replace("/hover/", "/"));
	//	$("#"+oldActive).parent().find("img.sportsselectorLarge").attr("src", $("#"+oldActive).parent().find("img.sportsselectorLarge").attr("src").replace("/hover/", "/"));
	}
	
	// Change text color of the radio buttons back
	if( !$("#"+newActive).parent().hasClass('active')){
		$("#"+newActive).parent().addClass('active');
		$("#"+newActive).parent().find("img.sportsselectorSmall").attr("src", $("#"+newActive).parent().find("img.sportsselectorSmall").attr("src").replace("/textimage/", "/textimage/hover/"));
	//	$("#"+newActive).parent().find("img.sportsselectorLarge").attr("src", $("#"+newActive).parent().find("img.sportsselectorLarge").attr("src").replace("/textimage/", "/textimage/hover/"));
	}
	return;
}

function switchIcons( newActive ){

	// set the old variable
	if( "b2b"==newActive ) var oldActive = "b2c";
	else
	if( "b2c"==newActive ) var oldActive = "b2b";
	else
		return;
		
	$("#sport_buttons_"+newActive).show();
	$("#sport_buttons_"+oldActive).hide();
	
}

// Give the button the correct link and style
function fillLink(link){
	return	/* functon is disabled, since there isn't a 'go' button anymore */
	// When a link is given
	if( ""!=link && undefined!=link ){
		// Place the link in the button
		$("#sportSelectorGoBtn").attr("href",link);
		$("#sportSelectorGoBtn").parent().fadeTo(fadeDelay, '1');
	}
	// Else remove the link, and lower the opacity
	else{
		$("#sportSelectorGoBtn").attr("href","javascript:void(0);");
		$("#sportSelectorGoBtn").parent().fadeTo(fadeDelay, '0.3');
	}
	return;
}

// Give every sublink
function placeLinks(){
	
	// Place link to sportsSelector (no pre-settings)
	if( $("#openSportSelector").length!=0 ){
		var link = $("#openSportSelector").attr("href");
		$("#openSportSelector").attr("href", "javascript:void(0)");
		$("#openSportSelector").click(function(link){
			showSportSelector(link);
		});
	}
	
	// Place link to sportsSelector, b2b preselected
	if( $("a.b2b_sportsSelector").length!=0 ){
		$("a.b2b_sportsSelector").each(function(){
			$(this).attr("href","javascript:void(0);");
			$(this).click(function(){
				showSportSelector("B2B");
			});
		});
	}
	
	// Place link to sportsSelector, b2c preselected
	if( $("a.b2c_sportsSelector").length!=0 ){
		$("a.b2c_sportsSelector").each(function(){
			$(this).attr("href","javascript:void(0);");
			$(this).click(function(){
				showSportSelector("B2C");
			});
		});
	}
	
}

/**
*	Show the sportsSelector, when its not avalible jet, load it from the server
*/
function showSportSelector( link ){

	// On the search domain, use the 
	if( typeof(mylapsDomain) !== "undefined" ){
		var url = mylapsDomain+"/detect/Websites/sportselector";
	}
	else{
		var url = "/detect/Websites/sportselector";
	}
	
	// When the object doesn't exist, create it and show it
	if( $('#pageBackground').length==0 ){
		$('body').append('<div id="pageBackground"><img src="/extension/amb/design/site/images/ajax-loader.gif" alt="Please wait" /></div>');
		$('#pageBackground').css({ opacity: 0.6,'width':$(document).width(),'height':$(document).height()});
		
		// Change backgound size when the windows size changes
		$(window).bind("resize", function(){
			// First make the backgound small, else we can't detect when the windows size becomes smaller (the backgound will still be big)
			$('#pageBackground').css({ 'width':1,'height':1});
			// Change background size to windows size
			$('#pageBackground').css({ 'width':$(document).width(),'height':$(document).height()});
		 });
	}
	// When the object exists, show it
	else{
		$("#pageBackground").show();
		$("#pageBackground").fadeTo(fadeDelay, '0.6');
	}

	// When the object doesn't exist, get it and show it
	if( $('#sportSelector').length==0 ){
		$.get( url, "", function(data){
				$('body').append(data);
				initSportsSelector(link);
			});
	}
	// When the object exists, show it
	else{
		if( ""!= link && link!=undefined ){
			switchSport( link.toLowerCase() );
			switchRadio( link.toLowerCase() );
			switchIcons( link.toLowerCase() );
		}
		$("#sportSelector").show();
		$("#sportSelector").fadeTo(fadeDelay, '1');
	}
}

/**
* Preselect your current sport / URL
*/
function preSelectCurrent( preLink ){
	
	// Detect b2x and sport, using the URL
	if( jQuery.url.segment(2)=="Websites" ){
		var b2x=jQuery.url.segment(3);
		var sport=jQuery.url.segment(4);
	}else
	if( jQuery.url.segment(1)=="Websites" ){
		var b2x=jQuery.url.segment(2);
		var sport=jQuery.url.segment(3);
	}
	else{
		var b2x='';
		var sport='';
		
	}
	
	// When using preLinks
	if( ""!=preLink ){
		
		var b2_old='';
		var b2_new='';
		
		if( "B2B"==preLink ){
			var b2_old='b2c';
			var b2_new='b2b';
			b2x=preLink;
		}else
		if( "B2C"==preLink ){
			var b2_old='b2b';
			var b2_new='b2c';
			b2x=preLink;
		}
		
		// Do we need to change b2x?
		if( ''!=b2_new && ''!=b2_old ){
			// Show & hide the icons
			$("#sport_buttons_"+b2_old).hide();
			$("#sport_buttons_"+b2_new).show();
			
			switchRadio( b2_new );
			
			if( ""==sport )
				return;
		}
		
	}
	
/* Cookie can not be read, since its on the .mylaps.com domain, not www.mylaps.com...
	// when the sport is "home" or empty, try using a cookie
	if( (sport=="home" || sport=="")  && $.cookie('amb_selectionSport') ){
	
		sport=$.cookie('amb_selectionSport');
		// Use the sport ID to decide if its B2C or B2B (not the best way)
		b2x = (sport <=231) ? 'B2B' :'B2C';
	}
//*/
	
	if( b2x=='B2B' ){
		// Use the text label to match the sport
		$("#sport_buttons_b2b .splico_txt").each(function() {
			
			// the alt text of the image should be lowercase
			if( $(this).attr("alt").toLowerCase()==sport ){
				// Set sport icon active
				$(this).parent().addClass('active').children("img").fadeTo(1, '1');
				
				// Change text color, and change the image style (to hover)
				$(this).parent().children('.splico_txt').addClass('active').attr('src', $(this).parent().children('.splico_txt').attr("src").replace("/textimage/", "/textimage/hover/"));
				
				// Check the correct radio button
				switchRadio( "b2b" );
				$("#sport_buttons_b2b").show();
				$("#sport_buttons_b2c").hide();
				
				// Place the correct link in the go button
				var link=$(this).parent().attr("href");
				fillLink( link );
				return;
			}
		});
	}
	else if( b2x=='B2C' ){
		$("#sport_buttons_b2c .splico_txt").each(function() {
			
			if( $(this).attr("alt").toLowerCase()==sport ){
				// Set sport icon active
				$(this).parent().addClass('active').children("img").fadeTo(1, '1');
				
				// Change text color, and change the image style (to hover)
				$(this).parent().children('.splico_txt').addClass('active').attr('src', $(this).parent().children('.splico_txt').attr("src").replace("/textimage/", "/textimage/hover/"));
				
				// Check the correct radio button
				switchRadio( "b2c" );
				$("#sport_buttons_b2c").show();
				$("#sport_buttons_b2b").hide();
				
				// Place the correct link in the go button
				var link=$(this).parent().attr("href");
				fillLink( link );
				return;
			}
		
		});
	}
	
}

// After the visitor has clicked on the link, hide the selector (not the waitor)
function showTimer(object){

	// Only show the timer when we open a real link
	if( "javascript:void(0);" != object.href ){
		$("#sportSelector").hide();
		
		// IE doens't seem to open te href...
		if( $.browser.msie ){
			window.open( object.href, '_self' );
		}
		return true;
	}
}

/*
*	init the round corners for the sportSelector
*/
function initCorners(object, radius) {
	if( radius=="" ) radius=15;
    var settings = {
      tl: { radius: radius },
      tr: { radius: radius },
      bl: { radius: radius },
      br: { radius: radius },
      antiAlias: true
    }
	curvyCorners(settings, object);
}

