//HOMEPAGE PROJECTOR

var projector = {
		switchSlide: function (requestedSlideClass){
				var currentSlide = $('#slides img.selected');	
				var currentSlideClass = $(currentSlide).attr('class').replace(" selected", "");	
				var nextInLine;	
				var auto = (typeof(requestedSlideClass) == 'undefined');// if looping, this is true	
				if (auto) {//non user-initiated	
					if (currentSlideClass.indexOf('3')!=-1){// the indexOf referenced here should me the total # of slides
						nextInLine = $('#slides img.slide1');	
						requestedSlideClass = 'slide1';	
					}//wrap back to first slide if on last	
					else {	
						nextInLine = $('#slides img.selected').next();	
						requestedSlideClass = $(nextInLine).attr('class');	
					}
			}
			else {
				clearInterval(projector.autoRotate);
				nextInLine = $('#slides img.'+requestedSlideClass).css('bottom', '-320px');	
			}
			$(currentSlide).css('z-index','1');
			$(nextInLine).animate({bottom: 0},400,"swing", function(){
				$(currentSlide).removeClass('selected').removeAttr('style');
				$(nextInLine).addClass('selected').removeAttr('style');	
				if(!auto){setTimeout(projector.rotate,6400);}	//restart loop after a click

			});	
			$('#controls a.selected').removeClass('selected');
			$('#controls a.'+requestedSlideClass).addClass('selected');	
		},			

		nextSlide: function(){
			$('#slides img:animated').stop(false, false);//stops the current animation (if there is one)					
			if ($(this).hasClass('selected')) {return false;}					
			else {
				var requestedSlideClass = ($(this).attr('class')).match(/slide\d+/);
				projector.switchSlide(requestedSlideClass);
			}
			return false;			
		},

		sendUser: function(){
			var whereTo = $(this).attr('class').match(/slide\d+/);	
			whereTo = $('#controls a[class *= "'+whereTo+'"]').attr('href');			
			if (whereTo.match('mywebgrocer.com')){			
				pageTracker._link(whereTo); 				
			}						
			else {			
				document.location = whereTo;				
			}
		},
		
		loadUp : function(){
			$('#controls a').click(projector.nextSlide);
			$('#slides img').click(projector.sendUser).hover(
				function(){$(this).css({cursor:"pointer"});},
				function(){}
			);
			projector.rotate();
			
		},

		rotate: function(){			
			clearInterval(projector.autoRotate);
			projector.autoRotate = setInterval("projector.switchSlide()", 8000);	
		}				
}



$(document).ready(function() {
		if ($('body').attr('id') == 'homePage') {projector.loadUp();}

		// Preload all rollovers
		$("#nav img").each(function() {
			// Set the original src
			rollsrc = $(this).attr("src");
			rollON = rollsrc.replace('OFF', 'ON');
			$("<img>").attr("src", rollON);
		});		

		// Navigation rollovers
		$("#nav a").mouseover(function(){
			imgsrc = $(this).children("img").attr("src");
			if (typeof(imgsrc) != 'undefined') {
				imgsrcON = imgsrc.replace('OFF', 'ON');
				$(this).children("img").attr("src", imgsrcON);
			}
		});	

		$("#nav a").mouseout(function(){
			if (typeof(imgsrc) != 'undefined') {
				$(this).children("img").attr("src", imgsrc);
			}
		});

		$('#nav ul li:last-child').css('borderBottom','none');

		function isdefined(variable){
		 return (typeof(window[variable]) == "undefined")?  false: true;

		}
		if (($('body').attr('id')) != 'homePage'){
			if (location.pathname.substring(0)) {
				// only match the path string if a path hasn't already been specified for app compatibility
			  if (isdefined('thePath') == false){
			 	  // Determine 'active' nagivation link
				  thePath = '';   
				  // get the path
				  //thePath = location.pathname.substring(0); // This grabs just the path, not the domain
				  thePath = location.href;
				  //thePath = thePath.match(/^\/?([a-z0-9_\.-]+)/); // only get the first part of the path
				  thePath = thePath.replace(/\/$/ig, ''); // strip off trailing slash
				  //thePath = thePath.replace(/\?(.*)$/ig, ''); // strip off query string
			  }			
	
				// Search via css attribute selector (case sensitive)
				//navLink = $('#nav a[@href="' + thePath + '"]');
				// Since searching on url's in the navigation must be case-insensitive, 
				// We have to iterate over every link within #nav
				$('#nav a').each(function() {
					navLink = $(this).attr('href');
					if (navLink.toLowerCase() == thePath.toLowerCase()) {
						// Add the 'ON' css class to this link
						$(this).addClass('ON');
						// find this item's parent and grandparent, up the navigation tree
						parent1 = $(this).parent().parent().parent().children('a');
						parent1.addClass("ON");
						parent2 = parent1.parent().parent().parent().children('a');
						parent2.addClass("ON");
						// one more time...
						parent3 = parent2.parent().parent().parent().children('a');
						parent3.addClass("ON");
					}
				});
			}
		}
		

		//table striping
		$('table.striped tr:even').addClass('alt');		

		// clear every other product dl 
		//$('dl.product:nth-child(odd)').css("clear","left");

		$('dl.product:nth-child(even)').after('<br class="clear">');	
			
		// Some style overrides to specific types of form elements
		$('input[type="submit"]').addClass('submit');
		$('input[type="checkbox"], input[type="radio"]').css("border","none");		

		//addThis stuff
		var addthis_pub="foodlion";
		var addthis_brand = "Food Lion";
		
		$('#addThis').hover(
			function(){addthis_open(this, '', '[URL]', '[TITLE]');},
			function(){addthis_close()});
		$('#addThis').click(function(){return addthis_sendto();});		

		//google  tracking for mywebgrocer links

		$('a[href*="mywebgrocer.com"]:not("#projector a")').click(function(){
			pageTracker._link(this.href); 
			return false;
		});
		
		// #ft height adjust (for right-borders by links)
		if($('#ft').length>0){
		var ftHeight = $('#ft').height() - 50;			
			$('#ft div').height(ftHeight).prepend('<code></code>').find('code').height(ftHeight).width(180).parent()
				.hover(
					function(){$(this).find('code').fadeIn()},
					function(){$(this).find('code').fadeOut()}
				)
			;
		}
		
});

