//var IE7_PNG_SUFFIX = ".png";

$(function(){
	// Document is ready
	$('.rollover').each(function() {
		var src = $(this).attr('src')
		$(this).bind('mouseover', function() {
			$(this).attr('src', src.replace(/_off.([gif|jpg|png])/,'_over.$1'));
		});
		$(this).bind('mouseout', function() {
			$(this).attr('src', src);
		});
	});
	$("#nutrition .accordion .nutrition_accordion_header").each(function(){
		$(this).bind("click",function(){
			accordion_toggle($(this));
		});
		$(this).hover(
			function() {
				$(this).css("background-image", $(this).css("background-image").replace("bkg_nutrition_read_article.gif","bkg_nutrition_read_article_over.gif"));
			},
			function () {
				$(this).css("background-image", $(this).css("background-image").replace("bkg_nutrition_read_article_over.gif","bkg_nutrition_read_article.gif"));
			}
		)
	});
	
	$("#nutrition .our_articles a").each(function(){
		$(this).bind("click",function(event){
			var target = $('#'+$(this).attr('name'));
			accordion_toggle(target,true);
			event.preventDefault();
		});
	});
	
	$('.bistro_man_day area').click(function(e){
		e.preventDefault();
		_tag.dcsMultiTrack("DCS.dcsuri","http://www.facebook.com/stouffersbistro?v=app_7146470109",
		"WT.ti",'Bistro Man Day Like',"WT.dl","20","DCS.dcsref","http://www.facebook.com/stouffersbistro?v=app_7146470109");
		 _tag.DCS.dcsuri=_tag.DCS.dcsref=_tag.WT.ti=_tag.WT.dl="";
		 var url = $(this).attr('href');
		document.location.href = url;
	});

});
function accordion_toggle(header,scroll) {
	header.siblings(".nutrition_accordion_body").slideToggle();
	if(header.css("background-image").indexOf("bkg_nutrition_read_article")!=-1){
		header.css("background-image", header.css("background-image").replace("bkg_nutrition_read_article.gif","bkg_nutrition_hide_article.gif"));
		if(scroll) $.scrollTo(header);
	} else {
		header.css("background-image", header.css("background-image").replace("bkg_nutrition_hide_article.gif","bkg_nutrition_read_article.gif"));
	}
}
function recipe(num) {
	if(num=='2'){
		$('#recipes_page1').css('display','none');
		$('#recipes_page2').css('display','block');
		$.scrollTo($("#content"));
		$('#recipes .pager span.one').html('<a href="javascript:recipe(\'1\');">1</a>');
		$('#recipes .pager span.two').html('2');
	} else {
		$('#recipes_page1').css('display','block');
		$.scrollTo($("#content"));
		$('#recipes_page2').css('display','none');
		$('#recipes .pager span.two').html('<a href="javascript:recipe(\'2\');">2</a>');
		$('#recipes .pager span.one').html('1');
	}
}

// Bistro recipe pager
function bistroRecipe(num) {
	//check for valid range
	if (num < 1 || num > 3) {
		return;
	}

	// Hide the current table, and show the right one
	// Slide
/*
	$('#products div.current').stop(true,true).animate ({width:0},100, function () {
			$('#products div.current table.current').css('display','none');
			$('#products div.current table.current').removeClass('current');
			$('#products div.current table.page' + num).css('display','block');
			$('#products div.current table.page' + num).addClass('current');
			$('#products div.current').animate ({width:'100%'},400);
		});
*/
	// Fade
	$('#products div.current .table.page' + window.page).addClass('current');
	
	$('#products div.current').stop(true,true).fadeOut(100, function () {
			$('#products div.current .table.current').hide().removeClass('current');
			$('#products div.current .table.page' + num).show().addClass('current');
			$('#products div.current').fadeIn(400);
		});


	// Set the pager links
	if(num=='3'){
		$('#recipes .pager span.one').html('<a href="javascript:bistroRecipe(1);">1</a>');
		$('#recipes .pager span.two').html('<a href="javascript:bistroRecipe(2);">2</a>');
		$('#recipes .pager span.three').html('3');
	} else if(num=='2'){
		$('#recipes .pager span.one').html('<a href="javascript:bistroRecipe(1);">1</a>');
		$('#recipes .pager span.two').html('2');
		$('#recipes .pager span.three').html('<a href="javascript:bistroRecipe(3);">3</a>');
	} else {
		$('#recipes .pager span.one').html('1');
		$('#recipes .pager span.two').html('<a href="javascript:bistroRecipe(2);">2</a>');
		$('#recipes .pager span.three').html('<a href="javascript:bistroRecipe(3);">3</a>');
	}

	// Set page num in global bistro var.
	bistro.pageURL(num);
}

// Bistro recipe product selector
function bistroProduct(name) {

	// check if we're already displaying the info, do nothing.
	if ($('#products #bistro #' + name).hasClass('current')) {
		return;
	}

	// Switch the Tab images
	//var curSrc = $('#recipes #bistro_tabs .current').attr('src');
	//$('#recipes #bistro_tabs .current').attr('src', curSrc.replace(/on/,'off')+'');
	$('#recipes #bistro_tabs .current').removeClass('current');
	//$('#recipes #bistro_tabs .' + name).attr('src', '../../images/sub_nav_' + name + '_on.png');
	$('#recipes #bistro_tabs #' + name).addClass('current');
	
	$('.main_tout').removeClass('main_tout_Sliders');
	$('.main_tout').removeClass('main_tout_Melts');
	$('.main_tout').removeClass('main_tout_Pizzeria');
	$('.main_tout').removeClass('main_tout_Crustinis');
	$('.main_tout').removeClass('main_tout_Paninis');
	
	$('.main_tout').addClass('main_tout_'+name);
	
	//alert('main_tout_'+name);


	// Hide current info, then show the right one
	// Slide
/*
	$('#products div.current').stop(true,true).slideUp(100, function() {
		$(this).find('table.current').css('display','none');
		$(this).find('table.current').removeClass('current');
		$('#products #bistro' + name + ' table:first').css('display','block');
		$('#products #bistro' + name + ' table:first').addClass('current');
		$('#products #bistro' + name).slideDown(400);
	});
*/
	// Fade
	$('#products div.current').stop(true,true).fadeOut(100, function() {
		$(this).find('.table.current').hide().removeClass('current');
		$('#products #bistro' + name).show();
		$('#products #bistro' + name + ' .table:first').addClass('current').fadeIn(400);
		
	});


	// Set the pager links
	if (name == "Crustinis") {
		$('#recipes .pager').html('<p>page:	<span class="one current">1</span> | <span class="two"><a href="javascript:bistroRecipe(\'2\');">2</a></span> | <span class="three"><a href="javascript:bistroRecipe(\'3\');">3</a></span></p>');
	}
	else if (name == "Melts") {
		$('#recipes .pager').html('<p>page:	<span class="one current">1</span> | <span class="two"><a href="javascript:bistroRecipe(\'2\');">2</a></span></p>');
	}
	else {
		$('#recipes .pager').html('<p>&nbsp;</p>');
	}

	// Remove 'current' class and add it to div just clicked
	$('#products div.current').removeClass('current');
	$('#products #bistro' + name).addClass('current');
	
	// Set product name in global bistro var.
	bistro.prodURL(name);
}

//Check URL for bistro/recipes page...activate tabbing script
if (window.location.href.split('bistro/recipes',2)[1]) {
	var bistro = new Object();				// Global object to hold current product and page.
	bistro._prod = 'Sliders'; 			// current product var
	bistro._page = '1';						// current page var
	bistro.prodURL = function(name) {		// set or get URL product var
		if (name != undefined) {
			if (name != this._prod) {
				this._prod = name;
				this._page = '1';
				this.updateURL();
				return true;
			}
		}
		else {
			return bistro.getURLInfo('prod',0);
		}
	}
	bistro.pageURL = function(num) {		// set or get URL page var
		if (num != undefined) {
			if (num != this._page) {
				this._page = num;
				this.updateURL();
				return true;
			}
		}
		else {
			return bistro.getURLInfo('page',1);
		}
	}
	bistro.updateURL = function() {
		// Build hash portion of URL, and replace with old hash val of URL
		var anchr = '#prod=' + this._prod + '&page=' + this._page;
		window.location.href = window.location.href.split('#',2)[0] + anchr;
	},
	bistro.getURLInfo = function(name, num) {
		// Check for a hash mark in the URL
		var anchr = window.location.href.split('#',2)[1];
		if (anchr) {
			// Split hash val by 'name' var and get it's value
			var tmp = anchr.split('&',2)[num].split(name+'=',2)[1];
			if (tmp) {
				return tmp;
			}
		}
		return false;
	},
	bistro.start = function() {
		// Check for an anchor in the URL
		if (this.prodURL()) {
			// Check for a change in the prod or page val
			if (this._prod != this.prodURL()) {
				bistroProduct(this.prodURL());
			}
			if (this._page != this.pageURL()) {
				bistroRecipe(this.pageURL());
			}
		}
	}

	// When the DOM is ready...
	$(function () {
		// check the URL for hash data---enables bookmarking
		if (bistro.prodURL()) {
			bistroProduct(bistro.prodURL());
			if (bistro.pageURL()) {
				bistroRecipe(bistro.pageURL());
			}
		}
		// start regularly checking URL every 200 msecs---enables back button on browser---works for FF, not in IE
		setInterval('bistro.start()',200);
	});
}

// JS method of opening a window.
function open_rules(url) {
	window.open(url,"rules","width=500,height=600,menubar=0,location=0,status=0,toolbar=0,scrollbars=1");
}

function font_size(dir) {
	/*var currentSize = $('#main_content').css('font-size').replace('px', '');
	currentSize = parseFloat(currentSize);
	if(dir == 'up') {
		if(currentSize >= '12') {
			$('#main_content').css('font-size', '115%');
		} else  {
			$('#main_content').css('font-size', '100%');
		}
	} else if(dir == 'down') {
		if(currentSize <= '12') {
			$('#main_content').css('font-size', '85%');
		} else {
			$('#main_content').css('font-size', '100%');
		}
	}*/
	if(dir == 'up') {
		if($('#main_content').hasClass('small')) {
			$('#main_content').removeClass('small');
		} else {
			$('#main_content').removeClass('small');
			$('#main_content').addClass('large');
		}
	} else if(dir == 'down') {
		if($('#main_content').hasClass('large')) {
			$('#main_content').removeClass('large');
		} else {
			$('#main_content').removeClass('large');
			$('#main_content').addClass('small');
		}
	}
	return(false);
}
