function ahs_price() {
        var realprice = parseFloat($('#vara .verd').attr('rel'));
        var op = parseFloat($('#vara .tilbodsverd').attr('rel'));
        var price = 0;
        $.each(window.prices, function(i, item) {
			price +=parseFloat(item);
		})		
		$('#vara .rverd span').html(format_price(realprice+price))
		$('#vara .tilbodsverd span').html(format_price(op+price))
}

$(document).ready(function(e) {
	/* Breytir verði á vörusíðu eftir völdum aukahlutum	*/
	window.prices = { }
	$('.buybox select').each(function(i, item) {
			window.prices[$(item).attr('rel')] = $(item).find('option:selected').attr('price')
			ahs_price();
	})
    $('.buybox select').change(function(e) {
        window.prices[$(this).attr('rel')] = $(this).find('option:selected').attr('price')
        ahs_price();
    })
	$('a.thebutt').bind('click', function() {
		$('form.vara').ajaxSubmit({
			target: '#ahs_msg',
			success: function() {
				$('#karfa').fadeIn();
				var ct = parseInt($('.korfuct').text());
				ct++;
				$('.korfuct').text(ct)
			}
		});
		return false; // <-- important!
	});    
	$('input[type=checkbox]').bind('click', function() {
		if($(this).is(':checked')) {
			$.ajax({
				url: 'http://icepark.is/index.php?no_html=1&option=com_n-shop&do=add&Itemid=32&msg=Bætt við í körfu&forgetaboutme=jksd&id[]='+$(this).attr('sqlid'),
				success: function(e) {
					$('#ahs_msg').html(e).show().fadeOut(3000);
				}
			})
		} else {
			$.ajax({
				url: 'http://icepark.is/index.php?no_html=1&option=com_n-shop&do=delVara&Itemid=32&msg=Fjarlægt úr körfu&forgetaboutme=jksd&id='+$(this).attr('sqlid'),
				success: function(e) {
					$('#ahs_msg').html("Fjarlægt úr körfu").show().fadeOut(3000);
				}
			})
		}
		/*$('form.vara').ajaxSubmit({
			target: '#ahs_msg',
			success: function() {
				var ct = parseInt($('.korfuct').text());
				ct++;
				$('.korfuct').text(ct)
			}
		});*/
		//return false; // <-- important!
	});  
	
});
function popkorn (path)
{
    window.open(path, 'popupwindow','width=600,height=500,scrollbars,resizable');
    return false;
}
function showsearch ()
{
    var searchbox = document.getElementById('searchbox');
    if (searchbox.style.display == 'none') {
        searchbox.style.display = 'block';
        searchbox.style.visibility = 'visible';
        var sinput = document.getElementById('sinput');
        sinput.focus();
    } else {
        searchbox.style.display = 'none';
        searchbox.style.visibility = 'hidden';
    }
}
function show_friendbox ()
{
    var friendbox = document.getElementById('friendbox');
    if (friendbox.style.display == 'none') {
        friendbox.style.display = 'block';
        friendbox.style.visibility = 'visible';
        //var sinput = document.getElementById('sinput');
        //sinput.focus();
    } else {
        friendbox.style.display = 'none';
        friendbox.style.visibility = 'hidden';
    }
}
function send_friend ()
{
    var friend_email = escape(dgi('friend_email').value);
    var your_email = escape(dgi('your_email').value);
    var friend_name = escape(dgi('friend_name').value);
    
    var vara = dgi('voruid').value;
    var Itemid = dgi('Itemid').value;
    
    request('option=com_ahsshop&do=send_friend&no_html=1&friend_email='+friend_email+'&your_email='+your_email+'&friend_name='+friend_name+'&vara='+vara+'&Itemid='+Itemid);


}
function dgi (id)
{
    return document.getElementById(id);
}
function createRequestObject() {
    var ro;
    var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer"){
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    }else{
        ro = new XMLHttpRequest();
    }
    return ro;
}

var http = createRequestObject();

function request(query_string)
{
    document.getElementById('msg').innerHTML = 'sendi...';
    http.onreadystatechange = handleResponse;
    /* ÞETTA ER FYRIR GET
    http.open('get', 'rpc.php?'+query_string);
    http.send(null);
    */

    /* try instead */
    http.open('POST', 'index.php', true);
    http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    http.setRequestHeader("Content-length", query_string.length);
    http.setRequestHeader("Connection", "close");
    http.send(query_string);
}

function handleResponse() {

    var response = http.responseText;
    if(http.readyState == 4){
        document.getElementById('msg').innerHTML = response;
    }
}

