/*
	Copyright DTDigital         :: www.dtdigital.com.au ::
	Unauthorised modification / use is a criminal offence, and
	will be prosecuted to the fullest extent permitted by law.
	All Rights Reserved
*/

NAVarr = new Array("home", "stores", "range", "diy", "gifts", "sustainability", "services", "contact", "entersite");
if (document.images)
{
	for (var NAVitem in NAVarr)
	{ 
		 for (var NAVicount=0; NAVicount < 2; NAVicount++)
		 { 
				eval("b" + NAVarr[NAVitem] + NAVicount + " = new Image()");
				eval("b" + NAVarr[NAVitem] + NAVicount + ".src = 'assets/swap_" + NAVarr[NAVitem] + "_" + NAVicount + ".gif'");
		 } 
	} 
}

function BturnOn(imageName)
{
	if (document.images)
	{
		var imageNumber = "";
		if (arguments.length > 1) imageNumber = arguments[1];
		document [imageName + imageNumber].src = eval("b" + imageName + "1.src");
	}
}

function BturnOff(imageName)
{
	if (document.images)
	{
		var imageNumber = "";
		if (arguments.length > 1) imageNumber = arguments[1];
		document [imageName + imageNumber].src = eval("b" + imageName + "0.src");
	}
}

function BturnOnInput(obj,imageName)
{
	if (obj.src)
	{
		obj.src = eval("b" + imageName + "1.src");
	}
}

function BturnOffInput(obj,imageName)
{
	if (obj.src)
	{
		obj.src = eval("b" + imageName + "0.src");
	}
}

function ddRedirect(dd)
{
	if (dd.options[dd.selectedIndex].value != "") self.location = dd.options[dd.selectedIndex].value;
}


function ismaxlength(obj, maxlength)
{
	if (obj.value.length>maxlength)
	{
		obj.value=obj.value.substring(0,maxlength)
	}
}

function addQuantityToLink(link)
{
	var quantityValue = document.getElementById("ProductDetailQuantity").value
	
	return link.href + "&quantity=" + quantityValue;
}


//Online Catalogue
function openOnlineCataloguePopup()
{
	Width = 1000;
	Height = 680;
	TopPosn = screen.height ? (screen.height - Height) / 2 : 100;
	LeftPosn = screen.width ? (screen.width - Width) / 2 : 100;
    window.open("emag/bunnings/index.html?document=catalogue/" + getEmagRegion(), "_blank", "top=" + TopPosn + ",left=" + LeftPosn + ",width=" + Width + ",height=" + Height + ", resizable=yes");
}
	              
function getEmagRegion()
{
	if (window.emagRegion)
	{
		return emagRegion;
	}
	else 
	{
		return "catalogue";
	}
}


var _jQueryLoaded = (typeof(jQuery) == "function");

if (_jQueryLoaded)
{
    $(document).ready(function () {
        $("a.hideselectsinie6").mouseover(function () {
            $("select.hideselectinie6").css("visibility", "hidden");
        });

        $("a.hideselectsinie6").mouseout(function () {
            $("select.hideselectinie6").css("visibility", "visible");
        });


        // Open 'Join Our Team' links in a blank tab
        $("a span:contains('Join Our Team')").parent().attr('target', '_blank');
        $("a:contains('Join Our Team')").attr('target', '_blank');
        $('a').click(function () {
            if ($(this).attr('title') == 'Join Our Team') {
                $(this).attr('target', '_blank');
            }
        });

        // Add height to image wrapper to allow room for the 'Added to Cart' message
        var listView = $('#listview-overview .list').size();
        var addToCartDisplayed = $('.quantity-wrap').size();
        if (listView == 0 & addToCartDisplayed > 0) {
            $('.one-column-2 .img-placeholder').css('height', '172px');
        }

        // Show 'Added to Cart' message after product selection


        // Applies tooltip functionality
        tooltip();
        listQuestions();

        $("#maincontent textarea").keypress(function (e) {
            if (e.which == 13) {
                $(this).val($(this).val() + "\n");
                return false;
            }
        });

        // add clear div just above footer
        $('#footer').before('<div class="clear">&nbsp;</div>');


        // Fixes flicker on nav in IE6.
        var isLocationActive = false;

        $(".hoverify").hover(function () {
            if ($(this).find("img:first").attr("src").match("_1")) {
                isLocationActive = true;
            } else {
                $(this).find("img:first").attr("src", ($(this).find("img").attr("src").replace("_0", "_1")));
            }
        }, function () {
            (isLocationActive) ? isLocationActive = false : $(this).find("img:first").attr("src", ($(this).find("img").attr("src").replace("_1", "_0")));
        });

        // Hide store feedback form items
        $("#checkbox_style6").hide();

        // General forms - Show/hide based on radio selected
        $(".radio01 input[type=radio]").click(function () {
            ($(this).val() == "No") ? $("#checkbox_style6").show() : $("#checkbox_style6").hide();
        });

        // General forms - Show/hide required fields depending on checkbox
        $("input[type=checkbox].checkbox01").click(function () {
            $(".required01").toggle().parent().next("input:text").next('.error').hide();
        });

        // General forms - if the check box field is checked, then we show the required fields.
        if ($("input[type=checkbox].checkbox01").is(":checked")) {
            $(".required01").show();
        }
        else {
            $(".required01").hide();
        }

        // General forms - Show/hide required fields depending on radiobutton selected value.
        $(".radio01 input[type=radio]").click(function () {

            if ($(this).val() == "Yes") {
                $(".required02").show().parent().next("input:text").next('.error').hide();
            }
            else {
                $(".required02").hide().parent().next("input:text").next('.error').hide();
            }
        });

        // Checkbox fields = .required01
        // Radio fields = .required02

        // General forms - if radio button is checked, then we show the required fields.        
        if ($(".radio01 input[type=radio]:checked").val() == "Yes") {
            $(".required02").show();
        } else {
            $(".required02").hide();
        }

        //show/hide visacard/mastercard message
        $(".visacardholder").hide();
        $(".mastercardholder").hide();

        $(".search1_autocomplete").autocomplete(
			"WebServices/keywords.aspx",
			{
			    delay: 0,
			    minChars: 0,
			    maxItemsToShow: 10
			}
		);


        $(".creditcardtype").change(function () {

            if ($(this).val() == "Visa") {
                $(".visacardholder").show();
                $(".mastercardholder").hide();
            } else if ($(this).val() == "MasterCard") {
                $(".mastercardholder").show();
                $(".visacardholder").hide();
            } else {
                $(".mastercardholder").hide();
                $(".visacardholder").hide();
            }
        });

        //Show/hide green hover box. 
        $(".h-box-link").hover(function () {
            $(".hover-box").removeClass("hover-box-lostfocus");
            $(".hover-box").addClass("hover-box-focus").css("display", "block").css("left", "auto").css("right", "-100px").css("top", "20px");
        }, function () {

        });

        $(".hover-box").mouseover(function () {

            if ($(".hover-box").hasClass("hover-box-lostfocus")) {
                $(".hover-box").removeClass("hover-box-lostfocus");
                $(".hover-box").addClass("hover-box-focus").css("display", "block").css("left", "auto").css("right", "-100px").css("top", "20px");
            } else {
                $(".hover-box").addClass("hover-box-focus").css("display", "block").css("left", "auto").css("right", "-100px").css("top", "20px");
            }
        }).mouseout(function () {

            if ($(".hover-box").hasClass("hover-box-focus")) {

                $(".hover-box").addClass("hover-box-lostfocus");

                $(".hover-box").removeClass("hover-box-focus").css("display", "none").css("right", "auto").css("left", "-9999px");

            }
        });


        //-------------------------------
        // Clear Inputs
        //-------------------------------
        var clearInputPrevious = '';
        // clear input on focus
        $('input:text, textarea').focus(function () {
            if ($(this).val() == $(this).attr('title')) {
                clearInputPrevious = $(this).val();
                $(this).val('');
            }
        });
        // if field is empty afterward, add text again
        $('input:text, textarea').blur(function () {
            if ($(this).val() == '') {
                $(this).val($(this).attr('title'));
            }
        }).trigger("blur");
        // if field is empty afterward, add text again
        $('.buttons input').bind("focus mouseenter", function () {
            $('input:text, textarea').each(function () {
                if ($(this).val() == $(this).attr('title')) {
                    $(this).val('');
                }
            });
        }).bind("blur mouseleave", function () {
            $('input:text, textarea').each(function () {
                if ($(this).val() == '') {
                    $(this).val($(this).attr('title'));
                }
            });
        });


        //-------------------------------
        // Print 
        //-------------------------------
        $('.wishlist-toolbar').prepend('<a class="print-this btn wishlist-print" href="#print">print wishlist<span class="btn-img"></span></a>');

        $('a.print-this').click(function () {
            window.print();
            return false;
        });

        //-------------------------------
        // Equal Height 
        //-------------------------------


        $(".compare_title a.underl_link").equalHeights();
        $(".compare_feat").equalHeights();
        $(".compare_spec").equalHeights();
        $(".product-overview-2 .col").equalHeights();
        $(".four-column-2 .four_col-row .col").equalHeights();


        //-------------------------------
        // Wishlist Actions
        //-------------------------------
        var wishlistCookie = $.cookie('BunningsWarehouseWishlist');
        var wishlistIDs = (wishlistCookie != null ? wishlistCookie.split('|') : null);

        // Initialises wishlist action buttons for each product displayed
        $('.product-overview, .product-overview-2, .products_detail, .compare_title').each(function () {
            var button = $(this).find('a.wishlist-action');
            if (button.length <= 0) return;
            var productID = button.attr('href').replace('#', '');
            var exists = false;

            if (wishlistCookie != null && wishlistIDs.length > 0) {
                for (var x = 0; x < wishlistIDs.length; x++) {
                    if (productID == wishlistIDs[x]) {
                        exists = true;
                        break;
                    }
                }
            }

            if (exists) {
                button.removeClass('wishlist-add').addClass('wishlist-remove').attr('title', 'Remove from wishlist').html('Remove from wishlist<span class="btn-img"></span>');
            } else {
                button.removeClass('wishlist-remove').addClass('wishlist-add').attr('title', 'Add to wishlist').html('Add to wishlist<span class="btn-img"></span>');
            }

        });

        // Attaches event to product's wishlist action buttons
        $('a.wishlist-action').click(function () {
            var button = $(this);
            var type = button.hasClass('wishlist-add') ? 'Add' : 'Remove';
            var productID = button.attr('href').replace('#', '');

            $.ajax({
                type: 'POST',
                url: 'WebServices/Wishlist.asmx/' + type,
                data: '{productItemId: "' + productID + '"}',
                contentType: 'application/json; charset=utf-8',
                dataType: 'json',
                success: function (msg) {

                    if (type == 'Add') {
                        button.removeClass('wishlist-add').addClass('wishlist-remove').attr('title', 'Remove from wishlist').html('Remove from wishlist<span class="btn-img"></span>');
                    } else {
                        button.removeClass('wishlist-remove').addClass('wishlist-add').attr('title', 'Add to wishlist').html('Add to wishlist<span class="btn-img"></span>');
                    }
                    //updateWishlistWidget();
                    $('#wishlistItemCount').text(msg.d);
                },
                error: function () {
                    return false;
                }
            });

            return false;
        });


        $('a.remove-from-wishlist-in-member-account-page').click(function () {
            var button = $(this);
            var productID = button.attr('href').replace('#', '');

            $.ajax({
                type: 'POST',
                url: 'WebServices/Wishlist.asmx/Remove',
                data: '{productItemId: "' + productID + '"}',
                contentType: 'application/json; charset=utf-8',
                dataType: 'json',
                success: function (msg) {

                    $('#wishlistItemCount').text(msg.d);

                    // Why you can't use getJSON - http://encosia.com/using-jquery-to-consume-aspnet-json-web-services/
                    // to consume ASP.NET AJAX web services that are JSON serialized
                    // refresh wish list.                    
                    $.ajax({
                        type: 'POST',
                        url: 'Webservices/Wishlist.asmx/RefreshWishList',
                        data: '{}',
                        contentType: 'application/json; charset=utf-8',
                        dataType: 'json',
                        success: function (wishListMsg) {

                            if (wishListMsg.d.ItemCount < 3) {
                                button.parent().parent().fadeOut().slideUp();
                            }

                            if (wishListMsg.d.ItemCount == 0) {
                                button.parent().parent().parent().fadeOut().slideUp();
                                $(".wish-list-title").fadeOut().slideUp();
                                return;
                            }

                            if (wishListMsg.d.WishListItems[0] != null) {

                                $("div.account-content div.three-col:nth-child(1) a.product-name").attr("href", wishListMsg.d.WishListItems[0].ProductUrl);
                                $("div.account-content div.three-col:nth-child(1) a.product-name").html(wishListMsg.d.WishListItems[0].ImageUrl);

                                $("div.account-content div.three-col:nth-child(1) .description a.prod-title").attr("href", wishListMsg.d.WishListItems[0].ProductUrl);
                                $("div.account-content div.three-col:nth-child(1) .description a.prod-title").html(wishListMsg.d.WishListItems[0].ProductName);

                                $("div.account-content div.three-col:nth-child(1) .description span.product-sku").html(wishListMsg.d.WishListItems[0].Sku);

                                if (wishListMsg.d.WishListItems[0].Availability == "inStore") {
                                    var $inStoreDivOne = $("div.account-content div.three-col:nth-child(1) .instore-jq");
                                    if ($inStoreDivOne != null) {
                                        $inStoreDivOne.removeClass("hide");
                                        $inStoreDivOne.html("<strong>Available In Store</strong>");
                                    }

                                    var $specialOrderDivOne = $("div.account-content div.three-col:nth-child(1) .special-order-jq");
                                    if ($specialOrderDivOne != null) {
                                        $specialOrderDivOne.addClass("hide");
                                    }

                                    var $inStoreDivOneNonJq = $("div.account-content div.three-col:nth-child(1) .instore");
                                    if ($inStoreDivOneNonJq != null) {
                                        $inStoreDivOneNonJq.addClass("hide");
                                    }

                                    var $specialOrderDivOneNonJq = $("div.account-content div.three-col:nth-child(1) .special-order");
                                    if ($specialOrderDivOneNonJq != null) {
                                        $specialOrderDivOneNonJq.addClass("hide");
                                    }
                                }

                                if (wishListMsg.d.WishListItems[0].Availability == "specialOrder") {
                                    var $specialOrderDivOne = $("div.account-content div.three-col:nth-child(1) .special-order-jq");
                                    if ($specialOrderDivOne != null) {
                                        $specialOrderDivOne.removeClass("hide");
                                        $specialOrderDivOne.html("<span class=\"ico special-2\" title=\"These products are not available in store but may be ordered.\">Special Order</span>");
                                    }

                                    var $inStoreDivOne = $("div.account-content div.three-col:nth-child(1) .instore-jq");
                                    if ($inStoreDivOne != null) {
                                        $inStoreDivOne.addClass("hide");
                                    }

                                    var $inStoreDivOneNonJq = $("div.account-content div.three-col:nth-child(1) .instore");
                                    if ($inStoreDivOneNonJq != null) {
                                        $inStoreDivOneNonJq.addClass("hide");
                                    }

                                    var $specialOrderDivOneNonJq = $("div.account-content div.three-col:nth-child(1) .special-order");
                                    if ($specialOrderDivOneNonJq != null) {
                                        $specialOrderDivOneNonJq.addClass("hide");
                                    }
                                }

                                if (wishListMsg.d.WishListItems[0].Active.toString().toLowerCase() == "true") {
                                    var $spanProductPrice = $("div.account-content div.three-col:nth-child(1) .description  span.prod-price");
                                    if ($spanProductPrice != null) {
                                        $spanProductPrice.text(wishListMsg.d.WishListItems[0].Price);
                                    }
                                    var $spanProductPerUnit = $("div.account-content div.three-col:nth-child(1) .description  span.prod-per-unit");
                                    if ($spanProductPerUnit != null) {

                                        if (wishListMsg.d.WishListItems[0].SaleUnit != "") {
                                            $spanProductPerUnit.text(wishListMsg.d.WishListItems[0].SaleUnit);
                                        }
                                        else {
                                            $spanProductPerUnit.text("each");
                                        }
                                    }
                                }

                                $("div.account-content div.three-col:nth-child(1) .description a.remove-from-wishlist-in-member-account-page").attr("href", "#" + wishListMsg.d.WishListItems[0].ProductId);

                            } // end of first element

                            if (wishListMsg.d.WishListItems[1] != null) {

                                $("div.account-content div.three-col:nth-child(2) a.product-name").attr("href", wishListMsg.d.WishListItems[1].ProductUrl);
                                $("div.account-content div.three-col:nth-child(2) a.product-name").html(wishListMsg.d.WishListItems[1].ImageUrl);

                                $("div.account-content div.three-col:nth-child(2) .description a.prod-title").attr("href", wishListMsg.d.WishListItems[1].ProductUrl);
                                $("div.account-content div.three-col:nth-child(2) .description a.prod-title").html(wishListMsg.d.WishListItems[1].ProductName);

                                $("div.account-content div.three-col:nth-child(2) .description span.product-sku").html(wishListMsg.d.WishListItems[1].Sku);

                                if (wishListMsg.d.WishListItems[1].Availability == "inStore") {
                                    var $inStoreDivTwo = $("div.account-content div.three-col:nth-child(2) .instore-jq");
                                    if ($inStoreDivTwo != null) {
                                        $inStoreDivTwo.removeClass("hide");
                                        $inStoreDivTwo.html("<strong>Available In Store</strong>");
                                    }

                                    var $specialOrderDivTwo = $("div.account-content div.three-col:nth-child(2) .special-order-jq");
                                    if ($specialOrderDivTwo != null) {
                                        $specialOrderDivTwo.addClass("hide");
                                    }

                                    var $inStoreDivTwoNonJq = $("div.account-content div.three-col:nth-child(2) .instore");
                                    if ($inStoreDivTwoNonJq != null) {
                                        $inStoreDivTwoNonJq.addClass("hide");
                                    }
                                    var $specialOrderDivTwoNonJq = $("div.account-content div.three-col:nth-child(2) .special-order");
                                    if ($specialOrderDivTwoNonJq != null) {
                                        $specialOrderDivTwoNonJq.addClass("hide");
                                    }
                                }

                                if (wishListMsg.d.WishListItems[1].Availability == "specialOrder") {
                                    var $specialOrderDivTwo = $("div.account-content div.three-col:nth-child(2) .special-order-jq");
                                    if ($specialOrderDivTwo != null) {
                                        $specialOrderDivTwo.removeClass("hide");
                                        $specialOrderDivTwo.html("<span class=\"ico special-2\" title=\"These products are not available in store but may be ordered.\">Special Order</span>");
                                    }

                                    var $inStoreDivTwo = $("div.account-content div.three-col:nth-child(2) .instore-jq");
                                    if ($inStoreDivTwo != null) {
                                        $inStoreDivTwo.addClass("hide");
                                    }

                                    var $inStoreDivTwoNonJq = $("div.account-content div.three-col:nth-child(2) .instore");
                                    if ($inStoreDivTwoNonJq != null) {
                                        $inStoreDivTwoNonJq.addClass("hide");
                                    }

                                    var $specialOrderDivTwoNonJq = $("div.account-content div.three-col:nth-child(2) .special-order");
                                    if ($specialOrderDivTwoNonJq != null) {
                                        $specialOrderDivTwoNonJq.addClass("hide");
                                    }
                                }

                                if (wishListMsg.d.WishListItems[1].Active.toString().toLowerCase() == "true") {
                                    var $spanProductPrice = $("div.account-content div.three-col:nth-child(2) .description  span.prod-price");
                                    if ($spanProductPrice != null) {
                                        $spanProductPrice.text(wishListMsg.d.WishListItems[1].Price);
                                    }
                                    var $spanProductPerUnit = $("div.account-content div.three-col:nth-child(2) .description  span.prod-per-unit");
                                    if ($spanProductPerUnit != null) {

                                        if (wishListMsg.d.WishListItems[1].SaleUnit != "") {
                                            $spanProductPerUnit.text(wishListMsg.d.WishListItems[1].SaleUnit);
                                        }
                                        else {
                                            $spanProductPerUnit.text("each");
                                        }
                                    }

                                }

                                $("div.account-content div.three-col:nth-child(2) .description a.remove-from-wishlist-in-member-account-page").attr("href", "#" + wishListMsg.d.WishListItems[1].ProductId);
                            } // end of second element

                            if (wishListMsg.d.WishListItems[2] != null) {

                                $("div.account-content div.three-col:nth-child(3) a.product-name").attr("href", wishListMsg.d.WishListItems[2].ProductUrl);
                                $("div.account-content div.three-col:nth-child(3) a.product-name").html(wishListMsg.d.WishListItems[2].ImageUrl);

                                $("div.account-content div.three-col:nth-child(3) .description a.prod-title").attr("href", wishListMsg.d.WishListItems[2].ProductUrl);
                                $("div.account-content div.three-col:nth-child(3) .description a.prod-title").html(wishListMsg.d.WishListItems[2].ProductName);

                                $("div.account-content div.three-col:nth-child(3) .description span.product-sku").html(wishListMsg.d.WishListItems[2].Sku);

                                if (wishListMsg.d.WishListItems[2].Availability == "inStore") {
                                    var $inStoreDivThree = $("div.account-content div.three-col:nth-child(3) .instore-jq");
                                    if ($inStoreDivThree != null) {
                                        $inStoreDivThree.removeClass("hide");
                                        $inStoreDivThree.html("<strong>Available In Store</strong>");
                                    }

                                    var $specialOrderDivThree = $("div.account-content div.three-col:nth-child(3) .special-order-jq");
                                    if ($specialOrderDivThree != null) {
                                        $specialOrderDivThree.addClass("hide");
                                    }

                                    var $inStoreDivThreeNonJq = $("div.account-content div.three-col:nth-child(3) .instore");
                                    if ($inStoreDivThreeNonJq != null) {
                                        $inStoreDivThreeNonJq.addClass("hide");
                                    }

                                    var $specialOrderDivThreeNonJq = $("div.account-content div.three-col:nth-child(3) .special-order");
                                    if ($specialOrderDivThreeNonJq != null) {
                                        $specialOrderDivThreeNonJq.addClass("hide");
                                    }
                                }

                                if (wishListMsg.d.WishListItems[2].Availability == "specialOrder") {
                                    var $specialOrderDivThree = $("div.account-content div.three-col:nth-child(3) .special-order-jq");
                                    if ($specialOrderDivThree != null) {
                                        $specialOrderDivThree.removeClass("hide");
                                        $specialOrderDivThree.html("<span class=\"ico special-2\" title=\"These products are not available in store but may be ordered.\">Special Order</span>");
                                    }

                                    var $inStoreDivThree = $("div.account-content div.three-col:nth-child(3) .instore-jq");
                                    if ($inStoreDivThree != null) {
                                        $inStoreDivThree.addClass("hide");
                                    }

                                    var $inStoreDivThreeNonJq = $("div.account-content div.three-col:nth-child(3) .instore");
                                    if ($inStoreDivThreeNonJq != null) {
                                        $inStoreDivThreeNonJq.addClass("hide");
                                    }

                                    var $specialOrderDivThreeNonJq = $("div.account-content div.three-col:nth-child(3) .special-order");
                                    if ($specialOrderDivThreeNonJq != null) {
                                        $specialOrderDivThreeNonJq.addClass("hide");
                                    }
                                }

                                if (wishListMsg.d.WishListItems[2].Active.toString().toLowerCase() == "true") {
                                    var $spanProductPrice = $("div.account-content div.three-col:nth-child(3) .description  span.prod-price");
                                    if ($spanProductPrice != null) {
                                        $spanProductPrice.text(wishListMsg.d.WishListItems[2].Price);
                                    }
                                    var $spanProductPerUnit = $("div.account-content div.three-col:nth-child(3) .description  span.prod-per-unit");
                                    if ($spanProductPerUnit != null) {

                                        if (wishListMsg.d.WishListItems[2].SaleUnit != "") {
                                            $spanProductPerUnit.text(wishListMsg.d.WishListItems[2].SaleUnit);
                                        }
                                        else {
                                            $spanProductPerUnit.text("each");
                                        }
                                    }

                                }

                                $("div.account-content div.three-col:nth-child(3) .description a.remove-from-wishlist-in-member-account-page").attr("href", "#" + wishListMsg.d.WishListItems[2].ProductId);
                            } // end of third element
                        }
                    });

                },
                error: function () {
                    return false;
                }
            });

            return false;
        });


        // Remove button within wishlist page
        $('a.remove-from-wishlist').click(function () {
            var button = $(this);
            var productID = button.attr('href').replace('#', '');

            $.ajax({
                type: 'POST',
                url: 'WebServices/Wishlist.asmx/Remove',
                data: '{productItemId: "' + productID + '"}',
                contentType: 'application/json; charset=utf-8',
                dataType: 'json',
                success: function (msg) {
                    button.parent().fadeOut().slideUp();
                    //updateWishlistWidget();
                    $('#wishlistItemCount').text(msg.d);

                },
                error: function () {
                    return false;
                }
            });

            return false;
        });


        // Remove button within wishlist page
        $('.WishListClearButton').click(function () {

            $.ajax({
                type: 'POST',
                url: 'WebServices/Wishlist.asmx/Clear',
                data: '{}',
                contentType: 'application/json; charset=utf-8',
                dataType: 'json',
                success: function (msg) {
                    $('#wishlistitemsdiv').fadeOut().slideUp();
                    $('#wishlistitemsdiv').html('<div class="one-column-1"><div class="content-2"><p>You have no products in your wishlist.</p></div></div>');
                    $('#wishlistitemsdiv').fadeIn().slideDown();
                    $('#wishlistItemCount').text(msg.d);
                },
                error: function () {
                    return false;
                }
            });
            return false;
        });






        //        $(document).ready(function () {          
        //            // Wishlist side widget item count
        //            if ($('#wishlistItemCount').length > 0) {
        //                updateWishlistWidget();
        //            }

        //        });


        //----------------------------------------------------------------------------------------------------------------------------------------------
        //                                                             RemoteCart Actions
        //------------------------------------------------------------------------------------------------------------------------------------------------

        // Initialises RemoteCart action buttons for each product displayed
        $('.product-overview, .product-overview-2, .products_detail, .compare_title').each(function () {
            var button = $(this).find('a.RemoteCart-action');
            if (button.length <= 0) return;
            var productID = button.attr('href').replace('#', '');
            var select = $(this).find('select.RemoteCart-QantitySelect');

            if (button.hasClass('RemoteCart-add')) {
                button.attr('title', 'Add to Cart').html('Add to Cart<span class="btn-img"></span>');
                select.show();
            }
            //            else {
            //                button.attr('title', 'Remove from RemoteCart').html('Remove from RemoteCart<span class="btn-img"></span>');
            //                select.hide();
            //            }
        });

        // Attaches event to product's RemoteCart action buttons
        $('a.RemoteCart-action').click(function () {
            var button = $(this);
            var quantitySelect = $(this).parent().find('select.RemoteCart-QantitySelect');
            var type = button.hasClass('RemoteCart-add') ? 'Add' : 'Remove';
            var productID = button.attr('href').replace('#', '');
            var productQuantity;
            var requestValue;

            if (type == 'Add') {
                productQuantity = quantitySelect.val();
                if (productQuantity == null || productQuantity == "") {
                    alert("Please choose a Quantity!");
                    return false;
                } else {
                    requestValue = '{productItemId: "' + productID + '", quantity:' + productQuantity + '}';
                }
            }
            if (type == 'Remove') {
                requestValue = '{productItemId: "' + productID + '"}';
            }

            $(this).parent().parent().prev().find('.cart-confirm').fadeIn("slow");
            setTimeout(function () {
                $('p.cart-confirm').fadeOut('slow');
            }, 2000);

            $(this).next().fadeIn("slow");
            setTimeout(function () {
                $('p.cart-confirm-list').fadeOut('slow');
            }, 2000);

            $.ajax({
                type: 'POST',
                url: 'WebServices/RemoteCart.asmx/' + type,
                data: requestValue,
                contentType: 'application/json; charset=utf-8',
                dataType: 'json',
                success: function (msg) {

                    //                    if (type == 'Add') {
                    //                        button.removeClass('RemoteCart-add').addClass('RemoteCart-remove').attr('title', 'Remove from RemoteCart').html('Remove from RemoteCart<span class="btn-img"></span>');
                    //                        quantitySelect.hide();
                    //                    } else {
                    //                        button.removeClass('RemoteCart-remove').addClass('RemoteCart-add').attr('title', 'Add to RemoteCart').html('Add to RemoteCart<span class="btn-img"></span>');
                    //                        quantitySelect.show();
                    //                    }

                    var partsArray = msg.d.split('-');

                    var quantity = parseInt(partsArray[0]);
                    var price = parseFloat(partsArray[1]);
                    updateShoppingCartWidget(quantity, price);
                },
                error: function () {
                    alert("Please choose a Quantity!");
                    return false;
                }
            });


            return false;
        });

        function updateShoppingCartWidget(quantity, price) {
            var shoppingCartWidget = $('#panel_shopping-cart');

            if (shoppingCartWidget.length > 0) {
                if (quantity > 0) {
                    shoppingCartWidget.show();

                    $('.shoppingcartquantity').parent("p").show();
                    $('.shoppingcartquantity').html(quantity);

                    if (price > 0) {
                        $('.shoppingcartprice').parent("p").show();
                        $('.shoppingcartprice').html($.currency(price));
                    } else {
                        $('.shoppingcartprice').parent("p").hide();
                    }
                } else {
                    shoppingCartWidget.hide();
                }
            }

            var shoppingCartToolbar = $('#toolbar_shopping-cart');

            if (shoppingCartToolbar.length > 0) {
                if (quantity > 0) {
                    shoppingCartToolbar.show();
                    $('#toolbar_shopping-cart_quantity').html(quantity);
                } else {
                    shoppingCartToolbar.hide();
                }
            }
        }

        //-------------------------------
        // Product Compare Actions
        //-------------------------------

        var compareCheckboxes = $('input[name="compareProducts"]');
        if (compareCheckboxes.length > 0) {

            var compareProductCookie = $.cookie('BunningsWarehouseProductCompare');
            var compareProductIDs = (compareProductCookie != null ? compareProductCookie.split('|') : null);

            // Sets the state of the checkboxes
            if (compareProductCookie != null && compareProductIDs.length > 0) {
                for (var x = 0; x < compareProductIDs.length; x++) {
                    $('#compare' + compareProductIDs[x]).attr('checked', 'checked');
                }
            }

            checkCompareLimit();

            // Action for when checkbox is clicked
            compareCheckboxes.click(function () {
                var productID = $(this).val();
                var checked = $(this).attr('checked');

                var type = (checked ? 'Add' : 'Remove');

                $.ajax({
                    type: 'POST',
                    url: 'WebServices/ProductCompare.asmx/' + type,
                    data: '{productItemId: "' + productID + '"}',
                    contentType: 'application/json; charset=utf-8',
                    dataType: 'json',
                    success: function () {
                        checkCompareLimit();
                        return true;
                    },
                    error: function () {
                        return false;
                    }
                });
            });
        }

        $('a.remove-compare-product').click(function () {
            var $this = $(this);
            var productID = $this.attr('href').replace('#', '');

            $.ajax({
                type: 'POST',
                url: 'WebServices/ProductCompare.asmx/Remove',
                data: '{productItemId: "' + productID + '"}',
                contentType: 'application/json; charset=utf-8',
                dataType: 'json',
                success: function () {
                    $this.parent().parent().fadeOut();
                }
            });

            return false;
        });

        //-------------------------------


        //-------------------------------
        // Narrow by
        //-------------------------------
        $('#filter .narrow-by h3').wrapInner('<a href="#" />').children('a').append('<span class="ico-narrow-arrow"></span>');

        $('#filter .close .list-container').hide();

        $('#filter .narrow-by h3 a').click(function (event) {
            $(this).parent().next('.list-container').toggle().parent().toggleClass('close');
            event.preventDefault();
        });

        //-------------------------------

    });
}

//-------------------------------
// Wishlist Functions
//-------------------------------

// Wishlist side widget item count
function updateWishlistWidget() {
    $.ajax({
        type: 'POST',
        url: 'WebServices/Wishlist.asmx/ItemCount',
        data: '{}',
        contentType: "application/json; charset=utf-8",
        dataType: 'json',
        success: function (msg) {
            $('#wishlistItemCount').text(msg.d);
        },
        error: function () {
            return false;
        }
    });
}

//-------------------------------
// Product Compare Functions
//-------------------------------

function checkCompareLimit() {
    $.ajax({
        type: 'POST',
        url: 'WebServices/ProductCompare.asmx/ProductItemIds',
        data: '{}',
        contentType: "application/json; charset=utf-8",
        dataType: 'json',
        success: function (msg) {
            var ids = msg.d;
            var selector = '';

            if (ids.length > 0) {
                for (var x = 0; x < ids.length; x++) {
                    selector += (x != 0 ? ', ' : '') + '#compare' + ids[x]
                }
                $('input[name = "compareProducts"]').not(selector).attr('checked', '');
                $(selector).attr('checked', 'checked');
            }
        }
    });
}

//-------------------------------

// Simple tooltip script without the need for plugins
this.tooltip = function(){	
	/* CONFIG */		
		var xOffset = 0;
		var yOffset = 0;	
		// these 2 values are fallback values if the browser fails to determine the offset		
	/* END CONFIG */		
	
	// Glossary link tooltip
	$("a.tooltip").hover(function(e){											  
		xOffset = $(this).offset().top;
		yOffset = $(this).offset().left;
		this.t = this.title;
		this.title = "";	
		$(this).css({ position: "relative" });	
		
		$("body").append("<div id='tooltip'><div class='tooltip-top'>&nbsp;</div><div class='tooltip-content'><div class='tooltip-padding'><div class='container-info noborder'><h4>"+ this.t.split("::::")[0] + "</h4>" + this.t.split("::::")[1] + "</div></div></div><div class='tooltip-btm'>&nbsp;</div></div>");
		var tooltipHeight = $("#tooltip").height();
		$("#tooltip")
			.css({
				top: (xOffset-tooltipHeight) + "px",
				left: (yOffset) + "px"
			}).show();
	},function(){
		this.title = this.t;
		$("#tooltip").remove();
	})
};

function listQuestions() 
{
	$(".list-questions a.qns").veil({
		showIndex: "none", 
		speed: 400, 
		activeClass: "active",
		hoverClass: "hover"
	});
}

