function initPage()
{
    clearFormFields({
        clearInputs: true,
        clearTextareas: true,
        passwordFieldText: false,
        addClassFocus: "focus",
        filterClass: "default"
    });
}
function clearFormFields(o)
{
    if (o.clearInputs == null) o.clearInputs = true;
    if (o.clearTextareas == null) o.clearTextareas = true;
    if (o.passwordFieldText == null) o.passwordFieldText = false;
    if (o.addClassFocus == null) o.addClassFocus = false;
    if (!o.filterClass) o.filterClass = "default";
    if(o.clearInputs) {
        var inputs = document.getElementsByTagName("input");
        for (var i = 0; i < inputs.length; i++ ) {
            if((inputs[i].type == "text" || inputs[i].type == "password") && inputs[i].className.indexOf(o.filterClass) == -1) {
                inputs[i].valueHtml = inputs[i].value;
                inputs[i].onfocus = function ()	{
                    if(this.valueHtml == this.value) this.value = "";
                    if(this.fake) {
                        inputsSwap(this, this.previousSibling);
                        this.previousSibling.focus();
                    }
                    if(o.addClassFocus && !this.fake) {
                        this.className += " " + o.addClassFocus;
                        this.parentNode.className += " parent-" + o.addClassFocus;
                    }
                }
                inputs[i].onblur = function () {
                    if(this.value == "") {
                        this.value = this.valueHtml;
                        if(o.passwordFieldText && this.type == "password") inputsSwap(this, this.nextSibling);
                    }
                    if(o.addClassFocus) {
                        this.className = this.className.replace(o.addClassFocus, "");
                        this.parentNode.className = this.parentNode.className.replace("parent-"+o.addClassFocus, "");
                    }
                }
                if(o.passwordFieldText && inputs[i].type == "password") {
                    var fakeInput = document.createElement("input");
                    fakeInput.type = "text";
                    fakeInput.value = inputs[i].value;
                    fakeInput.className = inputs[i].className;
                    fakeInput.fake = true;
                    inputs[i].parentNode.insertBefore(fakeInput, inputs[i].nextSibling);
                    inputsSwap(inputs[i], null);
                }
            }
        }
    }
    if(o.clearTextareas) {
        var textareas = document.getElementsByTagName("textarea");
        for(var i=0; i<textareas.length; i++) {
            if(textareas[i].className.indexOf(o.filterClass) == -1) {
                textareas[i].valueHtml = textareas[i].value;
                textareas[i].onfocus = function() {
                    if(this.value == this.valueHtml) this.value = "";
                    if(o.addClassFocus) {
                        this.className += " " + o.addClassFocus;
                        this.parentNode.className += " parent-" + o.addClassFocus;
                    }
                }
                textareas[i].onblur = function() {
                    if(this.value == "") this.value = this.valueHtml;
                    if(o.addClassFocus) {
                        this.className = this.className.replace(o.addClassFocus, "");
                        this.parentNode.className = this.parentNode.className.replace("parent-"+o.addClassFocus, "");
                    }
                }
            }
        }
    }
    function inputsSwap(el, el2) {
        if(el) el.style.display = "none";
        if(el2) el2.style.display = "inline";
    }
}
if (window.addEventListener)
    window.addEventListener("load", initPage, false);
else if (window.attachEvent)
    window.attachEvent("onload", initPage);

function fixMenuOverlap(targetSelector, intersectorsSelector) {
    var intersectors = [];

    var $target = $(targetSelector);
    if (!$target.length) return;

    var tAxis = $target.offset();
    var t_x = [tAxis.left, tAxis.left + $target.outerWidth()];
    var t_y = [tAxis.top, tAxis.top + $target.outerHeight()];

    $(intersectorsSelector).each(function() {
          var $this = $(this);
          var thisPos = $this.offset();
          var i_x = [thisPos.left, thisPos.left + $this.outerWidth()]
          var i_y = [thisPos.top, thisPos.top + $this.outerHeight()];

          if ( t_x[0] < i_x[1] && t_x[1] > i_x[0] &&
               t_y[0] < i_y[1] && t_y[1] > i_y[0]) {
            $this.addClass('offset');
            $this.css('marginTop', t_y[1] - i_y[0]);
          }
    });
}

(function($) {
    $(function () {
        fixMenuOverlap('.visual', '.sub-menu');

        $('nav > ul > li').hover(
            function () {
                if (!$(this).is('.current-menu-item')) {
                    $('nav li.current-menu-item').addClass('supress');
                }
                $(this).addClass('hover');
            },
            function () {
                $('nav li.current-menu-item').removeClass('supress');
                $(this).removeClass('hover');
            }
        );
        var visiblePage = $('.multi-page:visible');
        $('#next-page').click(function(event){
            event.preventDefault();
            var nextPage = visiblePage.next();
            if(nextPage.length==0)
            {
                return;
            }
            visiblePage.fadeOut("fast",function(){
                nextPage.fadeIn("fast");
                visiblePage = nextPage;
            });
        });

        $('#prev-page').click(function(event){
            event.preventDefault();
            var nextPage = visiblePage.prev();
            if(nextPage.length==0)
            {
                return;
            }
            visiblePage.fadeOut("fast",function(){
                nextPage.fadeIn("fast");
                visiblePage = nextPage;
            });
        });
    });

    $(window).load(function () {
        if ($("body").is(".inner2")) {
            var $holder = $("#aside .holder");
            var $textBox = $("#specification-part .text-box");
            var maxheight = 475;

            if ($holder.height() > maxheight) {
                $textBox.height($textBox.height() - ($holder.height() - maxheight));
            } else {
                $textBox.height(224);
            }
        }

        if ($('body').is('.inner1')) {
            var $holder = $('.tbwrapper');
            var $textBox = $('.tbwrapper .text-box');
            var maxheight = 475 - $('.contact-info').height();

            if ($holder.height() > maxheight) {
                $textBox.height($textBox.height() - ($holder.height() - maxheight));
            } else {
                $textBox.height($textBox.height() + (maxheight - $holder.height()));
            }
        }
    });

}(jQuery));
