var cookieName = 'recipe_browse_by';
var defaultTab = 'style';
function createCookie(name,value,days,path) {	
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = '; expires='+date.toGMTString();
	} else {
		var expires = "";
	}
	if (!path) {
		path='/';
	}
	document.cookie = name+'='+value+expires+'; path='+path;
}

function readCookie(name) {
	var nameEQ = name + '=';
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,'',-1);
}


function removeExplore () {
		$('#sidebar .recipebox').removeClass('explorename');
		$('#sidebar .recipebox').removeClass('explorestyle');		
		$('#sidebar .recipebox').removeClass('exploremeal');	
		$('#sidebar .recipebox').removeClass('exploremain');	
		$('#sidebar .recipebox').removeClass('explorecuisine');	

		$('#sidebar .byname:not(h4), #sidebar .bystyle:not(h4), #sidebar .bymeal:not(h4),	#sidebar .bymain:not(h4), #sidebar .bycuisine:not(h4)').hide();
}

function rememberTab(which) {
	createCookie(cookieName,which);
}

function setTab(id) {
	removeExplore();
	$('#sidebar .by'+id+':not(h4)').fadeIn(90);
	$('#sidebar .recipebox').addClass('explore'+id);	
	rememberTab(id);
}

$(document).ready(function() {
/* sidebar recipe box */
	var mainCat = '';
	$.each($('#filterprimary h3'), function() {
		txt = $(this).text().toLowerCase();	
		if (txt.indexOf('style') > -1) {
			mainCat = 'style';
			setTab(mainCat);
		} else if (txt.indexOf('meal') > -1) {
			mainCat = 'meal';
			setTab(mainCat);			
		} else if (txt.indexOf('main') > -1) {
			mainCat = 'main';
			setTab(mainCat);			
		} else if (txt.indexOf('cuisine') > -1) {
			mainCat = 'cuisine';
			setTab(mainCat);			
		}
	});

	if (mainCat=='') {
		lastTab = readCookie(cookieName);
		if(lastTab && (lastTab=='name' || lastTab=='style' ||
			lastTab=='meal' ||	lastTab=='main' || lastTab=='cuisine')) {
			
			setTab(lastTab);
		} else {
			setTab(defaultTab);
		}
	}	
	$('#sidebar h4').fadeIn(150);

	$('#sidebar h4.byname').click(function(e) {
		setTab('name');
		e.preventDefault();		
	});

	$('#sidebar h4.bystyle').click(function(e) {
		setTab('style');
		e.preventDefault();	
	});

	$('#sidebar h4.bymeal').click(function(e) {
		setTab('meal');
		e.preventDefault();	
	});

	$('#sidebar h4.bymain').click(function(e) {
		setTab('main');
		e.preventDefault();	
	});

	$('#sidebar h4.bycuisine').click(function(e) {
		setTab('cuisine');
		e.preventDefault();	
	});

/* recipe categories */
	var mainCode = '';
	var origTotal = $('#total').text();
	var mainText = '';
	$.each($('#filterprimary option'), function() {
		if (this.id.indexOf('all-group') > -1) {
			mainCode = 'c'+this.id.substring(10);
			$(this).remove();
		}		
		if ($(this).attr('selected')==true) {
			mainText = $(this).text();
			$('#total #r').text(' in '+ mainText);		
		}
	});
	$('#filterprimary select').change(function() {
		var url = this.options[this.selectedIndex].id.substring(1);
		window.location.href = '../'+url+'/';	
	});

	$.each($('.dropline select'), function() {
		$(this).change(function() {
			var sel = [];
			var alls = '';
			var cnt = 0;
			var filText = '';
			$.each($('.dropline select'), function() {	
				var oid = this.options[this.selectedIndex].id;
				if 	(oid.indexOf('all-group') > -1) {
					alls += 'c'+oid.substring(10);
				} else {
					filText += ', ' + $('#'+oid).text();
					cnt = sel.push(oid);				
				}
			});	
			var newTotal = 0;
			$.each($('.recipe_pix'), function() {	
				if (alls.length==6) {
					matches = {c2: true, c3: true, c4: true, c5: true};
				} else {
					matches = {c2: false, c3: false, c4: false, c5: false};				
					matches[mainCode] = true;				
					var cl = $(this).attr('class');
					cl = cl.split(' ');
					var len = cl.length;
					for (i=1;i<len;i++) {
						code = cl[i].substring(0,2);
						if (alls.indexOf(code)==-1 && code!=mainCode) {
							for(j=0;j<cnt;j++) {						
								if (cl[i].substring(3)==sel[j]) {
									matches[code] = true;
								}
							}						
						} else {
							matches[code] = true;						
						}
					}
				}
				if (matches['c2'] && matches['c3']  && matches['c4'] && matches['c5']) {
					$(this).css('display','block');
					newTotal++;
				} else {
					$(this).css('display','none');
				}				
			});	
			$('#total #n').text(newTotal);
			if (newTotal==1) {
				$('#total #adds').text('');
			} else {
				$('#total #adds').text('s');
			}
			$('#total #r').html(' in ' +mainText + filText);	
			$('#total').animate({'backgroundColor': '#0f0'},1000);
			
		});
	});


/* recipe permalink page */
	$.each($('#recipeblock'), function() {
		var blockheight = $('#recipeblock').height();
		$('#recipeblock').css('height',blockheight+'px');
		if ($('#tiphead').length) {
			$('#tiphead, #nuthead').css('display','block');
		} else {
			$('#nuthead').css({left:'150px',display:'block'});		
		}
		if (!$('#tiphead').length && !$('#nuthead').length ) {
			$('#recipehead').css('border-right','1px solid #9fd142');		
		} else if (!$('#nuthead').length) {
			$('#tiphead').css('border-right','1px solid #9fd142');		
		}
			
		$('#recipehead a').click(function(event) {
			event.preventDefault();
			$('#recipeblock').attr('class','trecipe');
		});	
		
		$('#tiphead a').click(function(event) {
			event.preventDefault();		
			$('#recipeblock').attr('class','ttip');
		});	
		
		$('#nuthead a').click(function(event) {
			event.preventDefault();		
			$('#recipeblock').attr('class','tnut');
		});	

		$('#print a').click(function(event) {
			event.preventDefault();		
			$('#recipeblock').attr('class','tprint');
		});	
		
		$('#poption1').click(function(event) {
			event.preventDefault();	
			$('body').removeClass('pnoimg').removeClass('pcompact');			
			window.print();
		});

		$('#poption2').click(function(event) {
			event.preventDefault();	
			$('body').removeClass('pcompact');
			$('body').addClass('pnoimg');
			window.print();
		});

		$('#poption3').click(function(event) {
			event.preventDefault();	
			$('body').removeClass('pnoimg');
			$('body').addClass('pcompact');
			window.print();
		});		
	});
	var txtmin = 0;
	$.each($('#recipeblock .recipetxt'), function() {
		var h = $(this).height();
		if (h > txtmin) { txtmin = h;}
	});
	$('#recipeblock .recipetxt').css('min-height',txtmin);
});