$(document).ready(function(){
	
	$('#js_warning').remove();

	$("#contact_form").append('<input type="hidden" id="sec" name="sec" value="true" />');

	
	if(typeof(page) == 'undefined')
	{	/* if Environment is NOT Editor */
		$(".pristabell tr").mouseover(function() {$(this).addClass("over");}).mouseout(function() {$(this).removeClass("over");});
		$(".pristabell tr:even").addClass("even");
		$(".pristabell tr:odd").addClass("odd");
		var th = $(".pristabell tr").get(0);
		$(th).removeClass('even')
		$(th).addClass("th");
	}

	
	
	
	$(document.body).bind("click", function()
	{
		if($("#search_tip").length > 0)
		{
			$("#search_tip").remove();
		}
	})
	
	$("#search_word")
		.bind("click", function()
		{
			if($(this).val() == 'Sök på webbplatsen...' || $(this).val() == 'Search the website...')
			{
				$(this).val('');
				$(this).removeClass("display");
				$(this).addClass("write");
			}
		})
		.bind("keyup", function(e)
		{
			if($(this).val() != '' && $(this).val() != 'Sök på webbplatsen...' && $(this).val() != 'Search the website...' )
			{
				$("#search_clear").show();
			}
			
			if($(this).val() != ''&& $(this).val().length > 2)
			{
				$("#search_tip .content").empty();
				/* search_query */
				var query_string = 	'search_word='+ escape($("#search_word").val()) + 
									'&site_id='+ tmpl_page.site_id +
									'&site_language='+ tmpl_page.language;
				if($("#search_tip").length < 1)
				{
				  		var pos = $("#search_word").offset();  
				  		var width = $("#search_word").width();
						/* search_tip HTML */
						var search_tip_html = '<div id="search_tip">'+
							'<div class="header"></div>'+
							'<div class="content"></div>'+
							'<div class="footer"></div></div>';
				  		
				  		$(search_tip_html).insertBefore("#subnav");
				  		$("#search_tip").css( { "left": (pos.left - 142) + "px", "top":pos.top+ 20 + "px" } );
				}
									
									
				$.ajax({
					type: "POST",
					url: "/System/Templates/Sites/166/php/search_results.php",
					data: ""+query_string+"",
					success: function(s)
					{
						if(s == '')
						{
							$("#search_tip").hide();
						}
						else
						{
							$("#search_tip .content").html(s);
				  			$("#search_tip").show();
						}
					}
				});
			}
			else if($(this).val().length < 3)
			{
				$("#search_tip").hide();
			}
			
		})
		.bind("blur", function() 
		{
			if(typeof(tmpl_page) != 'undefined')
			{
				if($(this).val() == '')
				{
					if(tmpl_page.language == 'EN')
					{
						$(this).val('Search the website...');
					}
					else
					{
						$(this).val('Sök på webbplatsen...');
					}
					$(this).removeClass("write"); 
					$(this).addClass("display");
				}
			}
		});
		
	$("#search_clear")
		.hide()
		.bind("click", function()
		{
			$("#search_word").val(''); 
			$("#search_tip").remove();
			
			return false;
		});

		$('a.contact').each(function()
		{
			var host = window.location.protocol+'//'+window.location.host;
			var b = 'eon-se';
			var a = $(this).attr("href");
			a = a.replace(host,'');
			a = a.replace('/','@');
			b = b.replace('-','.');
			$(this).attr("href",'mailto:'+a+b);
		});
		
		
});

function newsletter_subscribe()
{
	var qs = 'subscriber_name=' + $("#subscriber_name").val() + '&subscriber_email=' + $("#subscriber_email").val() + '&HttpRequest=true';
	$.ajax({
		type: "POST",
		url: "/System/Templates/Sites/166/php/newsletter_subscribe.php",
		data: ""+qs+"",
		success: function(msg)
		{
			if(msg == 'error')
			{
				$("#newsletter_info").html('<p class=\"alert\">Ett fel uppstod med registreringen. Var vänlig försök igen lite senare.</p>')
			}
			else if(msg == 'valid_false')
			{
				$("#newsletter_info").html('<p class=\"alert\">Var vänlig fyll i Namn och E-postadress, och försök igen.</p>')
			}
			else if(msg == 'subscriber_exists')
			{
				$("#newsletter_info").html('<p class=\"alert\">E-postadressen finns redan registrerad!</p>')
			}
			else if(msg == 'subscriber_created')
			{
				$("#newsletter_info").html('<p><b>Tack för din registrering</b>, du är nu prenumerant på vårt nyhetsbrev!</p>')
				$("#subscriber_name").val('');
				$("#subscriber_email").val('')
			}
		}
	});
	return false;
}


