$(function() {
	$("body.faq #content dt span").click(function() {
		$(this).closest("dl").toggleClass("opened");
	})

	if ( $("#dishes").length) {
		$("#dishes").scrollable({
			circular: true,
			items: 'ul',
			activeClass: 'current',
			onBeforeSeek: function(){
				$("#dishes li.current").removeClass("current");
			},
			onSeek: function(Evt, N){
				var el = $("#dishes li:not(.cloned)")[N];
				$(el).addClass("current");
			}
		});
	}

	$('#comments fieldset span.stars').rating({
				'default':$('#comments input[name=rating]').val(),
				zeroStarsHl: true,
				click:function(Rate){$('#comments input[name=rating]').val(Rate)}
			});

	$(function(){
		$('dl.tabs dt').click(function(){
			$(this)
				.siblings().removeClass('selected').end()
				.next('dd').andSelf().addClass('selected');

			if($(this).nextAll("dt").length == 0) {
				$("#book .nextB").removeClass("next-on");
			} else {
				$("#book .nextB").addClass("next-on");
			}

			if($(this).prevAll("dt").length == 0) {
				$("#book .prevB").removeClass("prev-on");
			} else {
				$("#book .prevB").addClass("prev-on");
			}
		});

		$("#book .next-on").click(function() {
			var cur_tab = $("#book dt.selected");
			var cur_index = parseInt(str_replace("books-item-dt-", "", cur_tab.attr("id")));
			var next_index = cur_index + 1;
			var sub_next_index = cur_index + 2;

			if ($("#books-item-dt-"+next_index).length > 0) {
				$("#book .prevB").addClass("prev-on");

				$("#books-item-dt-"+cur_index).removeClass("selected");
				$("#books-item-dd-"+cur_index).removeClass("selected");

				$("#books-item-dt-"+next_index).addClass("selected");
				$("#books-item-dd-"+next_index).addClass("selected");

				if($("#books-item-dt-"+sub_next_index).length == 0) {
					$("#book .next-on").removeClass("next-on");
				}
			}
		})

		$("#book .prev-on").live("click", function() {
			var cur_tab = $("#book dt.selected");
			var cur_index = parseInt(str_replace("books-item-dt-", "", cur_tab.attr("id")));
			var prev_index = cur_index - 1;

			if ($("#books-item-dt-"+prev_index).length > 0) {
				$("#book .nextB").addClass("next-on");
				$("#books-item-dt-"+cur_index).removeClass("selected");
				$("#books-item-dd-"+cur_index).removeClass("selected");

				$("#books-item-dt-"+prev_index).addClass("selected");
				$("#books-item-dd-"+prev_index).addClass("selected");
			}

			if(prev_index < 2) {
				$("#book .prev-on").removeClass("prev-on");
			}
		})
	});

	$(function(){
		$("#menu>li").hover(function() {
			var menu_width = 0;
			$(this).find(".menu-second li").each(function(index) {
			    menu_width = menu_width + $(this).outerWidth()+10;
			});
			$(this).find(".menu-second ul").width(menu_width);
			$(this).find(".menu-second").width(menu_width);
		}, function() {
			$(this).find(".menu-second ul").width(0);
			$(this).find(".menu-second").width(0);
		});
	})

	$(function() {
		$(".products-nav ul li a").hover(function() {
			$(this).closest("li").siblings("li").removeClass("hover").find("span").removeClass("hover");
			$(this).closest("li").addClass("hover").find("span").addClass("hover");

		}, function() {
			$(this).closest("li").removeClass("hover").find("span").removeClass("hover");
		})
	})

	$(function() {
		$(".products-list a.more").click(function() {
			$(this).hide().closest("li").addClass("opened").find(".text").fadeIn(200);
			return false;
		})
	})

	$(function() {
		$(".products-list .bubble a.more").click(function() {
			$(this).hide().siblings("div.text").fadeIn(200);
			return false;
		})
	})
});

$(function() {
	function next(el) {
		var parent = el.parent('div.fact');
		var source = parent.attr('pp:source');
		var id = parent.attr('pp:id');

		if(source != undefined) {
			$.getJSON(source, {id: id},function(factData) {
				var p = parent.children('p');

				p.fadeOut(200, function() {
					p.html(factData['text']).fadeIn(200);
				});

				parent.attr('pp:id', factData['id']);
			});
		}
	}

	/* setup and load */
	$('div.fact>a').click(function() {
		next($(this));
		return false;
	});

	$("a.print").click(
		function() {
			window.print();
			return false;
		}
	);
});

$(document).ready(function(){
	$('.share-recipe-content .left-content form select').sSelect().change(function(){
		$(this).addClass($(this).find(":selected").attr("class"))
	});
	
	$(".share-recipe-content .left-content form .complexity .newList li:eq(0)").addClass("d1");
	$(".share-recipe-content .left-content form .complexity .newList li:eq(1)").addClass("d2");
	$(".share-recipe-content .left-content form .complexity .newList li:eq(2)").addClass("d3");
	
	$(".complexity .newList li").click(function(){
		var dIndex = $(this).index()+1;
		$(this).closest(".newListSelected")
			.find(".selectedTxt")
			.removeClass("d1")
			.removeClass("d2")
			.removeClass("d3")
			.addClass("d"+dIndex);
	});
	
	var complexitySelected = 0;
	
	$.each($("#recipe-complexity option"), function(key, value){
		
	   if($(value).attr("sel") == "sel") {
			complexitySelected = key+1;
		}
	});
	
	$(".complexity .selectedTxt").addClass("d"+complexitySelected);
	
	if($("body.root").size() > 0) {
		$("#new div:eq(0)").show();
		/*
		function showNewItem() {
			var prevIndex = -1;
			var size = $("#new div").size()-1;
			var index = prevIndex;
			
			while(index == prevIndex) {
				index = randomFromTo(0, size);
			}
			prevIndex = index;
			
			$("#new div").animate({
				opacity: 0
			}, 1000, function() {
				$("#new div").hide();
				
				var showItem = $("#new div:eq("+index+")");
				showItem.show();
				showItem.animate({
					opacity: 1
				}, 1000);
			});
		}
	
		showNewItem();
		
		setInterval(function() {
			showNewItem();
		}, 6000);
		
		*/
	}
});

function str_replace(search, replace, subject) {
	return subject.split(search).join(replace);
}

function randomFromTo(from, to){
	return Math.floor(Math.random() * (to - from + 1) + from);
}

var attributes = {
   wmode: "transparent"
};

swfobject.embedSWF("/swf/map.swf", "mir-map", "100%", "810", "9.0.0","expressInstall.swf", {}, attributes, attributes);
swfobject.embedSWF(
	'/swf/techno.swf',
	"factory-flash",
	"646",
	"411",
	"10.0.0",
	"expressInstall.swf",
	{},
	{ "wmode": "window", "allowScriptAccess": "sameDomain" },
	{}
);
