Cufon.replace('p.button a');
Cufon.replace('div.header ul li a');
Cufon.replace('h1');
Cufon.replace('h2');

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', 
			left:        '50%', 
			top:        '50%', 
			zIndex:        '1000'
		}).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: 10
		}).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 gallupVote()
{
	if (!$.exists("#js_gallup")){
		return false;
	}

	if (($("[name=js_gallupvote]:checked").val()) != 'undefined'){
		gallupid = parseInt($("#js_gallup_id").val());
		nr = parseInt($("[name=js_gallupvote]:checked").val());
		if (!gallupid || !nr){
			return false;
		}
		$("#js_gallup").fadeOut("normal", function(){
			$.ajax({
				url: ajax_path + "gallup/vote/gallupid/" + gallupid + "/nr/" + nr,
				cache: false,
				success: function(html){
					if (html != 'error'){
						$("#js_gallup").html(html);
						Cufon.replace('p.button a');
						$("#js_gallup").fadeIn("normal");
					}
				}
			});
		});
	}

}


function sendToFriend()
{
	$.dimScreen(300, 0.7, function() {
		$("#js_sendfriend").center().show();
		genCaptcha();
		return false;
	});
}

function genCaptcha()
{

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

}

function showFlash2()
{
	var flashvars = {
		xmlUrl: "flash/retseptibanner/recipes.xml"
	};
	var params = {
	  menu: "false",
	  wmode: "opaque"
	};
	var attributes = {
	  id: "flashContent",
	  name: "flashContent"
	};
	swfobject.embedSWF("flash/retseptibanner/swf/recipe_rotator.swf", "altContent2", "650", "300", "9.0.0", "swf/expressInstall.swf", flashvars, params, attributes);
}

function sendToFriendSubmit()
{
	$("#js_send_button").fadeOut("fast");
	ajax_url = ajax_path + "sendtofriend/";
	$.post(ajax_url, 
		{
			path: $("#js_path").val(), 
			name: $("#js_name").val(),
			email: $("#js_email").val(),
			friend1: $("#js_friend1").val(),
			friend2: $("#js_friend2").val(),
			friend3: $("#js_friend3").val(),
			captcha: $("#js_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;
						if (field == 'captcha' && status == 'validate') {
							$("#js_" + field + "_label").hide();
							$("#js_" + field + "_label_wrong").show();
						}
						$("#js_" + field + "_label").css('color', 'red');
					} else {
						$("#js_" + field + "_label").css('color', 'black');
					}
				});
				if (success && result.returnhtml){
					$("#js_sendtofriend_content").html(result.returnhtml);
				}
			}
		},
		"json"
	);
}

function doSearch() 
{
	query = $("input#js_search").val();
	if (query.length>1) {
		query = encodeURI(query);
		query = query.replace(/\//g, "-fs-");
		query = query.replace(/\./g, "-dot-");
		window.location = root_path + ((pathname.substr(1,3) == "eng" || pathname.substr(1,3) == "rus") ? "/" + pathname.substr(1,3) : "") +  "/_search/" + query;
	}
}


$(function(){
	
	if (location.href.match('_saada_sobrale')) {
		sendToFriend();
	}

	root_path = location.protocol + "//" + location.host;
	ajax_path = root_path + "/_ajax/";
	pathname = location.pathname;

	if ($.exists("#js_frontbanner") && $("#js_langcode").val()){

		var flashvars = {
			local: "false",
			swfPrefix: "flash/",
			xmlUrl: "flash/content_" + $("#js_langcode").val() + ".xml"
		};
		var params = {
			menu: "false",
			wmode: "opaque"
		};
		var attributes = {
			id: "flashContent",
			name: "flashContent"
		};
		swfobject.embedSWF("flash/campaigns_loader.swf", "js_frontbanner", "650", "360", "9.0.0", "swf/expressInstall.swf", flashvars, params, attributes);
		
		$('#js_sendtofriend_content').find('input').each(function(i){
			if ($(this) == $('#js_captcha')){
				$(this).bind('keypress', function(e) {
					if (e.keyCode == 13) {
						alert('nii');
						sendToFriendSubmit();
						return false;
					}
				});
			} else {
				$(this).bind('keypress', function(e) {
					if (e.keyCode == 13) {
						alert('nii');
						$(this).next().focus();
						return false;
					}
				});
			}
		});

	}

	$(document).keyup(function(event){
		if (event.keyCode == 27) {
			if($("#js_sendfriend").css('display') == 'block'){
				$.dimScreenStop(); 
				$("#js_sendfriend").hide();
			}
		}
	});

});
