var $__ = jQuery;

jQuery(document).ready(function($) {
    // add to the existing functionality
    $('#contact-form .submit-btn').click(function(event) {
        if (validate("contact-form"))
            postbackASPFormByID("contactSubmit", "input")

    });
});


jQuery(document).ready(function($) {
    $(window).load(function() {
        var l = $('#sidebar-left').height();
        var m = $('#MapBox').height();

        if (l > m) {
            $('#MapBox').height(l);
        } else {
            $('sidebar-left').height(m);
        }
        $('#map').hide();


    });
});

function validate(divtoValidate) {
    var shouldSubmit = true;
    $__("#requiredError").hide();
    $__("#emailError").hide();
    $__("#" + divtoValidate + " input, #" + divtoValidate + " textarea").each(
        function() {
            if ($__(this).hasClass("required")) {
                if ($__(this).val() == '') {
                    // inline to make sure the style overrides other classes
                    $__(this).attr("style", "border:solid red 1px;");
                    shouldSubmit = false;
                    $__("#requiredError").show();
                }
            }

            if ($__(this).hasClass("email")) {
                var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/ ;
                var address = $__(this).val();
                if (reg.test(address) == false) {
                    $__(this).attr("style", "border:solid red 1px;");
                    shouldSubmit = false;
                    $__("#emailError").show();
                }
            }
        }
    );
    return shouldSubmit;
}


function postbackASPFormByID(objID, type) {
    var obj = $__(type + "[id*='" + objID + "']");
    obj.trigger("click");
}


// zoomi - A zoom for images ~ Sean Catchpole - Version 0.9
(function($) {

    $.fn.zoomi = function() {
        $(this).filter("img").each(function() {
            if (!this.z) {
                $(this).zoom1().mouseover(function() { $(this).zoom2().show(); });
                $(this.z).mouseout(function() { $(this).hide(); });
            }
        });
        return this;
    }

    $.fn.zoom1 = function() {
        $(this).each(function() {
            var e = this;
            $(e).css({ 'position': 'relative', 'z-index': '8' }).after('<img class="' + e.className + '">');
            e.z = e.nextSibling;
            $(e.z).removeClass("zoomi").addClass("zoom2").attr("src", e.alt || e.src)
                .css({ 'position': 'absolute', 'z-index': '10' });
            $(e.z).hide();
        });
        return this;
    }

    $.fn.zoom2 = function() {
        var s = [];
        this.each(function() {
            var e = this;
            if (!e.z) e = $(e).zoom1()[0];
            s.push(e.z);
            if (!e.z.complete) return;
            if (!e.z.width) {
                $(e.z).show();
                e.z.width = e.z.width;
                $(e.z).hide();
            }
            $(e.z).css({
                    left: $(e).offsetLeft() - (e.z.width - e.scrollWidth) / 2 + 'px',
                    top: $(e).offsetTop() - (e.z.height - e.scrollHeight) / 2 + 'px'
                });
        });
        return this.pushStack(s);
    }

    $.fn.offsetLeft = function() {
        var e = this[0];
        if (!e.offsetParent) return e.offsetLeft;
        return e.offsetLeft + $(e.offsetParent).offsetLeft();
    }

    $.fn.offsetTop = function() {
        var e = this[0];
        if (!e.offsetParent) return e.offsetTop;
        return e.offsetTop + $(e.offsetParent).offsetTop();
    }

    $(function() { $('img.zoomi').zoomi(); });

})(jQuery);
