function toggleHover(element, setOn)
{
    var top = setOn ? '46px' : '-900px';
    $(element).find('.mainSubMenu').css('top', top);
}

$(document).ready(function(){
    $('.mainSubMenu').parent().each(function(i) {
        $(this).css('position', 'relative');
        $(this).hover(
            function (e) {
                $(this).children(".mainSubMenu").css('top', '46px');
            },
            function (e){
                var $that = $(this), hoverOut = function () {$that.children('.mainSubMenu').css('top', '-900px')};
                setTimeout(hoverOut , 50);
            }
        );
    });
    $('.mainSubSubMenu').parent().each(function(i) {
        $(this).css('position', 'relative');
        $(this).hover(
            function (e) {
                $(this).children(".mainSubSubMenu").css('top', '0');
            },
            function(e){
                var $that = $(this), hoverOut = function () {$that.children('.mainSubSubMenu').css('top', '-900px')};
                setTimeout(hoverOut , 50);
            }
        );
    });
});

function getUrlParams( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

function performClick()
{
    var elementId = getUrlParams('click');
    var clkElement = null;
    var clkMethod = null;
    if (elementId && elementId != "")
    {
        clkElement = document.getElementById('click_'+elementId);
        if (clkElement != null && clkElement.onclick)
        {
            clkMethod = clkElement.onclick;
            if (clkMethod != null && typeof clkMethod == 'function') {
                return clkMethod.call();
            }
        }
    }
    return false;
}

function toggleElement( divId, scrollToId, callback )
{
    $('#'+divId).toggle('normal', function() {
        if (scrollToId)
        {
            if (callback != null && typeof callback == 'function') {
                $.scrollTo( '#' + scrollToId, 500, callback );
            }
            else {
                 $.scrollTo( '#' + scrollToId, 500 );
            }
        }
    });
    return false;
}

function toggleSub( divId, scrollToId, callback )
{
    $('#'+divId).toggle('normal', function() {
        if (scrollToId)
        {
            if (callback != null && typeof callback == 'function') {
                $.scrollTo( '#' + scrollToId, 500, callback );
            }
            else {
                 $.scrollTo( '#' + scrollToId, 500 );
            }
        }
    });
    return false;
}

function search(form)
{
    var searchSite = "www.drzvulunov.com";
    var isSearchSite = form.context[0].checked;
    var searchString = form.query.value;
    var url = 'http://www.google.co.il/search?q=';
    url += isSearchSite ? 'site%3a' +searchSite + '+' : '';
    if (searchString != "")
    {
        searchString = searchString.replace(/ /g,"+");
        window.open(url + searchString, '_blank');
    }
    return false;
}

function isValidEmail(email)
{
    var result = false;
    var regex = new RegExp(/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/);
    result = regex.test(email);
    return result;
}

/**
 * Class FormController
 */
FormController = function()
{
    this.data = new Object();
    this.NON_INPUT_TYPES = "button;submit;image;reset;hidden";
}
FormController.prototype = {
    init            : function(formObj)
    {
        this.data       = formObj;
        this.callback   = null;
        this.resetFieldAlerts();
    },
    post            : function()
    {
        var form = this.data;
        var dataType = $(form).attr('datatype') ? $(form).attr('datatype') : 'text';
        $.ajax({
            type    : $(form).attr('method'),
            url     : $(form).attr('action'),
            dataType: dataType,
            data    : $(form).serialize(),
            success : function(transport)
            {
                switch(form.name)
                {
                    case "contactForm":
                    {
                        $(form).html("<h2 id='applyFormResponse'>Thank you for applying!</h2>");
                        $("#applyFormResponse").css("text-align", "center");
                        $("#applyFormResponse").css("margin-top", "60px");
                        $("#applyFormResponse").css("direction", "ltr");
                    }
                    break;
                }
            }
        });
        return false;
    },
    validateForm    : function()
    {
        var elements = this.data.elements;
        for (var e=0; e < elements.length; e++)
        {
            if (elements[e].tagName.toLowerCase() == "input" || elements[e].tagName.toLowerCase() == "textarea")
            {
                if (!this.isInputValid(elements[e].name))
                {
                    this.alertField(elements[e]);
                    return false;
                }
            }
        }
        return true;
    },
    submit          : function(formObj)
    {
        this.init(formObj);
        if (this.validateForm())
        {
            return this.post();
        }
        else
        {
            return false;
        }
    },
    isInputValid    : function(key)
    {
        var result = true;
        var input = this.data.elements[key];
        if (input != null && this.NON_INPUT_TYPES.indexOf(input.type) == -1)
        {
            switch(input.name)
            {
                case "email":
                    result = isValidEmail(input.value);
                    break;
                default:
                    result = input.value != null && input.value.replace(/^[ \t]+|[ \t]+$/, "") != "";
                    break;
            }
            if (result)
            {
                try {
                    input.style.backgroundColor = '#fff';
                } catch(ex) {}
            }
        }
        return result;
    },
    alertField      : function(field)
    {
        try {
            field.style.backgroundColor = '#9ff';
        } catch(ex) {}
        field.focus();
    },
    resetFieldAlerts: function()
    {
        var elements = this.data.elements;
        for(var i=0; i < elements.length; i++)
        {
            try {
                elements[i].style.backgroundColor   = 'transparent';
            } catch(ex) {}
        }
    }
}
var FC = new FormController();

function indexPageLoaded()
{
    $("#loader").hide();
    $("#loadTriger").show( 100, function(){
        $("h1").show("slide", { direction: "right" }, 1000 ) ;
        $("h2").show("slide", { direction: "right" }, 1000 ) ;
        $("#slogan").show("slide", { direction: "up" }, 1000  );
        $("#girlImage").show("slide", { direction: "down" }, 1000, function() {
            $("p").show("normal", function(){
                $("#topMenu").show("normal", function(){
                    $("#footer").show(100, function(){
                        $("#flags").show(200);
                    });
                });
            });
        });
    });
}
