Cufon.replace('ul.nav a', { fontFamily: 'slate', hover: true } );
Cufon.replace('ul.lang a', { fontFamily: 'slate_cond', hover: true } );
Cufon.replace('h1', { fontFamily: 'slate_medium', hover: true } );
Cufon.replace('.slate', { fontFamily: 'slate', hover: true } );
Cufon.replace('.slate_medium', { fontFamily: 'slate_medium', hover: true } );
Cufon.replace('div.box p.title', { fontFamily: 'slate_medium'} );
Cufon.replace('div.content div.content_inner div.center p .text-on-image', { fontFamily: 'slate_medium'} );
Cufon.replace('.bickham', { fontFamily: 'bickham', hover: true } );
Cufon.replace('.warnock', { fontFamily: 'warnock', hover: true } );




jQuery.exists = function(selector) {return ($(selector).length > 0);}

jQuery.fn.center = function (absolute) {
	return this.each(function () {
		var t = jQuery(this);
		t.css({
			position:    absolute ? 'absolute' : 'fixed', 
			left:        '50%', 
			top:        '50%', 
			zIndex:        '99'
		}).css({
			marginLeft:    '-' + (t.outerWidth() / 2) + 'px', 
			marginTop:    '-' + (t.outerHeight() / 2) + 'px'
		});

		if (absolute) {
			t.css({
				marginTop:    parseInt(t.css('marginTop'), 10) + jQuery(window).scrollTop(), 
				marginLeft:    parseInt(t.css('marginLeft'), 10) + jQuery(window).scrollLeft()
			});
		}
	});
};

//dimScreen()
//by Brandon Goldman
jQuery.extend({
	//dims the screen
	dimScreen: function(speed, opacity, callback) {
		if(jQuery('#__dimScreen').size() > 0) return;
		
		if(typeof speed == 'function') {
			callback = speed;
			speed = null;
		}

		if(typeof opacity == 'function') {
			callback = opacity;
			opacity = null;
		}

		if(speed < 1) {
			var placeholder = opacity;
			opacity = speed;
			speed = placeholder;
		}
		
		if(opacity >= 1) {
			var placeholder = speed;
			speed = opacity;
			opacity = placeholder;
		}

		speed = (speed > 0) ? speed : 500;
		opacity = (opacity > 0) ? opacity : 0.5;
		return jQuery('<div></div>').attr({
				id: '__dimScreen'
				,fade_opacity: opacity
				,speed: speed
			}).css({
			background: '#000'
			,height: $(document).height() + "px"
			,left: '0px'
			,opacity: 0
			,position: 'absolute'
			,top: '0px'
			,width: '100%'
			,zIndex: 95
		}).appendTo(document.body).fadeTo(speed, opacity, callback);
	},
	
	//stops current dimming of the screen
	dimScreenStop: function(callback) {
		var x = jQuery('#__dimScreen');
		var opacity = x.attr('fade_opacity');
		var speed = x.attr('speed');
		x.fadeOut(speed, function() {
			x.remove();
			if(typeof callback == 'function') callback();
		});
	}
});

function mailt(str1,str2,el)
{
	el.href = 'mailto:' + str1 + '@' + str2;
}


var submenu_timeout = null;
var submenu_open = null;

function hideSubmenu() {
	$('ul#menu').find('div.submenu').hide();
	clearTimeout(submenu_timeout);
	if (submenu_open) {
		submenu_open.show();
	}
}


function doSearch() 
{
	query = $('input[name="_search"]').val();
	if (query.length>1) {
		query = encodeURI(query);
		query = query.replace(/\//g, "-fs-");
		query = query.replace(/\./g, "-dot-");
		window.location = root_path + langpath +  "/_search/" + query;
	}
	return false;
}

function filterProducts()
{

	filter = $("#js_productfilter").val();
	if (!filter){
		return false;
	}

	currentpath = root_path + pathname;

	if (currentpath.match(/\/_filter\//)){
		location.href = currentpath.replace(/\/_filter\/(.*)/i, ((filter != 'all') ? "/_filter/" + filter : ''));
	} else if (filter != 'all') {
		location.href = currentpath + "/_filter/" + filter;
	}

	return false;

}


function getComments(id, domain_id, lang_id, page)
{

	if (!id){
		id = $("#js_recipe_id").val();
		domain_id = $("#js_domain_id").val();
		lang_id = $("#js_lang_id").val();
		page = 1;
	}

	$.ajax({
		url: ajax_path + "getcomments/id/" + id + "/domain_id/" + domain_id + "/lang_id/" + lang_id + "/page/" + page,
		success: function(html) {
			$(".comments").html(html);
			Cufon.replace('.slate', { fontFamily: 'slate', hover: true } );
		},
		dataType: "html"
	});

}


function addComment()
{

	$("#js_send_button").hide();
	$("#js_send_loading").width($("#js_send_button").width());
	$("#js_send_loading").show();

	var params = {
			ID : $("#js_recipe_id").val(), 
			domain_id : $("#js_domain_id").val(), 
			lang_id : $("#js_lang_id").val()
	};
	$(".comment_form")
		.find("input[type='text'], textarea")
		.filter(":enabled")
		.each(function() {
			params[this.name] = this.value;
		});
	$.post(ajax_path + "addcomment/",
		params, 
		function(result){
			$("#js_send_loading").hide();
			$("#js_send_button").show();
			success = true;
			if (typeof(result)=="object"){
				$.each(result.status, function(field, status){
					if (status!="ok"){
						success = false;
						if (field == "comment"){
							$("#js_" + field).addClass("textareaerror");
						} else {
							$("#js_" + field).addClass("error");
						}
						if (field == 'comment' && status == "maxlength"){
							$("#js_comment_error").show("fast");
						}
					} else {
						$("#js_" + field).removeClass("error");
						$("#js_" + field).removeClass("textareaerror");
						if (field == "comment"){
							$("#js_comment_error").hide("fast");
						}
					}
				});
				if (success){
					//$(".comment_form").hide("normal", function(){
						getComments();
					//});
				}
			}
		},
		"json"
	);

	return false;
}


function genCaptcha()
{
	$('.js_captcha_img').attr('src', root_path + '/_cms/captcha/?fkasj=' + Math.floor(Math.random()*11)); 
	return false;
}


function toggleSend_to_friend()
{
	if($("#js_sendfriend").css('display') == 'block'){
		$.dimScreenStop(); 
		$("#js_sendfriend").hide();
	} else {
		$.dimScreen(300, 0.7, function() {
			$("#js_sendfriend").center().show();
			genCaptcha();
			return false;
		});
	}
}


function sendToFriendSubmit()
{
	$("#js_send_button").fadeOut("fast");
	ajax_url = ajax_path + "sendtofriend/";
	$.post(ajax_url, 
		{
			path: $("#js_sf_path").val(), 
			name: $("#js_sf_name").val(),
			email: $("#js_sf_email").val(),
			friend1: $("#js_sf_friend1").val(),
			friend2: $("#js_sf_friend2").val(),
			friend3: $("#js_sf_friend3").val(),
			captcha: $("#js_sf_captcha").val()
		}, 
		function(result){
			$("#js_send_button").fadeIn("fast");
			success = true;
			if (typeof(result)=="object"){
				if (result.error == 'mail_error'){
					$("#js_send_error").show();
				} else {
					$("#js_send_error").hide();
				}
				$.each(result.status, function(field, status){
					if (status!="ok"){
						success = false;
						$("#js_sf_" + field).addClass('error');
					} else {
						$("#js_sf_" + field).removeClass('error');
					}
				});
				if (success && result.returnhtml){
					$("#send_button").hide();
					$("#js_sendtofriend_content").html('<p style="margin-left:20px;">' + result.returnhtml + '</p>');
				}
			}
		},
		"json"
	);
}


$(function(){
	if ($.exists(".slideshow")){
		$('.slideshow').cycle({
			fx: 'fade',
			timeout: 6000, 
			speed: 1000
		});
	}


	root_path = location.protocol + "//" + location.host;
	ajax_path = root_path + "/_ajax/";
	pathname = location.pathname;
	langpath = $("#js_langpath").val();

	$('input[name="_search"]').bind('keypress', function(e) {
		if (e.keyCode == 13) {
			doSearch();
			return false;
		}
	});

	$('a.js_link_new_window').click(function(){
		window.open(this.href);
		return false;
	});

	submenu_open = $('ul#menu').children('li').find('div.submenu:visible');
	$('ul#menu').children('li').hover(
		function() {
			if (submenu_timeout) {
				clearTimeout(submenu_timeout);
			}
			$('ul#menu').find('div.submenu').hide();
			if ($(this).children('div.submenu').length > 0) {
				$(this).children('div.submenu').show();
			}
		},
		function() {
			submenu_timeout = setTimeout('hideSubmenu()',500);
		}
	);

	if ($.exists(".comments")){
		getComments($("#js_recipe_id").val(), $("#js_domain_id").val(), $("#js_lang_id").val(), 1);
	}

	if ($.exists("#js_sendfriend")){
		$(document).keyup(function(event){
			if (event.keyCode == 27) {
				toggleSend_to_friend();
			}
		});
	}

	if ($.exists(".add_to_favourites")){
		if ($.browser.msie || $.browser.mozilla || $.browser.opera){
			$('.add_to_favourites').jFav();
		} else {
			$(".add_to_favourites").hide();
		}
	}

	if ($.exists("#js_recipes_flash")){
		var flashvars = {
			xmlUrl: root_path + "/xml/recipes_" + $("#js_domain").val() + "_" + $("#js_current_lang").val() + ".xml"
		};
		var params = {
			menu: "false",
			wmode: "transparent"
		};
		var attributes = {
			id: "js_recipes_flash",
			name: "recipes"
		};
		swfobject.embedSWF(root_path + "/gfx/recipes.swf", "js_recipes_flash", "544", "212", "9.0.0", false, flashvars, params, attributes);
	}

});