function TapeTech() {

    this.onAjaxComplete = function (scope) {

    }

    this.init = function (scope) {
        bind(scope);
        bindAjaxLogic(scope);
        bindWatermarks(scope);
        initializeForms(scope);       
    }
   
    function bind(scope) {
        $('#navigation li').hover(
                            function () {
                                //show its submenu
                                if ($('ul', this).css('display') != 'block')
                                    $('ul', this).slideDown(100);
                            },
                            function () {
                                //hide its submenu
                                $('ul', this).slideUp(100);
                            });

        if ($.browser.msie) {
            $("#navigation li a").corner("5px keep cc:#757575 bottom");
            $("#navigation li a").corner("5px keep cc:#909090 top")
        }
        else { $("#navigation li a").corner("keep 4px"); }
        $(".dropdown").uncorner();

        /* Product Detail magnifier */
        $('#product-page .product-image').loupe({ loupe: 'zoom' });

        /* Product Details Magnifier reset and image swap */
        $('.alternative-image img').click(
		function () {

		    var index = $('.alternative-image img').index(this);

		    $('#product-page .product-image').hide();
		    if ($('#product-page .product-image').length - 1 < index)
		        $('#product-page .product-image:first').show();
		    else
		        $('#product-page .product-image').eq(index).show();

		    //var imageSource = $(this).attr('src');
		    //imageSource = imageSource.replace('_thumb', '_large'); // This line changes the image url. It is based on the specific naming convention of _thumb and _large being interchangable.
		    //$('#product-page .product-image').attr('src', imageSource);
		    $('#product-page .product-image').loupe('stop'); // Loupe has been modified to accept a "stop" command so it can reset after the image swap
		    $('#product-page .product-image').loupe({ loupe: 'zoom' });
		});
    }

    function bindAjaxLogic(scope) {
        // Bind AJAX Loading indicator
        $("#ajax-loader", scope).bind("ajaxSend", function () {
            
            var scrollLeft = (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
            var scrollTop = (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);

            var clientWidth;
            if (window.innerWidth) {
                clientWidth = (window.__safari ? window.innerWidth : Math.min(window.innerWidth, document.documentElement.clientWidth));
            } else {
                clientWidth = document.documentElement.clientWidth;
            }
            var clientHeight;
            if (window.innerHeight) {
                clientHeight = (window.__safari ? window.innerHeight : Math.min(window.innerHeight, document.documentElement.clientHeight));
            } else {
                clientHeight = document.documentElement.clientHeight;
            }

            $(this).css("left", scrollLeft + ((clientWidth - 66) / 2) + 'px');
            $(this).css("top", scrollTop + ((clientHeight - 66) / 2) + 'px');


            $(this).show();
            $('body', scope).css("cursor", "wait");
        }).bind("ajaxComplete", function () {
            $(this).hide();
            $('body', scope).css("cursor", "default");
        });

        // Bind Ajax Error notification
        $(document).ajaxError(function (e, xhr, settings, exception) {

            $("#ajax-loader", scope).hide();

            // Don't worry about 4xx series errors, we are using them for Bad Request codes (invalid post data, etc)
            if (xhr.status < 500)
                return;
        });
    }

    function bindWatermarks(scope) {

        $('INPUT[type="text"][class="watermark"]', scope).each(function (index) {
            
            var txtBox = $(this);
            txtBox.val(txtBox.attr('title'));

            txtBox.focus(function () {

                $(this).filter(function () {

                    // We only want this to apply if there's not
                    // something actually entered
                    return $(this).val() == "" || $(this).val() == txtBox.attr('title')

                }).removeClass("watermark").val("");
            });

            // Define what happens when the textbox loses focus
            // Add the watermark class and default text
            txtBox.blur(function () {

                $(this).filter(function () {

                    // We only want this to apply if there's not
                    // something actually entered
                    return $(this).val() == ""

                }).addClass("watermark").val(txtBox.attr('title'));

            });
        });
    }

    var dialogContainer;
    function initializeForms(scope) {

        dialogContainer = $("#dialogFormContainer", scope);

        var articleFormUrl = $("#article-form-url-hidden", scope).val();
        var questionFormUrl = $("#question-form-url-hidden", scope).val();
        var feedbackFormUrl = $("#feedback-form-url-hidden", scope).val();

        $(".submitArticle", scope).each(function (index) {
            bindSubmitForm($(this), articleFormUrl, 'Successfully submitted Article.Close the window to continue.', 'Error occurred');
        });
                
        $(".submitQuestion", scope).each(function (index) {
            bindSubmitForm($(this), questionFormUrl, "Successfully submitted Question.Close the window to continue.", "Error occurred");
        });

        $(".submitFeedback", scope).each(function (index) {
            bindSubmitForm($(this), feedbackFormUrl, "Successfully submitted Feedback.Close the window to continue.", "Error occurred");
        });
    }

    function bindSubmitForm(sender, actionName, successMessage, failureMessage) {

        sender.click(function (e) {

            e.preventDefault();

            dialogContainer.load(actionName, function (response, status, xhr) {

                if (status == "error") {
                    alert('Some error: ' + xhr.status + " " + xhr.statusText);
                }

                dialogContainer.dialog({
                    title: '',
                    autoOpen: true,
                    width: 525,
                    modal: true
                });

                AttachFormEvents(successMessage, failureMessage);
            });
        });
    }  

    function AttachFormEvents(successMessage, failureMessage) { 

        $(".uploadArticlePdf", dialogContainer).click(function (e) {
            e.preventDefault();
            $("#articleForm .hidden", dialogContainer).show();
        });

        $('form', dialogContainer).ajaxForm({
            success: function (data) {
                if (data == 'Success') {
                    dialogContainer.dialog("option", "title", successMessage).hide("slow");
                }
                else if (data == 'Failure') {
                    dialogContainer.dialog("option", "title", failureMessage);
                }
                else {
                    dialogContainer.html(data);
                    AttachFormEvents(successMessage, failureMessage);
                }
            }
        });
    }
}

var tapeTech = new TapeTech();


// **** Global site init logic
$(document).ready(function () {
    tapeTech.init(this);
    tapeTech.onAjaxComplete(this);
});
// END init logic *******


// Validate Zipcode
function IsValidZip(s) {
    var reZip = new RegExp(/(^\d{5}(-\d{4})?$)|(^[ABCEGHJKLMNPRSTVXY]{1}\d{1}[A-Z]{1} *\d{1}[A-Z]{1}\d{1}$)/);
    if (!reZip.test(s)) {
        return false;
    }
    return true;
}
