$(document).ready(function() {

	$('.hidden').slideUp();
	
	$('#add_bag').css('display','');
	
	
	
	$('#size').change(function(){
		$(this).attr('priceplus',$('option[value="' + $(this).val() + '"]').attr('priceplus'));
		
		base_price = $(this).attr('basicprice');
		price_plus = $(this).attr('priceplus');
		
		new_price = parseInt(base_price) + parseInt(price_plus);
		
		$('#price').html(new_price + ' &euro;');
	});
	
	$('#shiphere').click(function(){
		$('#ship_address1').val('');
		$('#ship_address2').val('');
		$('#ship_city').val('');
		$('#ship_county').val('');
		$('#ship_postcode').val('');
		$('#ship_country').val('');
		
	});
	
	$('.whereship').change(function(){
		if($(this).val() == '1'){
			$('#address2_slide').slideDown();
		}else{
			$('#address2_slide').slideUp();
		}
	});
	
	$('#sign2').hide();
	
	$('#discount_code').click(function(){
		if($(this).val() == 'ENTER CODE'){
			$(this).val('');
		}
	});
	
	$('#discount_code').blur(function(){
		if($(this).val() == ''){
			$(this).val('ENTER CODE');
		}
	});
	
	$('.checkout_continue_2').click(function(){
		window.location = "index.php"
	});
	
	$('.checkout_continue').click(function(){
		window.location = "index.php"
	});
	
	$('#popular_heading').click(function(){
		$('#popular_slider').slideToggle('slow','easeInOutExpo');
	});
	
	$('#openclose_collection').click(function(){
		$('#collections_filters_slide').slideToggle('slow','easeInOutExpo');
	});
	
	$('#openclose_category').click(function(){
		$('#categories_filters_slide').slideToggle('slow','easeInOutExpo');
	});
	
	$('#openclose_brands').click(function(){
		$('#brand_filters_slide').slideToggle('slow','easeInOutExpo');
	});
	
	$('#openclose_materials').click(function(){
		$('#materials_filters_slide').slideToggle('slow','easeInOutExpo');
	});
	
	$('#openclose_price').click(function(){
		$('#price_filters_slide').slideToggle('slow','easeInOutExpo');
	});
	
	$('#openclose_occasion').click(function(){
		$('#occasion_filters_slide').slideToggle('slow','easeInOutExpo');
	});
	
	$('#openclose_style').click(function(){
		$('#style_filters_slide').slideToggle('slow','easeInOutExpo');
	});
	
	$('#openclose_setting').click(function(){
		$('#setting_filters_slide').slideToggle('slow','easeInOutExpo');
	});
	
	$("#signup_mail").keyup(function(event){
	  if(event.keyCode == 13){
		  if($(this).val() != 'Subscribed. Thank you!'){
				var email = $(this).val();
				$('body').find('.MailPressFormEmail').val(email);
				$('body').find('.MailPressFormName').val('n/a');
				$('body').find('.MailPressFormSubmit').click();
				$(this).val('Subscribed. Thank you!');
				alert('You were subscribed. Thank you!')
		  }
	  }
	});

	$("#signup_mail").click(function(){
		if($(this).val() == 'enter your email address'){
			$(this).val('');
		}
	});	
	
	$("#signup_mail").blur(function(){
		if($(this).val() == ''){
			$(this).val('enter your email address');
		}
	});	
	
	
	$(".search_field").click(function(){
		if($(this).val() == 'SEARCH'){
			$(this).val('');
		}
	});	
	
	$(".search_field").blur(function(){
		if($(this).val() == ''){
			$(this).val('SEARCH');
		}
	});
	
	
	$('.material_check').click(function(){
		if($(this).hasClass('active')){
			$(this).removeClass('active');
		}else{
			$(this).addClass('active');
		}
		filter_list();
	});
	
	$('#loading').slideUp();
	//$('#seo_text').css('','');
	
	$('#categories_menu a').hover(
		function(){
			$(this).animate({ color: "black" }, 300);
		},
		function(){
			$(this).animate({ color: "#999999" }, 300);
		}
	);
	
	$('#desc_openclose').click(function(){
		var contains = $(this).html();
		
		if(contains.indexOf('Close') > -1){
			$('#description').slideUp();
			$(this).html('DESCRIPTION<div class="openclose" id="openclose"> Open <img src="images/open.jpg" alt="Close" width="17" height="16" />');
		}
		
		if(contains.indexOf('Open') > -1){
			$('#description').slideDown();
			$(this).html('DESCRIPTION<div class="openclose" id="openclose"> Close <img src="images/close.jpg" alt="Close" width="17" height="16" />');
		}
	});
	
	
	$('#det_openclose').click(function(){
		var contains = $(this).html();
		
		if(contains.indexOf('Close') > -1){
			$('#details').slideUp();
			$(this).html('DETAILS<div class="openclose"> Open <img src="images/open.jpg" alt="Close" width="17" height="16" />');
		}
		
		if(contains.indexOf('Open') > -1){
			$('#details').slideDown();
			$(this).html('DETAILS<div class="openclose"> Close <img src="images/close.jpg" alt="Close" width="17" height="16" />');
		}
	});
	
	
	$('#wearitwith_openclose').click(function(){
		var contains = $(this).html();
		
		if(contains.indexOf('Close') > -1){
			$('#wearitwith').slideUp();
			$(this).html('WEAR IT WITH<div class="openclose"> Open <img src="images/open.jpg" alt="Close" width="17" height="16" />');
		}
		
		if(contains.indexOf('Open') > -1){
			$('#wearitwith').slideDown();
			$(this).html('WEAR IT WITH<div class="openclose"> Close <img src="images/close.jpg" alt="Close" width="17" height="16" />');
		}
	});
	
	$('#recently_openclose').click(function(){
		var contains = $(this).html();
		
		if(contains.indexOf('Close') > -1){
			$('#recently').slideUp();
			$(this).html('RECENTLY VIEWED<div class="openclose"> Open <img src="images/open.jpg" alt="Close" width="17" height="16" />');
		}
		
		if(contains.indexOf('Open') > -1){
			$('#recently').slideDown();
			$(this).html('RECENTLY VIEWED<div class="openclose"> Close <img src="images/close.jpg" alt="Close" width="17" height="16" />');
		}
	});
	
	
});

function add_wish(product_id, user_id){
	
	if(!user_id){
		alert('You need to be logged in in order to create a wishlist.');
		return false;
	}
	
	$.ajax({
		type: 'POST',
		data: "product_id=" + product_id + "&user_id=" + user_id,
		url: "ajax_add_wish.php",
		success: function(msg){
			if(msg == 'ok'){
				alert('Added to your wishlist.');
			}else{
				alert(msg);
			}
		},
		error: function(a,msg){
			alert(msg);
		}
	});
	
}

function remove_wish(product_id, user_id){
	
	$.ajax({
		type: 'POST',
		data: "product_id=" + product_id + "&user_id=" + user_id,
		url: "ajax_remove_wish.php",
		success: function(msg){
			if(msg == 'ok'){
				$('#row_' + product_id).remove();
				$('#row_' + product_id + '_2').remove();
				
			}else{
				alert(msg);
			}
		},
		error: function(a,msg){
			alert(msg);
		}
	});
	
}

function submit_details(form, master_id){
	var name = $('#name').val();
	var surname = $('#surname').val();
	var email = $('#email').val();
	var address1 = $('#address1').val();
	var city = $('#city').val();
	var country = $('#country').val();
	
	var title = $('#title').val();
	var company = $('#company').val();
	var address2 = $('#address2').val();
	var county = $('#county').val();
	var postcode = $('#postcode').val();
	var phone_day = $('#phone_day').val();
	var phone_evening = $('#phone_evening').val();
	var ship_address1 = $('#ship_address1').val();
	var ship_address2 = $('#ship_address2').val();
	var ship_city = $('#ship_city').val();
	var ship_county = $('#ship_county').val();
	var ship_postcode = $('#ship_postcode').val();
	var ship_country = $('#ship_country').val();
	
	if(!name){
		alert('Name required');
		return false;
	}
	if(!surname){
		alert('Surname required');
		return false;
	}
	if(!email){
		alert('Email required');
		return false;
	}
	if(!address1){
		alert('Address required');
		return false;
	}
	if(!city){
		alert('City required');
		return false;
	}
	if(!county){
		alert('County required');
		return false;
	}
	
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	if(reg.test(email) == false) {
		alert('Invalid Email Address, please correct.');
		return false;
   	}
   
   	$.ajax({
		type: 'POST',
		data: "name=" + name + "&surname=" + surname + "&email=" + email + "&address1=" + address1 + "&city=" + city + "&county=" + county + "&title=" + title + "&company=" + company + "&address2=" + address2 + "&country=" + country + "&postcode=" + postcode + "&phone_day=" + phone_day + "&phone_evening=" + phone_evening + "&ship_address1=" + ship_address1 + "&ship_address2=" + ship_address2 + "&ship_city=" + ship_city + "&ship_county=" + ship_county + "&ship_postcode=" + ship_postcode + "&ship_country=" + ship_country + "&master_id=" + master_id,
		url: "ajax_submit_details.php",
		success: function(msg){
			if(msg == 'ok'){
				$('#' + form).submit();
			}else{
				alert(msg);
			}
		},
		error: function(a,msg){
			alert(msg);
		}
	});
   
		
	
}

function checklogin(ssid){
	
	$('.right_half').hide();
	$('.left_half').hide();
	
	$.ajax({
		type: 'POST',
		data: "ssid=" + ssid,
		url: "ajax_check_login.php",
		success: function(msg){
			if(msg == 'ok'){
				
				$('#login_check').html('<h2>Already logged in...</h2><div class="center_text"><br /><div class="signin_btn" onclick="document.location=\'checkout_three.php\'">PROCEED TO PAYMENT</div><br /><br /></div>');
				$('.right_half').slideUp();
				$('.left_half').slideUp();
				
			}else if(msg='not_found'){
				
				$('#login_check').delay(1000).html('<h2> Not Signed in... </h2><div class="center_text"><br /><br /><br /></div>').delay(1000).slideUp();
				$('.right_half').delay(2500).slideDown();
				$('.left_half').delay(2500).slideDown();
				
			}else{
				alert(msg);
			}
		},
		error: function(a,msg){
			alert(msg);
		}
	});
}

function show_collection(collection_id){
	document.location = "collection.php?id=" + collection_id;
}

function guest_login(email, order_master_id){
	
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	if(reg.test(email) == false) {
		alert('Invalid Email Address, please correct.');
		return false;
   }
	
	$.ajax({
		type: 'POST',
		data: "email=" + email + "&order_master_id=" + order_master_id,
		url: "ajax_guest_login.php",
		success: function(msg){
			if(msg == 'ok'){
				window.location="checkout_three.php";
			}else{
				alert(msg);
			}
		},
		error: function(a,msg){
			alert(msg);
		}
	});
}

function login(email, password, stay){
	
	$.ajax({
		type: 'POST',
		data: "email=" + email + "&password=" + password,
		url: "ajax_login.php",
		success: function(msg){
			if(msg == 'ok'){
				if(stay){
					$('#sign1').slideUp();
					$('#sign2').slideDown();
				}else{
					window.location="checkout_three.php";
				}
			}else{
				alert(msg);
			}
		},
		error: function(a,msg){
			alert(msg);
		}
	});
	
}

function check_register_form(){
	var name = $('#name').val();
	var surname = $('#surname').val();
	var email = $('#email').val();
	var password = $('#password').val();
	var password2 = $('#password2').val();
	var address1 = $('#address1').val();
	var city = $('#city').val();
	var country = $('#country').val();
	var missing = 0;
	
	if(name){
		$('#name').css('background-color','');
	}else{
		$('#name').css('background-color','#0FF');
		missing = missing + 1;
	}
	
	if(surname){
		$('#surname').css('background-color','');
	}else{
		$('#surname').css('background-color','#0FF');
		missing = missing + 1;
	}
	
	if(email){
		$('#email').css('background-color','');
	}else{
		$('#email').css('background-color','#0FF');
		missing = missing + 1;
	}
	
	if(password){
		$('#password').css('background-color','');
	}else{
		$('#password').css('background-color','#0FF');
		missing = missing + 1;
	}
	
	if(password2){
		$('#password2').css('background-color','');
	}else{
		$('#password2').css('background-color','#0FF');
		missing = missing + 1;
	}
	
	if(address1){
		$('#address1').css('background-color','');
	}else{
		$('#address1').css('background-color','#0FF');
		missing = missing + 1;
	}
	
	if(city){
		$('#city').css('background-color','');
	}else{
		$('#city').css('background-color','#0FF');
		missing = missing + 1;
	}
	
	if(country){
		$('#country').css('background-color','');
	}else{
		$('#country').css('background-color','#0FF');
		missing = missing + 1;
	}
	
	if(missing){
		alert('Some of the required info is missing. Please fill the turquoise fields.');
		return false;
	}else{
		// email validation
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		if(reg.test(email) == false) {
			alert('Invalid Email Address, please correct.');
			return false;
	   }
		
		// password check
		if(password != password2){
			alert('Passwords do not match, please correct.');
			return false;
		}
		
		$('#reg_frm').submit();
	}

}

function apply_discount(master_id, percent){
	$.ajax({
		type: 'POST',
		data: "percent=" + percent + "&master_id=" + master_id,
		url: "ajax_apply_discount.php",
		success: function(msg){
			if(msg == 'ok'){
				alert('Your discount code has been accepted and your discount applied.');
				location.reload(true);
			}else{
				alert(msg);
			}
		},
		error: function(a,msg){
			alert(msg);
		}
	});
}

function submit_discount(master_id){
	var code = $('#discount_code').val();
	
	$.ajax({
		type: 'POST',
		data: "code=" + code + "&master_id=" + master_id,
		url: "ajax_submit_discount_code.php",
		success: function(msg){
			if(msg == 'err'){
				// saved, going to step 2
				alert('Sorry, your discount code has not been recognized. Please contact sales@rocks.ie');
			}else if(msg == 'err2'){
				alert('Sorry, you have used a discount code already.');
			}else{
				apply_discount(master_id, msg);
			}
		},
		error: function(a,msg){
			alert(msg);
		}
	});
	
}

function step_2(master_id){
	
	//saving wrap note
	
	var wrap_note = $("#wrap_note").attr('value');
	
	$.ajax({
		type: 'POST',
		data: "wrap_note=" + wrap_note + "&master_id=" + master_id,
		url: "ajax_save_wrap_note.php",
		success: function(msg){
			if(msg == 'ok'){
				// saved, going to step 2
				window.location = "checkout_two.php"
			}else{
				alert(msg);
			}
		},
		error: function(a,msg){
			alert(msg);
		}
	});
}

function set_wrapping_type(master_id){
	
	var wrapping = $("input[name=wrapping]:checked").attr('value');
	
	$.ajax({
		type: 'POST',
		data: "wrapping=" + wrapping + "&master_id=" + master_id,
		url: "ajax_set_wrapping_type.php",
		success: function(msg){
			if(msg == 'ok'){
				// saved, nothing
			}else{
				alert(msg);
			}
		},
		error: function(a,msg){
			alert(msg);
		}
	});
}

function change_wrapping(order_id){
	
	if ($('#gift_' + order_id).attr('checked')) {
		wrap = '1';
	}else{
		wrap = '0';
	}
	
	$.ajax({
		type: 'POST',
		data: "order_id=" + order_id + "&wrap=" + wrap,
		url: "ajax_set_wrapping.php",
		success: function(msg){
			if(msg == 'ok'){
				recalculate(order_id);
			}else{
				alert(msg);
			}
		},
		error: function(a,msg){
			alert(msg);
		}
	});
}

function set_shipping(name, price, master_order_id){
	$('.thirds_wrap').slideUp();
	$('.loading').slideDown();
	$('.plusminus').slideUp();
	$.ajax({
		type: 'POST',
		data: "name=" + name + "&price=" + price + "&master_order_id=" + master_order_id,
		url: "ajax_set_shipping.php",
		success: function(msg){
			if(msg == 'ok'){
				$('#delivery_price').text(price);
				recalculate(master_order_id);
			}else{
				alert(msg);
			}
		},
		error: function(a,msg){
			alert(msg);
		}
	});
}

function recalculate(master_order_id){
	
	$('.thirds_wrap').slideUp();
	$('.loading').slideDown();
	$('.plusminus').slideUp();
	
	var total = 0;
	$('.total_price').each(function(){
		price = parseFloat($(this).text());
		total = total + price;
	});
	
	$('#veletotal').text(total);
	
	var delivery = parseFloat($('#delivery_price').text());
	var master_total = parseFloat(delivery + total).toFixed(2);
	
	$('#master_total').text(master_total);
	
	$.ajax({
		type: 'POST',
		data: "master_order_id=" + master_order_id + "&delivery=" + delivery + "&total=" + total + "&master_total=" + master_total,
		url: "ajax_save_price.php",
		success: function(msg){
			if(msg != 'ok'){
				alert(msg);
			}
			$('.thirds_wrap').slideDown();
			$('.loading').slideUp();
			$('.plusminus').slideDown();
		},
		error: function(a,msg){
			alert(msg);
		}
	});
	
}

function remove_bag(order_id){
	$.ajax({
		type: 'POST',
		data: "order_id=" + order_id,
		url: "ajax_remove_from_bag.php",
		success: function(msg){
			if(msg == 'ok'){
				$('#row_' + order_id + '_1').remove();
				$('#row_' + order_id + '_2').remove();
				recalculate(order_id);
			}else{
				alert(msg);
			}
		},
		error: function(a,msg){
			alert(msg);
		}
	});
}

function q_minus(order_id){
	var q = $('#quantity_' + order_id).val();
	if(q > 1){
		var newq = parseInt(q) - 1;
		$('#quantity_' + order_id).val(newq);
		quantity_change(order_id);
		recalculate(order_id);
	}
}

function q_plus(order_id){
	var q = $('#quantity_' + order_id).val();
	if(q < 9){
		var newq = parseInt(q) + 1;
		$('#quantity_' + order_id).val(newq);
		quantity_change(order_id);
		recalculate(order_id);
	}
}

function quantity_change(order_id){
	var new_quantity = $('#quantity_' + order_id).val();
	$.ajax({
		type: 'POST',
		data: "order_id=" + order_id + "&new_quantity=" + new_quantity,
		url: "ajax_change_quantity.php",
		success: function(msg){
			if(msg == 'ok'){
				var new_quantity = $('#quantity_' + order_id).val();
				var oneprice = $('#price_' + order_id).text();
				var allprice = (parseFloat(new_quantity) * parseFloat(oneprice)).toFixed(2);
				//alert(new_quantity + ' X ' + oneprice + ' = ' + parseFloat(new_quantity) * parseFloat(oneprice));
				$('#total_' + order_id).text(allprice);
				recalculate(order_id);
			}else{
				alert(msg);
			}
		},
		error: function(a,msg){
			alert(msg);
		}
	});
}

function add_to_bag(product_id,ssid){
	
	//if there is a size drop-down, check if size has been chosen

		if(($('#size').val() == '') && ($('#size').length > 0)){
			alert('Please select your size first.');
			return false;
		}else{
			var size_id = $('#size').val();
			var basicprice = $('#size').attr('basicprice');
			var priceplus = $('#size').attr('priceplus');
			
			
			$.ajax({
				type: 'POST',
				data: "product_id=" + product_id + "&ssid=" + ssid + "&size_id=" + size_id + "&basicprice=" + basicprice + "&priceplus=" + priceplus,
				timeout : 4000,
				tryCount : 0,
				retryLimit : 4,
				url: "ajax_add_to_bag.php",
				beforeSend: function(){
					$('#add_bag').addClass('disabled_addbag');
				},
				success: function(msg){
					if(msg == 'ok'){
						// added, play the effect
						//$('#add_bag').removeClass('disabled_addbag');
						alert('Added to your bag');
						inbag = parseInt($('#inthebag').text());
						inbag = inbag + 1;
						$('#inthebag').text(inbag);
					}
				}
			});
			
			
		}
}


function per_page_fn(){
	
	var actual = $('#perpage').html();
	
	if(actual == '20'){
		$('#perpage').html('100')
		$('#showperpage').html('show 20 per page')
		per_page = '100';
	}

	if(actual == '100'){
		$('#perpage').html('20')
		$('#showperpage').html('show 100 per page')
		per_page = '20';
	}
		
	filter_list();
}

function filter_list(){
	
	// BRANDS
	brands_string = '';
	$('#brand_filters_slide').find(':checkbox').each(function(){
		if($(this).attr('checked')){
			brand = $(this).attr('id');
			brand = brand.substring(6,brand.length);
			brands_string = brands_string + brand + '|';
		}
	});
	
	// COLLECTIONS
	collections_string = '';
	$('#collections_filters_slide').find(':checkbox').each(function(){
		if($(this).attr('checked')){
			collection = $(this).attr('id');
			collection = collection.substring(11,collection.length);
			collections_string = collections_string + collection + '|';
		}
	});
	
	// CATEGORIES
	categories_string = '';
	$('#categories_filters_slide').find(':checkbox').each(function(){
		if($(this).attr('checked')){
			category = $(this).attr('id');
			category = category.substring(9,category.length);
			categories_string = categories_string + category + '|';
		}
	});
	
	// STYLES
	styles_string = '';
	$('#style_filters_slide').find(':checkbox').each(function(){
		if($(this).attr('checked')){
			style = $(this).attr('id');
			style = style.substring(6,style.length);
			styles_string = styles_string + style + '|';
		}
	});
	
	// SETTINGS
	settings_string = '';
	$('#setting_filters_slide').find(':checkbox').each(function(){
		if($(this).attr('checked')){
			setting = $(this).attr('id');
			setting = setting.substring(8,setting.length);
			settings_string = settings_string + setting + '|';
		}
	});
	
	
	// MATERIALS
	materials_string = ''
	$('#materials_filters_slide').find('.material_check').each(function(){
		if($(this).hasClass('active')){
			material = $(this).attr('id');
			material = material.substring(9,material.length);
			materials_string = materials_string + material + '|';
		}
	});
	
	// OCCASIONS
	occasions_string = '';
	$('#occasion_filters_slide').find(':checkbox').each(function(){
		if($(this).attr('checked')){
			occasion = $(this).attr('id');
			occasion = occasion.substring(9,occasion.length);
			occasions_string = occasions_string + occasion + '|';
		}
	});
	
	$('#materials_filter').html(materials_string);
	$('#occasions_filter').html(occasions_string);
	$('#collections_filter').html(collections_string);
	$('#categories_filter').html(categories_string);
	
	if(!($('#brands_filter').hasClass('protected'))){
		$('#brands_filter').html(brands_string);
	}
	
	$('#styles_filter').html(styles_string);
	$('#settings_filter').html(settings_string);
	
	category_id = $('#category').html();
	per_page = $('#perpage').html();
	pricesort = $('#pricesort').html();
	brands_filter = $('#brands_filter').html();
	page_nr = $('#current_page').html();
	pricefrom = $('#pricefrom').html();
	priceto = $('#priceto').html();
	occasions = $('#occasions_filter').html();
	styles = $('#styles_filter').html();
	settings = $('#settings_filter').html();
	collections = $('#collections_filter').html();
	categories = $('#categories_filter').html();
	
	load_products(category_id, per_page, page_nr, pricesort, brands_filter, materials_string, pricefrom, priceto, occasions, styles, settings, collections, categories);
}

$(function() {
    $("#slider-range").slider({
      range: true,
      min: 0,
      max: 5000,
	  step: 100,
      values: [0, 5000],
      slide: function(event, ui) {
        $("#price_from").html('&euro;' + ui.values[0]);
		dodo = ui.values[1];
		if(dodo==5000){
			dodo = dodo + '+';
		}
		$("#price_to").html('&euro;' + dodo);
      },
	  change: function(event, ui) {
		  $('#pricefrom').html(ui.values[0]);
		  $('#priceto').html(ui.values[1]);
		  filter_list();
		  
	  }
    });
	
	
    $("#price_from").html('&euro;' + $( "#slider-range" ).slider( "values", 0 ));
	dodo = $( "#slider-range" ).slider( "values", 1 );
	if(dodo==5000){
		dodo = dodo + '+';
	}
	
	$("#price_to").html('&euro;' + dodo);
});


function load_products(category_id, per_page, page_nr, pricesort, brands_filter, material_filter, pricefrom, priceto, occasions, styles, settings, collections, categories){
	
	//alert(pricefrom + '-' + priceto);
	if(typeof pricefrom == 'undefined') pricefrom = '';
	if(typeof priceto == 'undefined') priceto = '';
	if(typeof occasions == 'undefined') occasions = '';
	if(typeof material_filter == 'undefined') material_filter = '';
	if(typeof brands_filter == 'undefined') brands_filter = '';
	if(typeof pricesort == 'undefined') pricesort = '';
	if(typeof page_nr == 'undefined') page_nr = '';
	if(typeof per_page == 'undefined') per_page = '';
	if(typeof category_id == 'undefined') category_id = '';
	if(typeof styles == 'undefined') styles = '';
	if(typeof settings == 'undefined') settings = '';
	if(typeof collections == 'undefined') collections = '';
	if(typeof categories == 'undefined') categories = '';
	
	if(settings == null) settings = '';
	if(styles == null) styles = '';
	if(collections == null) collections = '';
	if(categories == null) categories = '';
	
	$.ajax({
		type: 'POST',
		data: "category_id=" + category_id + "&per_page=" + per_page + "&page_nr=" + page_nr + "&pricesort=" + pricesort + "&brands_filter=" + brands_filter + "&material_filter=" + material_filter + "&pricefrom=" + pricefrom + "&priceto=" + priceto + "&occasions=" + occasions + "&styles_filter=" + styles + "&settings_filter=" + settings + "&collections=" + collections + "&categories=" + categories,
		timeout : 4000,
	    tryCount : 0,
    	retryLimit : 4,
		url: "ajax_load_products.php",
		beforeSend: function(){
			$('#loading').slideDown();
			//$('#product_list').slideUp();
		},
		success: function(msg){
			$('#product_list').html(msg);
			$('#loading').slideUp();
			//$('#product_list').slideDown();
		},
		error: function(){
			this.tryCount++;
			if (this.tryCount <= this.retryLimit) {
                //try again
                $.ajax(this);
                return;
				$('#loading').slideUp();
            	return;
            }
		}
	});
}

function price(highlow){
	$('#pricesort').html(highlow);
	filter_list();
}
