$(function(){	
	
	$('.fadeMe').hover(
		function()
		{
			$(this).fadeTo('fast',0.6);
		},
		function()
		{
			$(this).fadeTo('fast',1);
		}
	);
	
	$('.LogoHit').click(
		function()
		{
			window.location='?';
		}
	);
	
	$('.productbutton').hover(
		function()
		{
			$(this).clearQueue();
			$(this).animate({
				backgroundPosition : "-218px -240px"
			},300);
		},
		function()
		{
			$(this).animate({
				backgroundPosition : "-230px -240px"
			},300);
		}
	);
	
	$('.bigorderbutton').hover(
		function()
		{
			$(this).clearQueue();
			$(this).animate({
				backgroundPosition : "-220px -259px"
			},300);
		},
		function()
		{
			$(this).animate({
				backgroundPosition : "-230px -259px"
			},300);
		}
	);
	
	$('.sendorderbutton').bind({
		mouseover :	function()
					{
						$(this).clearQueue();
						$(this).animate({
							backgroundPosition : "-205px -259px"
						},300);
					},
		mouseout : function()
					{
						$(this).animate({
							backgroundPosition : "-215px -259px"
						},300);
					},
		click : function()
					{
						var name = $("#name").val();
						var kennitala = $("#kennitala").val();
						var address = $("#address").val();
						var email = $("#email").val();
						var phone = $("#phone").val();
						var gsm = $("#gsm").val();
						
						var totalprod = 0;
						
						var error = "";
						
						if(name == "")
						{
							error += "Nafn\n";
						}
						if(kennitala == "")
						{
							error += "Kennitala\n";
						}
						if(address == "")
						{
							error += "Heimilisfang\n";
						}
						if(email == "")
						{
							error += "Netfang\n";
						}
						if(email.indexOf('@') == -1)
						{
							error += "Netfang verður að vera gilt\n";
						}
						if(phone == "")
						{
							error += "Símanúmer\n";
						}
						if(gsm == "")
						{
							error += "Gsm númer\n";
						}

						$('.quantity').each(function(i){
							totalprod += $(this).val();
						});			
						
						if(totalprod == 0)
						{
							error += "Engar vörur valdar";
						}
						
						if(error != "")
						{
							var errorMsg = "Ekki var hægt að halda áfram með pöntunina.  Eftirfarandi atriði eru óútfyllt eða ekki í lagi\n";
							errorMsg += error;
							alert(errorMsg);
							return false;
						}else
						{
							$('#buyform').submit();		
						}
					}
	});
	
	$('#InfobaseItem > .Title > h1').bind({
		mouseenter : function()
		{
			$(this).addClass('titleHover');
		},
		mouseleave : function()
		{
			$(this).removeClass('titleHover');
		},
		click : function()
		{
			$('#submenu').slideToggle('fast');
		}
	});
	
	$('.systemMsg').live({
		mouseover : function()
		{
			$(this).addClass('msgHover');
		},
		mouseout : function()
		{
			$(this).removeClass('msgHover');
		},
		click : function()
		{
			$(this).slideUp('fast');
		}
	});
	
	$('#submenu').live({
		mouseleave : function()
		{
			$(this).slideToggle('fast');
		}
	});
	
	$('.buttonmenu > .button').hover(
		function(){
			$(this).clearQueue();
			$(this).fadeTo('fast',0.8);
		},function()
		{
			$(this).fadeTo('slow',1);
		}
	);
	
	$(".fancyme").fancybox({
			'transitionIn'	:	'elastic',
			'transitionOut'	:	'elastic',
			'speedIn'		:	600,
			'speedOut'		:	200,
			'overlayShow'	:	true			
	});
	
	$(".fancy_frame").fancybox({
			'type'			: 'iframe',
			'transitionIn'	: 'elastic',
			'transitionOut'	: 'elastic',
			'speedIn'		: 600, 
			'speedOut'		: 200, 
			'overlayShow'	: true,
			'width' 		: 650,
			'height'		: 750
	});
	
	$('.row_a').hover(
		function(){
			$(this).addClass('row_hover');
		},
		function()
		{
			$(this).removeClass('row_hover');
		}
	);
	$('.row_b').hover(
		function(){
			$(this).addClass('row_hover');
		},
		function()
		{
			$(this).removeClass('row_hover');
		}
	);
	
	$('.quantity').keyup(function()
	{
		var totalsum = 0;
		$('.quantity').each(function(i)
		{
			var id = $(this).attr('id');
			var p = $(this).attr('rel');
			var n = $(this).val();
			var t = parseInt(n * p);
			
			totalsum = totalsum + t;
					
			$('#total_'+id).html("kr."+t);
			
		});
		$('#total').val("kr."+totalsum);
	});
	$('#ajaxmaillist').submit(function(event) {
		var email = $('#email').val();
		var maillist = $('#maillist_nr').val();
		var color = '';
		if (maillist == 2) {
			color = '96c74f';
		}
		else {
			color = 'ffe66a';
		}
		
		var left_pos = ($('body').innerWidth() - 360) / 2;
		var top_pos = ($('body').innerHeight() - 100) / 2;
		
		if (email.length > 0) {
			$.ajax({
				type: "POST",
				url: "ajax.php",
				data: "action=add_to_maillist&email="+email+"&maillist_nr="+maillist,
				success: function(msg){
					$('body').prepend('<div id="maillist_msg" style="position: fixed; top: '+top_pos+'px; left: '+left_pos+'px; background: #'+color+';">'+msg+'</div>');
					$('#maillist_msg').delay(3000).fadeTo('slow', 0, function() {
						$('#maillist_msg').remove();
						$('#email').val('').focus();
					});
				}
			});
		}
		event.preventDefault();
	});
	$('.sendbutton').click(function(event) {
		$('#ajaxmaillist').submit();
		event.preventDefault();
	});
});

function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}
function checkForm() {
	if (document.getElementById('name').value == "") { alert("Vinsamlegast fyllið inn Nafn"); return; }
	else if (document.getElementById('email').value == "") { alert("Vinsamlegast fyllið inn Netfang"); return; }
	else if (document.getElementById('address').value == "") { alert("Vinsamlegast fyllið inn Heimilisfang"); return; }
	else if (document.getElementById('phone').value == "") { alert("Vinsamlegast fyllið inn Sími"); return; }
	document.forms["buyform"].submit()
}
function checkTotal() {
	tprice = 0;
	for (i = 1; i <= titems; i++) {
		price = document.getElementById("price"+i);
		if (price.getAttribute("tprice") != null) tprice = parseInt(tprice) + parseInt(price.getAttribute("tprice"));
	}
	document.getElementById("total").value = addDots(tprice);
}
function addDots(tprice) {
	if (tprice.toString().length > 6) {
		tprice = tprice.toString().substr(0,tprice.toString().length - 6) + "." + tprice.toString().substr(tprice.toString().length-6,tprice.toString().length-4) + "." + tprice.toString().substr(tprice.toString().length-3,tprice.toString().length);
	} else if (tprice.toString().length > 3) {
		tprice = tprice.toString().substr(0,tprice.toString().length - 3) + "." + tprice.toString().substr(tprice.toString().length-3,tprice.toString().length);
	}
	return tprice;
}
function checkPrice(obj) {
	num = obj.getAttribute("num")
	quant = document.getElementById("quant"+num);
	price = document.getElementById("price"+num);
	if (!isInteger(quant.value)) obj.value = 0;
	tprice = quant.value * price.getAttribute("price");
	price.setAttribute("tprice",tprice);
	price.value = addDots(tprice);
	checkTotal();
}
