$(document).ready(function () {

    /* PAGE ANCHORS */
    $('a[href^="#"]').click(function (event) {
        //get the full url - like mysitecom/index.htm#home
        var full_url = this.href;

        //split the url by # and get the anchor target name - home in mysitecom/index.htm#home
        var parts = full_url.split("#");
        var trgt = parts[1];

        //get the top offset of the target anchor
        if ($('a[name^="' + trgt + '"]').length >= 1) {
            //prevent the default action for the click event
            event.preventDefault();

            var target_offset = $('a[name^="' + trgt + '"]').offset();
            var target_top = target_offset.top;

            //goto that anchor by setting the body scroll top to anchor top
            $('html, body').animate({ scrollTop: target_top }, 500);

            window.location.hash = trgt;
        }

    });

    /*PRODUCT IMAGE BORDERS*/
    //var piWidth = $('#mainimageblock img').width();
    //var piWHeight = $('#mainimageblock img').height();

    //$('#mainimageblock').css({
    //"width": piWidth,
    //"height": piHeight
    //});

    /* TABS */
    $(".tab_content").hide();
    $(".product-tabcontrols li:first").addClass("active").show();
    $(".tab_content:first").show();

    $("ul.product-tabcontrols li").click(function () {

        if ($(this).hasClass("active")) {
            //Already selected, do nothing
        } else {
            $("ul.product-tabcontrols li").removeClass("active");
            $(this).addClass("active");
            $(".tab_content").hide();
            var indx = $("ul.product-tabcontrols li").index($(this));
            var activeTab = $(".tab_container").children(".tab_content:eq(" + indx + ")");
            $(activeTab).fadeIn();
        }
    });

    //    if ($("ul.product-tabcontrols li:first").click()) {
    //            $('.tab01-lb').add();
    //        });
    //    } else { ($("ul.product-tabcontrols li:last").click()) {
    //            $('.tab01-lb').remove();
    //        });
    //    }


    /* ACCORDION */
    function initAccordion() {
        var questionFAQ = $('.b-qNa .category > ul > li:nth-child(odd)');
        var answerFAQ = $('.b-qNa .category > ul > li:nth-child(even)');

        questionFAQ.addClass("plusImage");
        answerFAQ.css("padding", "5px 0 5px 5px");

        answerFAQ.hide();

        questionFAQ.click(function () {
            if ($(this).hasClass("plusImage")) {
                $(this).next().stop(true, true).slideToggle('normal');
                $(this).removeClass("plusImage");
                $(this).addClass("minusImage");
            } else {
                $(this).next().stop(true, true).slideToggle('normal');
                //alert('hello');
                $(this).removeClass("minusImage");
                $(this).addClass("plusImage");
            }
        });
    }

    initAccordion();

    //Authorize.net text to Credit Card

    $("#provider_list label:first").html("").attr("id", "payCC");
    $("#payCC").append("Credit Card");

    $("#provider_list").append("<img width='261' height='29' alt='Accepted Credit Cards' src='/store/templates/Lavi/images/lavi/creditcardTypes.jpg'>");
    $("#provider_list img").css("margin-top", "15px");

    //Checkout Steps Text
    //$("#ChooseBillingAddress h2").prepend("<span class='processSteps'>Step 1 of 5: </span>");
    //$("#ChooseShippingAddress h2").prepend("<span class='processSteps'>Step 2 of 5: </span>");
    //$("#lang_ChooseShippingProvider").prepend("<span class='processSteps'>Step 3 of 5: </span>");
    //$("#lang_ConfirmYourOrder").prepend("<span class='processSteps'>Step 4 of 5: </span>");
    //$(".payOrder h2").prepend("<span class='processSteps'>Step 5 of 5: </span>");

    //KILL EXTRA BR'S IN CART ACCOUNT
    $(".AccountOrdersList .FloatRight br").remove();

    //REMOVE BUTTON VALUES
    $("#createAccountNow").val("");
    //$("#bottom_payment_button").val("");

    /*KILL EXTRA P TAGS*/
    $("#DidYouKnowBlock p:empty").remove();
    $("#CreateAccountForm p:empty").remove();

    $("#CreateAccountForm .Submit br").remove();

    /*KILL EXTRA BR IN BILLING/SHIPPING ADDRESS*/
    $(".ShippingAddressList p br:first").remove();

    //function removeP() {
    //if ($('#DidYouKnowBlock')

    /*WRITE A REVIEW*/
    $(".lavi-proddetails_writeareview").hide();
    $(".write_review").click(function () {
        $(".lavi-proddetails_writeareview").toggle();
    });

    /* OTHER FORMS */
    //$('.Module_Form').wrap("<div class='contactForm'></div>");

    /* SLIDESHOW */
    $('.slideshow').cycle({ fx: 'fade', pause: 0 });

    /* DISCOUNT */
    $('.discount').cycle({ fx: 'fade', timeout: 8000, pause: 0 });

    /* REMOVE EXTRA HR FROM REVIEWS */
    //$('#tab02 ul').children('li:last');

    /* FOOTER LINK BLOCK */
    $('.b-footerlinkblock ul').children('li:last').css({ border: 'none' });

    /* REVIEW VALIDATION */
    var errorMsg = 'Please fill out all form fields to continue';
    $('#rating_box input[type=submit]').click(function () {
        if ($('#rating_box input[type=input], #rating_box textarea').val() == '') {
            alert(errorMsg);
            return false;
        } else {
            //Successful, submit the form
        }
    });


    /* NEWSLETTER FORM */
    $('.newsletter form').children('input[type=text]').focus(function () {

        if ($(this).val().toLowerCase() == 'enter your email address') {

            $(this).val('');
        }

    }).blur(function () {
        if ($(this).val() == '') {
            $(this).val('Enter your email address');
        }
    });

    $('.newsletter form').children('.b-footer-nl_submit').click(function () {
        //VALIDATE EMAIL ADDRESS
        var errorMsg = validateEmail($('.newsletter form').children('input[type=text]'), 'Enter your email address');
        if (errorMsg != '') {
            alert(errorMsg);
        } else {
            newsFormUrl = $('.newsletter form').attr('action');
            newsFieldName = $('.newsletter form').children('input[type=text]').attr('name');
            emailAddress = $('.newsletter form').children('input[type=text]');

            var dataString = newsFieldName + '=' + emailAddress;

            $('.newsletter form').children('input[type=text]').val('');
            $('.newsletter form, .newsletter p').fadeOut(300);

            $.ajax({
                type: "POST",
                contentType: "multipart/form-data",
                url: newsFormUrl,
                data: dataString,
                success: function () {
                    $('.newsletter .message').fadeIn(400);
                }
            });
        }

        return false;
    });
    /* NEWSLETTER FORM END */


    /* CONTACT FORM */
    /*$('.gp-contactus_right > .Module_Form > form > fieldset > .Buttons > button').click(function () {
    alert('sdf');
    });*/
    /* CONTACT FORM END */


});


//EMAIL VALIDATION
function trim(s) {
    return s.replace(/^\s+|\s+$/, '');
}

function validateEmail(fld, emptyFiller) {
    var emailError = "";
    var tfld = trim($(fld).val());                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/;
    var illegalChars = /[\(\)\<\>\,\;\:\\\"\[\]]/;

    if ($(fld).val() == emptyFiller || $(fld).val() == '') {
        emailError = "Please enter an e-mail address";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        emailError  = "Please enter a valid e-mail address";
    } else if ($(fld).val().match(illegalChars)) {
        emailError = "Please enter a valid e-mail address";
    }

    return emailError;
}

//VALIDATE RADIO ON SHIPPING METHODS
$(function () {
    $('.btn-continue').click(function () {
        if ($('#ChooseShippingProvider').find('input:radio:checked').length == 0) {
            alert('Please select a shipping method for your LAVI products');
            return false;
        }
    });
});

