// JavaScript Document

// variables for scripps ads
var site       = "RR";
var bug        = "0"
var section    = "";	<!--sections are children of categories (n/a now but site will grow into)-->
var subsection = "";	<!--subsections are children of sections of categories (n/a now but site will grow into)-->

// minimum flash version allowed for SWFObject
var flashVersion = '8';


function changeTab( tab ) {
	var objOff = document.getElementById(tab);
	var objOn = tab == 'newest' ? document.getElementById( 'popular' ) : document.getElementById( 'newest' );
	objOff.style.display = 'none';
	objOn.style.display = 'block';
}


function addLoadEvent( func ) {
	var oldonload = window.onload;
	if ( typeof window.onload != 'function' ) { window.onload = func; } 
	else {
		window.onload = function() {
			if (oldonload) { oldonload(); }
			func();
		}
	}
}

addLoadEvent( 
	function() {
		if ( document.forms['search'] && document.forms['search'].querytop ) {
			 document.forms['search'].querytop.onfocus = function () {
				if ( document.forms['search'].querytop.value == 'Search Recipes' ) {
					document.forms['search'].querytop.value = '';
				}
			 };
			 document.forms['search'].querytop.onblur = function () {
				if ( document.forms['search'].querytop.value == '' ) {
					document.forms['search'].querytop.value = 'Search Recipes';
				}
			 };
		}
	}
);

addLoadEvent( 
	function() {
		if ( document.getElementById && document.getElementById('printer') ) {
			document.getElementById('printer').onclick = printIt;
		}
	}
);

addLoadEvent( 
	function() {
		if ( document.forms['search'] ) {
			 document.forms['search'].onsubmit = checkSubmit;
		}
	}
);

addLoadEvent ( 
	function() {
		if ( document.getElementById && document.getElementById ('resetForm') ) {
			document.getElementById('resetForm').onclick = resetForm;
		}
	}
);

addLoadEvent( 
	function() {
		if ( document.getElementById && document.getElementById('yumoNewsletter') ) {
			document.getElementById('yumoNewsletter').onclick = yumoNewsletter;
		} 
	}
);


function printIt() {
	var track = false;
	if ( this.pathname.search('recipe.php') != -1 ) {
		var lnk = '/print/';
		var queries = this.search.slice(1);
		queries = queries.split('&');
		for (i in queries) {
			var q = queries[i].split('=');
			if ( q[0] == 'recipe_id' ) { 
				if ( q[1] == 2420 || q[1] == 2418 || q[1] == 2419 || q[1] == 2417 || q[1] == 2416 ) {
					lnk += queries[i]; 
					track = true;
				}
			}
		}
		if ( typeof( pageTracker._trackPageview ) == "function" && track ) {
			pageTracker._trackPageview(lnk);
		}
	}
	print();
	return false;
};


function checkSubmit() {
	document.forms['search'].submit();
}

function removeFormElement( id ) {
	var element = document.getElementById( id );
	if ( element.parentNode && element.parentNode.removeChild ) {
		element.parentNode.removeChild( element );
	}
}
	
// Yum-o! newsletter signup pop up window
function yumoNewsletter() {
	window.open( 'https://app.e2ma.net/app/view:Join/signupId:30188', 'newsWindow', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=540,height=480');
	return false;
}


// toggle left subNav items
function toggle( which ) {
	var list = document.getElementById( which + 'List' );
	var toggle = document.getElementById( which + 'Toggle' );
	var style, padding, background
	var display = list.style.display;
	if ( display == 'block' ) {
		style = 'none';
		padding = '4px';
		background = 'url("../images/icon_expand.gif")';
	}
	else {
		style = 'block';
		padding = '0';
		background = 'url("../images/icon_collapse.gif")';
	}
	list.style.display = style;
	list.parentNode.style.paddingBottom = padding;
	toggle.style.backgroundImage = background;
}

function isValidEmail( email ) {
	var validRegExp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})$/;
	return validRegExp.test( email ) 
}