//all these functions load when the dom is ready

$(document).ready(function(){
	
	//test cookie
	//$.cookie( 'APC-selectcountry', '1',  {  path: '/' } );
	
	//if we do have either type of cookie then check to see if we have usonline redirect, if not then hand off to apc.fr to figure out
	if( $.cookie('APC-redirection') !== null || $.cookie('APC-selectcountry') !== null) { 
		if ( $.cookie('APC-redirection') != 'sCountryRedirection=jp\http://usonline.apc.fr/' )	window.location.replace("http://apc.fr");
	}
	//if we're coming in from the main www.apc.fr site drop the cookie to prevent the country selector... this is a hack until french team switches cooke to root domain
	var ref = document.referrer.toLowerCase();
	if (ref.match(/^https?:\/\/([^\/]+\.)?www\.apc\.fr(\/|$)/i)) {
	  $.cookie( 'APC-redirection', 'sCountryRedirection=jp\http://usonline.apc.fr/',  {  path: '/', domain: 'usonline.apc.fr' } );
	}
});


//all these functions won't load till all elements and images are loaded
$(window).load(function(){

	
	
	//superbox store selector
	$(function(){
		$.superbox();
		jQuery.fn.openCountryBox = function() { 
				$.superbox.open('<div id="box-lang"><h1>CHOOSE YOUR DELIVERY COUNTRY:</h1><ul><li><a href="http://www.apc.fr/frfr/index.html?first">FRANCE</a></li><li><a id="selectUS" href="http://usonline.apc.fr/">NORTH AMERICA</a></li><li><a href="http://www.apc.fr?country=jp&amp;url=http://www.apcjp.com/jpn/index.php">JAPAN</a></li><li><a href="http://www.apc.fr/wwfr/index.html?first">Other<br>Countries (FR)</a></li><li><a href="http://www.apc.fr/wwuk/index.html?first">Other<br>countries (en)</a></li></ul></div>', { 
					boxWidth : '390',
					boxHeight: "auto", 
					boxClasses : 'superbox-simple', 
					afterOpen: function(){
						$('#selectUS').bind('click', function(e){
								e.preventDefault();
						    $.cookie( 'APC-redirection', 'sCountryRedirection=jp\http://usonline.apc.fr/',  {  path: '/', domain: 'apc.fr', expires: 365  } );
						    $.superbox.close();
						});	
					} 
				});
			};
			$('a#countryBox').click(function(e) {
				e.preventDefault()
			  $().openCountryBox();
			});

			//if we don't have either type of cookie then let user select - all selections except us redirect through and rely on apc.fr
		if( $.cookie('APC-redirection') != 'sCountryRedirection=jp\http://usonline.apc.fr/' && $.cookie('APC-selectcountry') === null) { 
			$().openCountryBox();		
			$('#superbox-overlay').add('#superbox-wrapper').unbind('click');
			
		} /*
else {
		
		// else lets do superbox sale
		$(function(){
			$.superbox();
			if( $.cookie('wintersale') == null ) { 
				$.superbox.open('<a href="/sale">SALE<br/>-30% to -50%</a>', { 
					boxWidth : '500',
					boxHeight: "150",
					boxClasses : 'superbox-sale',
					afterOpen: function(){
						$('#superbox a').bind('click', function(){
							    $.cookie( 'wintersale', '1',  {  path: '/' } );
							    
						});
				  } 
				});
				$('#superbox-overlay').add('#superbox-wrapper').unbind('click');

				setTimeout(function() {
					$('#superbox .close a').trigger('click');
				}, 8000);
			}
		});
		
		
		} 
*/
	});	


});

