function getQueryString(key, default_) {

    if (default_ == null) default_ = "";
    key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regex = new RegExp("[\\?&]" + key + "=([^&#]*)");
    var qs = regex.exec(window.location.href);
    if (qs == null)
        return default_;
    else
        return qs[1];
}

function getText(t) {
    if ($.browser.msie) {
    }
    else {
    }
}

(function ($) {
    $.fn.watermark = function (c, t) {
        return this.each(function () {
            var i = $(this);
            i.focus(function () {
                i.hasClass(c) && i.removeClass(c).val('');
            })
			.blur(function () {
			    !i.val() && i.addClass(c).val(t);
			})
			.change(function () {
			    i.hasClass(c) && i.removeClass(c);
			    !i.val() && i.addClass(c).val(t);
			})
			.closest('form').submit(function () {
			    i.removeWatermark(c);
			});
            i.blur();
        });
    };
    $.fn.removeWatermark = function (c) {
        return this.each(function () {
            $(this).hasClass(c) && $(this).val('');
        });
    };
})(jQuery);

/***************************************************/

function resizeWindow() {
    try {
        $('.fb_ltr').height('auto');
    }
    catch (err) {
        $('.fb_ltr').height('auto');
    }
};

$(window).load(function () {
    if ($.browser.safari);
    resizeWindow();
});

$(document).ready(function () {
    var h2all, h2a, h2b;
    $('.productDetailHeader p, .productDetailHeader h1, .productDetailHeader h2, .goPlaySection p, .accordionContent ul li, .stainSolver .solveSteps .stepsWrapper dl dd p').each(function () {
        if( $(this).attr("class") == "titlehasbreak")
  	{
   	return;
}

        h2all = $.trim($(this).html());
        h2a = h2all.slice(0, h2all.lastIndexOf(' '));
        hyp = h2a.slice(h2a.lastIndexOf(' '), h2a.length);
        if (hyp.indexOf('-') > 0) {
            h2a = h2a.slice(0, h2a.lastIndexOf(' ')) + "<br />" + hyp;
        }
        h2b = "&nbsp;" + h2all.slice(h2all.lastIndexOf(' ') + 1);
        var fullContent = (h2a + h2b);    
        $(this).html(fullContent);
    });

    /*To set site actions menu on the top of the navigation bar*/
    $(".ms-siteactionsmenuhover, .ms-siteactionsmenuinner").click(function () {
        $("#header").css("z-index", "0")
    });

    $(".formDetails .uppercase span").html("<div class='defaultText'>Promotions</div>");

    $('.coupon_list li').click(function () {
        var titleString = "<div class='c" + $(this).attr("id") + "'> ";
        titleString += " - " + $(this).children('div').html();
        titleString += "</div>";
        if ($(this).hasClass("selected")) {
            $(".c" + $(this).attr("id")).remove();
        }
        else {
            $(".formDetails .uppercase span").append(titleString);
        }
        if ($(".formDetails .uppercase span div").length == 1) {
            $(".defaultText").css("display", "block");
        }
        else {
            $(".defaultText").css("display", "none");
        }
    });


    /* Common functions */
    $("a[href^='http']").each(function () {
        if (this.href.indexOf(location.hostname) == -1 && this.href.indexOf("shoutitout.com") == -1) {
            $(this).attr('target', '_blank');
        }
    });

    $('img').each(function () {
        $(this).attr({
            title: $(this).attr('alt')
        });
    });

    $('.currentYear').html((new Date()).getFullYear());
    $('.navigation_bottom li a').removeClass('active');



    var productsAct = window.location.pathname;
    productsAct = productsAct.toLowerCase();

    if (productsAct.indexOf("/products/") > 0) {
        $('#productsNav').removeClass('productsHover').addClass('productsAct');
    }
    else if (productsAct.indexOf("/stain-solutions/") > 0) {
        $('#stainSolutionsNav').removeClass('solutionsHover').addClass('stainSolutionsAct');
    }
    else if (productsAct.indexOf("go-play.aspx") > 0) {
        $('.goPlay').css('background-position', '-301px 37px')
    }
    else if (productsAct.indexOf("coupons-and-offers.aspx") > 0) {
        $('.couponsOffers').css('background-position', '-452px 37px')
    }

    if ($('.landingHeaders').html() == '') {
        $('.landingHeaders').css({ "height": "0px", "overflow": "hidden" });
    }

    /*Main Nav*/
    $('#products').hover(function () {
        $('#productsNav').removeClass("products").addClass("productsHover");
        $('div.productsSubNav dl dd').first().show();
        $('div.productsSubNav dl dt').first().find('span').removeClass("productsSpan").addClass("productsSpanHover");
        $('#productsNav').removeClass('productsAct').addClass('productsHover');
    }, function () {
        $('#productsNav').removeClass("productsHover").addClass("products");
        $('div.productsSubNav dl dd').hide();
        $('div.productsSubNav dl dt').find('span').removeClass("productsSpanHover").addClass("productsSpan");
        if (productsAct.indexOf("/products/") > 0) {
            $('#productsNav').addClass('productsAct');
        }
    });

    $('div.productsSubNav dl dt').hover(function () {
        $('div.productsSubNav dl dd').hide();
        $('div.productsSubNav dl dt').find('span').removeClass("productsSpanHover").addClass("productsSpan");
        $(this).next().show();
        $(this).find('span').removeClass("productsSpan").addClass("productsSpanHover");
    }, function () {
    });

    $('div.productsSubNav dl dt:last').addClass("last");

    $('#stainSolutions').hover(function () {
        $('#stainSolutionsNav').removeClass("stainSolutions").addClass("solutionsHover");
        $('div.stainSolutionsSubNav dl dd').first().show();
        $('div.stainSolutionsSubNav dl dt').first().find('span').removeClass("solutionsSpan").addClass("solutionsSpanHover");
        $('#stainSolutionsNav').removeClass('stainSolutionsAct').addClass('solutionsHover');
    }, function () {
        $('#stainSolutionsNav').removeClass("solutionsHover").addClass("stainSolutions");
        $('div.stainSolutionsSubNav dl dd').hide();
        $('div.stainSolutionsSubNav dl dt').find('span').removeClass("solutionsSpanHover").addClass("solutionsSpan");
        if (productsAct.indexOf("/stain-solutions/") > 0) {
            $('#stainSolutionsNav').addClass('stainSolutionsAct');
        }
    });

    $('div.stainSolutionsSubNav dl dt').hover(function () {
        $('div.stainSolutionsSubNav dl dd').hide();
        $('div.stainSolutionsSubNav dl dt').find('span').removeClass("solutionsSpanHover").addClass("solutionsSpan");
        $(this).next().show();
        $(this).find('span').removeClass("solutionsSpan").addClass("solutionsSpanHover");
    }, function () {
    });

    $('div.stainSolutionsSubNav dl dt:last').addClass("last");

    /*shoutStainSolver Page starts*/
    $('.atHome a').removeClass("atHomeAnchor").addClass("atHomeHover");

    $('.uniqueSituationOptions a:first').addClass("current");

    $("ul.categoryList li a:first").parent().addClass("current");
    var stainId = $("ul.categoryList li a:first").attr("id");
    $(".optionsSection ul li").hide();
    $("." + stainId).parent().show();
    $("." + stainId).show();

    /*shoutStainSolver Page Ends*/

    /*Footer Facebook*/
    $('div.footerContent').find('.fbMoreIdeas').hide();
    $('a.fbShare').click(function () {
        $('div.footerContent').find('.fbShare').toggleClass("fbSharehover");
        if ($(this).hasClass("fbSharehover")) {
            $('div.footerContent').find('.fbIdeas').hide();
            $('div.footerContent').find('.fbMoreIdeas').show();
        }
        else {
            $('div.footerContent').find('.fbMoreIdeas').hide();
            $('div.footerContent').find('.fbIdeas').show();
        }
        //$('div.footerContent').find('.fbIdeas').toggle();
        $('div.fbDetails').toggle();
    });

    if ($.browser.safari) {
        $('.fbLike').width('88px');
    }

    /*shoutStainSolver Page starts*/
    $('.onTheGo a').click(function () {
        $('.onTheGo a').removeClass("OnTheGoAnchor").addClass("onTheGoHover");
        $('.atHome a').removeClass("atHomeHover").addClass("atHomeAnchor");
        return false;
    });

    $('.atHome a').click(function () {
        $('.onTheGo a').removeClass("onTheGoHover").addClass("OnTheGoAnchor");
        $('.atHome a').removeClass("atHomeAnchor").addClass("atHomeHover");
        return false;
    });

    $('.uniqueSituationOptions a').click(function () {
        $('.uniqueSituationOptions a').removeClass("current");
        $(this).addClass("current");
        return false;
    });

    $("ul.categoryList li").click(function () {
        $("ul.categoryList li").removeClass("current");
        $(this).addClass("current");
        var stainId = $(this).find("a").attr("id");
        $(".optionsSection ul li").hide();
        $("." + stainId).parent().show();
        $("." + stainId).show();
        return false;
    });

    $(".optionsSection ul li a").click(function () {
        $(".optionsSection ul li a").removeClass("current");
        $(this).addClass("current");
        var stainVal = $(".optionsSection ul li a.current").text();
        $('.stainSelName').show().text(stainVal);
        return false;
    });
    /*shoutStainSolver Page Ends*/

    /*shoutStainSolverSearchResults Page Starts*/
    $('#onTheGoSolverResult').hide();
    $('#atHomeSolverResult').hide();

    var stainCondition = window.location.hash;
    stainCondition = stainCondition.replace('#', '');
    displaySolution(stainCondition);

    $('.solveOnTheGo').click(function () {
        displaySolution('OnTheGo');
    });
    $('.solveAtHome').click(function () {
        displaySolution('AtHome');
    });
    /*shoutStainSolverSearchResults Page Ends */

    $('a.faceBookIcon').click(function () {
        window.location.href = "http://www.facebook.com"
    });

    $('.dropDownBtn').click(function () {
        var posX = $(this).position();
        $('ul.selectList').css({ "left": posX.left - 58, "top": posX.top + 40 }).show();
        $('ul.selectList').show();
        return false;
    });

    $('ul.selectList').mouseleave(function () {
        $(this).hide();
    });

    $('ul.selectList li').click(function () {
        $("ul.selectList li").removeClass("current");
        $(this).addClass("current");
        var stateVal = $("ul.selectList li.current").text();
        if (stateVal != "Select") {
            $('.selState').val(stateVal);
            $('.hdnState').val(stateVal);
            $('ul.selectList').hide();
        }
    });

    $('div.checkBox input').click(function () {
        $('div.checkBox').toggleClass("checkBoxSelected");
    });

    $('a.tool-tip').each(function () {
        $(this).hover(function () {
            var tiptext = $(this).find("span").html();
            var posX = $(this).position();
            $('div.tooltip .tooltipMiddle').html(tiptext);
            $('div.tooltip').css({ "left": posX.left + 20, "top": posX.top - 8 }).show();
        }, function () {
            $('div.tooltip').hide().html().empty();
        });
    });

    //Refresh facebook frame 
    $('.faceBookProductReview').prev().click(function () {
        $('.faceBookProductReview iframe').attr('src', $('.faceBookProductReview iframe').attr('src'))
    });

});
/*shoutStainSolverSearchResults Page Starts*/

function displaySolution(par) {
    if (par.toLowerCase() == 'onthego') {
        $('#atHomeSolverResult').hide();
        $('#onTheGoSolverResult').show();
    }
    else {
        $('#atHomeSolverResult').show();
        $('#onTheGoSolverResult').hide();
    }
}
/*shoutStainSolverSearchResults Page Ends */


