
function wjf_AjaxLoading() {
	$('.global-loading').hide()
		.ajaxStart(function() {
			$(this).show();
		})
		.ajaxStop(function() {
			$(this).fadeOut();
	});
}

function wjf_SwitchHeader(imagegroupid,color) {
	$('#pform-header').css({'background-image':'url("http://www.whyprint.fi/_images/products/'+imagegroupid+'_header.jpg")'});
	$('#pform-header-h1').css({'color':'#'+color+''});
	$('.pform-header-switcher-img').removeClass('pform-header-switcher-img-selected');
	$('#pform-header-switcher-img-'+itemid).addClass('pform-header-switcher-img-selected');
}

function wjf_PreventFormSubmission(field,event) {
	var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
	if ( keyCode == 13 ) {
		var i;
		for ( i = 0 ; i < field.form.elements.length ; i++ ) {
			if ( field == field.form.elements[i] ) {
				break;
			}
		}
		i = ( i + 1 ) % field.form.elements.length;
		field.form.elements[i].focus();
		return false;
	}
	else { 	
		return true;
	}
}

function wjf_SwitchSupportImage(thumbimageid,paragraphid,previewimageid,imagetext) {
	$('#image-preview-'+paragraphid).html('<img src="http://www.whyprint.fi/_images/supportimages/'+previewimageid+'.jpg">');
	$('#image-text-'+paragraphid).html(imagetext);
	$('#image-thumbs-'+paragraphid+'-'+thumbimageid).addClass('selected').siblings().removeClass('selected');
}


function selectSize(field) {
	if ( field.name == 's' && field.value == 0 ) {
		$('#form_w').val('85');
		$('#form_h').val('48');
	}
	else if ( field.name == 's' && field.value != 0 ) {
		$('#form_w').val('0');
		$('#form_h').val('0');
	}
	else if ( field.name == 'w' ) {
		$('#productform_options_size_select').val('0');
	}
	else if ( field == 'form_h' ) {
		$('#productform_options_size_select').val('0');
	}
}

function customsize(action) {
	if ( action == "hide" )
		$('#productform_options_size_custom').hide();
	else if ( action == "show" )
		$('#productform_options_size_custom').show();
}

function wjf_SwitchProductformImage(what,itemid) {
	if ( what == "size" ) {
		if ( itemid == 0 ) {
			$('#productform_options_size_custom').show();
		}
		else {
			$('#productform_options_size_custom').hide();
		}
	}
	else {
		if ( what == "amount" ) {
			var testvalue = $('#pform-form-options-amount-input').val();
			var test = testvalue.substr(0,1);
			
			if ( test == 0 )
				var pos = '0px'; 
			else
				var pos = '-'+(test)+''+(test)+'0px'; 
			
		}
		else if ( what == "size" ) {
			var pos = '0px'; 
		}
		else {
			
			if ( itemid-1 == 0 )
				var pos = '0px'; 
			else
				var pos = '-'+(itemid-1)+''+(itemid-1)+'0px'; 
	
		}
		$('#pform-form-options-'+what+'-image').css({'background-position':''+pos+' 0px'})
	}
}


function wjf_GetPrice(language) {
	var amount = parseInt($('#pform-form-options-amount-input').val());
	
	if ( !isNaN(amount) && amount > 0 ) {
		$.ajax({
			type: "GET",
			url: "/getprice.php",
			data: 'language='+language+'&'+$('#productform_form').serialize(),
			dataType: "json",
			success: function (data, status) {
				if ( typeof ( data.warningmessage ) != "undefined" ) {
					if ( data.warningmessage == "" && data.pricetotalexclvat != "" ) {
						
						if ( parseInt(data.usage) > 85 )
							var usagecolor = "090";
						else if ( parseInt(data.usage) > 65 )
							var usagecolor = "990";
						else
							var usagecolor = "F30";
						
						if ( parseInt(data.usage) > 0 )
							$('#pform-form-options-size-image').html('<span style="color:#'+usagecolor+'">'+data.usage+'%</span>');
						
						
						$('#pform-summary-price-amount').html(data.amount);
						$('#pform-summary-price-euro').html('<strong>'+data.pricetotalexclvat+'</strong> &euro;');
						$('#dev-pform-warnings').html('No Warnings.');
					}
					else {
						$('#pform-form-options-size-image').html("--");
						$('#pform-summary-price-euro').html('<strong>--</strong> &euro;');
						$('#pform-summary-price-amount').html('--');
						$('#dev-pform-warnings').html('Code: <strong>'+data.warningcode+'</strong><br />Type: <strong>'+data.warningtype+'</strong><br />'+data.warningmessage);
					}
				}
			},
			error: function (data, status, e) {
				$('#dev-pform-warnings').html(status);
			}
		 });
	}
	
}

function wjf_ShoppingCart(language,action,type) {
	if ( action == "add" ) {
		if ( type == "design" ) {
			var name = $('#productform-name').val();
			var filename = $('#productform-filename').val();
			var formdata = $('#productform_form').serialize();
			var productdata = 'name='+name+'&filename='+filename+'&'+formdata;
		}
		else {
			var productdata = $('#productform_form').serialize();
		}
		$("#navi-right-cart-text-num")
			.animate({paddingRight:"0px",paddingLeft:"6px"},40)
			.animate({paddingRight:"6px",paddingLeft:"0px"},40)
			.animate({paddingRight:"0px",paddingLeft:"6px"},40)
			.animate({paddingRight:"6px",paddingLeft:"0px"},40)
			.animate({paddingRight:"0px",paddingLeft:"6px"},40)
			.animate({paddingRight:"6px",paddingLeft:"0px"},40)
			.animate({paddingRight:"0px",paddingLeft:"6px"},40)
			.animate({paddingRight:"0px",paddingLeft:"0px"},40)
		;
		$('#navi-right-cart-text-num').load('/'+language+'/cart/add/',productdata);
	}
	else if ( action == "numberitems" ) {
		$('#navi-right-cart-text-num').load('/'+language+'/cart/numberitems/');
	}
}

function wjf_AccountShowOrderDetails(language,orderid) {
	$('#account-orders-list-order-'+orderid).addClass('account-orders-list-order-selected').siblings().removeClass('account-orders-list-order-selected');
	$('#account-orders-view').load('/account.orders.view.php?language='+language+'&orderid='+orderid);
}
