/*
 *   Random background image related code
 */
var vignetteImgIDs = Array("05","12","19","23","24","25","36","45","61")
var vignetteRIDs = Array("89","92","95","120","122","124","208","284","349")
var featuredRecipe;

function setRndBkImage () {
	var rnb=Math.round(Math.random()*8);
	featuredRecipe = vignetteRIDs[rnb];
	var bgImg = "/images/meal_planner/recipe_photos/recipe_" + vignetteImgIDs[rnb] + "_vignette.jpg";
	setBackgroundImage ("food_image", bgImg);
}

function setBackgroundImage (id, imageURL) {
  if (document.layers)
    document[id].background.src = imageURL == 'none' ? null : imageURL;
  else if (document.all)
    document.all[id].style.backgroundImage = imageURL == 'none' ? 'none'
: 'url(' + imageURL + ')';
  else if (document.getElementById)
    document.getElementById(id).style.backgroundImage = imageURL ==
'none' ? 'none' : 'url(' + imageURL + ')';
}

function gotoAnchor(anchor) {
  var hashValue=anchor;
  if(location.hash!=hashValue)
    location.hash=hashValue;
}

/*------------------------------------------*/

function openPopWindow(url,name,features) {
	newWindow = window.open(url,name,features);
	newWindow.focus();
}

function openRecipe(recipeNb,pf){


	if (pf){
		var qsPF = "&pf=" + pf;
	}else{
		var qsPF = "";
		//openPopWindow("/mealplanner/recipePF.aspx?rid=" + recipeNb + qsPF,'','scrollbars=yes,resizable=yes,width=800,height=600')
	}

	var _location = 'recipePF.aspx?rid=' + recipeNb + qsPF;
	
	// SSK add the day to the link //
	if(location.search.indexOf("r=") != -1){
		var lDay = "&r=" + location.search.split("=")[1];
		_location = 'weeklyrecipes.aspx?rid=' + recipeNb + qsPF + lDay;
	}
	
	var p = {};
	//p.loc = 'recipePF.aspx?rid=' + recipeNb + qsPF + lDay;
	//p.loc = 'weeklyrecipes.aspx?rid=' + recipeNb + qsPF + lDay;
	p.loc = _location;
	p.name = "RecipeWindow";
	p.nochrome = true;
	p.chrome = {};
	p.chrome.scrollbars = "yes";
	p.w = 850;
	
	PMG_newWindow(p);
	
}
function showRecipes(recipeIndex,recipeId){

	var screenW = 850, screenH = 600;
	var url = "weeklyrecipes.aspx?r="+recipeIndex+"&rid="+recipeId;

	var p = {};
	p.loc = url;
	p.name = "WeeklyWindow";
	p.nochrome = true;
	p.chrome = {};
	p.chrome.scrollbars = "yes";
	p.w = 850;
	
	PMG_newWindow(p);
	
	//newWindow = window.open(url,"recipes","width=" + screenW + ",height=" + screenH + ",location=no,menubar=no,status=no,toolbar=no,scrollbars=yes,resizable=yes");
	//newWindow.focus();
	
	
}


function newImage(arg) {
	if (document.images) {
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}else{
		return false;
	}
}

function changeImagesArray(array) {
	if (preloadFlag == true) {
		var d = document; var img;
		for (i=0;i<array.length;i+=2) {
			img = null; var n = array[i];
			if (d.images) {
				if (d.layers) {img = findElement(n,0);}
				else {img = d.images[n];}
			}
			if (!img && d.getElementById) {img = d.getElementById(n);}
			if (!img && d.getElementsByName) {
				var elms = d.getElementsByName(n);
				if (elms) {
					for (j=0;j<elms.length;j++) {
						if (elms[j].src) {img = elms[j]; break;}
					}
				}
			}
			if (img) {img.src = array[i+1];}
		}
	}
}
function changeImages() {
	changeImagesArray(changeImages.arguments);
}

var userAgent = window.navigator.userAgent;
var browserVers = parseInt(userAgent.charAt(userAgent.indexOf("/")+1),10);
var mustInitImg = true;
function initImgID() {var di = document.images; if (mustInitImg && di) { for (var i=0; i<di.length; i++) { if (!di[i].id) di[i].id=di[i].name; } mustInitImg = false;}}
function findElement(n,ly) {
	var d = document;
	if (browserVers < 4)		return d[n];
	if ((browserVers >= 6) && (d.getElementById)) {initImgID; return(d.getElementById(n))};
	var cd = ly ? ly.document : d;
	var elem = cd[n];
	if (!elem) {
		for (var i=0;i<cd.layers.length;i++) {
			elem = findElement(n,cd.layers[i]);
			if (elem) return elem;
		}
	}
	return elem;
}

function getSelectedRadio(buttonGroup) {
   // returns the array number of the selected radio button or -1 if no button is selected
   if (buttonGroup[0]) { // if the button group is an array (one button is not an array)
      for (var i=0; i<buttonGroup.length; i++) {
         if (buttonGroup[i].checked) {
            return i
         }
      }
   } else {
      if (buttonGroup.checked) { return 0; } // if the one button is checked, return zero
   }
   // if we get to this point, no radio button is selected
   return -1;
} // Ends the "getSelectedRadio" function

function getSelectedRadioValue(buttonGroup) {
   // returns the value of the selected radio button or "" if no button is selected
   var i = getSelectedRadio(buttonGroup);
   if (i == -1) {
      return "";
   } else {
      if (buttonGroup[i]) { // Make sure the button group is an array (not just one button)
         return buttonGroup[i].value;
      } else { // The button group is just the one button, and it is checked
         return buttonGroup.value;
      }
   }
} // Ends the "getSelectedRadioValue" function


function doExchange(menuId,altId,isLunchItem){

	var url = "exchgpop.aspx?id=" + menuId + "&altId=" + altId + "&isLI=" + isLunchItem;
	//var newWindow = window.open(url,"exchange","width=425,height=450,location=no,menubar=no,status=no,toolbar=no,scrollbars=yes,resizable=yes");
	//newWindow.focus();

	var p = {};
	p.loc = url;
	p.name = "exchange";
	p.nochrome = true;
	p.chrome = {};
	p.chrome.scrollbars = "yes";
	p.chrome.resizable = "yes";
	p.w = 450;
	p.h = 500;
	
	PMG_newWindow(p);
}

function showDinnerMenu(recipeID,event){
	//menuLayers.show('dinnerpopup', event)
}
