if ( !window.FL ) FL = {};

// Global configurations name space
FL.config = FL.config ? FL.config : {};

FL.initFilter = function() {

	FL.primaryNavElement = $('#product_filter');
	if (FL.primaryNavElement.length > 0) {
		FL.fieldFormatter.targetElement = FL.primaryNavElement;
		FL.fieldFormatter.init();
		
		FL.navExpander.targetElement = FL.primaryNavElement;
		FL.navExpander.init();
		
		FL.filterAjax.targetElement = $('form#product_filter');
		FL.filterAjax.init();
	}
	
}

// Add 'js' class to allow js aware css
$("html").addClass("js");

$(document).ready(function(){
	

	
// Promo and update ajax.
	function set_ajax_updates()
	{
		$('#more_content a').click(function(event) {
			  var href=$(event.target).attr('href');
			  $(event.target).replaceWith($('<a class="loading">Loading</a>'));
	      href = href.replace('/home/', '/updates/');
	      $.get(href, function(data) {
	    	  $('#more_content').parent().fadeOut(function(){
	    		  var div = $('<div>');
	    		  $('#more_content').parent().replaceWith(div);
	    		  div.hide();
	    		  div.append(data);
	    		  div.fadeIn();
			      set_ajax_updates();
	    	  });
	      });
	      return false;
	  });
	}

	set_ajax_updates();

	function set_ajax_promos()
	{
	  $('#more_promo_content a').click(function(event) {
	      var href=$(event.target).attr('href');
	      $(event.target).replaceWith($('<a class="loading">Loading</a>'));
	      href = href.replace('/home/', '/promos/');
	      $.get(href, function(data) {
	    	  $('#more_promo_content a').parent().parent().fadeOut(function() {
	    		  var div = $('<div>');
	    		  $('#more_promo_content').replaceWith(div);
	    		  div.hide();
	    		  div.append(data);
	    		  div.fadeIn();
		    	  set_ajax_promos();
	    	  });
	    	  
	      });
	      return false;
	  });
	}

	set_ajax_promos();


	
	// History
   $.address.change(function(event) { 
    	if (event.value == '/' && ("" + window.location).indexOf('/products') != -1) {
    		FL.filterAjax.ajaxLoadBack('/products/index/back', true);
    		return false;
    	}
    	if (event.value.indexOf("/back/") != -1) {
    		var value = ""+event.value;
    		if ($('form#product_filter').length)
    		{
    			value = value.replace('/back/', '/ajax/');
    			FL.filterAjax.ajaxLoadDetail(value);
    		} else {
    			FL.filterAjax.ajaxLoadBack(value);
    		}
    		return false;
    	}
    	if (event.value.indexOf("/detail/") != -1) {
    		FL.filterAjax.ajaxLoadFull(event.value);
    		return false;
    	}
    	return false;
        // do something depending on the event.value property, e.g.  
        // $('#content').load(event.value + '.xml');  
    });   
	
	$('#primary_navigation>li').addClass('during_load').delay(1000).queue(function(next){
    		$(this).removeClass('during_load');
  		next();
	});

	FL.splashScreen.init();

/* < product filter */
	FL.initFilter();
	
	// Set up for privacy policy acceptance form
	jQuery("#terms-acceptance input[type='radio']").bind("click",function(){
		
		jQuery("#terms-acceptance-form input[type='submit']").click();
		
	});
	
/* product filter /> */
	
	$('#selector_content').addClass('enabled').hide();

/* < stores page */

	var returnStore = function() {
	  	$('#store_selector>li>a').click(function(e) {
	  		  		  		  		  		  	
	  	$('#destroy').remove();
	  	
		e.preventDefault();
		
	  	var previous_class = $('#selector_content h3').attr('class');
	  	
		$('#store_selector').hide();
		
		
		var store_code = $(this).parent('li').attr('class').split("_");
		if (store_code[0] == 'store')
		{
		
			$('#selector_content').load('/stores/store/'+store_code[1], function(){ returnBack(); }).hide().fadeIn();
		} else {
		
			$('#selector_content').load('/stores/city/'+store_code[1], function(){ returnStore(); returnReset(); }).hide().fadeIn();
		}
		$('<p id="destroy" class="'+previous_class+'"></p>').appendTo('#page_content');
		
	   	});
   	
   	};
   	
	var returnBack = function() { 
		$('p.return a').click( function(e) { 
				
		e.preventDefault(); 
		
		var previous_class = $('#destroy').attr('class'); 
		
		$('#selector_content').load('/stores/country/'+previous_class, function(){ returnStore(); returnReset(); }); 
		
		}); 
		
	};
	
	var returnReset = function() { 
		$('p.reset a').click( function(e) { 
		
		var current_continent = $('#page_content').attr('class');
						
		if ( current_continent == 'me' ) { $(this).attr('href','/stores/index/me'); } else { $(this).attr('href','/stores/index/eu'); }
		
		}); 
		
	};
	
	var countChildren = function () { 
	
		i = 0;
		j = 0;
	
		$('#store_selector>li').each( function() { 
		
			i++;
			var height = $(this).outerHeight();
			j = j + height;
			
			
		});
		
		var average_height = j/i;
		
		$('#store_selector>li').each( function() {
		
			var height = $(this).outerHeight();
		
			if ( height > average_height ) { $(this).children('a').addClass('reduced_size'); };		
			
		});
		
	};
		
	$('#country_selector>li').each( function(){ 
		/*var randomnumber=Math.floor(Math.random()*11); 
		$(this).addClass('footlocker_'+randomnumber);
		*/
		var width = $(this).children('a').width();
		
		var a_width = width + 60;
		
		$(this).css('background-position', a_width);
			
	});
	
	$('#country_selector>li').mouseover( function() { $(this).addClass('current'); $(this).siblings('li').removeClass('current'); });
	$('#country_selector>li').mouseout( function() { $(this).removeClass('current'); });

	$('#selector_content.enabled').children().remove();

	
	
	$(document).ajaxStart(function() {
		
		$('#selector_content').addClass('hidden');

		if ( $('#product_content').length != 0 ) {

			$('<div id="loading_content"><p>Loading content</p></div>').appendTo('body');
			$('#loading_content').show();
			var loading_height = $('#page_content').outerHeight();
			var loading_width = $('#page_content').outerWidth();
			var loading_offset = $('#page_content').offset();
			
			$('#loading_content').height(loading_height);
			$('#loading_content').width(loading_width);
			$('#loading_content').offset(loading_offset);

			
				setTimeout( function(){ 
				
					$('#loading_content').fadeOut();
				
				}, 500);
		
		};
		
		if ( $('#product_results').length != 0 ) {

			$('<div id="loading_content"><p>Loading content</p></div>').appendTo('body');
			$('#loading_content').show();
			var loading_height = $('#product_results').outerHeight();
			var loading_width = $('#product_results').outerWidth();
			var loading_offset = $('#product_results').offset();
			
			$('#loading_content').height(loading_height);
			$('#loading_content').width(loading_width);
			$('#loading_content').offset(loading_offset);

			
				setTimeout( function(){ 
				
					$('#loading_content').fadeOut();
				
				}, 500);
		
		};

	}).ajaxStop(function() { 
	
		image_detector();
		$('#selector_content').removeClass('hidden');		

		



	
	});
	
	

	
	$('#country_selector>li>a').click( function(e) { 
		e.preventDefault(); 
				
		var country_code = $(this).attr('class');
		
		$('#selector_content.enabled').load('/stores/country/'+country_code, function(){ returnStore(); returnReset(); countChildren(); }).hide(); 
		var content = $('#selector_content.enabled');
		
		$(content).fadeIn('slow');
		
	
	});



	$('#store_selector>li>a').click( function() { returnStore(); });

/* stores page /> */	


/* < language selector top right */

	$('<div id="language_selector"></div>').appendTo('#navigation_language');
	$('#language_selector').load($('a#language_link').attr("href")).hide();

	$('#navigation_language').click().toggle(
			function(e){
						e.preventDefault();
						$('#language_selector').fadeIn();
						$('#languages li').children().unbind().click( function(e) {window.location = e.target.href});
					   }, 
			function(e){ 
						e.preventDefault(); $('#language_selector').fadeOut();
					   }
	);
	
/* language selector top right /> */

/* < moving background (for primary navigation) plugin */

	(function($) {
		if(!document.defaultView || !document.defaultView.getComputedStyle){ // IE6-IE8
			var oldCurCSS = jQuery.curCSS;
			jQuery.curCSS = function(elem, name, force){
				if(name === 'background-position'){
					name = 'backgroundPosition';
				}
				if(name !== 'backgroundPosition' || !elem.Style || elem.Style[ name ]){
					return oldCurCSS.apply(this, arguments);
				}
				var style = elem.style;
				if ( !force && style && style[ name ] ){
					return style[ name ];
				}
				return oldCurCSS(elem, 'backgroundPositionX', force) +' '+ oldCurCSS(elem, 'backgroundPositionY', force);
			};
		}
		
		var oldAnim = $.fn.animate;
		$.fn.animate = function(prop){
			if('background-position' in prop){
				prop.backgroundPosition = prop['background-position'];
				delete prop['background-position'];
			}
			if('backgroundPosition' in prop){
				prop.backgroundPosition = '('+ prop.backgroundPosition;
			}
			return oldAnim.apply(this, arguments);
		};
		
		function toArray(strg){
			strg = strg.replace(/left|top/g,'0px');
			strg = strg.replace(/right|bottom/g,'100%');
			strg = strg.replace(/([0-9\.]+)(\s|\)|$)/g,"$1px$2");
			var res = strg.match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);
			return [parseFloat(res[1],10),res[2],parseFloat(res[3],10),res[4]];
		}
		
		$.fx.step. backgroundPosition = function(fx) {
			if (!fx.bgPosReady) {
				var start = $.curCSS(fx.elem,'backgroundPosition');
				
				if(!start){//FF2 no inline-style fallback
					start = '0px 0px';
				}
				
				start = toArray(start);
				
				fx.start = [start[0],start[2]];
				
				var end = toArray(fx.options.curAnim.backgroundPosition);
				fx.end = [end[0],end[2]];
				
				fx.unit = [end[1],end[3]];
				fx.bgPosReady = true;
			}
			//return;
			var nowPosX = [];
			nowPosX[0] = ((fx.end[0] - fx.start[0]) * fx.pos) + fx.start[0] + fx.unit[0];
			nowPosX[1] = ((fx.end[1] - fx.start[1]) * fx.pos) + fx.start[1] + fx.unit[1];           
			fx.elem.style.backgroundPosition = nowPosX[0]+' '+nowPosX[1];
	
		};
	})(jQuery);


	$.fn.pause = function(duration) {
	    $(this).animate({ dummy: 1 }, duration);
	    return this;
	};
	
	$.fn.fadeToggle = function(speed, easing, callback) {
  	return this.animate({opacity: 'toggle'}, speed, easing, callback);
  	};

	/*$('#navigation_home').css( {backgroundPosition: "-50px 0px"} ).mouseover(function(){
		$(this).stop().animate({backgroundPosition:"(-75px -800px)"}, {duration:1000})}).mouseout(function(){
		$(this).stop().animate({backgroundPosition:"(-50px 0px)"}, {duration:500, complete:function(){	
		$(this).css({backgroundPosition: "-50px 0px"})
			
		}})
	});*/
	
/* moving background plugin /> */
	
/* < moving shoelace graphic if res higher than X */
	
	var viewport_width = $(window).width();

	if( viewport_width < 1200 ) { $('#accessibility #skip_to_content').addClass('standard'); $('#primary_navigation').addClass('standard'); }
	
/* moving shoelace graphic if res higher than X /> */

/* < primary nav background movement */
	
	$('#primary_navigation li').css( {backgroundPosition: "-50px 0px"} ).mouseover(function(){
		$(this).stop().animate({backgroundPosition:"(-50px -800px)"}, {duration:500})}).mouseout(function(){
		$(this).stop().animate({backgroundPosition:"(-50px 0px)"}, {duration:250, complete:function(){	
		$(this).css({backgroundPosition: "-50px 0px"})
			
		}})
	});
	
/* primary nav background movement /> */



/* < homepage carousel */


	$('#page_content_carousel').wrap('<div id="carousel_container" />');

	var carousel_items = 0;		
	
	$('#page_content_carousel>li').each( function() { carousel_items++; $(this).attr('id', 'carousel_'+carousel_items); });
	
	$('#carousel_container iframe').each( function(){
		
		var carousel_class = $(this).parents('li').attr('id');
	
		$(this).clone().appendTo('#carousel_container').addClass(carousel_class);
		$(this).remove();
	
	});



	if (carousel_items > 1) {

		$('<ul id="carousel_controls"><li id="previous_item" class="slide_selector">View previous slide</li><li id="pause_item">Pause slide</li><li id="next_item" class="slide_selector">View next slide</li></ul>').appendTo('#carousel_container').hide();
		$('#carousel_controls').show("slide", { direction: "right"},500);
	};	
	
	$('#page_content_carousel>li').hide();
	$('#page_content_carousel>li:eq(0)').delay(100).fadeIn(500);

	carousel_delay = 5000;
	carousel_number = 0;
	
	var carousels = $('#page_content_carousel>li');
			
	i = 0;
	

	
   	var show_like = function() {
   	
   	
   		var this_class = $(carousels[i]).attr('id');
   		$('#carousel_container iframe').each( function(){ $(this).hide(); });
   		$('#carousel_container iframe.'+this_class).stop(true, true).delay(500).fadeIn();

   	
   	};
   	
   	show_like();
	
	var next_carousel = 
			
			function() { 
		
			if ( i < carousels.length - 1 ) { 
			
								
				$(carousels[i]).stop(true, true).fadeOut(500);
				$(carousels[i]).next().stop(true, true).show("slide", { direction: "right"},750);
	

				
				i++; 
	
			
			} else { $(carousels[i]).stop(true, true).fadeOut(); $(carousels[0]).stop(true, true).show("slide", { direction: "right"},750); i=0; };
			
			show_like();
			
			};
		
	var previous_carousel = 
	
			function() { 
		
			if ( i > 0 ) { 
						
			
								
				$(carousels[i]).stop(true, true).fadeOut();
				$(carousels[i]).prev().stop(true, true).show("slide", { direction: "left"},750);
	
				
				i--; 
	
			
			} else { $(carousels[0]).stop(true, true).fadeOut(); $(carousels[carousels.length - 1]).stop(true, true).show("slide", { direction: "left"},750); i=carousels.length -1; };
		
			show_like();
			
			};
		
	var timer = true;
	
	$('#next_item').click( function(){ next_carousel(); clearInterval(carousel_interval); });
	$('#previous_item').click( function(){ previous_carousel(); clearInterval(carousel_interval); });
	$('#pause_item').click( function(){ clearInterval(carousel_interval); });
	
	if (carousel_items > 1) {
	
		carousel_interval = setInterval( next_carousel, 5000 );
	
	};
	
	
/* homepage carousel /> */

/* < rounded edge PNG adder code */
	
	$('#secondary_navigation li a').addClass('window_outside');
	$('#result_controls').addClass('window');

	$('.window_outside').each( function(){ $(this).append('<span class="window_1"></span><span class="window_2"></span><span class="window_3"></span><span class="window_4"></span>'); });
	$('.window').each( function(){ $(this).append('<span class="window_1"></span><span class="window_2"></span><span class="window_3"></span><span class="window_4"></span>'); });
	
/* rounded edge PNG adder code /> */


/* < news sharer url */


	$('#news_content>li').each( function(){ 
		
		var domain_name = document.domain;
		
		var view_more_url = $(this).children('p').children('a.view_more').attr('href');
		
		var total_url = "http://" + domain_name + view_more_url;
			
		$(this).children('p').children('a.share').attr('href', 'http://www.addthis.com/bookmark.php?url='+total_url);
		
	});

/* name sharer url /> */
	if (FL.filterAjax)
	{
		FL.filterAjax.onloadDetail();
	}
});

/* image width detector */

var image_detector = function(){

	$('#like_content').each( function(){
	
		$(this).find('img').each( function(){
		
			var this_width = $(this).outerWidth();
			var this_parent_width = $(this).parent().parent('li').outerWidth();
			
			if ( this_width > this_parent_width ) {
			
				var this_width = this_width / 4;
				$(this).css('marginLeft', '-'+this_width+'px');
			
			};
		
		});
	
	});
	
};

FL.splashScreen = FL.splashScreen ? FL.splashScreen : {
	width: 0,
	height: 0,
	viewport_width: 0,
	viewport_difference:0,
	init: function(){

		var splashed = readCookie('footlocker_splashed') ? readCookie('footlocker_splashed') : 0 ;

		splashEnabledPages = 0;
		
		if(location.pathname == "/"){
			splashEnabledPages = true;
		}else{
			splashEnabledPages = location.pathname.split('/');
			splashEnabledPages = ($.inArray( ('/'+splashEnabledPages[1]), FL.config.splashPages ) != -1);
		}
		
		if(splashed != 1 && splashEnabledPages){

			createCookie("footlocker_splashed", 1);

			$.get("/splash_page.html", function(html){

				$('body').after(html);

				var body_height = $(document).height();
				var viewport_width = $(window).width();
				var body_width = $('body').width();

				var viewport_difference = (viewport_width - body_width)/2;

				$("#splash_content").css({
					height: body_height,
					width: viewport_width
				});

				if(viewport_difference > 0) { 

					$(html).css( {left: -viewport_difference} );

				}
			});
		}
	}
};

function createCookie(c_name, value, days){
	var exdate = new Date();
	if (days != null) {
		exdate.setDate(exdate.getDate() + expiredays);
		document.cookie = c_name + "=" + escape(value) +
		((expiredays == null) ? "" : ";expires=" + exdate.toUTCString());
	}else{
		document.cookie = c_name + "=" + escape(value);
	}
}

function readCookie(c_name) {
	if (document.cookie.length>0)
	  {
	  c_start=document.cookie.indexOf(c_name + "=");
	  if (c_start!=-1)
	    {
	    c_start=c_start + c_name.length+1;
	    c_end=document.cookie.indexOf(";",c_start);
	    if (c_end==-1) c_end=document.cookie.length;
	    return unescape(document.cookie.substring(c_start,c_end));
	    }
	  }
	return "";
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}
