$(document).ready(function() {

    $("#productselect").change(function() {
        if (parseInt($(this).val()) == 0) {

            alert('Please select a size that is in stock');
            $("#productselect").attr('value', '');
        }
    });

    $('.product-tabs .tabset li').live('click', function() {
        $('.product-tabs .tab-content').hide();
        $('.product-tabs .tabset li').removeClass('active');
        $('#' + $(this).attr('id').replace('but', '')).show();
        $(this).addClass('active');
        return false;
    });

    $('.btn-add').live('click', function() {
        $('#addsubmit').click();
    });

    $('#nav a').click(function() {
        clearCatBrandOptions();
    });

    $('.salepages a').click(function() {
        clearCatBrandOptions();
    });

    $('#mens-bargains-map').click(function() {
        clearCatBrandOptions();
    });

    $('#ladies-bargains-map').click(function() {
        clearCatBrandOptions();
    });

    $('.btn-prev').click(function() { return false });
    $('.btn-next').click(function() { return false });
    $('.btn-add').click(function() { $('#speedorder').submit(); return false });

    $('#speedorder').submit(function() {
        idVal = $('#productselect').val();
        strQty = $('#intQuantity').val();
        if (parseInt(idVal) > 0) {
            if (parseInt(strQty) > 0) {
                $('html, body').animate({ scrollTop: 0 }, 'slow');
                var data_msg = "numLineCount=1&&V_ID=speedorder&numLanguageID=1&strAction=add&V1=" + idVal + "&Q1=" + strQty
                $.ajax({
                    type: "GET",
                    url: "/basket.asp",
                    data: data_msg,
                    success: function() {
                        reloadCart();
                        $('.tooltip').show();
                        $('.tooltip').css('left', '-75px');
                        setTimeout(function() {
                            $('.tooltip').fadeOut(1000);
                            setTimeout(function() {
                                $('.tooltip').css('left', '-9999999px');
                            }, 1000);
                        }, 1000);
                    }
                });
            } else {
                alert('Please select a quantity greater than 0');
            }
        } else {
            alert('Please select a product');
        }
        return false;
    });

    $('.normalthumb a img').mouseover(function() {
        $('#mainprodimage').attr('src', this.src.replace("_sm", "_lrg"));
        $('#zoomimage').attr('src', this.src.replace("_sm", "_xlrg"));
    });

    $('.gallery3 a img').mouseover(function() {
        $('#zoomimage').attr('src', this.src.replace("_sm", "_xlrg"));
    });

    $('.gallery3 .btn-prev').click(function() {
        strString = $('#zoomimage').attr('src').replace("_xlrg.jpg", "")

        intTotalItems = $(".thumbnails").children().length - 1

        strLetter = strString.substring(strString.length - 1, strString.length)
        if (strLetter == 'a') {
            if (intTotalItems == 2) {
                $('#zoomimage').attr('src', $('#zoomimage').attr('src').replace("a_xlrg", "b_xlrg"));
            }
            if (intTotalItems == 3) {
                $('#zoomimage').attr('src', $('#zoomimage').attr('src').replace("a_xlrg", "c_xlrg"));
            }
            if (intTotalItems == 4) {
                $('#zoomimage').attr('src', $('#zoomimage').attr('src').replace("a_xlrg", "d_xlrg"));
            }
        }
        if (strLetter == 'b') {
            $('#zoomimage').attr('src', $('#zoomimage').attr('src').replace("b_xlrg", "a_xlrg"));
        }
        if (strLetter == 'c') {
            $('#zoomimage').attr('src', $('#zoomimage').attr('src').replace("c_xlrg", "b_xlrg"));
        }
        if (strLetter == 'd') {
            $('#zoomimage').attr('src', $('#zoomimage').attr('src').replace("d_xlrg", "c_xlrg"));
        }
        return false;
    });
    $('.gallery3 .btn-next').click(function() {
        strString = $('#zoomimage').attr('src').replace("_xlrg.jpg", "")

        intTotalItems = $(".thumbnails").children().length - 1

        strLetter = strString.substring(strString.length - 1, strString.length)
        if (strLetter == 'a') {
            if (intTotalItems > 1) {
                $('#zoomimage').attr('src', $('#zoomimage').attr('src').replace("a_xlrg", "b_xlrg"));
            }
        }
        if (strLetter == 'b') {
            if (intTotalItems > 2) {
                $('#zoomimage').attr('src', $('#zoomimage').attr('src').replace("b_xlrg", "c_xlrg"));
            } else {
                $('#zoomimage').attr('src', $('#zoomimage').attr('src').replace("b_xlrg", "a_xlrg"));
            }
        }
        if (strLetter == 'c') {
            if (intTotalItems > 3) {
                $('#zoomimage').attr('src', $('#zoomimage').attr('src').replace("c_xlrg", "d_xlrg"));
            } else {
                $('#zoomimage').attr('src', $('#zoomimage').attr('src').replace("c_xlrg", "a_xlrg"));
            }
        }
        if (strLetter == 'd') {
            $('#zoomimage').attr('src', $('#zoomimage').attr('src').replace("d_xlrg", "a_xlrg"));
        }
        return false;
    });

    $('.zoombuttonimage').click(function() {
        return false;
    });

    $("#videoframe").fancybox({
        'width': '60%',
        'height': '75%',
        'overlayOpacity': 0.75,
        'overlayColor': '#000',
        'autoScale': true,
        'transitionIn': 'none',
        'transitionOut': 'none',
        'type': 'iframe'
    });

    $(".zoombutton").click(function() {
        screenheight = $(window).height() - 100
        intProportion = 1090 / screenheight
        screenwidth = 840 / intProportion

        if (screenheight > 1090) { screenheight = 1090; screenwidth = 840; }

        $('#gallery3').css("width", screenwidth)
        $('#gallery3').css("height", screenheight)
        $('.gallery3-holder').css("width", screenwidth)
        $('.gallery3-holder').css("height", screenheight)
        $('#zoomimage').attr("width", screenwidth)
        $('#zoomimage').attr("height", screenheight)

        intTotalItems = $(".thumbnails").children().length - 1
        if (intTotalItems == 1) {
            $(".btn-next").hide();
            $(".btn-prev").hide();
        }

    });


    $(".gallery2 .zoombutton").fancybox({
        'width': '60%',
        'height': '75%',
        'overlayOpacity': 0.75,
        'overlayColor': '#000',
        'transitionIn': 'none',
        'transitionOut': 'none'
    });

    setQuickBuy();

    $('.external a').click(function(event) {
        var url = $(this).attr("href").replace('&amp;','&');
        var windowName = "rssWindow";
        window.open(url, windowName);
        event.preventDefault();
    });

    $(".block").fancybox({
        'href': '#newslettersignup',
        'width': '60%',
        'height': '50%',
        'scrolling': 'no',
        'overlayOpacity': 0.75,
        'overlayColor': '#000',
        'transitionIn': 'none',
        'transitionOut': 'none'
    });


    $(".btn-signup").click(function() {
        strInfo = $(".newslettersignup").html();
        $.ajax({
            type: "POST",
            async: true,
            url: "/mailinglist_process.asp",
            data: { strEmailAddress: $("#news_email").val(), strName: $("#news_firstname").val() + " " + $("#news_surname").val(), strHTML: "y", strAddMe: "y" },
            success: function(strHtml) {
                $(".newslettersignup").html('<p><img src="/images/hc_logo.png" alt="Hardcloud.com Newsletter Signup"/></p><p class="title">MANY THANKS FOR SIGNING UP TO THE NEWSLETTER</p>');
                //setTimeout(function() {
                //    $(".newslettersignup").html(strInfo);
                //}, 2000)
            }
        });
        return false;
    });

    $(".feedback-form .submit").fancybox({
        'width': '60%',
        'height': '50%',
        'scrolling': 'no',
        'overlayOpacity': 0.75,
        'overlayColor': '#000',
        'transitionIn': 'none',
        'transitionOut': 'none',
        'onComplete': function() {
            $("#fancybox-title").hide();
            $.ajax({
                type: "POST",
                async: true,
                url: "/askaquestion.asp",
                data: { strEmailAddress: $("#ask_email").val(), strQuestion: $("#ask_question").val(), strProduct: window.location.pathname, strAddMe: "y" }
            });
            return false;
        }
    });

    $("#feedbacksubmit").fancybox({
        'href': '#feedbackthanks',
        'width': '60%',
        'height': '50%',
        'scrolling': 'no',
        'overlayOpacity': 0.75,
        'overlayColor': '#000',
        'transitionIn': 'none',
        'transitionOut': 'none',
        'onComplete': function() {
            $("#fancybox-title").hide();
            $.ajax({
                type: "POST",
                async: true,
                url: "/feedbacksubmit.asp",
                data: { strName: $("#strName").val(), strFeedback: $("#strFeedback").val(), strAddMe: "y" }
            });
            return false;
        }
    });


    $("#contactsubmit").fancybox({
        'href': '#contactthanks',
        'width': '60%',
        'height': '50%',
        'scrolling': 'no',
        'overlayOpacity': 0.75,
        'overlayColor': '#000',
        'transitionIn': 'none',
        'transitionOut': 'none',
        'onComplete': function() {
            $("#fancybox-title").hide();
            $.ajax({
                type: "POST",
                async: true,
                url: "/contact_process.asp",
                data: { strContactName: $("#strContactName").val(), strContactEmail: $("#strContactEmail").val(), strContactComments: $("#strContactComments").val(),strCheckSubmit: "y" }
            });
            return false;
        }
    });


    currencyMenu();
});

function currencyMenu() {

    $(".currency-block .box ul").click(function() {
        $(this).find("li").fadeIn();
        $(".currency-block .box ul li a").bind('click', function() {
            //alert('here');
            //alert($(this).attr("href"))
            window.location.href = $(this).attr("href")
        });
        setTimeout(function() {
            $(".currency-block .box ul li").hide();
            $(".currency-block .box ul li.active").show();
            $(".currency-block .box ul a").unbind("click");
        }, 10000);
        return false;
    });

    $("#strName").click(function() {
        if ($(this).val() == 'Your name...') {
            $(this).val('');
        }
    });

    $("#strFeedback").click(function() {
        if ($(this).val() == 'Your feedback...') {
            $(this).val('');
        }
    });

    $("#searchbox").click(function() {
        if ($(this).val() == 'Search') {
            $(this).val('');
        }
    });
    
    

}

function reloadCart() {
    var data_msg = "test=" + Math.floor(Math.random() * 1000)
    $.ajax({
        type: "GET",
        url: "/includes/getbasket.asp",
        data: data_msg,
        success: function(msg) {
            $("#baskettotals").html(msg);
        }
    });
};

function setQuickBuy() {
    $(".product-box .btn-buy, .gallery-frame a").fancybox({
        'width': '60%',
        'height': '50%',
        'overlayOpacity': 0.75,
        'overlayColor': '#000',
        'scrolling': 'no',
        'transitionIn': 'none',
        'transitionOut': 'none',
        'onComplete': function() {

            $("#productselect").change(function() {
                if (parseInt($(this).val()) == 0) {

                    alert('Please select a size that is in stock');
                    $("#productselect").attr('value', '');
                }
            });
            $('.lightbox .btn-buy').click(function() {
                return false;
            });
            $('.lightbox .btn-buy').click(function() {
                $('#speedorder').submit(function() {
                    idVal = $('#productselect').val();
                    strQty = $('#intQuantity').val();
                    if (parseInt(idVal) > 0) {
                        if (parseInt(strQty) > 0) {
                            var data_msg = "numLineCount=1&&V_ID=speedorder&numLanguageID=1&strAction=add&V1=" + idVal + "&Q1=" + strQty
                            $.ajax({
                                type: "GET",
                                url: "/basket.asp",
                                data: data_msg,
                                success: function() {
                                reloadCart();
                                    $(".lightbox .image img.mainpic").fadeOut(1000, function() {
                                        $(".lightbox .image img.added").fadeIn(1000, function() {
                                        $(".lightbox .image img.added").fadeOut(1000, function() {
                                                $(".lightbox .image img.mainpic").fadeIn(1000);
                                            });
                                        });
                                    });
                                }
                            });
                        } else {
                            alert('Please select a quantity greater than 0');
                        }
                    } else {
                        alert('Please select a product');
                    }
                    return false;
                });
                $('#speedorder').submit();
            });

            /*
            setQuickBuy();

            $(".gallery-holder .btn-next").hover(function() {
            intTotalWidth = (parseInt($(".gallery-frame ul li").size()) * 54) - parseInt($(".gallery-frame ul").outerWidth())
            $(".gallery-frame ul").animate({ marginLeft: intTotalWidth * -1 }, 2000);
            }, function() {
            $(".gallery-frame ul").stop();
            });

            $(".gallery-holder .btn-prev").hover(function() {
            if (parseInt($(".gallery-frame ul").css("marginLeft")) < 0) {
            intTotalWidth = (parseInt($(".gallery-frame ul li").size()) * 54) - parseInt($(".gallery-frame ul").outerWidth())
            $(".gallery-frame ul").animate({ marginLeft: intTotalWidth }, 2000);
            }
            }, function() {
            $(".gallery-frame ul").stop();
            });
            */
        }
    });
};


function clearCatBrandOptions() {
    $.cookie('PSPAGE', 1, { path: '/' })
    $.cookie('PSPAGESIZE', 20, { path: '/' })
    $.cookie('PSBRA', "", { path: '/' })
    $.cookie('PSVER', "", { path: '/' })
    $.cookie('PSSEA', "", { path: '/' })
    $.cookie('PSSTY', "", { path: '/' })
    $.cookie('PSPRO', "", { path: '/' })
    $.cookie('SSPAGE', 1, { path: '/' })
    $.cookie('SSPAGESIZE', 20, { path: '/' })
    $.cookie('SSBRA', "", { path: '/' })
    $.cookie('SSVER', "", { path: '/' })
    $.cookie('SSSEA', "", { path: '/' })
    $.cookie('SSSTY', "", { path: '/' })
    $.cookie('SSPRO', "", { path: '/' })
    $.cookie('SSKEYWORDS', "", { path: '/' })
    $.cookie('BSPAGE', 1, { path: '/' })
    $.cookie('BSPAGESIZE', 20, { path: '/' })
    $.cookie('BSBRA', "", { path: '/' })
    $.cookie('BSVER', "", { path: '/' })
    $.cookie('BSSEA', "", { path: '/' })
    $.cookie('BSSTY', "", { path: '/' })
    $.cookie('BSPRO', "", { path: '/' })
    $.cookie('BSPAGE', 1, { path: '/' })
    $.cookie('BGPAGESIZE', 20, { path: '/' })
    $.cookie('BGBRA', "", { path: '/' })
    $.cookie('BGVER', "", { path: '/' })
    $.cookie('BGSEA', "", { path: '/' })
    $.cookie('BGSTY', "", { path: '/' })
    $.cookie('BGPRO', "", { path: '/' })
    $.cookie('BGGENDER', "", { path: '/' })
}
