﻿/*
* Copyright (C) 2009-2012 Solmead Productions
*
* == BEGIN LICENSE ==
*
* Licensed under the terms of any of the following licenses at your
* choice:
*
*  - GNU General Public License Version 2 or later (the "GPL")
*    http://www.gnu.org/licenses/gpl.html
*
*  - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
*    http://www.gnu.org/licenses/lgpl.html
*
*  - Mozilla Public License Version 1.1 or later (the "MPL")
*    http://www.mozilla.org/MPL/MPL-1.1.html
*
* == END LICENSE ==
*/
System.SevenHills.MainNavigation = (function () {
    var PopUpObj = null;
    var HoverOff = false;
    var BDirUp = false;
    var BItem = null;

    var Obj = {
        Init: function () {
            if (!System.IsIphone()) {
                $("#navigation > li > a").hover(function () {
                    if (!HoverOff) {
                        Obj.MoveUp(this);
                    }
                }, function () {
                    if (!HoverOff) {
                        Obj.MoveDown(this);
                    }
                });
            }
            $("#navigation > li > a").click(function () {
                HoverOff = true;
                BItem = $(this);
                var B = ($(this).attr("tag") == "Bounce")
                if (B) {
                    Obj.Bounce();
                } else {
                    HoverOff = false;
                }
                return true;
            });

            if (!System.IsIphone()) {
                $('#navigation > li').hover(function () {
                    if (PopUpObj) {
                        PopUpObj.find('.PopUpNav').fadeOut('fast');
                        PopUpObj = null;
                    } //if
                    var Item = $(this).find('.PopUpNav');
                    //.position().top
                    Item.css('top', $(this).position().top - Item.height() + 10);
                    //- Item.width()/2 + $(this).width()/2
                    var L = $(this).position().left - Item.width() / 2 + $(this).find('a').width() / 4;
                    //alert($(".page").position().left);
                    if (L < -18)
                        L = -18;
                    Item.css('left', L);

                    Item.fadeIn('fast');
                }, function () {
                    PopUpObj = $(this);
                    System.Later(400, function () {
                        if (PopUpObj) {
                            PopUpObj.find('.PopUpNav').fadeOut('fast');
                            PopUpObj = null;
                        } //if
                    });
                });
            }
        },
        MoveUp: function (SelectedItem) {
            $(SelectedItem).stop().animate({ marginTop: "-10px" }, 200);
            $(SelectedItem).parent().find("span").stop().animate({ marginTop: "18px", opacity: 0.25 }, 200);
        },
        MoveDown: function (SelectedItem) {
            $(SelectedItem).stop().animate({ marginTop: "0px" }, 300);
            $(SelectedItem).parent().find("span").stop().animate({ marginTop: "1px", opacity: 1 }, 300);
        },
        Bounce: function () {
            if (BDirUp) {
                Obj.MoveUp(BItem);
            } else {
                Obj.MoveDown(BItem);
            }
            BDirUp = !BDirUp;
            System.Later(300, Obj.Bounce);
        }
    };
    $(function () {
        Obj.Init();
    });
    return Obj;

})();





