﻿$(document).ready(function() {

    //## use supersleight jQuery plugin to enable transparency on the given image
    //## when the image isn't a link
    $('.ieApplyTrans').supersleight();

    //## set up the slider functionality
    $("#productslider").easySlider({
        auto: true,
        continuous: true,
        nextText: '',
        prevText: '',
        pause: 5000,
        speed: 800,
        hideItems: true,
        containerWidth: 600
    });
    
    //----------------------------------------------------------------------------//

    $("#oimg").mouseleave(function() {
        $("#oimg").hide().unbind('click');
    });

    function hovers(img_x, img_y, img_w, img_h, id, nav_url) {
        $("#si_box").removeClass().addClass(id + "_img");
        $("#si_box > img").hide();
        $("#img_" + id).show();
        $("#si_box").removeClass().addClass(id + "_img");
        $("#" + id).show();
        $("#oimg")
                .css({ 'top': img_x + 'px', 'left': img_y + 'px', 'width': img_w + 'px', 'height': img_h + 'px' })
                .click(function() {
                    window.location = nav_url;
                })
                .show();
    }

    $("#oimg").hide().unbind('click');

    $("#logoLink").click(function() {
        window.location = "/";
    });

    $("#SizeGiudeLink")
        .mouseenter(showSizeGuide)
        .mouseleave(hideSizeGuide);

    $("#Matrix2")
        .blur(hideSizeGuide)
        .click(showSizeGuide);

    $("#Matrix2 option")
        .click(hideSizeGuide); 

    function showSizeGuide() {

        $("#sizeGuide" + $("#sizeGuideId").val()).show();
        $("#ProductContent").hide();
    }

    function hideSizeGuide() {

        $("#ProductContent").show();
        $("#sizeGuide1").hide();
        $("#sizeGuide2").hide();

    }

    // requires jquery.onfontresize.js
    $(document).bind("fontresize", function() {
        $sects.equalizeCols().find(".b, .bl, .br").each(function(i) {
            var thisHeight = $(this).parents(".section").height();
            $(this).height(thisHeight);
        });
    });

    var $askQ = $("#askQ")
		   .dialog({
		       bgiframe: true,
		       title: 'Ask a Question',
		       buttons: {
		           "Close": function() {
		               $(this).dialog("close");
		           }
		       },
		       width: 600,
		       autoOpen: false,
		       modal: true
		   });

    var $eFnd = $("#eFnd")
		   .dialog({
		       bgiframe: true,
		       title: 'Send an Email to a Friend',
		       buttons: {
		           "Close": function() {
		               $(this).css('position', 'absolute').dialog("close");
		           }
		       },
		       width: 600,
		       autoOpen: false,
		       modal: true
		   });

    $("#sendfriendLink").click(function() {
        $eFnd.css('position', 'relative').dialog('open');
        return false;
    });
    $("#askquestionlink").click(function() {
        $askQ.css('position', 'relative').dialog('open');
        return false;
    });

    $(function() {
        var zoomoptions = {
            zoomWidth: 265,
            zoomHeight: 300,
            xOffset: 44,
            yOffset: 0,
            position: "right",
            title: false,
            showEffect: 'show',
            hideEffect: 'fadeout',
            fadeoutSpeed: 'slow'
        };
        $(".jqzoom").jqzoom(zoomoptions);
    });

    $("#friendSend").click(function() {
        if (ValidateCustomForm('TellFriendForm', 'TellFriendValidationResult')) {
            $(".errortable").hide();
            var hasError = false;
            var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;

            var emailToVal = $("#FriendEmail").val();
            if (!emailReg.test(emailToVal)) {
                $("#FriendEmail").after('<span class="errortable">&nbsp;Invalid email</span>');
                hasError = true;
            }
            var emailFromVal = $("#Text1").val();
            if (!emailReg.test(emailFromVal)) {
                $("#Text1").after('<span class="errortable">&nbsp;Invalid email</span>');
                hasError = true;
            }

            if (hasError == false) {
                $("#friendSend").hide();
                $("#friendSend").after('<img src="/media/images/loading-bar-black.gif" alt="Loading" class="loading" />');

                var url = $("#formaction").val();
                var _YourName = $("#YourName").val();
                var _FriendName = $("#Text2").val();
                var Message = $("#Message").val();
                var ProductID = $("#ProductID").val();

                pausecomp(1000);

                $.post(url,
                  { _FriendEmail: emailToVal, _YourEmail: emailFromVal, Message: Message, ProductID: ProductID, _YourName: _YourName, _FriendName: _FriendName },
   	                    function(data) {
   	                        $("#friendSend").hide();
   	                        $(".loading").hide();
   	                        $("#submitFriendCell").append('<span class="errortable">&nbsp;Success, your email has been sent</span>');
   	                    }
                );

            }

        }
    });


    $("#questionSend").click(function() {
        if (ValidateCustomForm('AskQuestionForm', 'SendQuestionValidationResult')) {
            $(".errortable").hide();
            var hasError = false;
            var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;

            var questionEmail = $("#questionEmail").val();
            if (!emailReg.test(questionEmail)) {
                $("#questionEmail").after('<span class="errortable">&nbsp;Invalid email</span>');
                hasError = true;
            }

            if (hasError == false) {
                $("#questionSend").hide();
                $("#questionSend").after('<img src="/media/images/loading-bar-black.gif" alt="Loading" class="loading" />');

                var url = $("#questionformaction").val();
                var questionName = $("#questionName").val();
                var questionTel = $("#questionTel").val();
                var questionComments = $("#questionComments").val();
                var Subject = $("#questionSubject").val();
                var ProductID = $("#questionProductID").val();
                var StockCode = $("#questionStockCode").val();
                var Validate = "false";
                var EmailTemplateName = $("#QuestionEmailTemplateName").val();
                var Recipients = $("#questionRecipients").val();

                pausecomp(1000);

                $.post(url,
                  { _name: questionName,
                      _email: questionEmail,
                      _comments: questionComments,
                      telephone: questionTel,
                      ProductID: ProductID,
                      StockCode: StockCode,
                      Subject: Subject,
                      Validate: Validate,
                      EmailTemplateName: EmailTemplateName,
                      Recipients: Recipients

                  },
   	                    function(data) {
   	                        // alert(data);
   	                        $("#questionSend").hide();
   	                        $(".loading").hide();
   	                        $("#submitQuestionCell").append('<span class="errortable">&nbsp;Success, your email has been sent</span>');
   	                    }
                );

            }

        }
    });
});
function pausecomp(millis) {
    var date = new Date();
    var curDate = null;

    do { curDate = new Date(); }
    while (curDate - date < millis);
}

