jQuery(function($){
	var region = $.cookie('region');
	
	if (document.referrer == 'http://web.nseries.com/nseries_redirect.jsp') {
		if (region == null) {
			var $regionSelector = $('<div id="regionSelector" />');
			var $regionList = $('<div id="regionList" />');
			$regionSelector.append('<h4>Want to see the Nseries devices available near you?</h4><h5>Please select your region from the list below.</h5>');
			$regionSelector.append($regionList);
			$regionList.html('<h6>Region</h6><ul><li><a href="#" id="europe">Europe</a></li><li><a href="#" id="latin_america">Germany</a></li><li><a href="#" id="africa">Spain</a></li><li><a href="#" id="middle_east">UK</a></li><li><a href="#" id="asia_pacific">Middle East - English</a></li><li><a href="#" id="north_america">Middle East - French</a></li><li><a href="#" id="north_america">Brazil</a></li><li><a href="#" id="north_america">US</a></li><li><a href="#" id="north_america">Singapore</a></li><li><a href="#" id="north_america">China</a></li><li><a href="#" id="north_america">India</a></li></ul>');
			$regionSelector.appendTo($('body'))
			setTimeout(function(){
				$regionSelector.center();
			}, 100);
			$.jcorners($regionSelector, { radius: 5 })
			
			$regionList.click(function(e){
				var $target = $(e.target);
				if($target.is('a')){
					$.cookie('region', $target.attr('id'), { path: '/', expires: 30 });
				}
				
				loadRegion($target.attr('id'));
			})
		}
		else {
			loadRegion($.cookie('region'))
		}
	}
	
	$('#regionSelectorMenu')
		.show()
		.find('select')
		.change(function(){
			if($(this).val() != ''){
				$.cookie('region', $(this).val(), { path: '/', expires: 30 });
				loadRegion($(this).val());
			}
		})
});

(function($){
	$.fn.center = function(){
		return this.each(function(){
			var $win = $(window);
			var $this = $(this);
			var left = ($win.width() / 2) - ($this.innerWidth() / 2);
			var top = (($win.height() / 2) - ($this.innerHeight() / 2)) + $(window).scrollTop();
			$this.css({
				top: top, left: left
			});
		});
	}
})(jQuery);
