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:        '80%', 
			top:        '30%', 
			zIndex:        '100'
		}).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 getPopup() {
	if ($.browser.msie && $.browser.version.substr(0,1)<8) {
		$("#popup").center().show();
		$("#captcha_content").show();
		return false;
	} else {
		$.dimScreen(300, 0.6, function() {
			$("#popup").center().show();
			$("#captcha_content").show();
			return false;
		});
	}
}

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

function toggleDefList() {
	var dt = $(this).parent();
	var dd = dt.next();
	if (dt.hasClass('active')) {
		dd.hide();
		dt.removeClass('active');
	}
	else {
		dt.addClass('active');
		dd.show();
	}
	return false;
}

function toggleTermsList() {
	var li = $(this).parent();
	if (li.hasClass('active'))
	{
		li.children('span').hide();
		li.removeClass('active');
	}
	else
	{
		li.addClass('active');
		li.children('span').show();
	}
	return false;
}

function getContacts(domain, reserved, print) {

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

	ajax_url = ajax_path + "retrieve_contacts/";
	$.post(ajax_url, 
		{
			domain: domain, 
			captcha: $("#js_captcha").val(),
			lang: lang,
			reserved: reserved
		}, 
		function(result){
			if (result == 'captcha') {
				$('#captcha_error').show();
				$('#js_captcha').css('border-color', 'red');
				if (print == 1) {
					window.print();
				}
			} else {
				$("#captcha").fadeIn("fast");
				success = true;
				$('#captcha_content').hide();
				$('#contact').show();
				$('#contact').html(result);

				$('#hidden_captcha').val($("#js_captcha").val());
				if (print == 1) {
					window.print();
				}
			}
		},
		"html"
	);
	return false;
}

$(function(){

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

	$('input[name="_id"]').bind('keypress', function(e) {
		if (e.keyCode == 13) {
			document.domainsearch.submit();
		}
	});
	$('input[name="_captcha"]').bind('keypress', function(e) {
		if (e.keyCode == 13) {
			document.domainsearch.submit();
		}
	});
	
	$("#halduskontakt").hide();
	$("#tehniline-kontakt").hide();
	
	$("a[href$='#tehniline-kontakt']").bind('click', function(){
			$('#halduskontakt').hide();
			$("#tehniline-kontakt").toggle();
			return false;
	});
		
	$("a[href$='#halduskontakt']").bind('click', function(){
			$("#tehniline-kontakt").hide();
			$('#halduskontakt').toggle();
			return false;
	});
	
	$('#reasons dt a').bind('click', toggleDefList)
	$('#terms li a').bind('click', toggleTermsList)
});

