$(document).ready(function () {

    /*
    $(".home").css("display", "none");
    $(".home").hide();
    $(window).focus(function(){

    if($('.home').is(":hidden"))
    $('.home').show("slide", { direction: 'up', easing: 'easeInOutQuint' }, 1000);
    });	

    // leave the page
    var leaving_href;
    $('.top-nav a').click(function(event){

    event.preventDefault();
    leaving_href = $(this).attr('href');

    $('.home').hide("slide", {
    duration: 1000, 
    direction: 'up', 
    easing: 'easeInOutQuint', 
    complete: function(event){ window.location = leaving_href; }
    });
    });  	
    */

    // alert box
    if ($(".notifycontent").html().trim().length) {
	 $("#notify-text").html( $(".notifycontent").html() );
        $("#notify-container").delay(2000).slideDown(800, 'easeInOutQuint');
    }
    $("#notify-close a").click(function () {
        $("#notify-container").slideUp(800, 'easeInOutQuint');
    });


    // column widget
    $.widget("ui.hoverColumn", {
        // default options
        options: {
            opacity: .1,
            selected: false
        },
        _create: function () {

            var self = this,
            options = self.options;

            // initialize columns to faded
            self.element.children('.column').css({ opacity: options.opacity });

            self.element.bind('mouseenter.hoverColumn', function (event) { self.show(event); });
            self.element.bind('mouseleave.hoverColumn', function (event) { self.hide(event); });
            self.element.bind('click.hoverColumn', function (event) { self.click(event); });
        },
        _init: function () {

        },
        show: function (event) {

            var self = this,
            options = self.options;

            self.element.children('.column').stop();
            self.element.children('.column').fadeTo(200, 1, 'easeInQuad');

            return self;
        },
        showAll: function (event) {

            $.ui.hoverColumn.all = true;

            $(":ui-hoverColumn").each(function () {
                var $this = $(this);
                $this.hoverColumn("select");
                $this.hoverColumn("show");
            });
        },
        hideAll: function (event) {

            $.ui.hoverColumn.all = false;

            $(":ui-hoverColumn").each(function () {
                var $this = $(this);
                $this.hoverColumn("deselect");
                $this.hoverColumn("hide");
            });
        },
        select: function (event) {

            var self = this,
            options = self.options;

            options.selected = true;

            return self;
        },
        deselect: function (event) {

            var self = this,
            options = self.options;

            options.selected = false;

            return self;
        },
        hide: function (event) {

            var self = this,
            options = self.options;

            if (!options.selected) {
                self.element.children('.column').stop();
                self.element.children('.column').fadeTo(800, options.opacity, 'easeOutExpo');
            }

            return self;
        },
        click: function (event) {

            var self = this,
            options = self.options;

            if ($.ui.hoverColumn.all)
                return;

            // select this one
            options.selected = true;
            self.element.children('.column').stop();
            self.element.children('.column').fadeTo(200, 1, 'easeInQuad', function () {

                // hide all open columns, excluding this instance
                $(":ui-hoverColumn").not(self.element).each(function () {
                    var $this = $(this);
                    $this.hoverColumn("deselect");
                    $this.hoverColumn("hide");
                });
            });

            return self;
        },
        destroy: function () {
            $.Widget.prototype.destroy.apply(this, arguments);
        }
    });
    $.extend($.ui.hoverColumn, {
        all: false
    });

    $('.one').hoverColumn();
    $('.two').hoverColumn({ opacity: .2 });
    $('.three').hoverColumn();



    // column widget
    $.widget("ui.menuBox", {
        // default options
        options: {
            selected: false
        },
        _create: function () {

            var self = this,
            options = self.options;

            self.element.bind('click.menuBox', function (event) { self.click(event); });
        },
        _init: function () {

        },
        click: function (event) {

            var self = this,
            options = self.options;

            // select this one
            if (options.selected) {

                self.hide(event);
                $.ui.menuBox.active = null;
            }
            else {

                self.show(event);
                $.ui.menuBox.active = self;
            }

            event.preventDefault();

            return self;
        },
        show: function (event) {

            var self = this,
            options = self.options;

            old = $.ui.menuBox.active;
            self.element.siblings().slideDown(800, 'easeOutBounce', function () {
                if (old)
                    old.hide(event);
            });

            if (self.element.children('img').size() > 0) {

                self.element.children('img').addClass('selected');
                self.element.children('img').attr('src', self.element.children('img').attr('src').replace("-off.", "-on."));
            } else {

                self.element.addClass('selected');
            }
            options.selected = true;

            return self;
        },
        hide: function (event) {

            var self = this,
            options = self.options;

            self.element.siblings().slideUp(800, 'easeInOutQuint');

            if (self.element.children('img').size() > 0) {

                self.element.children('img').removeClass('selected');
                self.element.children('img').attr('src', self.element.children('img').attr('src').replace("-on.", "-off."));
            } else {

                self.element.removeClass('selected');
            }
            options.selected = false;

            return self;
        },
        destroy: function () {
            $.Widget.prototype.destroy.apply(this, arguments);
        }
    });
    $.extend($.ui.menuBox, {
        active: null
    });

    $('.menubox').menuBox();
    $('.factbox').menuBox();


    // resize photo
    function window_resize() {
        if ($('.wrapper').width() > '1400') {
            $('.photo > img').width(480).height(480);
        } else if ($('.wrapper').width() > '1200') {
            $('.photo > img').width(360).height(360);
        }
        else {
            $('.photo > img').width(0).height(0);
        }
    }
    $(window).resize(window_resize);
    window_resize();


    // show/hide all
    $('#toggle').click(function () {

        if ($.ui.hoverColumn.all) {

            // hide columns
            $.ui.hoverColumn.prototype.hideAll();

            // change link
            $(this).text('Show all');
            $(this).attr('title', 'Show all');
        }
        else {

            // show columns
            $.ui.hoverColumn.prototype.showAll();

            // change link
            $(this).text('Hide all');
            $(this).attr('title', 'Hide all');
        }

        return false;
    });


    // submit form onPressEnter
    $('.form-field').keypress(function (e) {
        if (e.which == 13) {
            $(this).blur();
            $('#Button1').focus().click();
        }
    });

});
