// JavaScript Document
$(document).ready(function () {

    $('.ms_overlay').hide();
	$('#pageholder').show();

    var numberOfMachine = 0;

    var updateResults = function (a) {
		if (a == 1) {
			$('.ms_resultsfound').empty().text(a + ' result of ' + startingMachines + ' found');
		} else {
			$('.ms_resultsfound').empty().text(a + ' results of ' + startingMachines + ' found');
		};
	};

    var startingMachines = 0;

  
    $('.ms_machine').each(function (index) {
        startingMachines += 1;
	  //var normalPrice = $(this).find('.ms_machine_price').text();
	  //var cleanedPrice = normalPrice.replace(/[^\d\.\-\ ]/g, '');
	  //var priceOnly = cleanedPrice.substr(0, 9);
	  //$('.ms_machine_price').parent().attr('sortprice', priceOnly);
	  $(this).hide();
    });

    var checkAmps = function (a, o, mt) {
            var amps = a;
            var mina = minAmp;
            var maxa = maxAmp;

            if (mt == 'Plasma') {
		    mina = minCut;
                maxa = maxCut;
            };

            if (amps >= mina && amps <= maxa) {
                o.show();
                numberOfMachine += 1;
            } else {
                o.hide();
            };
        };

    var numChecked = 0;

    var checkWarranty = function (a, o, mt) {
		var warrantyNum = o.find('.ms_machine_warranty').text();

		warrantyNum = parseInt(warrantyNum);

		if (guarenteeAmount <= warrantyNum) {
			o.show();
			checkAmps(a, o, mt);
		} else if (guarenteeAmount == 0) {
			o.show();
			checkAmps(a, o, mt);
		} else {
			o.hide();
		};
	};

    var checkMakes = function (a, o, mt) {

		var currentMake = o.find('.ms_machine_make').text();
		
		//alert(currentMake);

		$('.selectmake').find('input').each(function (index) {
			var isMake = $(this).attr('checked');
			
			if (isMake == true) {
				var selectedMake = $(this).attr('value');

				if (currentMake == selectedMake) {
					o.show();
					checkWarranty(a, o, mt);
				};
				numChecked += 1
			};
		});

		if (numChecked == 0) {
			checkWarranty(a, o, mt);
		};
	};

    var checkPrice = function (f, o, a, mt) {
		
		var minv = minVal;
		var maxv = maxVal;

		minv = parseFloat(minv);
		maxv = parseFloat(maxv);

		var machinePrice = f;

		var machineSub = machinePrice.substr(1, 50);

		var cleanstring = machineSub.replace(/[^\d\.\-\ ]/g, '');

		cleanstring = parseFloat(cleanstring);

		if (cleanstring >= minv && cleanstring <= maxVal) {
			
			checkMakes(a, o, mt);
		} else {
			o.hide();
		};
	};


        //Find Machine By Type
    var machineTypeFind = function (a) {
		
		var mt = '';

		switch (a) {
		case 'Mig':
			mt = 'MIG';
			break;
		case 'Tig':
			mt = 'TIG';
			break;
		case 'Arc':
			mt = 'ARC/Inverter';
			break;
		case 'Plasma':
			mt = 'Plasma';
			break;
		case '':
			mt = 'all';
			break;
		};

		$('.ms_machine_type').each(function (index) {
			if ($(this).text() == mt) {
				currentprice = $(this).parent().parent().parent().parent().parent().parent().find('.ms_machine_price').text();
				
				if (mt == 'Plasma') {	
					currentAmps = $(this).parent().parent().parent().parent().parent().parent().find('.ms_cleancut').text();
				} else {
					currentAmps = $(this).parent().parent().parent().parent().parent().parent().find('.ms_amps').text();
				};

				checkPrice(currentprice, $(this).parent().parent().parent().parent().parent().parent(), currentAmps, mt);

				updateResults(numberOfMachine);
			} else if (mt == 'all') {
				
				currentprice = $(this).parent().parent().parent().parent().parent().parent().find('.ms_machine_price').text();
				currentAmps = $(this).parent().parent().parent().parent().parent().parent().find('.ms_cleancut').text();

				var currentWarranty = $(this).parent().parent().parent().parent().parent().parent().find('.ms_machine_warranty').text();

				checkPrice(currentprice, $(this).parent().parent().parent().parent().parent().parent(), currentAmps, mt);

				updateResults(numberOfMachine);
			} else {
				updateResults(numberOfMachine);
			};
		});

	}

    var changePlasma = 0;

    var searchMachines = function () {
		//Hide Machines
		$('.ms_machine').each(function (index) {
			$(this).hide();
		});

		//Slider machine type
		var machineType = "";

		numChecked = 0;

		$('.typeform').find('input').each(function (index) {
			if ($(this).is(':checked')) {
				machineType = $(this).attr('name');

				if ($(this).attr('name') == 'Plasma') {
					$("#slider-amp").hide();
					$("#amerage").hide();
					$('.ampcut').hide();
					$("#slider-cut").show();
					$("#cleancut").show();
					$('.ccut').show();
					if (changePlasma != 0) {
						$(".ms_option_cleancut").show();
						$(".ms_option_amp").hide();
					}
				} else {
					$("#slider-amp").show();
					$("#amerage").show();
					$('.ampcut').show();
					$("#slider-cut").hide();
					$("#cleancut").hide();
					$('.ccut').hide();
					if (changePlasma != 0) {
						$(".ms_option_cleancut").hide();
						$(".ms_option_amp").show();
					}
				};
			};
		});

		numberOfMachine = 0;

		//Machines By Type
		machineTypeFind(machineType);
	};

    var minVal = 0;
    var maxVal = 11000;

    var minAmp = 0;
    var maxAmp = 1000;

    var minCut = 0;
    var maxCut = 60;

    var guarenteeAmount = '';

    var showRest = function() {
       $('.ms_option_make').show();
	 $('.ms_option_warranty').show();
    };

    $(function () {
        $("#slider-guarentee").slider({
            value: 0,
            min: 0,
            max: 3,
            step: 1,
            slide: function (event, ui) {
                $(".guarentee").text(ui.value + ' +');

                guarenteeAmount = ui.value;

                if (guarenteeAmount == 0) {
                    $(".guarentee").text('Any');
                }
                searchMachines();
            }
        });
        $(".guarentee").val($("#slider-guarentee").slider("value"));


        $("#slider-amp").slider({
            range: true,
            min: 0,
            max: 1000,
            values: [0, 1000],
            slide: function (event, ui) {
                $("#amerage").text(ui.values[0] + "amps - " + ui.values[1] + "amps");

                minAmp = ui.values[0];
                maxAmp = ui.values[1];
		    showRest();
                searchMachines();
            }
        });
        $("#amerage").text($("#slider-amp").slider("values", 0) + "amps - " + $("#slider-amp").slider("values", 1) + "amps");


        $("#slider-cut").slider({
            range: true,
            min: 0,
            max: 60,
            values: [0, 60],
            slide: function (event, ui) {
                $("#cleancut").text(ui.values[0] + "mm - " + ui.values[1] + "mm");

                minCut = ui.values[0];
                maxCut = ui.values[1];
		    showRest();
                searchMachines();
            }
        });
        $("#cleancut").text($("#slider-cut").slider("values", 0) + "mm - " + $("#slider-cut").slider("values", 1) + "mm");


        $("#slider").slider({
            value: 1,
            min: 0,
            max: 2,
            step: 1,
            slide: function (event, ui) {
                $("#typeNum").text(ui.value);
                searchMachines();
            }
        });
        $("#typeNum").text($("#slider").slider("value"));

    });

    $('.typeform').find('input').click(function () {
        $('.typeform').find('input').attr('checked', false);
        $('.typeform').removeClass('checked');
        $(this).attr('checked', true);
        $(this).addClass('checked');

	  $(".ms_option_price").show();
        searchMachines();
    });

    $('.selectmake').find('input').click(function () {
        searchMachines();
    });

    $("#slider-cut").hide();
    $("#cleancut").hide();
    $('.ccut').hide();

    function addCommas(nStr) {
        nStr += '';
        x = nStr.split('.');
        x1 = x[0];
        x2 = x.length > 1 ? '.' + x[1] : '';
        var rgx = /(\d+)(\d{3})/;
        while (rgx.test(x1)) {
            x1 = x1.replace(rgx, '$1' + ',' + '$2');
        };
        return x1 + x2;
    };

    var changeSelectors = function (mi) {
		var minval = mi;
		if (minval == 'Min Value') {
			$('.max_value').attr("disabled", "disabled");
		} else {
			$('.max_value').removeAttr("disabled");

			var cleanstring = minval.replace(/[^\d\.\-\ ]/g, '');

			cleanstring = parseInt(cleanstring);

			iCount = 10500;

			var newOptions = '';

			while (cleanstring <= iCount) {

				if (cleanstring <= 400) {
					cleanstring += 100;
				} else if (cleanstring == 500) {
					cleanstring += 500;
				} else {
					cleanstring += 1000;
				};
				newNumber = addCommas(cleanstring)

				newOptions += '<option>£' + newNumber + '</option>';
			};

			$('.max_value').empty().html('<option>Please Select</option>' + newOptions);
		};

	};

    $("select.min_value").change(function () {
        var str = "";
        $("select.min_value option:selected").each(function () {
            str += $(this).text();
        });
        changeSelectors(str);
    });
    $('.max_value').attr("disabled", "disabled");

    $("select.max_value").change(function () {
        var str = "";
        $("select.max_value option:selected").each(function () {
            str += $(this).text();

            var cleanMin = $("select.min_value option:selected").text().replace(/[^\d\.\-\ ]/g, '');
            var cleanMax = str.replace(/[^\d\.\-\ ]/g, '');

            minVal = cleanMin;
            maxVal = cleanMax;

		$('.typeform').find('input').each(function (index) {
			if ($(this).is(':checked')) {
				machineType = $(this).attr('name');

				if (machineType == 'Plasma') {
					$(".ms_option_cleancut").show();
					$(".ms_option_amp").hide();
				} else {
					$(".ms_option_amp").show();
					$(".ms_option_cleancut").hide();
				}
				changePlasma = 1;
			}
		});

            searchMachines();
        });
    });

    $('.emptyMake').click(function () {
        $('.make_selector').find('input').attr('checked', false);
        searchMachines();
    });

    $('.resetAll').click(function () {
        if (window.top == window) {
            window.setTimeout('location.reload()', 100);
        } else {

        };
    });

    //$('.ms_machine').each(function (index) {
    //    $(this).hide();
    //});

    $('.ms_machine_warranty').each(function (index) {
    		var ye = $(this).text();

		if (ye == 1 || ye == '1') {
			$(this).parent().find('span.yoy').empty().text('year');
		}
  });

  
});
