﻿(function($) {

    $.regionSet = function(options) {
        return $.regionSet.impl.init(options);
    };

    $.fn.regionSet = function(options) {
        return $.regionSet.impl.init(this, options);
    };

    /*
    * default options
    */
    $.regionSet.defaults = {

};

$.regionSet.impl = {

    /*
    * options
    */
    opts: null,

    /*
    * helper
    */
    helper: {},

    /*
    * isOpen
    */
    isOpen: false,

    /*
    * Initialize
    */
    init: function(template, options) {

        var self = this;

        this.opts = $.extend({}, $.regionSet.defaults, options);

        self.helper.template = template;
        self.helper.container = template.find('.container');
        self.helper.backgrounds = template.find('.background-set');
        self.helper.regions = template.find('.region-set');

        $('#logo')
                .ifixpng()
                ;

        self.background();
        self.container();
        self.events();

        return self;
    },

    /*
    * Events
    */
    events: function() {

        var self = this;

        self.helper.container
                .hover(function() {

                    self.helper.regions.find('.region')
                        .unbind()
                        .hover(function() {

                            var $region = $(this);

                            if (self.isAnimating == true)
                                return;

                            self.openRegion($region);

                        }, function() {

                            // do nothing

                        })
                        ;

                    self.helper.regions.find('.solo')
                        .hover(function() {

                        self.close();

                    }, function() {

                        // do nothing

                    })
                    ;

                }, function() {

                    self.close();

                    self.helper.regions.find('.region')
                        .unbind()
                        ;

                })
                ;

        self.helper.regions.find('a:[href="#"]')
                .click(function(e) {
                    e.preventDefault();

                    // do nothing

                }) 
                ;

        return;
    },

    /*
    * Open region
    */
    openRegion: function($region) {

        var self = this;
        var $countries = $region.find('.country-set');
        var h = self.helper.container.data('h') + $countries.data('h');

        if ($region.hasClass('region-active'))
            return;

        $region
                .addClass('region-active')
                ;

        $region.siblings()
                .removeClass('region-active')
                ;

        $region.siblings().find('.country-set')
                .css({
                    display: 'none'
                })
                ;

        $countries
                .queue('fx', [])
                .css({
                    display: 'none',
                    opacity: '1.00'
                })
                .fadeIn(400)
                ;

        self.helper.container
                .queue('fx', [])
                .animate({
                    height: h + 'px'
                }, 800, 'easeOutExpo', function() {

                    self.isAnimating = false;

                })
                ;

        return;
    },

    /*
    * Close
    */
    close: function() {

        var self = this;
        var h = self.helper.container.data('h');

        self.helper.regions.children()
                .removeClass('region-active')
                ;

        self.helper.regions.find('.country-set')
                .css({
                    display: 'none'
                })
                ;

        self.helper.container
                .queue('fx', [])
                .animate({
                    height: h + 'px'
                }, 800, 'easeOutExpo', function() {

                    self.isAnimating = false;

                })
                ;

        return;
    },

    /*
    * Background
    */
    background: function() {

        var self = this;
        var $background = self.helper.backgrounds.find('img').first();
        var src = $background.attr('src');

        $(new Image())
		        .load(function() {

		            var rat = $background.width() / $background.height()

		            if ($(window).width() / $(window).height() < rat)
		                $background
                            .css({
                                width: 'auto',
                                height: '100%'
                            })
                            ;
		            else
		                $background
                            .css({
                                width: '100%',
                                height: 'auto'
                            })
                            ;

		            $background
                        .fadeIn(2000, 'easeOutExpo')
                        ;

		        })
                .attr('src', src)
                ;

        return;
    },

    /*
    * Container
    */
    container: function() {

        var self = this;

        self.helper.container
		        .css({
		            top: Math.floor(($(window).height() - self.helper.container.outerHeight()) / 2) + 'px'
		        })
		        .data('h', self.helper.container.height())
		        ;

        self.helper.regions.find('.country-set')
		        .each(function(i, countries) {

		            var $countries = $(countries);

		            $countries
		                .data('h', $countries.outerHeight())
		                .css({
		                    display: 'none',
		                    left: 0
		                })
		                ;

		        })
		        ;

        return;
    }

};
})(jQuery)
