
document.observe('dom:loaded', init);

function init() {
    var ratingControl = new Control.Rating('userRatingControl');

    ratingControl.observe('afterChange', function(ratevalue){
      SetRecipeRating(ratevalue);
    });
    
    CheckQueryString();
}



location.querystring = (function() {   
			// The return is a collection of key/value pairs   
			var result = {};   

			// Gets the query string with a preceeding '?'   
			var querystring = location.search;   

			// document.location.search is empty if a query string is absent   
			if (!querystring)   
			    return result;   

			// substring(1) to remove the '?'   
			var pairs = querystring.substring(1).split("&");   
			var splitPair;   

			// Load the key/values of the return collection   
			for (var i = 0; i < pairs.length; i++) {   
			    splitPair = pairs[i].split("=");   
			    result[splitPair[0]] = splitPair[1];   
			}   

			return result;   
			})(); 

function CheckQueryString() {
	var advancedSearch = getQueryVariable('AdvancedSearch');
	var quickSearch = getQueryVariable('QuickSearch');
	var pageindex = getQueryVariable('PageNumber');
	var topRecipes = getQueryVariable('TopRecipes');
	var keyword = getQueryVariable('KeyWord');
	var chainonlyrecipes = getQueryVariable('ChainOnlyRecipes');
	var source = getQueryVariable('Source');

	var referringUrl = "";

	if (source != undefined)
	{
		referringUrl = "RecipeSearchResults.aspx";
	}

	if(referringUrl.indexOf("RecipeSearchResults.aspx") != -1)
	{
		var url = '/Recipes/RecipeSearchResults.aspx?';
		var urlLen = url.length;

		if(advancedSearch != undefined)
		{
			url = url + 'AdvancedSearch=' + advancedSearch;
		}

		if(quickSearch != undefined)
		{
			url = url + 'QuickSearch=' + quickSearch;
		}

		if(topRecipes != undefined)
		{
			url = url + 'TopRecipes=true'
		}

		if(pageindex != undefined)
		{
			url = url + '&PageIndex=' + pageindex;
		}

		if(keyword != undefined)
		{
			url = url + '&KeyWord=' + keyword;
		}

		if(chainonlyrecipes != undefined)
		{
			url = url + '&ChainOnlyRecipes=' + chainonlyrecipes;
		}

		if(url.length > urlLen)
		{
			var link = document.getElementById('recipeback');
			link.href = url;
			ShowHideBackButtons(link);
		}
	}
	else
	{
		var div = document.getElementById('recipecenterback');
		ShowHideBackButtons(div);
	}
}

function ShowHideBackButtons(activediv) {

	var recipeback = document.getElementById('recipeback');
	var recipecenterback = document.getElementById('recipecenterback');

	var divarray = [recipeback, recipecenterback];

	for(i=0; i < divarray.length; i++) {
		if(divarray[i] == activediv) {
			divarray[i].style.display = 'block';
			divarray[i].style.visibility = 'visible';
		}
		else {
			divarray[i].style.display = 'none';
			divarray[i].style.visibility = 'hidden';
		}
	}
}

function getQueryVariable(variable) {
		var query = window.location.search.substring(1);
		var vars = query.split("&");
		for(var i=0;i<vars.length;i++) {
		var pair = vars[i].split("=");
		if (pair[0] == variable) {
			return pair[1];
		}
		}	
} 

function SetRecipeRating(ratevalue) {

var recipeid = document.getElementById('recipeid');

SetUserRecipeRating(ratevalue);
RateRecipe(recipeid.value, ratevalue);
}

function SetUserRecipeRating(ratevalue) {
  var userRating = document.getElementById('userrating');
  var userRatingControl = document.getElementById('userRatingControl');

  var UserRecipeRating = document.getElementById('UserRecipeRating');

  if (UserRecipeRating != null) {
	  UserRecipeRating.style.display = 'block';
	  UserRecipeRating.style.visibility = 'visible';
  }

  if (ratevalue > 0) {
	  if (userRating != null) {
		  userRating.innerHTML = 'You have rated this recipe.';
	  }

	  if (userRatingControl != null) {
		  var rating = new Control.Rating('userRatingControl', { value: ratevalue, rated: false });
	  }
  }
  else {
	  if (userRating != null) {
		  userRating.innerHTML = 'Please rate this recipe.';
		  var rating = new Control.Rating('userRatingControl');
	  }
  }
}

function SetGlobalRecipeRating(ratevalue, numofratings) {
var recipeRating = document.getElementById('reciperating');
var globalRecipeRating = document.getElementById('globalRatingCount');
var globalRatingControl = document.getElementById('globalRatingControl');

if (recipeRating != null) {
  if (numofratings == '0') {
	  recipeRating.style.display = 'none';
	  recipeRating.style.visibility = 'hidden';
  }
  else {
	  recipeRating.style.display = 'block';
	  recipeRating.style.visibility = 'visible';

	  if (globalRecipeRating != null) {
		  globalRecipeRating.innerHTML = 'by ' + numofratings + ' user(s)';
	  }

	  if (globalRatingControl != null) {
		  var reciperating = new Control.Rating('globalRatingControl', { value: ratevalue, rated: true });
	  }
  }
}
}		

function AddRecipeCheckedItemsToShoppingList() {
	var form = $('aspnetForm');
	
	var checkboxes = form.getInputs('checkbox', 'chkbxRecipeIngredients');
	var productPlacementCheckboxes = form.getInputs('checkbox', 'chkbxRecipeProductPlacements');
	
	var ingredients = [];
	var placementIngredients = [];

	checkboxes.each(function(e) { if (e.checked) ingredients.push(e.value); });
	productPlacementCheckboxes.each(function(e) { if (e.checked) placementIngredients.push(e.value); });

	if (document.getElementById('ctl00_hfRequestVars') != null) {
		var url = document.location.protocol + '//' + document.location.host;
		AddRecipeIngredientArrayToShoppingList(url, requestVariables, ingredients, HandleShoppingListResponse, null);
		
		if(placementIngredients.length > 0){
			GetAddRecipeProductPlacementArrayToShoppingList(url, requestVariables, placementIngredients, HandleShoppingListResponse, null);
		}
	}

	if (typeof(BK_AddClickEventParams) == 'function') {
		var itemsAdded = false;
		ingredients.each(function(e) { itemsAdded = itemsAdded | BK_AddClickEventParams(e, 5); });

		if (itemsAdded) {
			CallBKTag(false);
		}
	}
}

function AddAllRecipeIngredients() {

	var form = $('aspnetForm');
	var checkboxes = form.getInputs('checkbox', 'chkbxRecipeIngredients');
	var productPlacementCheckboxes = form.getInputs('checkbox', 'chkbxRecipeProductPlacements');

	checkboxes.each(function(e) { e.checked = true; });
	productPlacementCheckboxes.each(function(e) { e.checked = true; });

	if (document.getElementById('ctl00_hfRequestVars') != null) {
		AddRecipeCheckedItemsToShoppingList();
	}
}

function ShowHideElementsByClassName(name, show) {
	
	var elements = document.getElementsByClassName(name);

	for (i =0; i < elements.length; i++) {

		if (show) {
			elements[i].show();
		}
		else {
			elements[i].hide();
		}
	}
}

function ToggleMetric() {

	var showMetric = $('chkMetric').checked;

	if (showMetric) {
		
		ShowHideElementsByClassName('MetricDescription', true);
		ShowHideElementsByClassName('ImperialDescription', false);
	}
	else {
		
		ShowHideElementsByClassName('MetricDescription', false);
		ShowHideElementsByClassName('ImperialDescription', true);
	}
	
	refreshAdPods();
}