/* SECONDARY PAGE AUTO-TABBING */

var threeTab = {

	frequency : 6000, //tabbing speed
	
	restartDelay : 40000, //applies post-click if stopAfterClick is false
	
	container : $('div[id $="ingTabs"]'), //scopes dom searches
	
	stopAfterClick : true, // set to true to disable auto-tabbing after a tab is clicked

	tabTo: function(targetTab, clicked) {
	//TODO: this is throwing a javascript error on pages that reference this file, but don't contain this markup
	//for example: http://foodlion.spunlogic.net/Health-and-Wellness/Whats-Your-Wellness-IQ
	    var lastTab = $('h4 a.active', threeTab.container).attr('class').replace('active', '').replace('oneliner', '').replace(' ', '' );
		var newTab = targetTab.attr('class').replace('oneliner', '').replace(' ', '');
	    if (targetTab.hasClass('active')) { return false; }
	    else {
	        $('h4 a.active', threeTab.container).removeClass('active');
	        targetTab.addClass('active');
	        $('img.' + lastTab + ', div.' + lastTab, threeTab.container).fadeOut();
	        if ($('div.' + newTab, threeTab.container).hasClass('tall')) {
	            var containerHeight = $('div.' + newTab, threeTab.container).height();
	            threeTab.container.animate({ height: containerHeight + 350 }, 300, "swing");
	        }
	        $('img.' + newTab + ',div.' + newTab, threeTab.container).fadeIn();
	        $('#tabRail').attr('class', newTab);
	        if (clicked && (!threeTab.stopAfterClick)){
	        		setTimeout(function(){
	        		threeTab.tabCount = $('h4 a', threeTab.container).index(targetTab);	        		
	        		threeTab.start();
	        	},threeTab.restartDelay);
	        }
	        return false;
	    }
	},
	
	tabCount : 0,
	
	tabInterval : null, // variable defined on start
	
	startTabbing : function (){
		threeTab.tabInterval =  setInterval(function(){
		threeTab.tabCount = (threeTab.tabCount+1)%3;
		threeTab.tabTo($('h4 a', threeTab.container).eq(threeTab.tabCount),false);
		},threeTab.frequency);
	},
			
	start : function (){	
		clearInterval(threeTab.tabInterval);
		threeTab.startTabbing();
	},
	stop : function(){clearInterval(threeTab.tabInterval);}

}

/*END AUTO-TABBING*/

$(document).ready(function() {

	threeTab.start(); // starts auto-tabber
	
	$('div[id $="ingTabs"] h4 a').click(function(){
		threeTab.stop();
		return threeTab.tabTo($(this),true);
	});
	
	

    $('div[id $="ingTabs"] img[class*=tab]').not('.dontlink')
        .hover(
			function() { $(this).css({ cursor: 'pointer' }) },
			function() { }
			)
		.click(function() {
		    var link = $(this).next().find('a[class *=navigate]');
		    if (link.length > 0) {
		        if (link.attr('target') == '_blank') { window.open(link.attr('href')) }
		        else { document.location = (link.attr('href')); }
		    }
		});


    //BABY & PET 

    function switchCoupons(tab) {
        var tabText = tab.html();
        if (tabText == 'Cats') { tab.parents('.couponHero').find('span').fadeIn(); } //update hero to cat
        else if (tabText == 'Dogs') { tab.parents('.couponHero').find('span').fadeOut(); } //update hero to dog
        whichCoupons = tab.parent().attr('class');
        tab.parent().siblings('.selected').removeClass('selected');
        tab.parent().addClass('selected');
        var listing = tab.parent().attr('class').replace('selected', '');
        $('dl.selected').hide().removeClass('selected');
        $('dl.' + listing).fadeIn().addClass('selected');
        $('#printToggle img').hide().parent().attr('class', listing).find('img').fadeIn();
        //update swf
        $('#flashPrinter').fadeOut(200, function() {
            printCoupons(coupons[whichCoupons]);
            $(this).fadeIn();
        });
    }

    $('.couponHero a').click(function() {
        if ($(this).parent().hasClass('selected')) { return false; }
        else { switchCoupons($(this)); }
        return false;
    });

    $('dl.coupons dt:odd, dl.coupons dd:odd').css({ background: '#ffffff' });

    function printCoupons(moviePath) {
        var so = new SWFObject(moviePath, 'flashPrinter', '220', '134', '8', '#FFFFFF');
        so.useExpressInstall('/content/flash/expressinstall.swf');
        so.addVariable('vpath', moviePath);
        so.addVariable("allowscale", "false");
        so.addParam("allowScriptAccess", "always");
        so.addParam("wmode", "transparent");
        so.write('flashPrinter');
    }

    //load "selected" tab's swf		
    if ($('body[id*=coupons]').length > 0) { printCoupons(coupons.initial) }


    //baby/ pet sponsor sliders
    var sponsor = new Array();
    sponsor.holder = $('#babyPetSponsors');
    sponsor.banner = sponsor.holder.find('.view').find('img');
    sponsor.src = sponsor.banner.attr('src');
    sponsor.pos = 0;
    sponsor.slideBy = 396;
    sponsor.height = 92;
    sponsor.first = true;
    sponsor.last = false;
    if (sponsor.holder.length > 0) {
        sponsor.imgWidth = sponsor.banner.width();
        sponsor.on = true;
    }
    if ($.browser.mozilla) {
        sponsor.banner.parent().append('<span id="Banner2"><span>'); //replace image so it isn't highlightable
        sponsor.banner.remove();
        sponsor.banner = $('#Banner2').css({ width: sponsor.imgWidth, height: sponsor.height, backgroundImage: 'url(' + sponsor.src + ')' });
    }

    $('.controller')
			.hover(
				function() { $(this).addClass('hover').css({ cursor: 'pointer' }) },
				function() { $(this).removeClass('hover') }
			)
			.click(function() {
			    if (sponsor.banner.is(':animated')) { return false }
			    sponsor.pos = sponsor.banner.css('left').replace('px', '');
			    if (sponsor.pos == 0) { sponsor.first = true; }
			    if (sponsor.pos == (0 - (sponsor.imgWidth - sponsor.slideBy))) { sponsor.last = true; }
			    if ($(this).attr('id').match('next'))
			    { sponsor.slideTo = '-=' + sponsor.slideBy; if (sponsor.last) { sponsor.stop = true } }
			    else { sponsor.slideTo = '+=' + sponsor.slideBy; if (sponsor.first) { sponsor.stop = true } }
			    if (!sponsor.stop) { sponsor.banner.animate({ left: sponsor.slideTo }, 350, "swing") }
			    sponsor.last = sponsor.first = sponsor.stop = false;
			    return false;
			})
		; //end .controller

});
		
		
