if(jQuery) {
	/*
	 * Less code repetition, also stop animation (if any) so they can't queue up.
	 */
	jQuery.fn.nhHover = function(x) {
		if(x == undefined)
			x = 0.6;
		$(this).hover(
			function() {
				$(this).stop();
				$(this).fadeTo('fast', x);
			},
			function() {
				$(this).stop();
				$(this).fadeTo('fast', 1);
			}
		);
	}
}

$(function()
{
	$("DIV.closeEmbed A").click( function() {
		$("DIV#embeddedContent").hide();
		$("DIV.Product:hidden").show();
		$("DIV.Products:hidden").show();
		$("DIV.closeEmbed").hide(500);
		$(".embeddedMenuLink").removeClass('embeddedMenuLinkSelected');
		$(".embeddedMenuLink").css('textDecoration','underline');
		$(".embeddedMenuLink").css('color','#00326d');
	});
	$("A.embeddedMenuLink").click( function() {
		var id = $(this).attr('rel');
		$.ajax({url: '/ajax.function.php', type: 'GET', dataType: 'html', data:'content=' + id, success: function(data) {
			$("DIV#embeddedContent").html(data);
			$("DIV.Product:visible").hide();
			$("DIV.Products:visible").hide();
			$("DIV#embeddedContent").show();
			$("DIV.closeEmbed").show(500);
		}});
		$(".embeddedMenuLink").removeClass('embeddedMenuLinkSelected');
		$(".embeddedMenuLink").css('textDecoration','underline');
		$(".embeddedMenuLink").css('color','#00326d');
		$(this).addClass('embeddedMenuLinkSelected');
		$(this).css('textDecoration','none');
		$(this).css('color','#424143');
	});
	$(".Language div").nhHover();
	$(".Language div").click(
	function()
	{
		/*if($(this).attr('rel') == "en")
		{
			go('http://borgarplast.com/en');
		}else if($(this).attr('rel') == "is")
		{
			go("http://www.borgarplast.is");
		}else if($(this).attr('rel') == "fr")
		{
			go("http://www.borgarplast.com/franski/index.html");
		}else
		{
			go('?lang='+$(this).attr('rel'));
		}
		*/
		switch($(this).attr('rel'))
		{
			case "is" :
				go("http://www.borgarplast.is");
			break;
			case "en" :
				go('http://borgarplast.com/en');
			break;
			case "fr" :
				go("http://www.borgarplast.com/franski/index.html");
			break;
			case "es" :
				go("http://www.borgarplast.com/spaenski/index.html");
			break;
			default : go('?lang='+$(this).attr('rel'));
		}
	});
	$(".pdfdownload_icon").nhHover();
	$(".pdfdownload_icon_small").nhHover();
	$(".pricecalc_icon").nhHover();
	$(".mailsubmit").nhHover();
	$(".VideoTitle").nhHover();
	$(".printbutton").nhHover();
	$(".sendMessage").nhHover();
	$(".ImageHolder").nhHover(0.7);
	$(".productthumb").nhHover(0.7);
	$(".fade_me").nhHover(0.7);
	$("DIV.back_button_icon").nhHover();
	$("#Cart").nhHover(0.7);
	$(".comparebutton").nhHover();
	$(".productthumb").click(
		function()
		{
			var relitem = $(this).attr('rel');
			$('#LargeImg').fadeTo('fast',0,
			function() {
				$('#LargeImg').attr('src','/thumb.php?file=/'+relitem+'&w=670&h=383');
			});
			$('#LargeImg').load(function () {
				$('#LargeImg').fadeTo('fast',1);
			});
		}
	);
	$("a.prodDetail").hover(
		function()
		{
			$(this).children('div').stop();
			$(this).children('div').fadeTo('fast',0.7);
		},
		function()
		{
			$(this).children('div').stop();
			$(this).children('div').fadeTo('fast',1);
		}
	);
	$("a.anchorItem").hover(
		function()
		{
			$(this).children('div').stop();
			$(this).children('div').fadeTo('fast',0.7);
		},
		function()
		{
			$(this).children('div').stop();
			$(this).children('div').fadeTo('fast',1);
		}
	);
	
	$(".productRow").hover(
		function()
		{
			$(this).css({backgroundColor:"#e5eaf0"});
		},
		function()
		{
			$(this).css({
				backgroundColor: "#FFFFFF"
			});
		}
	);
	$('#addtolist').click(
		function()
		{
			$.get('/session.php?addtosession='+$(this).attr('rel'),
				function(data)
				{
					$('#addtolist').fadeOut('fast',function()
					{
						$('#removefromlist').fadeIn('fast');
					});
					$('#Cart .carttext').fadeOut('fast',
						function()
						{
							$('#Cart .carttext').html(priceCalcText+' ('+data+') '+items_text);
							$('#Cart .carttext').fadeIn('slow');
							$('#Cart').removeClass('Cart noitems').addClass('Cart');
						}
					);
				}
			);			
		}
	);
	$('#removefromlist').click(
		function()
		{
			$.get('/session.php?removefromsession='+$(this).attr('rel'),
				function(data)
				{
					$('#removefromlist').fadeOut('fast',function()
					{
						$('#addtolist').fadeIn('fast');
					});
					$('#Cart .carttext').fadeOut('fast',
						function()
						{
							$('#Cart .carttext').html(priceCalcText+' ('+data+') '+items_text);
							$('#Cart .carttext').fadeIn('slow');
							
						}
					);
					if(data == 0)
					{
						$('#Cart').addClass('noitems');
					}
				}
			);			
		}
	);
	$('.addToList').click(
		function()
		{
			var item = $(this).attr('rel');
			$.get('/session.php?addtosession='+item,
				function(data)
				{
					$('#add_button_'+item).slideUp('fast');
					$('#remove_button_'+item).slideDown('fast');
					$('#Cart .carttext').fadeOut('fast',
						function()
						{
							$('#Cart .carttext').html(priceCalcText+' ('+data+') '+items_text);
							$('#Cart .carttext').fadeIn('slow');
							$('#Cart').removeClass('Cart noitems').addClass('Cart');
						}
					);
				}
			);			
		}
	);
	$('.removeFromList').click(
		function()
		{
			var item = $(this).attr('rel');
			$.get('/session.php?removefromsession='+item,
				function(data)
				{
					$('#add_button_'+item).slideDown('fast');
					$('#remove_button_'+item).slideUp('fast');
					$('#Cart .carttext').fadeOut('fast',
						function()
						{
							$('#Cart .carttext').html(priceCalcText+' ('+data+') '+items_text);
							$('#Cart .carttext').fadeIn('slow');
						}
					);
					if(data == 0)
					{
						$('#Cart').addClass('noitems');
					}
				}
			);			
		}
	);

	$('.removeRow').click(
		function()
		{
			var item = $(this).attr('rel');
			$.get('/session.php?removefromsession='+item,
				function(data)
				{
					$('#calc_row_'+item).fadeOut('fast',
						function()
						{
							$('#calc_row_'+item).remove();
						}
					);
					$('#Cart .carttext').fadeOut('fast',
						function()
						{
							$('#Cart .carttext').html(priceCalcText+' ('+data+') '+items_text);
							$('#Cart .carttext').fadeIn('slow');
							
							if(data < 1)
							{
								$('#requestform').fadeOut('fast');
							}
						}
					);
				}
			);			
		}
	);
	
	$('.errorMsg').click(
		function()
		{
			$(this).slideUp('slow');
		}
	);
	$('.systemMsg').click(
		function()
		{
			$(this).slideUp('slow');
		}
	);
});

function checkMessage()
{
	var name = $('#name').val();
	var company = $('#company_name').val();
	var email = $('#email').val();
	var phone = $('#phone').val();
	
	var error = "";
	
	if(name == "")
	{
		error += name_must_be_set+"<br>\n";
	}
	if(company == "")
	{
		error += company_must_be_set+"<br>\n";
	}
	if(email == "")
	{
		error += email_must_be_set+"<br>\n";
	}
	if(email.indexOf('@') == -1)
	{
		error += email_must_be_valid+"<br>\n";
	}
	if(phone == "")
	{
		error += phone_must_be_valid+"<br>\n";
	}
	
	if(error != "")
	{
		displayError("massageError",error);
		return false;
	}else
	{
		return true;
	}
}

function displayError(errorDisplayer,errorMsg)
{
	$('#'+errorDisplayer).html(errorMsg);
	$('#'+errorDisplayer).slideDown('fast');
}

function checkMailform()
{
	var email = $('#email').val();
	if(email == "")
	{
		displayError('mailError','Email must be set');
		return false;
	}else if(email.indexOf('@') == -1)
	{
		displayError('mailError','Must be a valid email address');	
		return false;
	}else
	{
		var lang = $('#lang').val();
		$.get('/ajax.function.php?action=addtomaillist&email='+email+'&lang='+lang,
			function(data)
			{
				if(data == 1)
				{
					$('#maillistform').hide();
					displayError('mailError','Your email has been registerd');
				}else if(data == 2)
				{
					displayError('mailError','Error in registration');
				}
			}
		);
		return false;
	}
}
