$('document').ready( function () {
	/* INIT */

	$('input.kassaamount').change( function () {
		var prodid = $(this).attr('prodid');
		var uniqueid = $(this).attr('id');
		var amount = $('#'+uniqueid).val();
		
		$.post('xmlhttp/cartaction.php', { action: 'update', uniqueid: uniqueid, amount: amount }, function (data) {

			var value = data.split('|||');

			if (data != 'ERROR') {
				$('.cart').html(value[0]);
				$('#tot_amount').html(value[2]+' st.');
				$('#tot').html(value[3]);
				$('#'+uniqueid).attr('value', value[1]);
				$('#price_'+uniqueid).html(value[4]);
			}
			else {
				alert('Antalet kunde inte uppdateras');
			}
		});
	});
	
	
	$('input.button-buy, a.btn-buy').click( function () {

		var name = $(this).attr('name');

		var form = $('#dustbag_form_'+name).formSerialize();

		var redirect = $('.redirect').val();

		$.post('xmlhttp/cartaction.php', form, function (data) {

			if(redirect.length > 5){
				document.location = redirect;
			}

			if (data != 'ERROR') {
				$('.cart').html(data);
			}
			else {
				alert('varan kunde inte läggas i kundvagnen');
			}
		});
	});



	$('a.btn-buyholder').click( function () {

		var form = $('#holder_form').formSerialize();

		var redirect = $('.holder_form .redirect').val();

		$.post('xmlhttp/cartaction.php', form, function (data) {

			if(redirect.length > 5){
				document.location = redirect;
			}

			if (data != 'ERROR') {
				$('.cart').html(data);
			}
			else {
				alert('varan kunde inte läggas i kundvagnen');
			}
		});
	});



	$('input.amount').change( function() {
		var row = $(this).attr('id');

		var amount = $(this).val();

		var totalt = parseInt(amount) * parseFloat($('.prodPrice_'+row).val());

		$('.totalt_'+row).html(totalt);

	});

	$('.quickshop .quantity-buy, .webshop_bags .quantity-buy').keyup( function() {
		var amount = $(this).val();
		var id = $(this).attr('id');
		var price = $('#startprice_'+id).val();

		if(parseInt(amount) < 1){
			amount = 1;
			$('#'+id).attr('value', amount);
		}

		var p = parseInt(amount) * parseFloat(price);

		$('.price_'+id).html(p);
	});

	$('#brand').change( function() {
		var key = $(this).val();

		$('#default').hide();
		$('.models').hide();
		$('div#'+key).show();
		$('.icon_brand').show();
		$('.icon_model').hide();
		$('.webshop_holder .last').hide();
		$('.holder_info').hide();
	});

	$('.models select').change( function() {
		
		var value = $(this).val();

		$('.webshop_holder .last').hide();
		$('.icon_model').hide();

		if(parseInt(value) != 0){

			$('.holderID').attr('value', value);

			$.get('xmlhttp/getDusbagForProduct.php', { productID: value }, function(data){
				showDustbag(data);
			});

			$.get('xmlhttp/getHolderPrice.php', { id: value }, function(data){
				if(data != 0){
					var data = data.split('|');
					$('.webshop_holder .price').html(data[0]);
					$('.webshop_holder #holder_nr').html(data[1]);
					$('.webshop_holder .last').show();
					$('.icon_model').show();
					$('.holder_info').show();
				}
			});

		}else{
			$('.webshop_holder .last').hide();
			$('.icon_model').hide();
			$('.holder_info').hide();
		}
	});

		
	$('a#close').click(function () {
		$('div#fade').hide();
		$('div#instructions').hide();
	});


	$("#webshop .webshop_holder td select.models").mouseover (function(){
		$(this).data("origWidth", $(this).css("width")).css("width", 'auto');
	});

	$("#webshop .webshop_holder td select.models").change (function(){
		$(this).data("origWidth", $(this).css("width")).css("width", '225px');
	});

	$("#webshop .webshop_holder td select.models").blur (function(){
		$(this).data("origWidth", $(this).css("width")).css("width", '225px');
	});


	//Hämta butiker efter att ha fyllt i stad i orderformuläret
	$('input#order_city').blur( function() {

		var city = $(this).val();

		$('div#shops').hide();

		$.post('xmlhttp/getShops.php', { city: city }, function(data){
			if(data != 0){
				$('div#shops .shops').html(data);
				$('div#shops').show();
			}
		});
	});
});


function showDustbag(id){

	$('.vacumm_types li').removeClass('active');

	$('.form').hide();

	$('#dust_'+id).addClass('active');
	$('#form_'+id).show();
}


function nemo(namn,foretag){
	location.href= 'mailto:'+namn+'@'+foretag;
	return false;
}

function findHolder(){

	var pID = $('input.productID').val();

	$.get('xmlhttp/getHolder.php', { holder: pID }, function(data) {
		if(data != 'error'){
			$('.pInfo').show();
			$('td.products').html(data);
		}else{
			$('td.products').html('Ingen hållare hittades');
		}
	});
}

function addProduct() {

	var pID = $('input.productID').val();
	var amount = $("input[@name='productAmount']:checked").val();

	if(pID != 'NR'){

		$('.orderlist').html('<img src="static/images/ajax-loader.gif">');

		$.get('xmlhttp/updateOrderlist.php', { pID: pID, amount: amount }, function(data){
			$('.orderlist').html(data);
		});

		$('.pInfo').hide();
		$('.productAmount').attr('value', 1);
		$('.products').html('');
	}
}

function addBag() {

	var amount = $("input[@name='bagAmount']:checked").val();

	$('.orderlist').html('<img src="static/images/ajax-loader.gif">');

	$.get('xmlhttp/updateOrderlist.php', { pID: 3104, amount: amount }, function(data){
		$('.orderlist').html(data);
	});
}

function addFilter() {

	var amount = $("input[@name='filterAmount']:checked").val();

	$('.orderlist').html('<img src="static/images/ajax-loader.gif">');

	$.get('xmlhttp/updateOrderlist.php', { pID: 5108, amount: amount }, function(data){
		$('.orderlist').html(data);
	});
}


function changeAmount(pID){

	var amount = $("select[@name='prod["+pID+"][amount]']").val();

	$('.orderlist').html('<img src="static/images/ajax-loader.gif">');

	$.get('xmlhttp/updateAmount.php', { pID: pID, amount: amount }, function(data){
		$('.orderlist').html(data);
	});
}

function deleteAmount(pID){

	$('.orderlist').html('<img src="static/images/ajax-loader.gif">');

	$.get('xmlhttp/updateAmount.php', { pID: pID, remove: 1}, function(data){
		$('.orderlist').html(data);
	});
}

function getUser(){

	var kundID = $('input#kundID').val();

	$.getJSON('xmlhttp/getUser.php', { kundID: kundID }, function(data) {

		if(data){
			//Fakturaadress
			if(data.firstname == 'Imported'){
				data.firstname = '';	
			}

			$('#firstname').attr('value', data.firstname);

			if(data.lastname == 'User'){
				data.lastname = '';	
			}
			$('#lastname').attr('value', data.lastname);
			$('#company').attr('value', data.company);
			$('#adress').attr('value', data.shipping_address);
			$('#zipcode').attr('value', data.shipping_zipcode);
			$('#city').attr('value', data.shipping_city);
			$('#country').attr('value', data.country);
			$('#email').attr('value', data.email);
			$('#phone').attr('value', data.phone);

			//Leveransadress
			$('#fak_adress').attr('value', data.address);
			$('#fak_zipcode').attr('value', data.zipcode);
			$('#fak_city').attr('value', data.city);
			$('#country').attr('value', data.country);

			$('#getKundID').attr('value', 1);
		}else{
			$('#getKundID').attr('value', 0);
		}

	});
}

function showMovie() {

	var height = $(document).height();

	$('div#fade').css('height', height+'px');

	$('div#fade').show();
	$('div#instructions').show();
}

function custompop(win, width, height, status, menu)
{
	bakom=window.open(win, 'bakom', 'toolbar=no,location=no,directories=no,status=' + status +  ',menubar=' + menu + ',scrollbars=yes,resizable=yes,copyhistory=no,width=' + width + ',height=' + height);
	bakom.focus();
}
