/*
 Function will check to see if the user has checked the 'No Preference' checkbox on step one.  If so
 the system will just make sure that the data in the measurement dropdowns is not used.  If not, then
 the system will grab the data from the width and height dropdowns
 */
function setStepOneInputToDropDowns(){

    var widthInch = Number($("#wInches").val());
    var widthPartial = Number($("#wPartial").val());

    var hInch = Number($("#hInches").val());
    var hPartial = Number($("#hPartial").val());

    if(hInch == 0 || widthInch == 0){
        alert("Please select a valid Height/Width combination for your opening.");
        return false;
    }

    // this code doesn't check to see if this is shower or bath doors, but it doesn't have to
    // since only bathtub can have a door with height less than 55.5
    if ((hInch + hPartial) < 55.5){
        alert ("The smallest size you can choose for bathtub shower doors opening height is 55 1/2 inches. Please choose a height greater than 55 1/2 inches.");
        return false;
    } else {

        $("#filterOPENING_WIDTHMax").val(widthInch + widthPartial);
        $("#filterOPENING_WIDTHMin").val(widthInch + widthPartial);
        $("#filterOPENING_HEIGHT").val(hInch + hPartial);
        return true;
    }
}

function updateCount(){

    var url = $('#selectionForm').serialize();
    // the category should be set by the 'landing' page
    url += "&categoryName=" + category;

    $.ajax({
        type: "GET",
        url: 'selectorCount',
        data: url,
        dataType: "json",
        success: handleJSReturn,
        error: function() {
            return false;
        }
    });

    // numProducts
}

function handleJSReturn(data){
    count = data.prodCount;
    $('#numProducts').html(count);

}

function specificDoorSelected(heightInch, heightPartial, widthInch, widthPartial){

    // by default, we want to uncheck the NA checkbox
    $('input[name=noPreferenceHW]').attr('checked', false);

    $('#wPartial option[value=' + widthPartial +']').attr('selected', 'true');
    $('#wInches option[value=' + widthInch +']').attr('selected', 'true');

    $('#hPartial option[value=' + heightPartial +']').attr('selected', 'true');
    $('#hInches option[value=' + heightInch +']').attr('selected', 'true');

}

$(function() {
	$(function() {
		var compareCount = getCompareArray(compareSection);
		$('#shwrDrSlctr-compareCount p span').html(compareCount.length);
		$('input.addToCompare').each(function () {
		
			var isCheckedValue = $(this).attr('value');
		    var checkArray = getCompareArray(compareSection);
		    for (var i=0; i<checkArray.length; i++) {
		        if (checkArray[i]==isCheckedValue) {
		            $(this).attr('checked', true);
		        }
		    }
	    });
	});
    $("#selectionForm").submit(function() {
        if ($("#_page").val() == '0') {
            // figure out if the check box is selected
            if ($("#noPreferenceHW").attr('checked')){
                return true;
            } else {

                var hInch = $("#hInches").val();
                var hPartial = $("#hPartial").val();
                var wInch = $("#wInches").val();

                if (hInch == "" || wInch == ""){
                    alert("Please select a valid Height/Width combination for your opening.");
                    return false;
                }

                // this code doesn't check to see if this is shower or bath doors, but it doesn't have to
                // since only bathtub can have a door with height less than 55.5
                if ((hInch + hPartial) < 55.5){
                    alert ("The smallest size you can choose for bathtub shower doors opening height is 55 1/2 inches. Please choose a height greater than 55 1/2 inches.");
                    return false;
                } else {

                    setStepOneInputToDropDowns();
                    return true;
                }
            }
        }
    });

    // code to build the input value for the height and width if the user clicks the 'NA' checkbox
    // on the first step
    //$("input[@name='noPreferenceHW']").click()(function(){
    $('#noPreferenceHW').change(function(){
        // if the check box is selected, just reset the height and width back to "".  If it is
        // not selected, then use the values that are in the drop downs
        if ($(this).is(':checked')){
            $("#filterOPENING_WIDTHMax").val("");
            $("#filterOPENING_WIDTHMin").val("");
            $("#filterOPENING_HEIGHT").val("");
        } else {
            setStepOneInputToDropDowns();
        }

    });

    // if the user chooses a value from the drop down, make the drop downs be the current values
    $(".hwDropDown").change(function() {
        // by default, we want to uncheck the NA checkbox
        $('input[name=noPreferenceHW]').attr('checked', false);
    });
	$(window).scroll(function() {
		if($(window).scrollTop() > ($('#shwrDrSlctr-content').height() - 380)) {
			$("#shwrDrSlctr-rightColumn-staticPos-glass").css("top", $('#shwrDrSlctr-content').height() - 330 + "px");
			$("#shwrDrSlctr-rightColumn-staticPos-glass").css("bottom", 0);
		} else {
	    	$("#shwrDrSlctr-rightColumn-staticPos-glass").css("top", $(window).scrollTop() + "px");
		}
		
		if($(window).scrollTop() > ($('#shwrDrSlctr-content').height() - 500)) {
			$("#shwrDrSlctr-rightColumn-staticPos-results").css("top", $('#shwrDrSlctr-content').height() - 450 + "px");
			$("#shwrDrSlctr-rightColumn-staticPos-results").css("bottom", 0);
		} else {
	    	$("#shwrDrSlctr-rightColumn-staticPos-results").css("top", $(window).scrollTop() + "px");
		}
	});

	
	$('input.backBtn, input.nextBtn, img.getStarted, input.viewResults, input.startOver, img.startOver').hover(
		function() {
			$(this).attr("src",$(this).attr("src").replace("-off","-on"));
		}, function() {
			$(this).attr("src",$(this).attr("src").replace("-on","-off"));
		});

    $("#sortBy").change(function() {
        $(this).parents("form").get(0).submit();
    });

    $('.itemsPerPage').change(function() {
        $('#selectionForm').attr("action", "wizard-results");
        $('#pageSize').val($(this).val());
        $('#selectionForm').append('<input type="hidden" name="pageNumber" value="1" />');
        $('#selectionForm').submit();
    });

    $('.sortBySelector').change(function() {

        if ($(this).val() != ""){
            $('#selectionForm').attr("action", "wizard-results");
            $('#sortBy').val($(this).val());
            $('#selectionForm').append('<input type="hidden" name="pageNumber" value="1" />');
            $('#selectionForm').submit();
        }
    });

    $('.viewResults').click(function() {
        $('#selectionForm').attr("action", "wizard-results");
        return true;
    });

    $('#shwrDrSlctr-default input.checkBox').click(function() {
        // check to see if this is the 'no preference' or not.  If it is, deselect every
        // other check box.  If not, the deselect the 'no preference' checkbox
        if($(this).attr('id') == 'noPreference'){
            $('input.checkBox').each(function(){
                $(this).attr('checked', false);
            });
            $('#noPreference').attr('checked', true);
        } else {
            // just deselect the 'noPreference' checkbox
            $('#noPreference').attr('checked', false);
        }

        updateCount();
    });

    $('#shwrDrSlctr-default input.radioBtn').click(function() {
        // check to see if this is the 'no preference' or not.  If it is, deselect every
        // other check box.  If not, the deselect the 'no preference' checkbox
        updateCount();
    });
	
    $('#shwrDrSlctr-glassTexture input.checkBox').click(function() {
        
		$('#noPreference').attr('checked', false);
        updateCount();
    });
	
	$('#shwrDrSlctr-glassPattern input.checkBox').click(function() {

		$('#noPreference').attr('checked', false);
        updateCount();
    });
	
	$("#shwrDrSlctr-default li.enlarge a").click(function() {
		var enlargedImg = $(this).parents("div.shwrDrSlctr-col-1col").children("img").attr("src").split("/");
		var enlargedImgName = enlargedImg[enlargedImg.length - 1];
		var enlargedImgTitle = $(this).parents("div.shwrDrSlctr-col-1col").children("img").attr("alt");
		
		$("#overlay-enlargeImg #enlargedPhoto").attr("src", "/common/images/enlarged/" + enlargedImgName);
		$("#overlay-enlargeImg #enlarge-title").text(enlargedImgTitle);
		
		var myScrollTop = $(document).scrollTop();
		if((myScrollTop > -1) && (!$.browser.safari)) {
			$('#overlay-enlargeImg').css("top", (myScrollTop + ($(window).height() / 2) - 180) + "px");
		} else {
			$('#overlay-enlargeImg').css("top", (self.pageYOffset + ($(window).height() / 2) - 180) + "px");
		}
		$("#overlay-enlargeImg-header img.close-enlargeImg").click(
		function() {
		$('#overlay-enlargeImg').hide();
		}
		);
		setTimeout(function() { $("#overlay-enlargeImg").show(); }, 300); // delay to prevent flicker
		$("#overlay-enlargeImg").jqDrag('#overlay-enlargeImg-header');
	});
	
	$("li.wizardEnlarge a").click(
		function() {
            var enlargedImg = $("#prodDtl-photo").children("img").attr("src").split("/");
	        var enlargedImgName = enlargedImg[enlargedImg.length - 1];
            var enlargedImgTitle = $(this).parents("#prodDtl-photo").children("img").attr("alt");

            $("#overlay-enlargeImg #enlargedPhoto").attr("src", "/onlinecatalog/images/jpg450x300/" + enlargedImgName);
            $("#overlay-enlargeImg #enlarge-title").text(enlargedImgTitle);
            $("#overlay-enlargeImg div#enlarge-caption").text($("#prodDtl-photo span").text());

            if((document.documentElement.scrollTop > -1) && (!$.browser.safari)) {
				$('#overlay-enlargeImg').css("top", (document.documentElement.scrollTop + ($(window).height() / 2) - 180) + "px");
			} else {
				$('#overlay-enlargeImg').css("top", (self.pageYOffset + ($(window).height() / 2) - 180) + "px");
			}
			$("#overlay-enlargeImg-header img.close-enlargeImg").click(
				function() {
					$('#overlay-enlargeImg').hide();
				}
			);

            $("#overlay-enlargeImg").show();
            $("#overlay-enlargeImg").jqDrag('#overlay-enlargeImg-header');
        }
	);
	
	$('#shwrDrSlctr-measurements-prepopulated-choices a').click(function() {
		$('#shwrDrSlctr-measurements-prepopulated-choices a').removeClass('activeStall');
		$(this).addClass('activeStall');
		var whichShowerStall = $(this).attr('rel');
		
		var arrowPosition = whichShower[whichShowerStall][0];
		var imgPosition = whichShower[whichShowerStall][1];
		$('#shwrDrSlctr-measurements-prepopulated-images img').attr('src', '/common/images/shwrDrSlctr-' + whichShowerStall + '.jpg');
		$('#shwrDrSlctr-measurements-prepopulated-choices img.blueArrow-activeStall').css('top', arrowPosition + 'px');
		$('#shwrDrSlctr-measurements-prepopulated-images img').css('top', imgPosition + 'px');
		$('#shwrDrSlctr-measurements-prepopulated-choices img.blueArrow-activeStall').css('display','block');
		$('#shwrDrSlctr-measurements-prepopulated-images img').css('display','block');
		return false;
	});
	
	
    $("#overlay-frameComparison-header img.close-frameComparison").click(
            function() {
                $('#overlay-frameComparison').hide();
            }
    );

    $("img.frameDesign-frameComparisonLink").click(function() {
        var myScrollTop = $(document).scrollTop();
        if((myScrollTop > -1) && (!$.browser.safari)) {
            $('#overlay-frameComparison').css("top", (myScrollTop + ($(window).height() / 2) - 180) + "px");
        } else {
            $('#overlay-frameComparison').css("top", (self.pageYOffset + ($(window).height() / 2) - 180) + "px");
        }
        $("#overlay-frameComparison").jqDrag('#overlay-frameComparison-header');
        $("#overlay-frameComparison").show();
        return false;
    });
	
	$('input.addToCompare').click(function() {
		if($(this).is(':checked')) {
			addToCompareSelector();
			var compareCount = getCompareArray(compareSection);
			$('#shwrDrSlctr-compareCount p span').html(compareCount.length);
		} else {
			var currentItem = $(this).attr('value');
			var result = [];
		    var compareArray = getCompareArray(compareSection);
		    for (var i=0; i<compareArray.length; i++) {
		        if (compareArray[i]!=currentItem) {
		            result[result.length] = compareArray[i];
		        }
		    }
		    setCompareCookie(compareSection, result);
			
			$('#shwrDrSlctr-compareCount p span').html(result.length);
		}
		
	});
});

