$(document).ready(function(){
	
	initMenu();
	
	$("#Menu select.category").change(function(){
		dow = $(this).parent().attr('dow');
		month = $("#Menu").attr('month');
		year = $("#Menu").attr('year');
		//window.location= WEB_ROOT+"ajax/menu/setPreference/"+year+"/"+month+"/"+dow+"/"+$(this).val();
		$.get(WEB_ROOT+"ajax/menu/setPreference/"+year+"/"+month+"/"+dow+"/"+$(this).val(),function(data){randomAll(dow);});
	});
	
	$("#MenuGenerate img.toggle").click(function(){
		if ($(this).siblings("input[type='radio']").attr("checked") == "") {
			$(this).siblings("input[type='radio']").attr("checked", "checked");
		} else {
			$(this).siblings("input[type='radio']").attr("checked", "");
		}
		menu_update_totals();
	}).css({cursor: 'pointer'});
	

	$("#MenuGenerate tr.Total input[type='text']").keyup(function(){
		menu_update_totals();
	});
	
	$("#MenuGenerate input[type='reset']").click(function(){
		$("#MenuGenerate input[type='radio']").attr("checked","");
		menu_update_totals();
	});

/*
	var ddlObj2=$("#DifficultySelector").finalselect({id:"kk"});
	
	ddlObj2.addItem('<span class="thistext">Easy</span>','');
	ddlObj2.addItem('<span class="thistext">Hard</span>','');
*/	

});

function menu_update_totals()
{
	$("#MenuGenerate [altsrc]").each(function(){
		if (!$(this).attr('origsrc'))
		{
			$(this).attr('origsrc',$(this).attr('src'));
		}
	});
	$("#MenuGenerate input[type='radio'][checked!=false]").each(function(){
		$(this).siblings("img").attr('src',$(this).siblings("img").attr('altsrc'));
	});
	$("#MenuGenerate input[type='radio'][checked!=true]").siblings("img").each(function(){
		$(this).attr('src',$(this).attr('origsrc'));
	});
	//$("#MenuGenerate tr.Total input[type='text']").val($("#MenuGenerate input[type='radio']").siblings("img").size() - $("#MenuGenerate input[type='radio'][checked!=true]").siblings("img").size());
		
}



function initMenu()
{
	$("#Menu .random").unbind('click');
	$("#Menu .random").click(loadIntoParent);
	$("#Menu td.day .select").click(function(){
		openPopup({
			"url":$(this).attr('href'),
			"width":800,
			"height":500,
			"func":menu_prepareReplaceList,
			"clickToClose":true
			});
		return false;
	});
	
	$("#Menu td.day *").removeClass("hidden");

	$("#Menu td.day .view").unbind('click');
	$("#Menu td.day .view").click(function(){
		openPopup({
			"url":$(this).attr('href'),
			"width":585,
			"height":500,
			"func": function(){
				recipe_wine_init();
				popup_addClose();
			},
			"clickToClose":true
			});
		return false;
	});
	
	$("#Menu tr.Key li.viewmode a").unbind('click');
	$("#Menu tr.Key li.viewmode a").click(function(){
		$("#MenuContainer").load($(this).attr('href'),function(){initMenu();});
		return false;
	});
	$("#MenuData a").unbind('click');
	$("#MenuDate a").click(function(){
		$("#MenuContainer").load($(this).attr('href'),function(){initMenu();});
		return false;
	});
	
	$("#Menu .randomall").unbind('click');
	$("#Menu .randomall").click(function(){
		dow = $(this).parent().attr('dow');
		randomAll(dow);
		return false;
	});
	
	/* Style fixes */
	$("#Menu > tbody > tr").each(function(){$(this).children("td:last").css({"border-right":"0px"});});
	$("#Menu tr.Days:last").find("td").css({"border-bottom":"0px"});

	$("a.favourite").unbind('click');
	$("a.favourite").click(function(){
		if (HVEIM.user_id == null) {
			popup_notloggedin();
			return false;
		}
		if ($(this).hasClass('favourited'))
		{
			$(this).removeClass('favourited');
		}
		else
		{
			$(this).addClass('favourited');
		}
		var img = $(this).children('img');
		var tsrc = img.attr('src');
		img.attr('src',img.attr('altsrc')).attr('altsrc',tsrc);
		$.get($(this).attr('href'));
		var thref = $(this).attr('href');
		$(this).attr('href',$(this).attr('althref')).attr('althref',thref);
		return false;
	});
	
	if ($("#Menu").size() > 0 && HVEIM.autoScroll)
	{
		var targetOffset = $("#Menu").offset().top;
		$('html,body').animate({scrollTop: targetOffset}, 1000);
	}
}

function menu_loadReplaceList(categoryid)
{
	day = $("#menuReplaceDay").val();
	month = $("#Menu").attr('month');
	year = $("#Menu").attr('year');
	//window.location= WEB_ROOT+"ajax/menu/setPreference/"+year+"/"+month+"/"+dow+"/"+$(this).val();
	//$.get(WEB_ROOT+"ajax/menu/setPreference/"+year+"/"+month+"/"+dow+"/"+$(this).val(),function(data){randomAll(dow);});
	$("#popupWindow").load(WEB_ROOT+"ajax/menu/getNewRecipe/"+year+"/"+month+"/"+day+"/"+categoryid,menu_prepareReplaceList);
}

function menu_prepareReplaceList()
{
	$("#menuReplaceLeft select.category").change(function(){
		menu_loadReplaceList($(this).val());
	});
	$("#menuReplaceList a").click(function(){
		$("#menuReplaceRight").load($(this).attr('href'),menu_prepareReplaceListRecipeLoaded);
		$("#menuReplaceRecipeId").val($(this).attr('recipeid'));
		return false;
	});
}
function menu_prepareReplaceListRecipeLoaded()
{
	$("#menuReplaceRight div.Left").prepend("<input type='button' value='Velja' class='menuReplaceSelect'>");
	//$("#menuReplaceRight div.Footer").html("<input type='button' value='Velja' class='menuReplaceSelect'>");
	
	$("input.menuReplaceSelect").click(function(){
		year = $("#Menu").attr('year');
		month = $("#Menu").attr('month');
		day = $("#menuReplaceDay").val();
		recipeid = $("#menuReplaceRecipeId").val();
		//window.location = WEB_ROOT+"ajax/menu/set_recipe/"+year+"/"+month+"/"+day+"/3/"+recipeid;
		$("#Menu td[day="+day+"]").load(WEB_ROOT+"ajax/menu/set_recipe/"+year+"/"+month+"/"+day+"/3/"+recipeid,initMenu);
		closePopup();
	});
}



function loadIntoParent()
{
	$(this).parents("td.day").load($(this).attr('href'),function(data){initMenu();});
	return false;
}

function randomAll(dow)
{
	$("#Menu td[dow="+dow+"]").each(function(){
		$(this).load($(this).find(".random").attr('href'),function(){initMenu();});
	});
}
