$(document).ready(function() {
    /* Function to check if an ID exists */
    jQuery.fn.exists = function() {
        return (this.is('*'))
    }

    /* Search box */
    if ($("#txtSearch").exists()) {
        var defaultString = "Enter search term here";
        $("#txtSearch").val(defaultString);

        $("#txtSearch").focus(function() {
            if ($(this).val() == defaultString) {
                $(this).val("");
            }
        }).blur(function() {
            if ($(this).val() == "") {
                $(this).val(defaultString);
            }
        });
    }

    $("#language").languageSelector();

    /* Suckerfish for FCS */

    $("#nav ul li").not(".three-tier-sub").children("ul").hide();

    $("#nav li").hover(function() {
        $(this).parent().find(".three-tier-sub").siblings().not(".sfHover").children("ul").hide();
        $(this).addClass("sfHover");
        $(this).children("ul").show();
        if ($(this).hasClass("three-tier")) {
            $(this).find(".three-tier-sub").addClass("sfHover").children("ul").show();
        }
        else if ($(this).hasClass("three-tier-sub") == false || $(this).parent().find(".three-tier-sub").hasClass("sfHover") == false) {
            $(this).parent().find(".three-tier-sub").removeClass("sfHover").children("ul").hide();
        }
    }, function() {
        $(this).removeClass("sfHover");
        /*if ($(this).hasClass("three-tier")) {
        $(this).parent().find(".three-tier-sub").addClass("sfHover").children("ul").show();
        }*/
    });

    /* Compare Fins functionality */
    if ($(".compareButton").exists()) {
        var compareFinsButton = $(".compareButton a");
        var compareFinsTip = $(".compareButton div");

        compareFinsButton.click(function() {
            if (!$(this).hasClass("active")) { // Do not compare if button is not active
                return false;
            }
        });

        var valueField = $("#hdnSelectedIdDiv input");
        var currentValue = valueField.val();
        var countValue = currentValue.substr(0, currentValue.length - 1);
        var prods = countValue.split(",");
        //var pageCount = $("#productList input:checked").size();
        var selectedCount = prods.length;

        // Add active class to button if more than 2 fins are checked
        if ((selectedCount) > 1) {
            if (compareFinsButton.hasClass("active") == false) {
                compareFinsButton.addClass("active");
            }
        }

        compareFinsTip.hide();
        compareFinsButton.hover(function() { // Compare Fins button tip hover
            compareFinsTip.show();
        }, function() {
            compareFinsTip.hide();
        });
        /*
        $("#productList input:checkbox").click(function() {
        var t = $("#hdnSelectedIdDiv input").val();
        t = t.substr(0, t.length - 1);
        var prods = t.split(",");
        var pageCount = $("#productList input:checked").size();
        var selectedCount = (t.length == 0) ? 0 : prods.length;
        alert((selectedCount + pageCount))

            // Add active class to button if more than 2 fins are checked
        if ((selectedCount + pageCount) > 1) {
        //                if (compareFinsButton.hasClass("active") == false) {
        compareFinsButton.addClass("active");
        //              }
        }
        // Remove active class if there aren't enough fins selected to compare
        else {
        compareFinsButton.removeClass("active");
        }

            // Disable selection of fin, show and scroll to Compare Fins tip
        if ((selectedCount + pageCount) > 3) {
        compareFinsTip.show();
        var compareBtnTargetOffset = $("#compareButton").offset();
        var compareBtnTargetTop = compareBtnTargetOffset.top;
        $("html, body").animate({ scrollTop: compareBtnTargetTop }, 400);
        return false;
        }
        });*/
    }

    /* Sub nav */
    $("ul.subNav li a").siblings("ul").children("li").hide();

    if ($("ul.subNav").exists() && $("#products").exists() == false) {
        $("ul.subNav").find("ul").children("li").hide();
        $("ul.subNav li a").click(function() { return true; });
    }

    if ($("ul.subNav li").hasClass("selected") && $("ul.subNav li").children("ul").length >= 1) {
        $("ul.subNav li.selected").children("ul").children("li").show();
        $("ul.subNav > li.selected > a").addClass("openDown");
    }

    $("ul.subNav li a").click(function() {
        if ($(this).siblings("ul").length >= 1) {
            if ($(this).hasClass("openDown")) {
                $(this).removeClass("openDown");
                $(this).siblings("ul").removeClass("open").children("li").hide();
                return false;
            }
            else {
                $(this).siblings("ul").addClass("open").children("li").show();
                $(this).addClass("openDown");
                return false;
            }
        }

    });
});

jQuery.fn.collapseContent = function() {
	$(this).find("h2.expand").next().hide();
	$(this).find("h2").click(function() {
		if ($(this).hasClass("collapse")) {
			$(this).removeClass("collapse").addClass("expand").next().hide();
		}
		else if ($(this).hasClass("expand")) {
			$(this).removeClass("expand").addClass("collapse").next().show();
		}
	});
}

jQuery.fn.languageSelector = function() {
	$("#languageSelect").hide();
	$(this).click(function() {
		$("#languageSelect").slideToggle(200);
		$('html,body').animate({scrollTop: 0}, 200);
		return false;				   
	});
}

function Resize () {
        var newWidth = $('#videoContent').width();
        $('#videoContent').css("margin-left", -(newWidth / 2));
}

function CheckRemove(value,checkField) {

    var valueField = $("#hdnSelectedIdDiv input");
    var currentValue = valueField.val();

    if (valueField.val().length == 0) {
        valueField.val(value + ',');
    }
    else {
        currentValue = valueField.val();
        var countValue = currentValue.substr(0, currentValue.length - 1);
        var prods = countValue.split(",");
        //var pageCount = $("#productList input:checked").size();
        var selectedCount = prods.length;

        if ($(".compareButton").exists()) {
            var compareFinsButton = $(".compareButton a");
            var compareFinsTip = $(".compareButton div");

            // Add active class to button if more than 2 fins are checked
            if ((selectedCount+1) > 1) {
                //                if (compareFinsButton.hasClass("active") == false) {
                compareFinsButton.addClass("active");
                //              }
            }
            // Remove active class if there aren't enough fins selected to compare
            else {
                compareFinsButton.removeClass("active");
            }

            // Disable selection of fin, show and scroll to Compare Fins tip
            if ((selectedCount) >= 3) {
         
                if (currentValue.indexOf(value) >= 0) {
                    var temp = currentValue.replace(value + ',', '');
                    valueField.val(temp);
                }
                else {
                    valueField.val(currentValue + value + ',');
                }
                currentValue = valueField.val();
                countValue = currentValue.substr(0, currentValue.length - 1);
                prods = countValue.split(",");
                //var pageCount = $("#productList input:checked").size();
                selectedCount = prods.length;

                if ((selectedCount) >= 3) {
                    compareFinsTip.show();
                    var compareBtnTargetOffset = $("#compareButton").offset();
                    var compareBtnTargetTop = compareBtnTargetOffset.top;
                    $("html, body").animate({ scrollTop: compareBtnTargetTop }, 400);
                    if (currentValue.indexOf(value) >= 0) {
                        var temp = currentValue.replace(value + ',', '');
                        valueField.val(temp);
                    }
                    
                    checkField.checked = false;
                    return false;
                }
            }
            else {
                if (currentValue.indexOf(value) >= 0) {
               var temp = currentValue.replace(value + ',', '');
                    //} else {
                        //temp = currentValue.replace(value, '');
                    //}
                    //alert(temp)
                    valueField.val(temp);
                }
                else {
                    valueField.val(currentValue + value + ',');
                }
                //alert(valueField.val());
            }
        }
    }

}
