$(document).ready( function() {

	$('input.kassaamount').change( function () {
		var uniqueid = $(this).attr('id');
		var amount = $('#'+uniqueid).val();

		$('#'+uniqueid+' + IMG.progressBar').show();
		$.post('xmlhttp/cartaction.php', { action: 'update', uniqueid: uniqueid, amount: amount }, function (data) {

			var value = data.split('|||');

			if (data != 'ERROR') {
				$('#'+uniqueid+' + IMG.progressBar').hide();

				$('.cart').html(value[0]);
				$('#tot_amount').html(value[2]+' st.');
				$('#tot').html(value[3]+' kr');
				$('#'+uniqueid).attr('value', value[1]);
				$('#price_'+uniqueid).html(value[4]+' kr');
			}
			else {
				alert('Antalet kunde inte uppdateras');
			}
		});
	});
	

	//Lägg i varukorgen
	$('input.button-buy').click( function () {

		var fdata = $('#productForm input, #productForm select').serialize();

		var error = '';

		$('.obl').each( function(){
			var val = $(this).val();

			var list = $(this).attr('class').split(' ');

			if(val == 0){
				error += ' - '+list[1]+' saknas\n';
			}
		});

		//Kolla om tryck valt och om namn har skrivits in
		if($('.print').val() > 0){
			if($("input[@name='print_text']").val().length < 2){
				error += ' - Text på namntryck saknas\n';
			}
		}

		if(error.length > 0){
			alert('Följande fel hittades:\n'+error);
		}else{
			$.post('xmlhttp/cartaction.php', fdata, function (data) {

				if (data != 'ERROR') {

					var scroll = $(window).scrollTop() + ($(window).height() / 2);

					$('#confirm_cart').css('top', scroll+'px');

					$('#confirm_cart').fadeIn("slow");
					$('.cart').html(data);

					setTimeout('$(\'#confirm_cart\').fadeOut("slow");', 2000);
				}
				else {
					alert('varan kunde inte läggas i kundvagnen');
				}
			});
		}		
	});


	//Lägg till i offert
	$('input.button-offert').click( function () {

		var fdata = $('#productForm input, #productForm select').serialize();

		var error = '';

		if(error.length > 0){
			alert('Följande fel hittades:\n'+error);
		}else{

			$.post('xmlhttp/offertaction.php', fdata, function (data) {

				if (data != 'ERROR') {

					var scroll = $(window).scrollTop() + ($(window).height() / 2);

					$('#confirm_cart').css('top', scroll+'px');

					$('#confirm_cart').fadeIn("slow");
					//$('.cart').html(data);

					setTimeout('$(\'#confirm_cart\').fadeOut("slow");', 2000);
				}
				else {
					alert('varan kunde inte läggas till i offerten');
				}
			});
		}		
	});


	$('#forgotform').submit( function() {

		var query = $('#field').val();

		if(query.length < 3){
			alert('Fyll i användarnamn eller e-mail');
		}else{
			$.get('xmlhttp/send_pass.php', { query: query }, function(data){
				if(data != 'ok'){
					alert(data);
				}else{
					$('.field').html('Uppgifterna har skickats');
				}
			});
		}

		return false;
	});

	$('.media').click( function() {

		var id = $(this).attr('id');

		$('.media').removeClass('on');

		$(this).addClass('on');

		$('.mediaimage').attr('value', id);

	});

	$('#image img').click( function() {
		$('#shadow, #image').hide();
	});


	$('.colorbox').click( function() {
		var list = $(this).attr('id').split('_');

		var name =  $(this).attr('name');

		$('#name_'+list[0]).html(name);

		$('#sect_'+list[0]).attr('value', list[1]);

		var ids = $(this).attr('id').split('_');

		getPropertyprice();

		$('.colorbox').removeClass('on');

		$('#'+$(this).attr('id')).addClass('on');
	});

	$('.print').change( function() {

		var value = $(this).val();

		if(parseInt(value) > 0){
			$('.print_text').show();
		}else{
			$('.print_text').hide();
		}

		getPropertyprice();
	});


	/* Subshop login */
	$('#subpass_send').click(function () {
		var error = false;
		var user = $('#subname').attr('value');
		var pass = $('#subpass').attr('value');

		if (! pass) {
			alert('Felaktigt login');
		} else {
			$.post('xmlhttp/subshop.php', { usr: user, pwd: pass }, function (data) {
				if (data == 1) {
					$('#subshoplogin').hide();
					$('#subshoplogin_form').hide();
					document.location.reload(true);
					return true;
				} else {
					alert('Felaktigt login');
				}
			});
		}
	});


	$('#productImageLargeTD .productimage').click( function() {

		var src = $(this).attr('src');

		alert("Källa: "+src);
	});


	$('.print_text_versal').keyup( function() {
		var value = $(this).val();

		value = value.toUpperCase();

		$('.print_text_versal').attr('value', value);
	});

	$('.print_text_versal').change( function() {
		var value = $(this).val();

		value = value.toUpperCase();

		$('.print_text_versal').attr('value', value);
	});
});

function getPropertyprice(){

	var fdata = $('#productForm input, #productForm select').serialize();

	$.post('xmlhttp/getPropertyPrice.php', fdata , function(data){
		var list = data.split('||');

		if(list[0] > 0){
			$('.showPrice').html(list[0]+' kr');
		}

		$('div.staffling').html(list[1]);
	});
}

function subShopLogin() {
	var error = false;
	var user = $('#subname').attr('value');
	var pass = $('#subpass').attr('value');
	var userID = $('#userID').val();

	if (! pass) {
		alert('Felaktigt login');
	} else {
		$.post('xmlhttp/subshop.php', { usr: user, pwd: pass, userID: userID }, function (data) {
			if (data == 1) {
				$('#subshoplogin').hide();
				$('#subshoplogin_form').hide();
				document.location.reload(true);
				return true;
			} else {
				alert('Felaktigt login');
			}
		});
	}
}

// Uppdatera antal på tryckprodukter
function updateImageAmount(rowKey, uniqueID, imageID){

	var amount = $('#'+imageID).val();

	$.post('xmlhttp/cartaction.php', { action: 'updateimageamount', uniqueID: uniqueID, amount: amount, rowKey: rowKey, imageID: imageID }, function (data) {

		var value = data.split('|||');

		if (data != 'ERROR') {
			$('.cart').html(value[0]);
			$('#tot_amount').html(value[2]+' st.');
			$('#tot').html(value[3]+' kr');
			$('#imgrow_'+rowKey+'_'+uniqueID).html(value[4]+' kr');
			$('#'+imageID).attr('value', value[1]);
		}
		else {
			alert('Antalet kunde inte uppdateras');
		}
	});
}

// Uppdatera antal på namntryckprodukter
function updatePrintAmount(rowKey, uniqueID, printID){

	var amount = $('#'+printID).val();

	$.post('xmlhttp/cartaction.php', { action: 'updateprintamount', uniqueID: uniqueID, amount: amount, rowKey: rowKey, printID: printID }, function (data) {

		var value = data.split('|||');

		if (data != 'ERROR') {
			$('.cart').html(value[0]);
			$('#tot_amount').html(value[2]+' st.');
			$('#tot').html(value[3]+' kr');
			$('#imgrow_'+rowKey+'_'+uniqueID).html(value[4]+' kr');
			$('#'+printID).attr('value', value[1]);
		}
		else {
			alert('Antalet kunde inte uppdateras');
		}
	});

}


function showImg(img){

	$('#image').html('<center><img src="'+img+'" onclick="$(\'#shadow, #image\').hide();"><br><br><a href="javascript:;" onclick="$(\'#shadow, #image\').hide();">Klicka på bilden för att stänga den</a></center>');

	$('#shadow, #image').show();

}

function showTipFriend(productID){

	$.get('xmlhttp/getTipFriend.php', { productID: productID }, function(data){
		$('#image').html(data);

		scroll(0,0);

		$('#image, #shadow').show();
	});
}

function showSizeGuide(articleID){

	$.get('xmlhttp/getSizeGuide.php', { articleID: articleID }, function(data){
		$('#image').html(data);

		scroll(0,0);

		$('#image, #shadow').show();
	});
}

function showMediaImages(sectionID, productID, page){

	$.get('xmlhttp/getMediaImages.php', { sectionID: sectionID, productID: productID, page: page }, function(data){
		$('#image').html(data);

		scroll(0,0);

		$('#shadow, #image').show();		
	});
}

function showBigImage(img, type, id, sectionID, image){

	$('#premade, #own, #navigation').hide();

	$('#big').html('<img src="'+img+'"><br><br><a href="javascript:;" onclick="selectmediaImage('+id+', '+sectionID+', \''+image+'\');">Välj bild</a> | <a href="javascript:;" onclick="$(\'#big\').hide();$(\'#'+type+', #navigation\').show();">Tillbaka till arkivet</a>');

	$('#big').show();
}

function updateOwnImages(sectionID){

	$.get('xmlhttp/updateOwnImages.php', { sectionID: sectionID }, function(data){
		$('#own').html(data);
		$('#premade').hide();
		$('#own').show();
	});
}


function changeProductMainPicture(img, imageID, productID, orgImg, propertyID){
	$('.thumb').removeClass('active');

	if(imageID > 0){
		$('#t_'+imageID).addClass('active');
	}

	if(img.length > 5){
		$('#productImageLargeTD').html('<img src="'+img+'" alt="Klicka f&ouml;r st&ouml;rre bild"><br><br><a href="javascript:;" onclick="showImg(\''+orgImg+'\')">Visa större bild</a>');
	}

	$('.colorbox').removeClass('coloractive');

	var id = $('.img_'+imageID).attr('id');

	var cl = $('#box_'+propertyID).attr('class');

	if(cl != undefined){
		var list = cl.split(' ');

		var sectionID = list[2];
		var propertyID = list[3];
	}
	var propertyname = $('.img_'+imageID).attr('name');

	//Skriver ut active
	$('#'+id).addClass('coloractive');

	//Sätter värde för sektionen
	$('#sect_'+sectionID).attr('value', propertyID);

	//Skriver ut vald färg
	$('#name_'+sectionID).html(propertyname);

	//Skriver ut active
	$('#box_'+propertyID).addClass('coloractive');

	$('#product_pdf').attr('href', 'product_pdf.php?productID='+productID+'&imageID='+imageID);
	$('#product_request, .profilera').attr('href', 'javascript:showPriceRequest(\''+productID+'\', '+imageID+')');
}


function selectmediaImage(id, sectionID, image){

	var propID = $('#printType').val();
	var propName = $('#printName').val();

	$('#sect_'+sectionID).attr('value', propID);

	$('#mediaimage').attr('value', id);

	var html = '<table cellpadding="0" cellspacing="0">';
	html += '<tr><th>Tryck:</th></tr><tr><td>'+propName+'</td></tr>';
	html += '<tr><th>Motiv:</th></tr><tr><td><img src="'+image+'" align="left"><a href="javascript:;deleteLabel('+sectionID+')" style="float: right;">Ta bort</a></td></tr>';
	html += '</table>';

	$('#profileimage').html(html);
	
	$('#shadow, #image').hide();
}

function deleteLabel(sectionID){
	$('#sect_'+sectionID).attr('value', '');
	$('#mediaimage').attr('value', 0);
	$('#profileimage').html('');
}


function showPriceRequest(productID, imageID){

	$.get('xmlhttp/getPriceRequest.php', { productID: productID, imageID: imageID }, function(data){
		$('#image').html(data);

		scroll(0,0);

		$('#image, #shadow').show();
	});
}


/* for payments*/

function custompop(url, width, height, status, menu) {
   win = window.open(url, 'addprofile_popup', 'toolbar=no,location=no,directories=no,status=' + status +  ',menubar=' + menu + ',scrollbars=yes,resizable=yes,copyhistory=no,width=' + width + ',height=' + height);
   win.focus();
}


function trigPayment() {
	$('#orderForm').submit();
}

function listenForPayment() {
	
	$.get('xmlhttp/dibscheck.php', null, function (data) {
		if (data == 'OK') {
			$('.confirm_order').show();
		}
		else {
			setTimeout('listenForPayment()', 5000 );
		}
	});
	
	
}


var newwin = null;
function doPopup() {
  newwin = window.open('', 'paywin', 'scrollbars,status,width=550,height=600');
  newwin.focus();
  return true;
}


//Visa offert
function showOffert(offertID){

	$.get('xmlhttp/getOffert.php', { offertID: offertID }, function(data){
		$('#image').html(data);

		scroll(0,0);

		$('#image, #shadow').show();
	});
}
