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 sendToFriend()
{
	$.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_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,2) == "en" || pathname.substr(1,2) == "ru") ? "/" + pathname.substr(1,2) : "") +  "/_search/" + query;
	}
}

function mailt(str1,str2,el)
{
	el.href = 'mailto:' + str1 + '@' + str2;
}

jQuery.exists = function(selector) {return ($(selector).length > 0);}


$(function(){

	root_path = location.protocol + "//" + location.host;
	ajax_path = root_path + "/_ajax";
	pathname = location.pathname;

	//open banner in new window
	$('.target_blank').click(function(){

		window.open(this.href);
		return false;
	});

	$('.lightbox').lightBox();
	
});
