//country switcher

//click handlers for flags:
 $(document).ready(function() {
	setDefaultLanguage();

	$("#country-switcher").change(function(){
  		countryCodeSwitcher($("#country-switcher").val())
	}); 
	
	//portal page click events
	$('#cl-us').click(function() {
	  countryCodeSwitcher("US")
	});
	$('#cl-fr').click(function() {
	  countryCodeSwitcher("FR")
	});
	$('#cl-de').click(function() {
	  countryCodeSwitcher("DE")
	});
	$('#cl-it').click(function() {
	  countryCodeSwitcher("IT")
	});
	$('#cl-es').click(function() {
	  countryCodeSwitcher("ES")
	});
	$('#cl-pt').click(function() {
	  countryCodeSwitcher("PT")
	});
	
});

function setDefaultLanguage(){
	var pathname = window.location.href;
	if(pathname.indexOf( "us.bollorethinpapers.com" ) > 0){
		$("#country-switcher").val("US")
	}

	if(pathname.indexOf( "fr.bollorethinpapers.com" ) > 0){
		$("#country-switcher").val("FR")
	}

	if(pathname.indexOf( "de.bollorethinpapers.com" ) > 0){
		$("#country-switcher").val("DE")
	}

	if(pathname.indexOf( "it.bollorethinpapers.com" ) > 0){
		$("#country-switcher").val("IT")
	}

	if(pathname.indexOf( "es.bollorethinpapers.com" ) > 0){
		$("#country-switcher").val("ES")
	}

	if(pathname.indexOf( "pt.bollorethinpapers.com" ) > 0){
		$("#country-switcher").val("PT")
	}

}

function cookieSwitcher(){
	var country = $.cookie("country")
	var pathname = window.location.href;
	if (country!=null){
		switch (country) {
			
			// USA
			case 'US':
				if(pathname.indexOf( "us.bollorethinpapers.com" ) == -1){
					document.location = 'http://us.bollorethinpapers.com';
				}
			break;
			
			// France
			case 'FR':
				if(pathname.indexOf( "fr.bollorethinpapers.com" ) == -1){
					document.location = 'http://fr.bollorethinpapers.com';
				}
			break;
	
			// German
			case 'DE':
				if(pathname.indexOf( "de.bollorethinpapers.com" ) == -1){
					document.location = 'http://de.bollorethinpapers.com';
				}
			break;
			
			// Italy
			case 'IT':
				if(pathname.indexOf( "it.bollorethinpapers.com" ) == -1){
					document.location = 'http://it.bollorethinpapers.com';
				}
			break;
			
			// Spain
			case 'ES':
				if(pathname.indexOf( "es.bollorethinpapers.com" ) == -1){
					document.location = 'http://es.bollorethinpapers.com';
				}
			break;
			
			// Portugal
			case 'PT':
				if(pathname.indexOf( "pt.bollorethinpapers.com" ) == -1){
					document.location = 'http://pt.bollorethinpapers.com';
				}
			break;		
			
			
			// Default
			default:
			// document.location = 'http://bolloreus.businesscatalyst.com';
			break;
	
		}
	}
}

function countryCodeSwitcher(country){
	var options = { path: '/', expires: 1000, domain: 'bollorethinpapers.com' };
	$.cookie("country", country, options);
	var pathname = window.location.href;

	switch (country) {
		
		// USA
		case 'US':
			if(pathname.indexOf( "us.bollorethinpapers.com" ) == -1){
				document.location = 'http://us.bollorethinpapers.com';
			}
		break;
		
		// France
		case 'FR':
			if(pathname.indexOf( "http://fr.bollorethinpapers.com" ) == -1){
				document.location = 'http://fr.bollorethinpapers.com';
			}
		break;

		// German
		case 'DE':
			if(pathname.indexOf( "de.bollorethinpapers.com" ) == -1){
				document.location = 'http://de.bollorethinpapers.com';
			}
		break;
		
		// Italy
		case 'IT':
			if(pathname.indexOf( "it.bollorethinpapers.com" ) == -1){
				document.location = 'http://it.bollorethinpapers.com';
			}
		break;
		
		// Spain
		case 'ES':
			if(pathname.indexOf( "es.bollorethinpapers.com" ) == -1){
				document.location = 'http://es.bollorethinpapers.com';
			}
		break;
		
		// Portugal
		case 'PT':
			if(pathname.indexOf( "pt.bollorethinpapers.com" ) == -1){
				document.location = 'http://pt.bollorethinpapers.com';
			}
		break;		
		
		
		// Default
		default:
		// document.location = 'http://bolloreus.businesscatalyst.com';
		break;

		}
}


